├── 01- CNN and MLP.ipynb ├── 02- Sheikh-Recognition ├── Sheikh_Recognition.ipynb ├── inference.py ├── label_map.npy ├── model.h5 └── test │ ├── image01.jpg │ ├── image02jpg │ ├── image04.jpg │ └── image3.jpg ├── 03- Sheikh-Recognition-bot ├── Sheikh_Recognition.ipynb ├── bot.py ├── model.h5 └── photos │ ├── file_20.jpg │ ├── file_21.jpg │ ├── file_22.jpg │ └── file_23.jpg ├── 04- Persian Face Recognition-bot └── preprocess.py ├── 05- 17FlowersClassification.ipynb ├── 07- UTKFace-Age prediction-Regression ├── README.md ├── config.py ├── inference.py ├── input │ ├── 07.jpg │ └── 08.jpg ├── model.h5 └── train.ipynb ├── 08- Gender Recognition └── Train.ipynb ├── 10- HousesPrice-ImageRegression ├── README.md ├── cnn_regression.py ├── inference.py ├── input │ ├── 011.jpg │ ├── 012.jpg │ ├── 013.jpg │ └── 014.jpg ├── pyimagesearch │ ├── datasets.py │ └── models.py └── weights │ └── model.h5 ├── 11- DCGAN_on_Mnist_dataset.ipynb ├── 12- DCGAN_on_Celeb_A_dataset.ipynb ├── 13-1- Mnist_ExpertMode.ipynb ├── 13-2- FaceRecognition-ExpertMode ├── FaceAlignment │ ├── README.md │ ├── __pycache__ │ │ ├── align_image.cpython-38.pyc │ │ ├── config.cpython-38.pyc │ │ ├── face_alignment.cpython-38.pyc │ │ └── landmarks_detector.cpython-38.pyc │ ├── face_alignment.py │ ├── files │ │ ├── retina-frame.py │ │ ├── retina-image.py │ │ └── without-prccesss.py │ ├── landmarks_detector.py │ └── requirements.txt ├── FaceRecognition_ExpertMode.ipynb ├── README.md ├── __pycache__ │ ├── config.cpython-38.pyc │ └── model.cpython-38.pyc ├── config.py ├── inference-img.py ├── input │ ├── Leila_Hatami.jpg │ ├── Scarlett-Johansson.jpg │ ├── angelina-jolie.jpg │ ├── behnam-bani.jpg │ ├── morgan-freeman.jpeg │ └── queen-elizabet.jpg ├── label_map.npy ├── model.py └── output │ ├── Leila_Hatami.jpg │ ├── Scarlett-Johansson.jpg │ ├── angelina-jolie.jpg │ ├── behnam-bani.jpg │ ├── morgan-freeman.jpg │ └── queen-elizabet.jpg └── README.md /02- Sheikh-Recognition/inference.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from argparse import ArgumentParser 4 | from keras.models import load_model 5 | from google.colab.patches import cv2_imshow 6 | import matplotlib.pyplot as plt 7 | 8 | parser = ArgumentParser() 9 | parser.add_argument("--input_image", type=str) 10 | args = parser.parse_args() 11 | 12 | # path = options['path'] 13 | 14 | # model = Model(options) 15 | # model.load_weights(os.path.join(path, 'Train/weight/Weights.hdf5')) 16 | model = load_model("model.h5") 17 | 18 | width = 224 19 | height = 224 20 | 21 | 22 | img = cv2.imread(args.input_image) 23 | img = cv2.resize(img, (width, height)) 24 | img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 25 | img_np = np.array(img1) 26 | img_np = img_np / 255.0 27 | img_np = img_np.reshape(1, width, height, 3) 28 | 29 | y_pred = model.predict(img_np) 30 | prediction = np.argmax(y_pred) 31 | 32 | label_map = np.load('label_map.npy',allow_pickle='TRUE').item() 33 | key_list = list(label_map.keys()) 34 | val_list = list(label_map.values()) 35 | 36 | position = val_list.index(prediction) 37 | label = key_list[position] 38 | 39 | plt.imshow(img1) 40 | print('Predicted label:', label) -------------------------------------------------------------------------------- /02- Sheikh-Recognition/label_map.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/02- Sheikh-Recognition/label_map.npy -------------------------------------------------------------------------------- /02- Sheikh-Recognition/model.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/02- Sheikh-Recognition/model.h5 -------------------------------------------------------------------------------- /02- Sheikh-Recognition/test/image01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/02- Sheikh-Recognition/test/image01.jpg -------------------------------------------------------------------------------- /02- Sheikh-Recognition/test/image02jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/02- Sheikh-Recognition/test/image02jpg -------------------------------------------------------------------------------- /02- Sheikh-Recognition/test/image04.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/02- Sheikh-Recognition/test/image04.jpg -------------------------------------------------------------------------------- /02- Sheikh-Recognition/test/image3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/02- Sheikh-Recognition/test/image3.jpg -------------------------------------------------------------------------------- /03- Sheikh-Recognition-bot/bot.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import telebot 3 | import cv2 4 | from keras.models import load_model 5 | 6 | bot = telebot.TeleBot("Your Token") 7 | 8 | model = load_model("model.h5") 9 | 10 | width = 224 11 | height = 224 12 | @bot.message_handler(commands=['start']) 13 | def start(messages): 14 | bot.send_message(messages.chat.id, f'welcome {messages.from_user.first_name}') 15 | bot.send_message(messages.chat.id, f'***Normal person or Sheikh***') 16 | bot.send_message(messages.chat.id, f'Please send me a photo😊') 17 | 18 | @bot.message_handler(content_types=['photo']) 19 | def photo(message): 20 | img_info = bot.get_file(message.photo[-1].file_id) 21 | downloaded_file = bot.download_file(img_info.file_path) 22 | src = img_info.file_path 23 | with open(src, 'wb') as img_file: 24 | img_file.write(downloaded_file) 25 | 26 | img = cv2.imread(src) 27 | img = cv2.resize(img, (width, height)) 28 | img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 29 | img_np = np.array(img1) 30 | img_np = img_np / 255.0 31 | img_np = img_np.reshape(1, width, height, 3) 32 | 33 | y_pred = model.predict(img_np) 34 | prediction = np.argmax(y_pred) 35 | 36 | if prediction == 1: 37 | bot.reply_to(message, 'sheikh') 38 | else: 39 | bot.reply_to(message, 'normal person') 40 | 41 | 42 | bot.polling() 43 | -------------------------------------------------------------------------------- /03- Sheikh-Recognition-bot/model.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/03- Sheikh-Recognition-bot/model.h5 -------------------------------------------------------------------------------- /03- Sheikh-Recognition-bot/photos/file_20.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/03- Sheikh-Recognition-bot/photos/file_20.jpg -------------------------------------------------------------------------------- /03- Sheikh-Recognition-bot/photos/file_21.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/03- Sheikh-Recognition-bot/photos/file_21.jpg -------------------------------------------------------------------------------- /03- Sheikh-Recognition-bot/photos/file_22.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/03- Sheikh-Recognition-bot/photos/file_22.jpg -------------------------------------------------------------------------------- /03- Sheikh-Recognition-bot/photos/file_23.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/03- Sheikh-Recognition-bot/photos/file_23.jpg -------------------------------------------------------------------------------- /04- Persian Face Recognition-bot/preprocess.py: -------------------------------------------------------------------------------- 1 | import os 2 | import cv2 3 | from retinaface import RetinaFace 4 | from argparse import ArgumentParser 5 | 6 | parser = ArgumentParser() 7 | parser.add_argument("--input_images_dir", type=str) 8 | parser.add_argument("--output_dir", type=str) 9 | args = parser.parse_args() 10 | 11 | list_of_images = os.listdir(args.input_images_dir) 12 | 13 | for i, im in enumerate(list_of_images): 14 | faces = RetinaFace.extract_faces(img_path = os.path.join(args.input_images_dir, im), align = True) 15 | 16 | for face in faces: 17 | face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) 18 | cv2.imwrite(os.path.join(args.output_dir, f'{i}.jpg'), face) -------------------------------------------------------------------------------- /05- 17FlowersClassification.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "17FlowersClassification.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | }, 17 | "accelerator": "GPU", 18 | "widgets": { 19 | "application/vnd.jupyter.widget-state+json": { 20 | "3640db3313e643329271f39748c32ed2": { 21 | "model_module": "@jupyter-widgets/controls", 22 | "model_name": "VBoxModel", 23 | "model_module_version": "1.5.0", 24 | "state": { 25 | "_view_name": "VBoxView", 26 | "_dom_classes": [], 27 | "_model_name": "VBoxModel", 28 | "_view_module": "@jupyter-widgets/controls", 29 | "_model_module_version": "1.5.0", 30 | "_view_count": null, 31 | "_view_module_version": "1.5.0", 32 | "box_style": "", 33 | "layout": "IPY_MODEL_8cf00314a8ed43359de691c6aaaa9a62", 34 | "_model_module": "@jupyter-widgets/controls", 35 | "children": [ 36 | "IPY_MODEL_72bf3152ccd847d6aa0093f08b1e6e7e", 37 | "IPY_MODEL_ff531e2a09e2440e84b69badb6867f70" 38 | ] 39 | } 40 | }, 41 | "8cf00314a8ed43359de691c6aaaa9a62": { 42 | "model_module": "@jupyter-widgets/base", 43 | "model_name": "LayoutModel", 44 | "model_module_version": "1.2.0", 45 | "state": { 46 | "_view_name": "LayoutView", 47 | "grid_template_rows": null, 48 | "right": null, 49 | "justify_content": null, 50 | "_view_module": "@jupyter-widgets/base", 51 | "overflow": null, 52 | "_model_module_version": "1.2.0", 53 | "_view_count": null, 54 | "flex_flow": null, 55 | "width": null, 56 | "min_width": null, 57 | "border": null, 58 | "align_items": null, 59 | "bottom": null, 60 | "_model_module": "@jupyter-widgets/base", 61 | "top": null, 62 | "grid_column": null, 63 | "overflow_y": null, 64 | "overflow_x": null, 65 | "grid_auto_flow": null, 66 | "grid_area": null, 67 | "grid_template_columns": null, 68 | "flex": null, 69 | "_model_name": "LayoutModel", 70 | "justify_items": null, 71 | "grid_row": null, 72 | "max_height": null, 73 | "align_content": null, 74 | "visibility": null, 75 | "align_self": null, 76 | "height": null, 77 | "min_height": null, 78 | "padding": null, 79 | "grid_auto_rows": null, 80 | "grid_gap": null, 81 | "max_width": null, 82 | "order": null, 83 | "_view_module_version": "1.2.0", 84 | "grid_template_areas": null, 85 | "object_position": null, 86 | "object_fit": null, 87 | "grid_auto_columns": null, 88 | "margin": null, 89 | "display": null, 90 | "left": null 91 | } 92 | }, 93 | "72bf3152ccd847d6aa0093f08b1e6e7e": { 94 | "model_module": "@jupyter-widgets/controls", 95 | "model_name": "LabelModel", 96 | "model_module_version": "1.5.0", 97 | "state": { 98 | "_view_name": "LabelView", 99 | "style": "IPY_MODEL_b4dad4a202944bfa9e58d91ec1e54d1c", 100 | "_dom_classes": [], 101 | "description": "", 102 | "_model_name": "LabelModel", 103 | "placeholder": "​", 104 | "_view_module": "@jupyter-widgets/controls", 105 | "_model_module_version": "1.5.0", 106 | "value": " 21.21MB of 21.21MB uploaded (0.00MB deduped)\r", 107 | "_view_count": null, 108 | "_view_module_version": "1.5.0", 109 | "description_tooltip": null, 110 | "_model_module": "@jupyter-widgets/controls", 111 | "layout": "IPY_MODEL_503c668904fb455e8e213772a48a8a10" 112 | } 113 | }, 114 | "ff531e2a09e2440e84b69badb6867f70": { 115 | "model_module": "@jupyter-widgets/controls", 116 | "model_name": "FloatProgressModel", 117 | "model_module_version": "1.5.0", 118 | "state": { 119 | "_view_name": "ProgressView", 120 | "style": "IPY_MODEL_78a19a4e3f994e4b8d92adf0fe3f0a60", 121 | "_dom_classes": [], 122 | "description": "", 123 | "_model_name": "FloatProgressModel", 124 | "bar_style": "", 125 | "max": 1, 126 | "_view_module": "@jupyter-widgets/controls", 127 | "_model_module_version": "1.5.0", 128 | "value": 1, 129 | "_view_count": null, 130 | "_view_module_version": "1.5.0", 131 | "orientation": "horizontal", 132 | "min": 0, 133 | "description_tooltip": null, 134 | "_model_module": "@jupyter-widgets/controls", 135 | "layout": "IPY_MODEL_fe6966d305b643e48701d128cf872570" 136 | } 137 | }, 138 | "b4dad4a202944bfa9e58d91ec1e54d1c": { 139 | "model_module": "@jupyter-widgets/controls", 140 | "model_name": "DescriptionStyleModel", 141 | "model_module_version": "1.5.0", 142 | "state": { 143 | "_view_name": "StyleView", 144 | "_model_name": "DescriptionStyleModel", 145 | "description_width": "", 146 | "_view_module": "@jupyter-widgets/base", 147 | "_model_module_version": "1.5.0", 148 | "_view_count": null, 149 | "_view_module_version": "1.2.0", 150 | "_model_module": "@jupyter-widgets/controls" 151 | } 152 | }, 153 | "503c668904fb455e8e213772a48a8a10": { 154 | "model_module": "@jupyter-widgets/base", 155 | "model_name": "LayoutModel", 156 | "model_module_version": "1.2.0", 157 | "state": { 158 | "_view_name": "LayoutView", 159 | "grid_template_rows": null, 160 | "right": null, 161 | "justify_content": null, 162 | "_view_module": "@jupyter-widgets/base", 163 | "overflow": null, 164 | "_model_module_version": "1.2.0", 165 | "_view_count": null, 166 | "flex_flow": null, 167 | "width": null, 168 | "min_width": null, 169 | "border": null, 170 | "align_items": null, 171 | "bottom": null, 172 | "_model_module": "@jupyter-widgets/base", 173 | "top": null, 174 | "grid_column": null, 175 | "overflow_y": null, 176 | "overflow_x": null, 177 | "grid_auto_flow": null, 178 | "grid_area": null, 179 | "grid_template_columns": null, 180 | "flex": null, 181 | "_model_name": "LayoutModel", 182 | "justify_items": null, 183 | "grid_row": null, 184 | "max_height": null, 185 | "align_content": null, 186 | "visibility": null, 187 | "align_self": null, 188 | "height": null, 189 | "min_height": null, 190 | "padding": null, 191 | "grid_auto_rows": null, 192 | "grid_gap": null, 193 | "max_width": null, 194 | "order": null, 195 | "_view_module_version": "1.2.0", 196 | "grid_template_areas": null, 197 | "object_position": null, 198 | "object_fit": null, 199 | "grid_auto_columns": null, 200 | "margin": null, 201 | "display": null, 202 | "left": null 203 | } 204 | }, 205 | "78a19a4e3f994e4b8d92adf0fe3f0a60": { 206 | "model_module": "@jupyter-widgets/controls", 207 | "model_name": "ProgressStyleModel", 208 | "model_module_version": "1.5.0", 209 | "state": { 210 | "_view_name": "StyleView", 211 | "_model_name": "ProgressStyleModel", 212 | "description_width": "", 213 | "_view_module": "@jupyter-widgets/base", 214 | "_model_module_version": "1.5.0", 215 | "_view_count": null, 216 | "_view_module_version": "1.2.0", 217 | "bar_color": null, 218 | "_model_module": "@jupyter-widgets/controls" 219 | } 220 | }, 221 | "fe6966d305b643e48701d128cf872570": { 222 | "model_module": "@jupyter-widgets/base", 223 | "model_name": "LayoutModel", 224 | "model_module_version": "1.2.0", 225 | "state": { 226 | "_view_name": "LayoutView", 227 | "grid_template_rows": null, 228 | "right": null, 229 | "justify_content": null, 230 | "_view_module": "@jupyter-widgets/base", 231 | "overflow": null, 232 | "_model_module_version": "1.2.0", 233 | "_view_count": null, 234 | "flex_flow": null, 235 | "width": null, 236 | "min_width": null, 237 | "border": null, 238 | "align_items": null, 239 | "bottom": null, 240 | "_model_module": "@jupyter-widgets/base", 241 | "top": null, 242 | "grid_column": null, 243 | "overflow_y": null, 244 | "overflow_x": null, 245 | "grid_auto_flow": null, 246 | "grid_area": null, 247 | "grid_template_columns": null, 248 | "flex": null, 249 | "_model_name": "LayoutModel", 250 | "justify_items": null, 251 | "grid_row": null, 252 | "max_height": null, 253 | "align_content": null, 254 | "visibility": null, 255 | "align_self": null, 256 | "height": null, 257 | "min_height": null, 258 | "padding": null, 259 | "grid_auto_rows": null, 260 | "grid_gap": null, 261 | "max_width": null, 262 | "order": null, 263 | "_view_module_version": "1.2.0", 264 | "grid_template_areas": null, 265 | "object_position": null, 266 | "object_fit": null, 267 | "grid_auto_columns": null, 268 | "margin": null, 269 | "display": null, 270 | "left": null 271 | } 272 | } 273 | } 274 | } 275 | }, 276 | "cells": [ 277 | { 278 | "cell_type": "code", 279 | "metadata": { 280 | "colab": { 281 | "base_uri": "https://localhost:8080/" 282 | }, 283 | "id": "4a1PRN3Zpv4A", 284 | "outputId": "40dabc55-b3e6-45c4-8533-bed4d70f27fa" 285 | }, 286 | "source": [ 287 | "cd /content/drive/MyDrive/DeepLearningTasks/17FlowersClassification" 288 | ], 289 | "execution_count": 2, 290 | "outputs": [ 291 | { 292 | "output_type": "stream", 293 | "name": "stdout", 294 | "text": [ 295 | "/content/drive/MyDrive/DeepLearningTasks/17FlowersClassification\n" 296 | ] 297 | } 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "metadata": { 303 | "id": "05jVpmNImhJk" 304 | }, 305 | "source": [ 306 | "!pip install wandb" 307 | ], 308 | "execution_count": null, 309 | "outputs": [] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "metadata": { 314 | "id": "JS7ndXLXEaie" 315 | }, 316 | "source": [ 317 | "import os\n", 318 | "import numpy as np\n", 319 | "import tensorflow as tf\n", 320 | "from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout\n", 321 | "import matplotlib.pyplot as plt\n", 322 | "import wandb\n", 323 | "from wandb.keras import WandbCallback\n", 324 | "from tensorflow.keras.preprocessing.image import ImageDataGenerator" 325 | ], 326 | "execution_count": 1, 327 | "outputs": [] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "metadata": { 332 | "id": "cxKnC8cjGg-2" 333 | }, 334 | "source": [ 335 | "# from google.colab import drive\n", 336 | "# drive.mount('/content/drive')" 337 | ], 338 | "execution_count": 2, 339 | "outputs": [] 340 | }, 341 | { 342 | "cell_type": "code", 343 | "metadata": { 344 | "colab": { 345 | "base_uri": "https://localhost:8080/", 346 | "height": 706, 347 | "referenced_widgets": [ 348 | "3640db3313e643329271f39748c32ed2", 349 | "8cf00314a8ed43359de691c6aaaa9a62", 350 | "72bf3152ccd847d6aa0093f08b1e6e7e", 351 | "ff531e2a09e2440e84b69badb6867f70", 352 | "b4dad4a202944bfa9e58d91ec1e54d1c", 353 | "503c668904fb455e8e213772a48a8a10", 354 | "78a19a4e3f994e4b8d92adf0fe3f0a60", 355 | "fe6966d305b643e48701d128cf872570" 356 | ] 357 | }, 358 | "id": "c96PT0-FmnKa", 359 | "outputId": "a8b93901-f617-4dda-e4cc-22ec7cad0120" 360 | }, 361 | "source": [ 362 | "wandb.init(project='17FlowersClassification')\n", 363 | "config=wandb.config\n", 364 | "config.learning_rate=0.0001" 365 | ], 366 | "execution_count": 22, 367 | "outputs": [ 368 | { 369 | "output_type": "display_data", 370 | "data": { 371 | "text/html": [ 372 | "Finishing last run (ID:izv3az0i) before initializing another..." 373 | ], 374 | "text/plain": [ 375 | "" 376 | ] 377 | }, 378 | "metadata": {} 379 | }, 380 | { 381 | "output_type": "display_data", 382 | "data": { 383 | "text/html": [ 384 | "
Waiting for W&B process to finish, PID 2754
Program ended successfully." 385 | ], 386 | "text/plain": [ 387 | "" 388 | ] 389 | }, 390 | "metadata": {} 391 | }, 392 | { 393 | "output_type": "display_data", 394 | "data": { 395 | "application/vnd.jupyter.widget-view+json": { 396 | "model_id": "3640db3313e643329271f39748c32ed2", 397 | "version_minor": 0, 398 | "version_major": 2 399 | }, 400 | "text/plain": [ 401 | "VBox(children=(Label(value=' 5.33MB of 5.33MB uploaded (0.00MB deduped)\\r'), FloatProgress(value=1.0, max=1.0)…" 402 | ] 403 | }, 404 | "metadata": {} 405 | }, 406 | { 407 | "output_type": "display_data", 408 | "data": { 409 | "text/html": [ 410 | "Find user logs for this run at: /content/wandb/run-20210924_104532-izv3az0i/logs/debug.log" 411 | ], 412 | "text/plain": [ 413 | "" 414 | ] 415 | }, 416 | "metadata": {} 417 | }, 418 | { 419 | "output_type": "display_data", 420 | "data": { 421 | "text/html": [ 422 | "Find internal logs for this run at: /content/wandb/run-20210924_104532-izv3az0i/logs/debug-internal.log" 423 | ], 424 | "text/plain": [ 425 | "" 426 | ] 427 | }, 428 | "metadata": {} 429 | }, 430 | { 431 | "output_type": "display_data", 432 | "data": { 433 | "text/html": [ 434 | "

Run summary:


\n", 437 | "
accuracy0.85407
best_epoch5
best_val_loss1.10269
epoch9
loss0.47593
val_accuracy0.65686
val_loss1.2743
" 438 | ], 439 | "text/plain": [ 440 | "" 441 | ] 442 | }, 443 | "metadata": {} 444 | }, 445 | { 446 | "output_type": "display_data", 447 | "data": { 448 | "text/html": [ 449 | "

Run history:


\n", 452 | "
accuracy▁▄▅▆▇▇▇███
epoch▁▂▃▃▄▅▆▆▇█
loss█▄▃▂▂▂▂▁▁▁
val_accuracy▁▃▅▇▇█▇██▇
val_loss█▅▃▂▁▁▂▁▁▂

" 453 | ], 454 | "text/plain": [ 455 | "" 456 | ] 457 | }, 458 | "metadata": {} 459 | }, 460 | { 461 | "output_type": "display_data", 462 | "data": { 463 | "text/html": [ 464 | "Synced 5 W&B file(s), 1 media file(s), 0 artifact file(s) and 1 other file(s)" 465 | ], 466 | "text/plain": [ 467 | "" 468 | ] 469 | }, 470 | "metadata": {} 471 | }, 472 | { 473 | "output_type": "display_data", 474 | "data": { 475 | "text/html": [ 476 | "\n", 477 | "
Synced lively-butterfly-17: https://wandb.ai/nahid-ebrahimian/17FlowersClassification/runs/izv3az0i
\n", 478 | " " 479 | ], 480 | "text/plain": [ 481 | "" 482 | ] 483 | }, 484 | "metadata": {} 485 | }, 486 | { 487 | "output_type": "display_data", 488 | "data": { 489 | "text/html": [ 490 | "...Successfully finished last run (ID:izv3az0i). Initializing new run:

" 491 | ], 492 | "text/plain": [ 493 | "" 494 | ] 495 | }, 496 | "metadata": {} 497 | }, 498 | { 499 | "output_type": "display_data", 500 | "data": { 501 | "text/html": [ 502 | "\n", 503 | " Tracking run with wandb version 0.12.2
\n", 504 | " Syncing run honest-hill-18 to Weights & Biases (Documentation).
\n", 505 | " Project page: https://wandb.ai/nahid-ebrahimian/17FlowersClassification
\n", 506 | " Run page: https://wandb.ai/nahid-ebrahimian/17FlowersClassification/runs/2mmqa47f
\n", 507 | " Run data is saved locally in /content/wandb/run-20210924_105204-2mmqa47f

\n", 508 | " " 509 | ], 510 | "text/plain": [ 511 | "" 512 | ] 513 | }, 514 | "metadata": {} 515 | } 516 | ] 517 | }, 518 | { 519 | "cell_type": "code", 520 | "metadata": { 521 | "colab": { 522 | "base_uri": "https://localhost:8080/" 523 | }, 524 | "id": "Ph5Ni4S_0eiF", 525 | "outputId": "d810b063-7304-4df4-ef44-9ab05b1686ba" 526 | }, 527 | "source": [ 528 | "dataset_path = '/content/drive/MyDrive/Datasets/Flowers/Flowers'\n", 529 | "width = height = 224\n", 530 | "batch_size = 16\n", 531 | "\n", 532 | "data_generator = ImageDataGenerator(\n", 533 | " rescale = 1/.255,\n", 534 | " horizontal_flip =True,\n", 535 | " rotation_range=20,\n", 536 | " zoom_range=0.1,\n", 537 | " validation_split=0.2\n", 538 | ")\n", 539 | "\n", 540 | "train_data = data_generator.flow_from_directory(\n", 541 | " os.path.join(dataset_path, \"Train\"),\n", 542 | " target_size=(width, height),\n", 543 | " batch_size = batch_size,\n", 544 | " class_mode='categorical',\n", 545 | " shuffle=True,\n", 546 | " subset='training'\n", 547 | ")\n", 548 | "\n", 549 | "\n", 550 | "val_data = data_generator.flow_from_directory(\n", 551 | " os.path.join(dataset_path, \"Train\"),\n", 552 | " target_size=(width, height),\n", 553 | " batch_size = batch_size,\n", 554 | " class_mode='categorical',\n", 555 | " shuffle=True,\n", 556 | " subset='validation'\n", 557 | ")\n", 558 | "\n", 559 | "print(np.bincount(train_data.labels))\n", 560 | "print(np.bincount(val_data.labels))" 561 | ], 562 | "execution_count": 23, 563 | "outputs": [ 564 | { 565 | "output_type": "stream", 566 | "name": "stdout", 567 | "text": [ 568 | "Found 884 images belonging to 17 classes.\n", 569 | "Found 204 images belonging to 17 classes.\n", 570 | "[52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52 52]\n", 571 | "[12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12]\n" 572 | ] 573 | } 574 | ] 575 | }, 576 | { 577 | "cell_type": "code", 578 | "metadata": { 579 | "id": "e2b53n2JxMJx" 580 | }, 581 | "source": [ 582 | "# train_images = next(train_data)[0]\n", 583 | "# plt.figure(figsize=(8,8))\n", 584 | "\n", 585 | "# for i in range(16):\n", 586 | "# plt.subplot(4,4,i+1)\n", 587 | "# plt.xticks([])\n", 588 | "# plt.yticks([])\n", 589 | "# plt.grid(False)\n", 590 | "# plt.imshow(train_images[i],cmap=plt.cm.binary)" 591 | ], 592 | "execution_count": 24, 593 | "outputs": [] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "metadata": { 598 | "colab": { 599 | "base_uri": "https://localhost:8080/" 600 | }, 601 | "id": "JxZ1abD7z8pn", 602 | "outputId": "0e377425-44b6-4177-90a2-89a066b8868a" 603 | }, 604 | "source": [ 605 | "# base_model = tf.keras.applications.VGG19(\n", 606 | "# input_shape=(width, height, 3),\n", 607 | "# include_top=False,\n", 608 | "# weights='imagenet'\n", 609 | "# )\n", 610 | "\n", 611 | "# base_model.summary()\n", 612 | "\n", 613 | "base_model = tf.keras.applications.MobileNetV2(\n", 614 | " input_shape=(width, height, 3),\n", 615 | " alpha=1.0,\n", 616 | " include_top=False,\n", 617 | " weights=\"imagenet\",\n", 618 | " input_tensor=None,\n", 619 | " pooling=None,\n", 620 | " classes=17,\n", 621 | " classifier_activation=\"softmax\",\n", 622 | " # **kwargs\n", 623 | ")\n", 624 | "base_model.summary()" 625 | ], 626 | "execution_count": 25, 627 | "outputs": [ 628 | { 629 | "output_type": "stream", 630 | "name": "stdout", 631 | "text": [ 632 | "Model: \"mobilenetv2_1.00_224\"\n", 633 | "__________________________________________________________________________________________________\n", 634 | "Layer (type) Output Shape Param # Connected to \n", 635 | "==================================================================================================\n", 636 | "input_5 (InputLayer) [(None, 224, 224, 3) 0 \n", 637 | "__________________________________________________________________________________________________\n", 638 | "Conv1 (Conv2D) (None, 112, 112, 32) 864 input_5[0][0] \n", 639 | "__________________________________________________________________________________________________\n", 640 | "bn_Conv1 (BatchNormalization) (None, 112, 112, 32) 128 Conv1[0][0] \n", 641 | "__________________________________________________________________________________________________\n", 642 | "Conv1_relu (ReLU) (None, 112, 112, 32) 0 bn_Conv1[0][0] \n", 643 | "__________________________________________________________________________________________________\n", 644 | "expanded_conv_depthwise (Depthw (None, 112, 112, 32) 288 Conv1_relu[0][0] \n", 645 | "__________________________________________________________________________________________________\n", 646 | "expanded_conv_depthwise_BN (Bat (None, 112, 112, 32) 128 expanded_conv_depthwise[0][0] \n", 647 | "__________________________________________________________________________________________________\n", 648 | "expanded_conv_depthwise_relu (R (None, 112, 112, 32) 0 expanded_conv_depthwise_BN[0][0] \n", 649 | "__________________________________________________________________________________________________\n", 650 | "expanded_conv_project (Conv2D) (None, 112, 112, 16) 512 expanded_conv_depthwise_relu[0][0\n", 651 | "__________________________________________________________________________________________________\n", 652 | "expanded_conv_project_BN (Batch (None, 112, 112, 16) 64 expanded_conv_project[0][0] \n", 653 | "__________________________________________________________________________________________________\n", 654 | "block_1_expand (Conv2D) (None, 112, 112, 96) 1536 expanded_conv_project_BN[0][0] \n", 655 | "__________________________________________________________________________________________________\n", 656 | "block_1_expand_BN (BatchNormali (None, 112, 112, 96) 384 block_1_expand[0][0] \n", 657 | "__________________________________________________________________________________________________\n", 658 | "block_1_expand_relu (ReLU) (None, 112, 112, 96) 0 block_1_expand_BN[0][0] \n", 659 | "__________________________________________________________________________________________________\n", 660 | "block_1_pad (ZeroPadding2D) (None, 113, 113, 96) 0 block_1_expand_relu[0][0] \n", 661 | "__________________________________________________________________________________________________\n", 662 | "block_1_depthwise (DepthwiseCon (None, 56, 56, 96) 864 block_1_pad[0][0] \n", 663 | "__________________________________________________________________________________________________\n", 664 | "block_1_depthwise_BN (BatchNorm (None, 56, 56, 96) 384 block_1_depthwise[0][0] \n", 665 | "__________________________________________________________________________________________________\n", 666 | "block_1_depthwise_relu (ReLU) (None, 56, 56, 96) 0 block_1_depthwise_BN[0][0] \n", 667 | "__________________________________________________________________________________________________\n", 668 | "block_1_project (Conv2D) (None, 56, 56, 24) 2304 block_1_depthwise_relu[0][0] \n", 669 | "__________________________________________________________________________________________________\n", 670 | "block_1_project_BN (BatchNormal (None, 56, 56, 24) 96 block_1_project[0][0] \n", 671 | "__________________________________________________________________________________________________\n", 672 | "block_2_expand (Conv2D) (None, 56, 56, 144) 3456 block_1_project_BN[0][0] \n", 673 | "__________________________________________________________________________________________________\n", 674 | "block_2_expand_BN (BatchNormali (None, 56, 56, 144) 576 block_2_expand[0][0] \n", 675 | "__________________________________________________________________________________________________\n", 676 | "block_2_expand_relu (ReLU) (None, 56, 56, 144) 0 block_2_expand_BN[0][0] \n", 677 | "__________________________________________________________________________________________________\n", 678 | "block_2_depthwise (DepthwiseCon (None, 56, 56, 144) 1296 block_2_expand_relu[0][0] \n", 679 | "__________________________________________________________________________________________________\n", 680 | "block_2_depthwise_BN (BatchNorm (None, 56, 56, 144) 576 block_2_depthwise[0][0] \n", 681 | "__________________________________________________________________________________________________\n", 682 | "block_2_depthwise_relu (ReLU) (None, 56, 56, 144) 0 block_2_depthwise_BN[0][0] \n", 683 | "__________________________________________________________________________________________________\n", 684 | "block_2_project (Conv2D) (None, 56, 56, 24) 3456 block_2_depthwise_relu[0][0] \n", 685 | "__________________________________________________________________________________________________\n", 686 | "block_2_project_BN (BatchNormal (None, 56, 56, 24) 96 block_2_project[0][0] \n", 687 | "__________________________________________________________________________________________________\n", 688 | "block_2_add (Add) (None, 56, 56, 24) 0 block_1_project_BN[0][0] \n", 689 | " block_2_project_BN[0][0] \n", 690 | "__________________________________________________________________________________________________\n", 691 | "block_3_expand (Conv2D) (None, 56, 56, 144) 3456 block_2_add[0][0] \n", 692 | "__________________________________________________________________________________________________\n", 693 | "block_3_expand_BN (BatchNormali (None, 56, 56, 144) 576 block_3_expand[0][0] \n", 694 | "__________________________________________________________________________________________________\n", 695 | "block_3_expand_relu (ReLU) (None, 56, 56, 144) 0 block_3_expand_BN[0][0] \n", 696 | "__________________________________________________________________________________________________\n", 697 | "block_3_pad (ZeroPadding2D) (None, 57, 57, 144) 0 block_3_expand_relu[0][0] \n", 698 | "__________________________________________________________________________________________________\n", 699 | "block_3_depthwise (DepthwiseCon (None, 28, 28, 144) 1296 block_3_pad[0][0] \n", 700 | "__________________________________________________________________________________________________\n", 701 | "block_3_depthwise_BN (BatchNorm (None, 28, 28, 144) 576 block_3_depthwise[0][0] \n", 702 | "__________________________________________________________________________________________________\n", 703 | "block_3_depthwise_relu (ReLU) (None, 28, 28, 144) 0 block_3_depthwise_BN[0][0] \n", 704 | "__________________________________________________________________________________________________\n", 705 | "block_3_project (Conv2D) (None, 28, 28, 32) 4608 block_3_depthwise_relu[0][0] \n", 706 | "__________________________________________________________________________________________________\n", 707 | "block_3_project_BN (BatchNormal (None, 28, 28, 32) 128 block_3_project[0][0] \n", 708 | "__________________________________________________________________________________________________\n", 709 | "block_4_expand (Conv2D) (None, 28, 28, 192) 6144 block_3_project_BN[0][0] \n", 710 | "__________________________________________________________________________________________________\n", 711 | "block_4_expand_BN (BatchNormali (None, 28, 28, 192) 768 block_4_expand[0][0] \n", 712 | "__________________________________________________________________________________________________\n", 713 | "block_4_expand_relu (ReLU) (None, 28, 28, 192) 0 block_4_expand_BN[0][0] \n", 714 | "__________________________________________________________________________________________________\n", 715 | "block_4_depthwise (DepthwiseCon (None, 28, 28, 192) 1728 block_4_expand_relu[0][0] \n", 716 | "__________________________________________________________________________________________________\n", 717 | "block_4_depthwise_BN (BatchNorm (None, 28, 28, 192) 768 block_4_depthwise[0][0] \n", 718 | "__________________________________________________________________________________________________\n", 719 | "block_4_depthwise_relu (ReLU) (None, 28, 28, 192) 0 block_4_depthwise_BN[0][0] \n", 720 | "__________________________________________________________________________________________________\n", 721 | "block_4_project (Conv2D) (None, 28, 28, 32) 6144 block_4_depthwise_relu[0][0] \n", 722 | "__________________________________________________________________________________________________\n", 723 | "block_4_project_BN (BatchNormal (None, 28, 28, 32) 128 block_4_project[0][0] \n", 724 | "__________________________________________________________________________________________________\n", 725 | "block_4_add (Add) (None, 28, 28, 32) 0 block_3_project_BN[0][0] \n", 726 | " block_4_project_BN[0][0] \n", 727 | "__________________________________________________________________________________________________\n", 728 | "block_5_expand (Conv2D) (None, 28, 28, 192) 6144 block_4_add[0][0] \n", 729 | "__________________________________________________________________________________________________\n", 730 | "block_5_expand_BN (BatchNormali (None, 28, 28, 192) 768 block_5_expand[0][0] \n", 731 | "__________________________________________________________________________________________________\n", 732 | "block_5_expand_relu (ReLU) (None, 28, 28, 192) 0 block_5_expand_BN[0][0] \n", 733 | "__________________________________________________________________________________________________\n", 734 | "block_5_depthwise (DepthwiseCon (None, 28, 28, 192) 1728 block_5_expand_relu[0][0] \n", 735 | "__________________________________________________________________________________________________\n", 736 | "block_5_depthwise_BN (BatchNorm (None, 28, 28, 192) 768 block_5_depthwise[0][0] \n", 737 | "__________________________________________________________________________________________________\n", 738 | "block_5_depthwise_relu (ReLU) (None, 28, 28, 192) 0 block_5_depthwise_BN[0][0] \n", 739 | "__________________________________________________________________________________________________\n", 740 | "block_5_project (Conv2D) (None, 28, 28, 32) 6144 block_5_depthwise_relu[0][0] \n", 741 | "__________________________________________________________________________________________________\n", 742 | "block_5_project_BN (BatchNormal (None, 28, 28, 32) 128 block_5_project[0][0] \n", 743 | "__________________________________________________________________________________________________\n", 744 | "block_5_add (Add) (None, 28, 28, 32) 0 block_4_add[0][0] \n", 745 | " block_5_project_BN[0][0] \n", 746 | "__________________________________________________________________________________________________\n", 747 | "block_6_expand (Conv2D) (None, 28, 28, 192) 6144 block_5_add[0][0] \n", 748 | "__________________________________________________________________________________________________\n", 749 | "block_6_expand_BN (BatchNormali (None, 28, 28, 192) 768 block_6_expand[0][0] \n", 750 | "__________________________________________________________________________________________________\n", 751 | "block_6_expand_relu (ReLU) (None, 28, 28, 192) 0 block_6_expand_BN[0][0] \n", 752 | "__________________________________________________________________________________________________\n", 753 | "block_6_pad (ZeroPadding2D) (None, 29, 29, 192) 0 block_6_expand_relu[0][0] \n", 754 | "__________________________________________________________________________________________________\n", 755 | "block_6_depthwise (DepthwiseCon (None, 14, 14, 192) 1728 block_6_pad[0][0] \n", 756 | "__________________________________________________________________________________________________\n", 757 | "block_6_depthwise_BN (BatchNorm (None, 14, 14, 192) 768 block_6_depthwise[0][0] \n", 758 | "__________________________________________________________________________________________________\n", 759 | "block_6_depthwise_relu (ReLU) (None, 14, 14, 192) 0 block_6_depthwise_BN[0][0] \n", 760 | "__________________________________________________________________________________________________\n", 761 | "block_6_project (Conv2D) (None, 14, 14, 64) 12288 block_6_depthwise_relu[0][0] \n", 762 | "__________________________________________________________________________________________________\n", 763 | "block_6_project_BN (BatchNormal (None, 14, 14, 64) 256 block_6_project[0][0] \n", 764 | "__________________________________________________________________________________________________\n", 765 | "block_7_expand (Conv2D) (None, 14, 14, 384) 24576 block_6_project_BN[0][0] \n", 766 | "__________________________________________________________________________________________________\n", 767 | "block_7_expand_BN (BatchNormali (None, 14, 14, 384) 1536 block_7_expand[0][0] \n", 768 | "__________________________________________________________________________________________________\n", 769 | "block_7_expand_relu (ReLU) (None, 14, 14, 384) 0 block_7_expand_BN[0][0] \n", 770 | "__________________________________________________________________________________________________\n", 771 | "block_7_depthwise (DepthwiseCon (None, 14, 14, 384) 3456 block_7_expand_relu[0][0] \n", 772 | "__________________________________________________________________________________________________\n", 773 | "block_7_depthwise_BN (BatchNorm (None, 14, 14, 384) 1536 block_7_depthwise[0][0] \n", 774 | "__________________________________________________________________________________________________\n", 775 | "block_7_depthwise_relu (ReLU) (None, 14, 14, 384) 0 block_7_depthwise_BN[0][0] \n", 776 | "__________________________________________________________________________________________________\n", 777 | "block_7_project (Conv2D) (None, 14, 14, 64) 24576 block_7_depthwise_relu[0][0] \n", 778 | "__________________________________________________________________________________________________\n", 779 | "block_7_project_BN (BatchNormal (None, 14, 14, 64) 256 block_7_project[0][0] \n", 780 | "__________________________________________________________________________________________________\n", 781 | "block_7_add (Add) (None, 14, 14, 64) 0 block_6_project_BN[0][0] \n", 782 | " block_7_project_BN[0][0] \n", 783 | "__________________________________________________________________________________________________\n", 784 | "block_8_expand (Conv2D) (None, 14, 14, 384) 24576 block_7_add[0][0] \n", 785 | "__________________________________________________________________________________________________\n", 786 | "block_8_expand_BN (BatchNormali (None, 14, 14, 384) 1536 block_8_expand[0][0] \n", 787 | "__________________________________________________________________________________________________\n", 788 | "block_8_expand_relu (ReLU) (None, 14, 14, 384) 0 block_8_expand_BN[0][0] \n", 789 | "__________________________________________________________________________________________________\n", 790 | "block_8_depthwise (DepthwiseCon (None, 14, 14, 384) 3456 block_8_expand_relu[0][0] \n", 791 | "__________________________________________________________________________________________________\n", 792 | "block_8_depthwise_BN (BatchNorm (None, 14, 14, 384) 1536 block_8_depthwise[0][0] \n", 793 | "__________________________________________________________________________________________________\n", 794 | "block_8_depthwise_relu (ReLU) (None, 14, 14, 384) 0 block_8_depthwise_BN[0][0] \n", 795 | "__________________________________________________________________________________________________\n", 796 | "block_8_project (Conv2D) (None, 14, 14, 64) 24576 block_8_depthwise_relu[0][0] \n", 797 | "__________________________________________________________________________________________________\n", 798 | "block_8_project_BN (BatchNormal (None, 14, 14, 64) 256 block_8_project[0][0] \n", 799 | "__________________________________________________________________________________________________\n", 800 | "block_8_add (Add) (None, 14, 14, 64) 0 block_7_add[0][0] \n", 801 | " block_8_project_BN[0][0] \n", 802 | "__________________________________________________________________________________________________\n", 803 | "block_9_expand (Conv2D) (None, 14, 14, 384) 24576 block_8_add[0][0] \n", 804 | "__________________________________________________________________________________________________\n", 805 | "block_9_expand_BN (BatchNormali (None, 14, 14, 384) 1536 block_9_expand[0][0] \n", 806 | "__________________________________________________________________________________________________\n", 807 | "block_9_expand_relu (ReLU) (None, 14, 14, 384) 0 block_9_expand_BN[0][0] \n", 808 | "__________________________________________________________________________________________________\n", 809 | "block_9_depthwise (DepthwiseCon (None, 14, 14, 384) 3456 block_9_expand_relu[0][0] \n", 810 | "__________________________________________________________________________________________________\n", 811 | "block_9_depthwise_BN (BatchNorm (None, 14, 14, 384) 1536 block_9_depthwise[0][0] \n", 812 | "__________________________________________________________________________________________________\n", 813 | "block_9_depthwise_relu (ReLU) (None, 14, 14, 384) 0 block_9_depthwise_BN[0][0] \n", 814 | "__________________________________________________________________________________________________\n", 815 | "block_9_project (Conv2D) (None, 14, 14, 64) 24576 block_9_depthwise_relu[0][0] \n", 816 | "__________________________________________________________________________________________________\n", 817 | "block_9_project_BN (BatchNormal (None, 14, 14, 64) 256 block_9_project[0][0] \n", 818 | "__________________________________________________________________________________________________\n", 819 | "block_9_add (Add) (None, 14, 14, 64) 0 block_8_add[0][0] \n", 820 | " block_9_project_BN[0][0] \n", 821 | "__________________________________________________________________________________________________\n", 822 | "block_10_expand (Conv2D) (None, 14, 14, 384) 24576 block_9_add[0][0] \n", 823 | "__________________________________________________________________________________________________\n", 824 | "block_10_expand_BN (BatchNormal (None, 14, 14, 384) 1536 block_10_expand[0][0] \n", 825 | "__________________________________________________________________________________________________\n", 826 | "block_10_expand_relu (ReLU) (None, 14, 14, 384) 0 block_10_expand_BN[0][0] \n", 827 | "__________________________________________________________________________________________________\n", 828 | "block_10_depthwise (DepthwiseCo (None, 14, 14, 384) 3456 block_10_expand_relu[0][0] \n", 829 | "__________________________________________________________________________________________________\n", 830 | "block_10_depthwise_BN (BatchNor (None, 14, 14, 384) 1536 block_10_depthwise[0][0] \n", 831 | "__________________________________________________________________________________________________\n", 832 | "block_10_depthwise_relu (ReLU) (None, 14, 14, 384) 0 block_10_depthwise_BN[0][0] \n", 833 | "__________________________________________________________________________________________________\n", 834 | "block_10_project (Conv2D) (None, 14, 14, 96) 36864 block_10_depthwise_relu[0][0] \n", 835 | "__________________________________________________________________________________________________\n", 836 | "block_10_project_BN (BatchNorma (None, 14, 14, 96) 384 block_10_project[0][0] \n", 837 | "__________________________________________________________________________________________________\n", 838 | "block_11_expand (Conv2D) (None, 14, 14, 576) 55296 block_10_project_BN[0][0] \n", 839 | "__________________________________________________________________________________________________\n", 840 | "block_11_expand_BN (BatchNormal (None, 14, 14, 576) 2304 block_11_expand[0][0] \n", 841 | "__________________________________________________________________________________________________\n", 842 | "block_11_expand_relu (ReLU) (None, 14, 14, 576) 0 block_11_expand_BN[0][0] \n", 843 | "__________________________________________________________________________________________________\n", 844 | "block_11_depthwise (DepthwiseCo (None, 14, 14, 576) 5184 block_11_expand_relu[0][0] \n", 845 | "__________________________________________________________________________________________________\n", 846 | "block_11_depthwise_BN (BatchNor (None, 14, 14, 576) 2304 block_11_depthwise[0][0] \n", 847 | "__________________________________________________________________________________________________\n", 848 | "block_11_depthwise_relu (ReLU) (None, 14, 14, 576) 0 block_11_depthwise_BN[0][0] \n", 849 | "__________________________________________________________________________________________________\n", 850 | "block_11_project (Conv2D) (None, 14, 14, 96) 55296 block_11_depthwise_relu[0][0] \n", 851 | "__________________________________________________________________________________________________\n", 852 | "block_11_project_BN (BatchNorma (None, 14, 14, 96) 384 block_11_project[0][0] \n", 853 | "__________________________________________________________________________________________________\n", 854 | "block_11_add (Add) (None, 14, 14, 96) 0 block_10_project_BN[0][0] \n", 855 | " block_11_project_BN[0][0] \n", 856 | "__________________________________________________________________________________________________\n", 857 | "block_12_expand (Conv2D) (None, 14, 14, 576) 55296 block_11_add[0][0] \n", 858 | "__________________________________________________________________________________________________\n", 859 | "block_12_expand_BN (BatchNormal (None, 14, 14, 576) 2304 block_12_expand[0][0] \n", 860 | "__________________________________________________________________________________________________\n", 861 | "block_12_expand_relu (ReLU) (None, 14, 14, 576) 0 block_12_expand_BN[0][0] \n", 862 | "__________________________________________________________________________________________________\n", 863 | "block_12_depthwise (DepthwiseCo (None, 14, 14, 576) 5184 block_12_expand_relu[0][0] \n", 864 | "__________________________________________________________________________________________________\n", 865 | "block_12_depthwise_BN (BatchNor (None, 14, 14, 576) 2304 block_12_depthwise[0][0] \n", 866 | "__________________________________________________________________________________________________\n", 867 | "block_12_depthwise_relu (ReLU) (None, 14, 14, 576) 0 block_12_depthwise_BN[0][0] \n", 868 | "__________________________________________________________________________________________________\n", 869 | "block_12_project (Conv2D) (None, 14, 14, 96) 55296 block_12_depthwise_relu[0][0] \n", 870 | "__________________________________________________________________________________________________\n", 871 | "block_12_project_BN (BatchNorma (None, 14, 14, 96) 384 block_12_project[0][0] \n", 872 | "__________________________________________________________________________________________________\n", 873 | "block_12_add (Add) (None, 14, 14, 96) 0 block_11_add[0][0] \n", 874 | " block_12_project_BN[0][0] \n", 875 | "__________________________________________________________________________________________________\n", 876 | "block_13_expand (Conv2D) (None, 14, 14, 576) 55296 block_12_add[0][0] \n", 877 | "__________________________________________________________________________________________________\n", 878 | "block_13_expand_BN (BatchNormal (None, 14, 14, 576) 2304 block_13_expand[0][0] \n", 879 | "__________________________________________________________________________________________________\n", 880 | "block_13_expand_relu (ReLU) (None, 14, 14, 576) 0 block_13_expand_BN[0][0] \n", 881 | "__________________________________________________________________________________________________\n", 882 | "block_13_pad (ZeroPadding2D) (None, 15, 15, 576) 0 block_13_expand_relu[0][0] \n", 883 | "__________________________________________________________________________________________________\n", 884 | "block_13_depthwise (DepthwiseCo (None, 7, 7, 576) 5184 block_13_pad[0][0] \n", 885 | "__________________________________________________________________________________________________\n", 886 | "block_13_depthwise_BN (BatchNor (None, 7, 7, 576) 2304 block_13_depthwise[0][0] \n", 887 | "__________________________________________________________________________________________________\n", 888 | "block_13_depthwise_relu (ReLU) (None, 7, 7, 576) 0 block_13_depthwise_BN[0][0] \n", 889 | "__________________________________________________________________________________________________\n", 890 | "block_13_project (Conv2D) (None, 7, 7, 160) 92160 block_13_depthwise_relu[0][0] \n", 891 | "__________________________________________________________________________________________________\n", 892 | "block_13_project_BN (BatchNorma (None, 7, 7, 160) 640 block_13_project[0][0] \n", 893 | "__________________________________________________________________________________________________\n", 894 | "block_14_expand (Conv2D) (None, 7, 7, 960) 153600 block_13_project_BN[0][0] \n", 895 | "__________________________________________________________________________________________________\n", 896 | "block_14_expand_BN (BatchNormal (None, 7, 7, 960) 3840 block_14_expand[0][0] \n", 897 | "__________________________________________________________________________________________________\n", 898 | "block_14_expand_relu (ReLU) (None, 7, 7, 960) 0 block_14_expand_BN[0][0] \n", 899 | "__________________________________________________________________________________________________\n", 900 | "block_14_depthwise (DepthwiseCo (None, 7, 7, 960) 8640 block_14_expand_relu[0][0] \n", 901 | "__________________________________________________________________________________________________\n", 902 | "block_14_depthwise_BN (BatchNor (None, 7, 7, 960) 3840 block_14_depthwise[0][0] \n", 903 | "__________________________________________________________________________________________________\n", 904 | "block_14_depthwise_relu (ReLU) (None, 7, 7, 960) 0 block_14_depthwise_BN[0][0] \n", 905 | "__________________________________________________________________________________________________\n", 906 | "block_14_project (Conv2D) (None, 7, 7, 160) 153600 block_14_depthwise_relu[0][0] \n", 907 | "__________________________________________________________________________________________________\n", 908 | "block_14_project_BN (BatchNorma (None, 7, 7, 160) 640 block_14_project[0][0] \n", 909 | "__________________________________________________________________________________________________\n", 910 | "block_14_add (Add) (None, 7, 7, 160) 0 block_13_project_BN[0][0] \n", 911 | " block_14_project_BN[0][0] \n", 912 | "__________________________________________________________________________________________________\n", 913 | "block_15_expand (Conv2D) (None, 7, 7, 960) 153600 block_14_add[0][0] \n", 914 | "__________________________________________________________________________________________________\n", 915 | "block_15_expand_BN (BatchNormal (None, 7, 7, 960) 3840 block_15_expand[0][0] \n", 916 | "__________________________________________________________________________________________________\n", 917 | "block_15_expand_relu (ReLU) (None, 7, 7, 960) 0 block_15_expand_BN[0][0] \n", 918 | "__________________________________________________________________________________________________\n", 919 | "block_15_depthwise (DepthwiseCo (None, 7, 7, 960) 8640 block_15_expand_relu[0][0] \n", 920 | "__________________________________________________________________________________________________\n", 921 | "block_15_depthwise_BN (BatchNor (None, 7, 7, 960) 3840 block_15_depthwise[0][0] \n", 922 | "__________________________________________________________________________________________________\n", 923 | "block_15_depthwise_relu (ReLU) (None, 7, 7, 960) 0 block_15_depthwise_BN[0][0] \n", 924 | "__________________________________________________________________________________________________\n", 925 | "block_15_project (Conv2D) (None, 7, 7, 160) 153600 block_15_depthwise_relu[0][0] \n", 926 | "__________________________________________________________________________________________________\n", 927 | "block_15_project_BN (BatchNorma (None, 7, 7, 160) 640 block_15_project[0][0] \n", 928 | "__________________________________________________________________________________________________\n", 929 | "block_15_add (Add) (None, 7, 7, 160) 0 block_14_add[0][0] \n", 930 | " block_15_project_BN[0][0] \n", 931 | "__________________________________________________________________________________________________\n", 932 | "block_16_expand (Conv2D) (None, 7, 7, 960) 153600 block_15_add[0][0] \n", 933 | "__________________________________________________________________________________________________\n", 934 | "block_16_expand_BN (BatchNormal (None, 7, 7, 960) 3840 block_16_expand[0][0] \n", 935 | "__________________________________________________________________________________________________\n", 936 | "block_16_expand_relu (ReLU) (None, 7, 7, 960) 0 block_16_expand_BN[0][0] \n", 937 | "__________________________________________________________________________________________________\n", 938 | "block_16_depthwise (DepthwiseCo (None, 7, 7, 960) 8640 block_16_expand_relu[0][0] \n", 939 | "__________________________________________________________________________________________________\n", 940 | "block_16_depthwise_BN (BatchNor (None, 7, 7, 960) 3840 block_16_depthwise[0][0] \n", 941 | "__________________________________________________________________________________________________\n", 942 | "block_16_depthwise_relu (ReLU) (None, 7, 7, 960) 0 block_16_depthwise_BN[0][0] \n", 943 | "__________________________________________________________________________________________________\n", 944 | "block_16_project (Conv2D) (None, 7, 7, 320) 307200 block_16_depthwise_relu[0][0] \n", 945 | "__________________________________________________________________________________________________\n", 946 | "block_16_project_BN (BatchNorma (None, 7, 7, 320) 1280 block_16_project[0][0] \n", 947 | "__________________________________________________________________________________________________\n", 948 | "Conv_1 (Conv2D) (None, 7, 7, 1280) 409600 block_16_project_BN[0][0] \n", 949 | "__________________________________________________________________________________________________\n", 950 | "Conv_1_bn (BatchNormalization) (None, 7, 7, 1280) 5120 Conv_1[0][0] \n", 951 | "__________________________________________________________________________________________________\n", 952 | "out_relu (ReLU) (None, 7, 7, 1280) 0 Conv_1_bn[0][0] \n", 953 | "==================================================================================================\n", 954 | "Total params: 2,257,984\n", 955 | "Trainable params: 2,223,872\n", 956 | "Non-trainable params: 34,112\n", 957 | "__________________________________________________________________________________________________\n" 958 | ] 959 | } 960 | ] 961 | }, 962 | { 963 | "cell_type": "code", 964 | "metadata": { 965 | "id": "SYZ-iICcH00X" 966 | }, 967 | "source": [ 968 | "for layer in base_model.layers:\n", 969 | " layer.trainable=False" 970 | ], 971 | "execution_count": 26, 972 | "outputs": [] 973 | }, 974 | { 975 | "cell_type": "code", 976 | "metadata": { 977 | "id": "8Bh44A2K3JvK" 978 | }, 979 | "source": [ 980 | "model = tf.keras.Sequential([\n", 981 | " base_model,\n", 982 | " Flatten(),\n", 983 | " # Dense(512, activation='relu'),\n", 984 | " # Dropout(0.2),\n", 985 | " # Dense(128, activation='relu'),\n", 986 | " Dense(17, activation='softmax')\n", 987 | "])" 988 | ], 989 | "execution_count": 27, 990 | "outputs": [] 991 | }, 992 | { 993 | "cell_type": "code", 994 | "metadata": { 995 | "id": "lmizS3MD5jgm" 996 | }, 997 | "source": [ 998 | "model.compile(loss=tf.keras.losses.categorical_crossentropy,\n", 999 | " optimizer=tf.keras.optimizers.Adam(learning_rate=config.learning_rate),\n", 1000 | " metrics=['accuracy'])" 1001 | ], 1002 | "execution_count": 28, 1003 | "outputs": [] 1004 | }, 1005 | { 1006 | "cell_type": "code", 1007 | "metadata": { 1008 | "colab": { 1009 | "base_uri": "https://localhost:8080/" 1010 | }, 1011 | "id": "nxxpxXb8796o", 1012 | "outputId": "552ef942-b4b1-424a-8636-0319a386bfca" 1013 | }, 1014 | "source": [ 1015 | "model.fit(train_data,\n", 1016 | " steps_per_epoch=train_data.samples/batch_size, #884/32\n", 1017 | " validation_data=val_data,\n", 1018 | " validation_steps=val_data.samples/batch_size, #204/32\n", 1019 | " epochs=15,\n", 1020 | " callbacks=[WandbCallback()])\n" 1021 | ], 1022 | "execution_count": 29, 1023 | "outputs": [ 1024 | { 1025 | "output_type": "stream", 1026 | "name": "stdout", 1027 | "text": [ 1028 | "Epoch 1/15\n", 1029 | "55/55 [==============================] - 25s 407ms/step - loss: 2.7431 - accuracy: 0.2670 - val_loss: 1.9482 - val_accuracy: 0.4461\n" 1030 | ] 1031 | }, 1032 | { 1033 | "output_type": "stream", 1034 | "name": "stderr", 1035 | "text": [ 1036 | "/usr/local/lib/python3.7/dist-packages/keras/utils/generic_utils.py:497: CustomMaskWarning: Custom mask layers require a config and must override get_config. When loading, the custom mask layer must be passed to the custom_objects argument.\n", 1037 | " category=CustomMaskWarning)\n" 1038 | ] 1039 | }, 1040 | { 1041 | "output_type": "stream", 1042 | "name": "stdout", 1043 | "text": [ 1044 | "Epoch 2/15\n", 1045 | "55/55 [==============================] - 21s 382ms/step - loss: 1.4674 - accuracy: 0.5452 - val_loss: 1.5067 - val_accuracy: 0.5490\n", 1046 | "Epoch 3/15\n", 1047 | "55/55 [==============================] - 21s 383ms/step - loss: 1.2714 - accuracy: 0.6109 - val_loss: 1.5740 - val_accuracy: 0.6029\n", 1048 | "Epoch 4/15\n", 1049 | "55/55 [==============================] - 22s 403ms/step - loss: 0.9908 - accuracy: 0.6957 - val_loss: 1.2598 - val_accuracy: 0.6324\n", 1050 | "Epoch 5/15\n", 1051 | "55/55 [==============================] - 21s 383ms/step - loss: 0.7999 - accuracy: 0.7568 - val_loss: 1.1805 - val_accuracy: 0.6618\n", 1052 | "Epoch 6/15\n", 1053 | "55/55 [==============================] - 21s 387ms/step - loss: 0.7298 - accuracy: 0.7794 - val_loss: 1.1331 - val_accuracy: 0.6814\n", 1054 | "Epoch 7/15\n", 1055 | "55/55 [==============================] - 21s 389ms/step - loss: 0.6081 - accuracy: 0.8066 - val_loss: 1.2163 - val_accuracy: 0.6667\n", 1056 | "Epoch 8/15\n", 1057 | "55/55 [==============================] - 21s 388ms/step - loss: 0.5416 - accuracy: 0.8428 - val_loss: 1.2518 - val_accuracy: 0.6961\n", 1058 | "Epoch 9/15\n", 1059 | "55/55 [==============================] - 21s 385ms/step - loss: 0.5613 - accuracy: 0.8167 - val_loss: 1.0629 - val_accuracy: 0.6618\n", 1060 | "Epoch 10/15\n", 1061 | "55/55 [==============================] - 21s 382ms/step - loss: 0.4852 - accuracy: 0.8382 - val_loss: 1.1458 - val_accuracy: 0.6912\n", 1062 | "Epoch 11/15\n", 1063 | "55/55 [==============================] - 22s 399ms/step - loss: 0.4140 - accuracy: 0.8688 - val_loss: 1.3169 - val_accuracy: 0.6275\n", 1064 | "Epoch 12/15\n", 1065 | "55/55 [==============================] - 21s 382ms/step - loss: 0.3725 - accuracy: 0.8824 - val_loss: 1.0782 - val_accuracy: 0.7059\n", 1066 | "Epoch 13/15\n", 1067 | "55/55 [==============================] - 21s 381ms/step - loss: 0.3250 - accuracy: 0.8982 - val_loss: 1.0405 - val_accuracy: 0.6961\n", 1068 | "Epoch 14/15\n", 1069 | "55/55 [==============================] - 21s 383ms/step - loss: 0.3040 - accuracy: 0.9027 - val_loss: 1.1594 - val_accuracy: 0.6667\n", 1070 | "Epoch 15/15\n", 1071 | "55/55 [==============================] - 21s 386ms/step - loss: 0.2952 - accuracy: 0.9084 - val_loss: 1.1196 - val_accuracy: 0.6912\n" 1072 | ] 1073 | }, 1074 | { 1075 | "output_type": "execute_result", 1076 | "data": { 1077 | "text/plain": [ 1078 | "" 1079 | ] 1080 | }, 1081 | "metadata": {}, 1082 | "execution_count": 29 1083 | } 1084 | ] 1085 | }, 1086 | { 1087 | "cell_type": "code", 1088 | "metadata": { 1089 | "id": "f7gFUXBhA70K", 1090 | "colab": { 1091 | "base_uri": "https://localhost:8080/" 1092 | }, 1093 | "outputId": "21367ae3-9200-4f51-b0c5-19e493238005" 1094 | }, 1095 | "source": [ 1096 | "test_data_path = '/content/drive/MyDrive/Datasets/Flowers/Flowers/Test'\n", 1097 | "\n", 1098 | "data_generator = ImageDataGenerator(rescale = 1/.255)\n", 1099 | "test_data = data_generator.flow_from_directory(\n", 1100 | " test_data_path,\n", 1101 | " target_size=(width, height),\n", 1102 | " batch_size = batch_size,\n", 1103 | " class_mode='categorical')\n", 1104 | "\n", 1105 | "model.evaluate(test_data)" 1106 | ], 1107 | "execution_count": 30, 1108 | "outputs": [ 1109 | { 1110 | "output_type": "stream", 1111 | "name": "stdout", 1112 | "text": [ 1113 | "Found 272 images belonging to 17 classes.\n", 1114 | "17/17 [==============================] - 3s 153ms/step - loss: 2.5941 - accuracy: 0.3750\n" 1115 | ] 1116 | }, 1117 | { 1118 | "output_type": "execute_result", 1119 | "data": { 1120 | "text/plain": [ 1121 | "[2.5940513610839844, 0.375]" 1122 | ] 1123 | }, 1124 | "metadata": {}, 1125 | "execution_count": 30 1126 | } 1127 | ] 1128 | }, 1129 | { 1130 | "cell_type": "code", 1131 | "metadata": { 1132 | "id": "aJi9YAaG_xQh" 1133 | }, 1134 | "source": [ 1135 | "" 1136 | ], 1137 | "execution_count": null, 1138 | "outputs": [] 1139 | } 1140 | ] 1141 | } -------------------------------------------------------------------------------- /07- UTKFace-Age prediction-Regression/README.md: -------------------------------------------------------------------------------- 1 | # UTKFace-Age prediction-Regression 2 | 3 | - Train Neural Network on UTKFace dataset using tensorflow and keras 4 | 5 | - [x] train.ipynb 6 | 7 | - [x] inference.py 8 | 9 | #### Dataset: 10 | 11 | Dataset link: [UTKFace-dataset]( https://www.kaggle.com/jangedoo/utkface-new) 12 | 13 | 14 | #### inference: 15 | 16 | 1- First install retina-face 17 | ``` 18 | !pip install retina-face 19 | ``` 20 | 21 | 2- Run the following command: 22 | 23 | ``` 24 | !python3 inference.py --image_path 'input/08.jpg' 25 | ``` 26 | -------------------------------------------------------------------------------- /07- UTKFace-Age prediction-Regression/config.py: -------------------------------------------------------------------------------- 1 | 2 | epochs = 10 3 | width = height = 224 -------------------------------------------------------------------------------- /07- UTKFace-Age prediction-Regression/inference.py: -------------------------------------------------------------------------------- 1 | 2 | import numpy as np 3 | import cv2 4 | import matplotlib.pyplot as plt 5 | from retinaface import RetinaFace 6 | import argparse 7 | from keras.models import load_model 8 | from config import * 9 | 10 | parser = argparse.ArgumentParser() 11 | parser.add_argument("--image_path", type=str) 12 | args = parser.parse_args() 13 | 14 | model = load_model('model.h5') 15 | 16 | faces = RetinaFace.extract_faces(img_path = args.image_path, align = True) 17 | image = faces[0] 18 | image =cv2.cvtColor(image, cv2.COLOR_BGR2RGB) 19 | image = cv2.resize(image, (width, height)) 20 | 21 | plt.imshow(image) 22 | 23 | image = image/255.0 24 | image = np.expand_dims(image, axis=0) 25 | 26 | age = model.predict(image) 27 | print('Age predicted: ', age[0]) -------------------------------------------------------------------------------- /07- UTKFace-Age prediction-Regression/input/07.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/07- UTKFace-Age prediction-Regression/input/07.jpg -------------------------------------------------------------------------------- /07- UTKFace-Age prediction-Regression/input/08.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/07- UTKFace-Age prediction-Regression/input/08.jpg -------------------------------------------------------------------------------- /07- UTKFace-Age prediction-Regression/model.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/07- UTKFace-Age prediction-Regression/model.h5 -------------------------------------------------------------------------------- /08- Gender Recognition/Train.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "GenderRecognition.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | }, 17 | "accelerator": "GPU" 18 | }, 19 | "cells": [ 20 | { 21 | "cell_type": "code", 22 | "metadata": { 23 | "id": "HxqpHZWJdSva" 24 | }, 25 | "source": [ 26 | "!pip install -q kaggle" 27 | ], 28 | "execution_count": null, 29 | "outputs": [] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "metadata": { 34 | "id": "-K_9g_8LE6C-" 35 | }, 36 | "source": [ 37 | "!mkdir ~/.kaggle" 38 | ], 39 | "execution_count": null, 40 | "outputs": [] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "metadata": { 45 | "id": "CrKxRIxaE72e" 46 | }, 47 | "source": [ 48 | "!cp kaggle.json ~/.kaggle" 49 | ], 50 | "execution_count": null, 51 | "outputs": [] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "metadata": { 56 | "colab": { 57 | "base_uri": "https://localhost:8080/" 58 | }, 59 | "id": "g9IpE3R8dhY5", 60 | "outputId": "6cda8877-9273-4fb5-c2c7-fd9f471d2fbc" 61 | }, 62 | "source": [ 63 | "!kaggle datasets download -d ashishjangra27/gender-recognition-200k-images-celeba" 64 | ], 65 | "execution_count": null, 66 | "outputs": [ 67 | { 68 | "output_type": "stream", 69 | "name": "stdout", 70 | "text": [ 71 | "Warning: Your Kaggle API key is readable by other users on this system! To fix this, you can run 'chmod 600 /root/.kaggle/kaggle.json'\n", 72 | "Downloading gender-recognition-200k-images-celeba.zip to /content\n", 73 | " 99% 1.31G/1.32G [00:08<00:00, 147MB/s]\n", 74 | "100% 1.32G/1.32G [00:08<00:00, 159MB/s]\n" 75 | ] 76 | } 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "metadata": { 82 | "id": "0bjeTk9td5RP" 83 | }, 84 | "source": [ 85 | "!unzip -qq gender-recognition-200k-images-celeba.zip" 86 | ], 87 | "execution_count": null, 88 | "outputs": [] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "metadata": { 93 | "id": "Dihc_6qTk8Mm" 94 | }, 95 | "source": [ 96 | "import os\n", 97 | "import numpy as np\n", 98 | "import tensorflow as tf\n", 99 | "from tensorflow.keras.layers import Conv2D, BatchNormalization, Flatten, Dense, MaxPool2D\n", 100 | "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", 101 | "# from config import *" 102 | ], 103 | "execution_count": null, 104 | "outputs": [] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "metadata": { 109 | "colab": { 110 | "base_uri": "https://localhost:8080/" 111 | }, 112 | "id": "BZA1n12dhg4i", 113 | "outputId": "6f70abdc-c8b1-4e22-bedf-8b36fb3b116d" 114 | }, 115 | "source": [ 116 | "from google.colab import drive\n", 117 | "drive.mount('/content/drive')" 118 | ], 119 | "execution_count": null, 120 | "outputs": [ 121 | { 122 | "output_type": "stream", 123 | "name": "stdout", 124 | "text": [ 125 | "Mounted at /content/drive\n" 126 | ] 127 | } 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "metadata": { 133 | "id": "0lAkrSVmlTL6" 134 | }, 135 | "source": [ 136 | "Dataset" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "metadata": { 142 | "colab": { 143 | "base_uri": "https://localhost:8080/" 144 | }, 145 | "id": "j_cEEoXhlSIM", 146 | "outputId": "95610842-d21b-4911-89ff-1c718f360ff9" 147 | }, 148 | "source": [ 149 | "data_generator = ImageDataGenerator(\n", 150 | " rescale = 1/.255,\n", 151 | " horizontal_flip=True,\n", 152 | " # zoom_range=0.3,\n", 153 | " # rotation_range=30\n", 154 | ")\n", 155 | "\n", 156 | "train_data = data_generator.flow_from_directory(\n", 157 | " \"/content/Dataset/Train\",\n", 158 | " target_size = (width, height),\n", 159 | " batch_size = batch_size,\n", 160 | " # class_mode = 'binary',\n", 161 | " class_mode = 'categorical',\n", 162 | " shuffle = True,\n", 163 | ")\n", 164 | "\n", 165 | "val_data = data_generator.flow_from_directory(\n", 166 | " \"/content/Dataset/Validation\",\n", 167 | " target_size = (width, height),\n", 168 | " batch_size = batch_size,\n", 169 | " # class_mode = 'binary',\n", 170 | " class_mode = 'categorical',\n", 171 | " shuffle = True,\n", 172 | ")\n", 173 | "\n", 174 | "print(np.bincount(train_data.labels))\n", 175 | "print(np.bincount(val_data.labels))" 176 | ], 177 | "execution_count": null, 178 | "outputs": [ 179 | { 180 | "output_type": "stream", 181 | "name": "stdout", 182 | "text": [ 183 | "Found 160000 images belonging to 2 classes.\n", 184 | "Found 22598 images belonging to 2 classes.\n", 185 | "[92845 67155]\n", 186 | "[13778 8820]\n" 187 | ] 188 | } 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "metadata": { 194 | "id": "l2aWeD8cnRp2" 195 | }, 196 | "source": [ 197 | "model = tf.keras.models.Sequential([\n", 198 | " Conv2D(32, (3, 3), activation='relu', input_shape=(width, height, 3)),\n", 199 | " Conv2D(32, (3, 3), activation='relu'),\n", 200 | " BatchNormalization(),\n", 201 | " MaxPool2D(),\n", 202 | " Conv2D(64, (3, 3), activation='relu'),\n", 203 | " Conv2D(64, (3, 3), activation='relu'),\n", 204 | " BatchNormalization(),\n", 205 | " MaxPool2D(),\n", 206 | " Conv2D(128, (3, 3), activation='relu'),\n", 207 | " Conv2D(128, (3, 3), activation='relu'),\n", 208 | " BatchNormalization(),\n", 209 | " MaxPool2D(),\n", 210 | " Flatten(),\n", 211 | " Dense(128, activation='relu'),\n", 212 | " Dense(2, activation='softmax')\n", 213 | "])\n", 214 | "# model.summary()" 215 | ], 216 | "execution_count": null, 217 | "outputs": [] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "metadata": { 222 | "id": "jxNqQlbeoobp" 223 | }, 224 | "source": [ 225 | "model.compile(loss=tf.keras.losses.categorical_crossentropy,\n", 226 | " optimizer=tf.keras.optimizers.Adam(learning_rate=lr),\n", 227 | " metrics=['accuracy'])\n", 228 | "# model.compile(loss=tf.keras.losses.binary_crossentropy,\n", 229 | "# optimizer=tf.keras.optimizers.Adam(learning_rate=lr),\n", 230 | "# metrics=['accuracy'])" 231 | ], 232 | "execution_count": null, 233 | "outputs": [] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "metadata": { 238 | "colab": { 239 | "base_uri": "https://localhost:8080/" 240 | }, 241 | "id": "XJt2vA5boudP", 242 | "outputId": "e448982f-25a5-4a6d-99ed-b9642db565c1" 243 | }, 244 | "source": [ 245 | "model.fit(train_data,\n", 246 | " steps_per_epoch=train_data.samples/batch_size,\n", 247 | " validation_data=val_data,\n", 248 | " validation_steps=val_data.samples/batch_size,\n", 249 | " epochs=epochs,\n", 250 | " class_weight={0: 1, 1: 1.4}\n", 251 | " )" 252 | ], 253 | "execution_count": null, 254 | "outputs": [ 255 | { 256 | "output_type": "stream", 257 | "name": "stdout", 258 | "text": [ 259 | "Epoch 1/15\n", 260 | "3333/3333 [==============================] - 453s 136ms/step - loss: 0.1649 - accuracy: 0.9438 - val_loss: 0.0849 - val_accuracy: 0.9667\n", 261 | "Epoch 2/15\n", 262 | "3333/3333 [==============================] - 444s 133ms/step - loss: 0.0855 - accuracy: 0.9720 - val_loss: 0.0725 - val_accuracy: 0.9714\n", 263 | "Epoch 3/15\n", 264 | "3333/3333 [==============================] - 444s 133ms/step - loss: 0.0691 - accuracy: 0.9780 - val_loss: 0.0782 - val_accuracy: 0.9705\n", 265 | "Epoch 4/15\n", 266 | "3333/3333 [==============================] - 445s 133ms/step - loss: 0.0567 - accuracy: 0.9823 - val_loss: 0.0621 - val_accuracy: 0.9761\n", 267 | "Epoch 5/15\n", 268 | "3333/3333 [==============================] - 444s 133ms/step - loss: 0.0473 - accuracy: 0.9853 - val_loss: 0.0810 - val_accuracy: 0.9703\n", 269 | "Epoch 6/15\n", 270 | "3333/3333 [==============================] - 444s 133ms/step - loss: 0.0389 - accuracy: 0.9883 - val_loss: 0.0692 - val_accuracy: 0.9752\n", 271 | "Epoch 7/15\n", 272 | "3333/3333 [==============================] - 445s 133ms/step - loss: 0.0334 - accuracy: 0.9900 - val_loss: 0.0640 - val_accuracy: 0.9795\n", 273 | "Epoch 8/15\n", 274 | "3333/3333 [==============================] - 446s 134ms/step - loss: 0.0274 - accuracy: 0.9920 - val_loss: 0.0646 - val_accuracy: 0.9793\n", 275 | "Epoch 9/15\n", 276 | "3333/3333 [==============================] - 445s 133ms/step - loss: 0.0229 - accuracy: 0.9933 - val_loss: 0.0735 - val_accuracy: 0.9783\n", 277 | "Epoch 10/15\n", 278 | "3333/3333 [==============================] - 445s 133ms/step - loss: 0.0195 - accuracy: 0.9944 - val_loss: 0.0737 - val_accuracy: 0.9800\n", 279 | "Epoch 11/15\n", 280 | "3333/3333 [==============================] - 446s 134ms/step - loss: 0.0160 - accuracy: 0.9953 - val_loss: 0.0996 - val_accuracy: 0.9749\n", 281 | "Epoch 12/15\n", 282 | "3333/3333 [==============================] - 446s 134ms/step - loss: 0.0147 - accuracy: 0.9956 - val_loss: 0.0721 - val_accuracy: 0.9795\n", 283 | "Epoch 13/15\n", 284 | "3333/3333 [==============================] - 445s 134ms/step - loss: 0.0136 - accuracy: 0.9960 - val_loss: 0.1012 - val_accuracy: 0.9774\n", 285 | "Epoch 14/15\n", 286 | "3333/3333 [==============================] - 443s 133ms/step - loss: 0.0115 - accuracy: 0.9966 - val_loss: 0.0799 - val_accuracy: 0.9798\n", 287 | "Epoch 15/15\n", 288 | "3333/3333 [==============================] - 444s 133ms/step - loss: 0.0104 - accuracy: 0.9968 - val_loss: 0.1069 - val_accuracy: 0.9784\n" 289 | ] 290 | }, 291 | { 292 | "output_type": "execute_result", 293 | "data": { 294 | "text/plain": [ 295 | "" 296 | ] 297 | }, 298 | "metadata": {}, 299 | "execution_count": 20 300 | } 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "metadata": { 306 | "id": "Lp0x2hefrv_P" 307 | }, 308 | "source": [ 309 | "# %cd /content/drive/MyDrive/DeepLearningTasks/ GenderClassification\n", 310 | "model.save(\"/content/drive/MyDrive/DeepLearningTasks/ GenderClassification/model4.h5\")" 311 | ], 312 | "execution_count": null, 313 | "outputs": [] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "metadata": { 318 | "id": "oN5LiykRsWEX" 319 | }, 320 | "source": [ 321 | "test_data_path = '/content/Dataset/Test'\n", 322 | "\n", 323 | "data_generator = ImageDataGenerator(rescale = 1/.255)\n", 324 | "\n", 325 | "test_data = data_generator.flow_from_directory(\n", 326 | " test_data_path,\n", 327 | " target_size=(width, height),\n", 328 | " batch_size = batch_size,\n", 329 | " class_mode='categorical')\n", 330 | "\n", 331 | "model.evaluate(test_data)" 332 | ], 333 | "execution_count": null, 334 | "outputs": [] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "metadata": { 339 | "colab": { 340 | "base_uri": "https://localhost:8080/" 341 | }, 342 | "id": "5fgTFQSEfdHV", 343 | "outputId": "9fcb2624-2ab5-4e08-d574-03f1b72de026" 344 | }, 345 | "source": [ 346 | "from sklearn.metrics import classification_report, confusion_matrix\n", 347 | "\n", 348 | "Y_pred = model.predict(test_data)\n", 349 | "y_pred = np.argmax(Y_pred, axis = 1)\n", 350 | "print('confusion Matrix')\n", 351 | "print(confusion_matrix(test_data.classes, y_pred))\n", 352 | "\n", 353 | "target_names = list(test_data.class_indices.keys())\n", 354 | "print('Classification Report')\n", 355 | "print(classification_report(test_data.classes, y_pred, target_names=target_names))" 356 | ], 357 | "execution_count": null, 358 | "outputs": [ 359 | { 360 | "output_type": "stream", 361 | "name": "stdout", 362 | "text": [ 363 | "confusion Matrix\n", 364 | "[[6821 4721]\n", 365 | " [4880 3579]]\n", 366 | "Classification Report\n", 367 | " precision recall f1-score support\n", 368 | "\n", 369 | " Female 0.58 0.59 0.59 11542\n", 370 | " Male 0.43 0.42 0.43 8459\n", 371 | "\n", 372 | " accuracy 0.52 20001\n", 373 | " macro avg 0.51 0.51 0.51 20001\n", 374 | "weighted avg 0.52 0.52 0.52 20001\n", 375 | "\n" 376 | ] 377 | } 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "metadata": { 383 | "id": "8hWc-lbCgj9a" 384 | }, 385 | "source": [ 386 | "" 387 | ], 388 | "execution_count": null, 389 | "outputs": [] 390 | } 391 | ] 392 | } -------------------------------------------------------------------------------- /10- HousesPrice-ImageRegression/README.md: -------------------------------------------------------------------------------- 1 | ## Houses Price Image Regression 2 | 3 | - Train Neural Network for house price prediction using images 4 | 5 | #### Dataset: 6 | 7 | A total of 535 houses are included in the dataset, and 2,140 total images. four images of each house: bedroom, bathroom, Kitchen, frontal view of the house. 8 | 9 | # 10 | 11 | #### Train: 12 | 13 | 1. Download dataset uising the following command: 14 | 15 | ``` 16 | !git clone https://github.com/emanhamed/Houses-dataset 17 | ``` 18 | 19 | 2. Run this command for training: 20 | 21 | ``` 22 | !python3 cnn_regression.py --dataset /content/Houses-dataset/Houses\ Dataset 23 | ``` 24 | # 25 | 26 | #### Inference: 27 | 28 | Put four images of house in `input` folder and run the following command. 29 | 30 | ``` 31 | !python3 inference.py --input_dir ./input 32 | ``` 33 | 34 | # 35 | 36 | **Reference** : [KerasRegressionandCNNs]( https://www.pyimagesearch.com/2019/01/28/keras-regression-and-cnns/) 37 | -------------------------------------------------------------------------------- /10- HousesPrice-ImageRegression/cnn_regression.py: -------------------------------------------------------------------------------- 1 | 2 | # import the necessary packages 3 | from tensorflow.keras.optimizers import Adam 4 | from sklearn.model_selection import train_test_split 5 | from pyimagesearch import datasets 6 | from pyimagesearch import models 7 | import numpy as np 8 | import argparse 9 | import locale 10 | import os 11 | # construct the argument parser and parse the arguments 12 | ap = argparse.ArgumentParser() 13 | ap.add_argument("-d", "--dataset", type=str, required=True, 14 | help="path to input dataset of house images") 15 | args = vars(ap.parse_args()) 16 | 17 | # construct the path to the input .txt file that contains information 18 | # on each house in the dataset and then load the dataset 19 | print("[INFO] loading house attributes...") 20 | inputPath = os.path.sep.join([args["dataset"], "HousesInfo.txt"]) 21 | df = datasets.load_house_attributes(inputPath) 22 | # load the house images and then scale the pixel intensities to the 23 | # range [0, 1] 24 | print("[INFO] loading house images...") 25 | images = datasets.load_house_images(df, args["dataset"]) 26 | images = images / 255.0 27 | # partition the data into training and testing splits using 75% of 28 | # the data for training and the remaining 25% for testing 29 | split = train_test_split(df, images, test_size=0.25, random_state=42) 30 | (trainAttrX, testAttrX, trainImagesX, testImagesX) = split 31 | 32 | # find the largest house price in the training set and use it to 33 | # scale our house prices to the range [0, 1] (will lead to better 34 | # training and convergence) 35 | maxPrice = trainAttrX["price"].max() 36 | trainY = trainAttrX["price"] / maxPrice 37 | testY = testAttrX["price"] / maxPrice 38 | # create our Convolutional Neural Network and then compile the model 39 | # using mean absolute percentage error as our loss, implying that we 40 | # seek to minimize the absolute percentage difference between our 41 | # price *predictions* and the *actual prices* 42 | model = models.create_cnn(64, 64, 3, regress=True) 43 | opt = Adam(lr=1e-3, decay=1e-3 / 200) 44 | model.compile(loss="mean_absolute_percentage_error", optimizer=opt) 45 | # train the model 46 | print("[INFO] training model...") 47 | model.fit(x=trainImagesX, y=trainY, 48 | validation_data=(testImagesX, testY), 49 | epochs=200, batch_size=8) 50 | 51 | model.save('model.h5') 52 | 53 | # make predictions on the testing data 54 | print("[INFO] predicting house prices...") 55 | preds = model.predict(testImagesX) 56 | # compute the difference between the *predicted* house prices and the 57 | # *actual* house prices, then compute the percentage difference and 58 | # the absolute percentage difference 59 | diff = preds.flatten() - testY 60 | percentDiff = (diff / testY) * 100 61 | absPercentDiff = np.abs(percentDiff) 62 | # compute the mean and standard deviation of the absolute percentage 63 | # difference 64 | mean = np.mean(absPercentDiff) 65 | std = np.std(absPercentDiff) 66 | # finally, show some statistics on our model 67 | locale.setlocale(locale.LC_ALL, "en_US.UTF-8") 68 | print("[INFO] avg. house price: {}, std house price: {}".format( 69 | locale.currency(df["price"].mean(), grouping=True), 70 | locale.currency(df["price"].std(), grouping=True))) 71 | print("[INFO] mean: {:.2f}%, std: {:.2f}%".format(mean, std)) -------------------------------------------------------------------------------- /10- HousesPrice-ImageRegression/inference.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | import numpy as np 4 | import cv2 5 | import numpy as np 6 | from tensorflow.keras.models import load_model 7 | import argparse 8 | 9 | parser = argparse.ArgumentParser() 10 | parser.add_argument("--input_dir", type=str) 11 | args = parser.parse_args() 12 | 13 | model = load_model("weights/model.h5") 14 | 15 | images = [] 16 | outputImage = np.zeros((64, 64, 3), dtype="uint8") 17 | 18 | for im in os.listdir(args.input_dir): 19 | image = cv2.imread(os.path.join(args.input_dir, im)) 20 | image = cv2.resize(image, (32, 32)) 21 | images.append(image) 22 | 23 | outputImage[0:32, 0:32] = images[0] 24 | outputImage[0:32, 32:64] = images[1] 25 | outputImage[32:64, 32:64] = images[2] 26 | outputImage[32:64, 0:32] = images[3] 27 | 28 | outputImage = np.array(outputImage) 29 | outputImage = outputImage / 255 30 | 31 | outputImage = outputImage.reshape(1, 64, 64, 3) 32 | pred = model.predict([outputImage]) 33 | print('Predicted Price: ', pred[0]) -------------------------------------------------------------------------------- /10- HousesPrice-ImageRegression/input/011.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/10- HousesPrice-ImageRegression/input/011.jpg -------------------------------------------------------------------------------- /10- HousesPrice-ImageRegression/input/012.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/10- HousesPrice-ImageRegression/input/012.jpg -------------------------------------------------------------------------------- /10- HousesPrice-ImageRegression/input/013.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/10- HousesPrice-ImageRegression/input/013.jpg -------------------------------------------------------------------------------- /10- HousesPrice-ImageRegression/input/014.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/10- HousesPrice-ImageRegression/input/014.jpg -------------------------------------------------------------------------------- /10- HousesPrice-ImageRegression/pyimagesearch/datasets.py: -------------------------------------------------------------------------------- 1 | 2 | def load_house_images(df, inputPath): 3 | import os 4 | import glob 5 | import pandas as pd 6 | import numpy as np 7 | import cv2 8 | 9 | # initialize our images array (i.e., the house images themselves) 10 | images = [] 11 | # loop over the indexes of the houses 12 | for i in df.index.values: 13 | # find the four images for the house and sort the file paths, 14 | # ensuring the four are always in the *same order* 15 | basePath = os.path.sep.join([inputPath, "{}_*".format(i + 1)]) 16 | housePaths = sorted(list(glob.glob(basePath))) 17 | # initialize our list of input images along with the output image 18 | # after *combining* the four input images 19 | inputImages = [] 20 | outputImage = np.zeros((64, 64, 3), dtype="uint8") 21 | # loop over the input house paths 22 | for housePath in housePaths: 23 | # load the input image, resize it to be 32 32, and then 24 | # update the list of input images 25 | image = cv2.imread(housePath) 26 | image = cv2.resize(image, (32, 32)) 27 | inputImages.append(image) 28 | # tile the four input images in the output image such the first 29 | # image goes in the top-right corner, the second image in the 30 | # top-left corner, the third image in the bottom-right corner, 31 | # and the final image in the bottom-left corner 32 | outputImage[0:32, 0:32] = inputImages[0] 33 | outputImage[0:32, 32:64] = inputImages[1] 34 | outputImage[32:64, 32:64] = inputImages[2] 35 | outputImage[32:64, 0:32] = inputImages[3] 36 | # add the tiled image to our set of images the network will be 37 | # trained on 38 | images.append(outputImage) 39 | # return our set of images 40 | return np.array(images) 41 | 42 | 43 | def load_house_attributes(inputPath): 44 | import pandas as pd 45 | 46 | # initialize the list of column names in the CSV file and then 47 | # load it using Pandas 48 | cols = ["bedrooms", "bathrooms", "area", "zipcode", "price"] 49 | df = pd.read_csv(inputPath, sep=" ", header=None, names=cols) 50 | # determine (1) the unique zip codes and (2) the number of data 51 | # points with each zip code 52 | zipcodes = df["zipcode"].value_counts().keys().tolist() 53 | counts = df["zipcode"].value_counts().tolist() 54 | # loop over each of the unique zip codes and their corresponding 55 | # count 56 | for (zipcode, count) in zip(zipcodes, counts): 57 | # the zip code counts for our housing dataset is *extremely* 58 | # unbalanced (some only having 1 or 2 houses per zip code) 59 | # so let's sanitize our data by removing any houses with less 60 | # than 25 houses per zip code 61 | if count < 25: 62 | idxs = df[df["zipcode"] == zipcode].index 63 | df.drop(idxs, inplace=True) 64 | # return the data frame 65 | return df -------------------------------------------------------------------------------- /10- HousesPrice-ImageRegression/pyimagesearch/models.py: -------------------------------------------------------------------------------- 1 | 2 | def create_cnn(width, height, depth, filters=(16, 32, 64), regress=False): 3 | import tensorflow as tf 4 | from tensorflow.keras.layers import Activation, Conv2D, BatchNormalization, Dropout, Dense, MaxPooling2D, Flatten 5 | from tensorflow.keras.models import Model 6 | 7 | # initialize the input shape and channel dimension, assuming 8 | # TensorFlow/channels-last ordering 9 | inputShape = (height, width, depth) 10 | chanDim = -1 11 | # define the model input 12 | inputs = tf.keras.Input(shape=inputShape) 13 | # loop over the number of filters 14 | for (i, f) in enumerate(filters): 15 | # if this is the first CONV layer then set the input 16 | # appropriately 17 | if i == 0: 18 | x = inputs 19 | # CONV => RELU => BN => POOL 20 | x = Conv2D(f, (3, 3), padding="same")(x) 21 | x = Activation("relu")(x) 22 | x = BatchNormalization(axis=chanDim)(x) 23 | x = MaxPooling2D(pool_size=(2, 2))(x) 24 | 25 | # flatten the volume, then FC => RELU => BN => DROPOUT 26 | x = Flatten()(x) 27 | x = Dense(16)(x) 28 | x = Activation("relu")(x) 29 | x = BatchNormalization(axis=chanDim)(x) 30 | x = Dropout(0.5)(x) 31 | # apply another FC layer, this one to match the number of nodes 32 | # coming out of the MLP 33 | x = Dense(4)(x) 34 | x = Activation("relu")(x) 35 | # check to see if the regression node should be added 36 | if regress: 37 | x = Dense(1, activation="linear")(x) 38 | # construct the CNN 39 | model = Model(inputs, x) 40 | # return the CNN 41 | return model -------------------------------------------------------------------------------- /10- HousesPrice-ImageRegression/weights/model.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/10- HousesPrice-ImageRegression/weights/model.h5 -------------------------------------------------------------------------------- /13-1- Mnist_ExpertMode.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Mnist-ExpertMode.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [] 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "language_info": { 15 | "name": "python" 16 | }, 17 | "accelerator": "GPU" 18 | }, 19 | "cells": [ 20 | { 21 | "cell_type": "code", 22 | "metadata": { 23 | "id": "wfZjqbTOWWs2" 24 | }, 25 | "source": [ 26 | "import tensorflow as tf\n", 27 | "from tensorflow.keras.layers import Dense, Conv2D, Flatten\n" 28 | ], 29 | "execution_count": null, 30 | "outputs": [] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "metadata": { 35 | "colab": { 36 | "base_uri": "https://localhost:8080/" 37 | }, 38 | "id": "hUxbocWJXVAr", 39 | "outputId": "d8239e89-28fc-4214-c8b0-f19b6f1c3025" 40 | }, 41 | "source": [ 42 | "print(tf.__version__)" 43 | ], 44 | "execution_count": null, 45 | "outputs": [ 46 | { 47 | "output_type": "stream", 48 | "name": "stdout", 49 | "text": [ 50 | "2.6.0\n" 51 | ] 52 | } 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": { 58 | "id": "q3jXtqShaE3t" 59 | }, 60 | "source": [ 61 | "### Prepare Dataset" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "metadata": { 67 | "id": "BPNng6ApXZ0V" 68 | }, 69 | "source": [ 70 | "mnist = tf.keras.datasets.mnist\n", 71 | "\n", 72 | "(X_train, Y_train), (X_test, Y_test) = mnist.load_data()\n", 73 | "X_train = X_train / 255.0\n", 74 | "X_test = X_test / 255.0" 75 | ], 76 | "execution_count": null, 77 | "outputs": [] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "metadata": { 82 | "colab": { 83 | "base_uri": "https://localhost:8080/" 84 | }, 85 | "id": "bKS9me2JX6yq", 86 | "outputId": "e8cef6b1-c44b-483c-e081-dd9d8f1f45a5" 87 | }, 88 | "source": [ 89 | "X_train.shape" 90 | ], 91 | "execution_count": null, 92 | "outputs": [ 93 | { 94 | "output_type": "execute_result", 95 | "data": { 96 | "text/plain": [ 97 | "(60000, 28, 28)" 98 | ] 99 | }, 100 | "metadata": {}, 101 | "execution_count": 33 102 | } 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "metadata": { 108 | "id": "IIBTO2ThX9DF" 109 | }, 110 | "source": [ 111 | "# add a channels dimention\n", 112 | "\n", 113 | "X_train = X_train[..., tf.newaxis].astype(\"float32\")\n", 114 | "X_test = X_test[..., tf.newaxis].astype(\"float32\")" 115 | ], 116 | "execution_count": null, 117 | "outputs": [] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "metadata": { 122 | "colab": { 123 | "base_uri": "https://localhost:8080/" 124 | }, 125 | "id": "AMeHpjsyYt9i", 126 | "outputId": "72cf5810-2b3e-480d-8490-b21820f29e61" 127 | }, 128 | "source": [ 129 | "X_train.shape" 130 | ], 131 | "execution_count": null, 132 | "outputs": [ 133 | { 134 | "output_type": "execute_result", 135 | "data": { 136 | "text/plain": [ 137 | "(60000, 28, 28, 1)" 138 | ] 139 | }, 140 | "metadata": {}, 141 | "execution_count": 35 142 | } 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "metadata": { 148 | "id": "zxYuA--uYvdt" 149 | }, 150 | "source": [ 151 | "train_data = tf.data.Dataset.from_tensor_slices((X_train, Y_train)).shuffle(10000).batch(32)\n", 152 | "test_data = tf.data.Dataset.from_tensor_slices((X_test, Y_test)).batch(32)" 153 | ], 154 | "execution_count": null, 155 | "outputs": [] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": { 160 | "id": "xxs5DBCOaaLa" 161 | }, 162 | "source": [ 163 | "### Define Model" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "metadata": { 169 | "id": "mIHYdVFvZqbR" 170 | }, 171 | "source": [ 172 | "class MyModel(tf.keras.Model):\n", 173 | " def __init__(self, num_class):\n", 174 | " super().__init__()\n", 175 | " self.conv1 = Conv2D(32, (3, 3), activation='relu')\n", 176 | " self.flatten = Flatten()\n", 177 | " self.fc1 = Dense(128, activation='relu')\n", 178 | " self.fc2 = Dense(num_class)\n", 179 | "\n", 180 | " def call(self, x):\n", 181 | " y = self.conv1(x)\n", 182 | " # y.shape = 26*26*32\n", 183 | "\n", 184 | " w = self.flatten(y)\n", 185 | " # w.shape = 1 * 21632\n", 186 | "\n", 187 | " z = self.fc1(w)\n", 188 | " # z.shape = 1 * 128\n", 189 | "\n", 190 | " output = self.fc2(z)\n", 191 | " # output.shape = 1 * 10\n", 192 | "\n", 193 | " return output\n", 194 | "\n", 195 | "model = MyModel(10)" 196 | ], 197 | "execution_count": null, 198 | "outputs": [] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "metadata": { 203 | "id": "1bfp11HIhbEl" 204 | }, 205 | "source": [ 206 | "loss_function = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)\n", 207 | "optimizer = tf.keras.optimizers.Adam()" 208 | ], 209 | "execution_count": null, 210 | "outputs": [] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "metadata": { 215 | "id": "VBTK-fgMjzbv" 216 | }, 217 | "source": [ 218 | "train_loss = tf.keras.metrics.Mean(name='train_loss')\n", 219 | "train_acc = tf.keras.metrics.SparseCategoricalAccuracy(name='train_acc')\n", 220 | "\n", 221 | "test_loss = tf.keras.metrics.Mean(name='test_loss')\n", 222 | "test_acc = tf.keras.metrics.SparseCategoricalAccuracy(name='test_acc')" 223 | ], 224 | "execution_count": null, 225 | "outputs": [] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "metadata": { 230 | "id": "NpqZ2re1kwh7" 231 | }, 232 | "source": [ 233 | "def train_step(images, y):\n", 234 | " with tf.GradientTape() as tape:\n", 235 | " y_pred = model(images, training = True)\n", 236 | " loss = loss_function(y, y_pred)\n", 237 | "\n", 238 | " # calculate gradient\n", 239 | " gradient = tape.gradient(loss, model.trainable_variables)\n", 240 | " \n", 241 | " # update\n", 242 | " optimizer.apply_gradients(zip(gradient, model.trainable_variables))\n", 243 | "\n", 244 | " train_loss(loss)\n", 245 | " train_acc(y, y_pred)\n" 246 | ], 247 | "execution_count": null, 248 | "outputs": [] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "metadata": { 253 | "id": "2yzgz41YuEpo" 254 | }, 255 | "source": [ 256 | "def test_step(images, y):\n", 257 | " y_pred = model(images, training=False)\n", 258 | " loss = loss_function(y, y_pred)\n", 259 | "\n", 260 | " test_loss(loss)\n", 261 | " test_acc(y, y_pred)" 262 | ], 263 | "execution_count": null, 264 | "outputs": [] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "metadata": { 269 | "id": "BWaO_Z040bBy" 270 | }, 271 | "source": [ 272 | "from tqdm import tqdm" 273 | ], 274 | "execution_count": null, 275 | "outputs": [] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "metadata": { 280 | "id": "-hK0AxCuk3hZ" 281 | }, 282 | "source": [ 283 | "def train():\n", 284 | " epochs = 5\n", 285 | "\n", 286 | " for epoch in range(epochs):\n", 287 | " train_loss.reset_state()\n", 288 | " train_acc.reset_state()\n", 289 | " test_loss.reset_state()\n", 290 | " test_acc.reset_state()\n", 291 | "\n", 292 | " for images, labels in tqdm(train_data):\n", 293 | " train_step(images, labels)\n", 294 | "\n", 295 | " for images, labels in tqdm(test_data):\n", 296 | " test_step(images, labels)\n", 297 | "\n", 298 | " print('epoch: ', epoch + 1)\n", 299 | " print('loss: ', train_loss.result())\n", 300 | " print('accuracy: ', train_acc.result())\n", 301 | "\n", 302 | " print('epoch: ', epoch + 1)\n", 303 | " print('val loss: ', test_loss.result())\n", 304 | " print('val accuracy: ', test_acc.result())" 305 | ], 306 | "execution_count": null, 307 | "outputs": [] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "metadata": { 312 | "colab": { 313 | "base_uri": "https://localhost:8080/" 314 | }, 315 | "id": "Z-gt43Gbtqs9", 316 | "outputId": "67863b51-82d7-4eef-8a7e-c856ccc70522" 317 | }, 318 | "source": [ 319 | "# fit\n", 320 | "\n", 321 | "train()" 322 | ], 323 | "execution_count": null, 324 | "outputs": [ 325 | { 326 | "output_type": "stream", 327 | "name": "stderr", 328 | "text": [ 329 | "100%|██████████| 1875/1875 [00:28<00:00, 64.84it/s]\n", 330 | "100%|██████████| 313/313 [00:02<00:00, 118.57it/s]\n" 331 | ] 332 | }, 333 | { 334 | "output_type": "stream", 335 | "name": "stdout", 336 | "text": [ 337 | "epoch: 1\n", 338 | "loss: tf.Tensor(0.0071229893, shape=(), dtype=float32)\n", 339 | "accuracy: tf.Tensor(0.9975833, shape=(), dtype=float32)\n", 340 | "epoch: 1\n", 341 | "val loss: tf.Tensor(0.071983896, shape=(), dtype=float32)\n", 342 | "val accuracy: tf.Tensor(0.9834, shape=(), dtype=float32)\n" 343 | ] 344 | }, 345 | { 346 | "output_type": "stream", 347 | "name": "stderr", 348 | "text": [ 349 | "100%|██████████| 1875/1875 [00:28<00:00, 65.44it/s]\n", 350 | "100%|██████████| 313/313 [00:02<00:00, 121.15it/s]\n" 351 | ] 352 | }, 353 | { 354 | "output_type": "stream", 355 | "name": "stdout", 356 | "text": [ 357 | "epoch: 2\n", 358 | "loss: tf.Tensor(0.004599823, shape=(), dtype=float32)\n", 359 | "accuracy: tf.Tensor(0.9985, shape=(), dtype=float32)\n", 360 | "epoch: 2\n", 361 | "val loss: tf.Tensor(0.06381927, shape=(), dtype=float32)\n", 362 | "val accuracy: tf.Tensor(0.9849, shape=(), dtype=float32)\n" 363 | ] 364 | }, 365 | { 366 | "output_type": "stream", 367 | "name": "stderr", 368 | "text": [ 369 | "100%|██████████| 1875/1875 [00:29<00:00, 64.42it/s]\n", 370 | "100%|██████████| 313/313 [00:02<00:00, 120.27it/s]\n" 371 | ] 372 | }, 373 | { 374 | "output_type": "stream", 375 | "name": "stdout", 376 | "text": [ 377 | "epoch: 3\n", 378 | "loss: tf.Tensor(0.0057296385, shape=(), dtype=float32)\n", 379 | "accuracy: tf.Tensor(0.9982833, shape=(), dtype=float32)\n", 380 | "epoch: 3\n", 381 | "val loss: tf.Tensor(0.07149253, shape=(), dtype=float32)\n", 382 | "val accuracy: tf.Tensor(0.985, shape=(), dtype=float32)\n" 383 | ] 384 | }, 385 | { 386 | "output_type": "stream", 387 | "name": "stderr", 388 | "text": [ 389 | "100%|██████████| 1875/1875 [00:28<00:00, 65.08it/s]\n", 390 | "100%|██████████| 313/313 [00:02<00:00, 121.81it/s]\n" 391 | ] 392 | }, 393 | { 394 | "output_type": "stream", 395 | "name": "stdout", 396 | "text": [ 397 | "epoch: 4\n", 398 | "loss: tf.Tensor(0.0044878284, shape=(), dtype=float32)\n", 399 | "accuracy: tf.Tensor(0.9985167, shape=(), dtype=float32)\n", 400 | "epoch: 4\n", 401 | "val loss: tf.Tensor(0.085839376, shape=(), dtype=float32)\n", 402 | "val accuracy: tf.Tensor(0.9838, shape=(), dtype=float32)\n" 403 | ] 404 | }, 405 | { 406 | "output_type": "stream", 407 | "name": "stderr", 408 | "text": [ 409 | "100%|██████████| 1875/1875 [00:28<00:00, 64.66it/s]\n", 410 | "100%|██████████| 313/313 [00:02<00:00, 122.21it/s]" 411 | ] 412 | }, 413 | { 414 | "output_type": "stream", 415 | "name": "stdout", 416 | "text": [ 417 | "epoch: 5\n", 418 | "loss: tf.Tensor(0.003135906, shape=(), dtype=float32)\n", 419 | "accuracy: tf.Tensor(0.9989333, shape=(), dtype=float32)\n", 420 | "epoch: 5\n", 421 | "val loss: tf.Tensor(0.07076751, shape=(), dtype=float32)\n", 422 | "val accuracy: tf.Tensor(0.9853, shape=(), dtype=float32)\n" 423 | ] 424 | }, 425 | { 426 | "output_type": "stream", 427 | "name": "stderr", 428 | "text": [ 429 | "\n" 430 | ] 431 | } 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "metadata": { 437 | "id": "Pu6Rnm3Oxjww" 438 | }, 439 | "source": [ 440 | "" 441 | ], 442 | "execution_count": null, 443 | "outputs": [] 444 | } 445 | ] 446 | } -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/FaceAlignment/README.md: -------------------------------------------------------------------------------- 1 | # Face Alignment 2 | 3 | Face Alignment using python 4 | 5 | Input Image | Aligned Face | Aligned Face | Aligned Face | 6 | --- | --- | --- | --- | 7 | ![model arch](../input/friends.jpg) | ![model arch](output/friends_0.jpg) | ![model arch](output/friends_2.jpg) | ![model arch](output/friends_1.jpg) | 8 | 9 | Input Image | Aligned Face | Input Image | Aligned Face | 10 | --- | --- | --- | --- | 11 | ![model arch](../input/trump.jpg) | ![model arch](output/trump_0.jpg) | ![model arch](../input/scarlett-johansson.jpeg) | ![model arch](output/scarlett-johansson_0.jpg) | 12 | 13 | 14 | ## Installation 15 | 16 | Install required packages 17 | ``` 18 | pip install -r requirements.txt 19 | ``` 20 | 21 | 22 | ## Inference 23 | 24 | This code processes an image and output to a directory 25 | 26 | ``` 27 | python3 align_image.py --input ./input/friends.jpg --output ./output 28 | ``` 29 | -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/FaceAlignment/__pycache__/align_image.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/FaceAlignment/__pycache__/align_image.cpython-38.pyc -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/FaceAlignment/__pycache__/config.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/FaceAlignment/__pycache__/config.cpython-38.pyc -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/FaceAlignment/__pycache__/face_alignment.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/FaceAlignment/__pycache__/face_alignment.cpython-38.pyc -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/FaceAlignment/__pycache__/landmarks_detector.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/FaceAlignment/__pycache__/landmarks_detector.cpython-38.pyc -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/FaceAlignment/face_alignment.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import numpy as np 4 | import scipy.ndimage 5 | import PIL.Image 6 | import cv2 7 | 8 | def image_align(img, face_landmarks, output_size=1024, transform_size=4096, enable_padding=True): 9 | # Align function from FFHQ dataset pre-processing step 10 | # https://github.com/NVlabs/ffhq-dataset/blob/master/download_ffhq.py 11 | 12 | lm = np.array(face_landmarks) 13 | # print(img.si) 14 | # print(lm) 15 | lm_chin = lm[0 : 17] # left-right 16 | lm_eyebrow_left = lm[17 : 22] # left-right 17 | lm_eyebrow_right = lm[22 : 27] # left-right 18 | lm_nose = lm[27 : 31] # top-down 19 | lm_nostrils = lm[31 : 36] # top-down 20 | lm_eye_left = lm[36 : 42] # left-clockwise 21 | lm_eye_right = lm[42 : 48] # left-clockwise 22 | lm_mouth_outer = lm[48 : 60] # left-clockwise 23 | lm_mouth_inner = lm[60 : 68] # left-clockwise 24 | 25 | # Calculate auxiliary vectors. 26 | eye_left = np.mean(lm_eye_left, axis=0) 27 | eye_right = np.mean(lm_eye_right, axis=0) 28 | eye_avg = (eye_left + eye_right) * 0.5 29 | eye_to_eye = eye_right - eye_left 30 | mouth_left = lm_mouth_outer[0] 31 | mouth_right = lm_mouth_outer[6] 32 | mouth_avg = (mouth_left + mouth_right) * 0.5 33 | eye_to_mouth = mouth_avg - eye_avg 34 | 35 | # Choose oriented crop rectangle. 36 | x = eye_to_eye - np.flipud(eye_to_mouth) * [-1, 1] 37 | x /= np.hypot(*x) 38 | x *= max(np.hypot(*eye_to_eye) * 2.0, np.hypot(*eye_to_mouth) * 1.8) 39 | y = np.flipud(x) * [-1, 1] 40 | c = eye_avg + eye_to_mouth * 0.1 41 | quad = np.stack([c - x - y, c - x + y, c + x + y, c + x - y]) 42 | qsize = np.hypot(*x) * 2 43 | 44 | # Load in-the-wild image. 45 | # if not os.path.isfile(src_file): 46 | # print('\nCannot find source image. Please run "--wilds" before "--align".') 47 | # return 48 | 49 | # img = PIL.Image.open(img) 50 | from PIL import Image 51 | img = Image.fromarray(img.astype('uint8'), 'RGB') 52 | 53 | # Shrink. 54 | 55 | shrink = int(np.floor(qsize / output_size * 0.5)) 56 | # print(shrink) 57 | if shrink > 1: 58 | rsize = (int(np.rint(float(img.size[0]) / shrink)), int(np.rint(float(img.size[1]) / shrink))) 59 | img = img.resize(rsize, PIL.Image.ANTIALIAS) 60 | quad /= shrink 61 | qsize /= shrink 62 | 63 | # Crop. 64 | border = max(int(np.rint(qsize * 0.1)), 3) 65 | crop = (int(np.floor(min(quad[:,0]))), int(np.floor(min(quad[:,1]))), int(np.ceil(max(quad[:,0]))), int(np.ceil(max(quad[:,1])))) 66 | crop = (max(crop[0] - border, 0), max(crop[1] - border, 0), min(crop[2] + border, img.size[0]), min(crop[3] + border, img.size[1])) 67 | crop2 = (max(crop[0] + border, 0), max(crop[1] + border, 0), min(crop[2] - border, img.size[0]), min(crop[3] - border, img.size[1])) 68 | if crop[2] - crop[0] < img.size[0] or crop[3] - crop[1] < img.size[1]: 69 | img = img.crop(crop) 70 | quad -= crop[0:2] 71 | 72 | # Pad. 73 | pad = (int(np.floor(min(quad[:,0]))), int(np.floor(min(quad[:,1]))), int(np.ceil(max(quad[:,0]))), int(np.ceil(max(quad[:,1])))) 74 | pad = (max(-pad[0] + border, 0), max(-pad[1] + border, 0), max(pad[2] - img.size[0] + border, 0), max(pad[3] - img.size[1] + border, 0)) 75 | if enable_padding and max(pad) > border - 4: 76 | pad = np.maximum(pad, int(np.rint(qsize * 0.3))) 77 | img = np.pad(np.float32(img), ((pad[1], pad[3]), (pad[0], pad[2]), (0, 0)), 'reflect') 78 | h, w, _ = img.shape 79 | y, x, _ = np.ogrid[:h, :w, :1] 80 | mask = np.maximum(1.0 - np.minimum(np.float32(x) / pad[0], np.float32(w-1-x) / pad[2]), 1.0 - np.minimum(np.float32(y) / pad[1], np.float32(h-1-y) / pad[3])) 81 | blur = qsize * 0.02 82 | img += (scipy.ndimage.gaussian_filter(img, [blur, blur, 0]) - img) * np.clip(mask * 3.0 + 1.0, 0.0, 1.0) 83 | img += (np.median(img, axis=(0,1)) - img) * np.clip(mask, 0.0, 1.0) 84 | img = PIL.Image.fromarray(np.uint8(np.clip(np.rint(img), 0, 255)), 'RGB') 85 | quad += pad[:2] 86 | 87 | # Transform. 88 | img = img.transform((transform_size, transform_size), PIL.Image.QUAD, (quad + 0.5).flatten(), PIL.Image.BILINEAR) 89 | if output_size < transform_size: 90 | img = img.resize((output_size, output_size), PIL.Image.ANTIALIAS) 91 | return cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR), crop2 92 | -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/FaceAlignment/files/retina-frame.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from config import * 4 | from keras.models import load_model 5 | from retinaface import RetinaFace 6 | 7 | model = load_model("weights/model.h5") 8 | 9 | video_cap = cv2.VideoCapture(0) 10 | video_cap.set(cv2.CAP_PROP_FPS, 1) 11 | 12 | frame_height = int(video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 13 | frame_width = int(video_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 14 | 15 | video_writer = cv2.VideoWriter('GenderClassification.avi', cv2.VideoWriter_fourcc(*'MJPG'), 20, (frame_width, frame_height)) 16 | 17 | while True: 18 | 19 | rec, frame = video_cap.read() 20 | if not rec: 21 | break 22 | 23 | frame_width, frame_height, _ = frame.shape 24 | face = RetinaFace.extract_faces(img_path=frame, align=True) 25 | face = face[0] 26 | 27 | if np.all(face != None): 28 | # cv2.imwrite('face.jpg', face) 29 | image = cv2.resize(face, (width, height)) 30 | image = image / 255.0 31 | img2 = image.reshape(1, width, height, 3) 32 | 33 | y_pred = model.predict(img2) 34 | print(y_pred) 35 | prediction = np.argmax(y_pred) 36 | 37 | if prediction == 0: 38 | y_pred = "With Mask" 39 | color = (255, 0, 0) 40 | 41 | else: 42 | y_pred = "Without Mask" 43 | color = (255, 0, 255) 44 | 45 | cv2.putText(frame, y_pred, (frame_width // 8, frame_height // 8), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2, 46 | cv2.LINE_AA) 47 | 48 | cv2.imshow('GenderClassification', frame) 49 | # video_writer.write(frame_blur) 50 | 51 | if cv2.waitKey(10) == ord('q'): 52 | break 53 | elif cv2.waitKey(1) == ord('s'): 54 | # cv2.imwrite('QrCode-Reader.jpg', frame) 55 | break 56 | 57 | video_cap.release() 58 | video_writer.release() -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/FaceAlignment/files/retina-image.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from config import * 4 | from keras.models import load_model 5 | import matplotlib.pyplot as plt 6 | from retinaface import RetinaFace 7 | 8 | model = load_model('weights/model.h5') 9 | image_path = cv2.imread('03.jpg') 10 | faces = RetinaFace.extract_faces(img_path=image_path, align=True) 11 | image = faces[0] 12 | image = cv2.resize(image, (width, height)) 13 | image = image / 255.0 14 | img2 = image.reshape(1, width, height, 3) 15 | 16 | plt.imshow(image) 17 | # plt.show() 18 | 19 | result = model.predict(img2) 20 | prediction = np.argmax(result) 21 | 22 | print(result) 23 | 24 | if prediction == 0: 25 | print("With Mask") 26 | # color = (255, 0, 0) 27 | 28 | else: 29 | print("Without Mask") 30 | # color = (255, 0, 255) -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/FaceAlignment/files/without-prccesss.py: -------------------------------------------------------------------------------- 1 | # import cv2 2 | # import os 3 | # 4 | # from align_image import align_img 5 | # 6 | # img = cv2.imread('input/friends.jpg') 7 | # print(img.shape) 8 | # # print(img) 9 | # img = align_img(img) 10 | # # output_file_path = os.path.join(output + "_" + str(i) + ".jpg") 11 | # cv2.imwrite('output/img1.jpg', img) 12 | # # print(img) 13 | 14 | 15 | import cv2 16 | import numpy as np 17 | from config import * 18 | from keras.models import load_model 19 | from align_image import align_img 20 | 21 | model = load_model("weights/model.h5") 22 | 23 | video_cap = cv2.VideoCapture(0) 24 | video_cap.set(cv2.CAP_PROP_FPS, 1) 25 | 26 | frame_height = int(video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 27 | frame_width = int(video_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 28 | 29 | video_writer = cv2.VideoWriter('GenderClassification.avi', cv2.VideoWriter_fourcc(*'MJPG'), 20, (frame_width, frame_height)) 30 | 31 | while True: 32 | 33 | rec, frame = video_cap.read() 34 | if not rec: 35 | break 36 | 37 | frame_width, frame_height, _ = frame.shape 38 | frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 39 | image = cv2.resize(frame, (width, height)) 40 | image = image / 255.0 41 | img2 = image.reshape(1, width, height, 3) 42 | 43 | y_pred = model.predict(img2) 44 | print(y_pred) 45 | prediction = np.argmax(y_pred) 46 | if prediction == 0: 47 | y_pred = "With Mask" 48 | color = (255, 0, 0) 49 | 50 | else: 51 | y_pred = "Without Mask" 52 | color = (255, 0, 255) 53 | 54 | cv2.putText(frame, y_pred, (frame_width // 8, frame_height // 8), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2, 55 | cv2.LINE_AA) 56 | 57 | cv2.imshow('GenderClassification', frame) 58 | # video_writer.write(frame_blur) 59 | 60 | if cv2.waitKey(10) == ord('q'): 61 | break 62 | elif cv2.waitKey(1) == ord('s'): 63 | # cv2.imwrite('QrCode-Reader.jpg', frame) 64 | break 65 | 66 | video_cap.release() 67 | video_writer.release() -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/FaceAlignment/landmarks_detector.py: -------------------------------------------------------------------------------- 1 | import dlib 2 | 3 | 4 | class LandmarksDetector: 5 | def __init__(self, predictor_model_path): 6 | """ 7 | :param predictor_model_path: path to shape_predictor_68_face_landmarks.dat file 8 | """ 9 | self.detector = dlib.get_frontal_face_detector() # cnn_face_detection_model_v1 also can be used 10 | self.shape_predictor = dlib.shape_predictor(predictor_model_path) 11 | 12 | def get_landmarks(self, img): 13 | # img = dlib.load_rgb_image(img) 14 | # print(type(img)) 15 | dets = self.detector(img, 1) 16 | 17 | for detection in dets: 18 | face_landmarks = [(item.x, item.y) for item in self.shape_predictor(img, detection).parts()] 19 | yield face_landmarks 20 | -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/FaceAlignment/requirements.txt: -------------------------------------------------------------------------------- 1 | cmake 2 | dlib 3 | numpy 4 | scipy 5 | pillow 6 | opencv-python -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/FaceRecognition_ExpertMode.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "id": "t1OqPl8a7AE0" 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "!pip install wandb" 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 2, 17 | "metadata": { 18 | "colab": { 19 | "base_uri": "https://localhost:8080/" 20 | }, 21 | "id": "fdMd5RQy9OtG", 22 | "outputId": "5d2b2b8c-ca20-478b-d04c-ec0164ce1558" 23 | }, 24 | "outputs": [ 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "/content/drive/MyDrive/DeepLearningTasks/FaceRecognition-ExpertMode\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "cd /content/drive/MyDrive/DeepLearningTasks/FaceRecognition-ExpertMode" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": { 40 | "id": "sOxl0Hr7-OCL" 41 | }, 42 | "source": [ 43 | "### Settig Parameters and Imports" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 35, 49 | "metadata": { 50 | "colab": { 51 | "base_uri": "https://localhost:8080/" 52 | }, 53 | "id": "EDxFc3mb-StO", 54 | "outputId": "fa1f7256-8e40-40c8-c73f-179c4c455dfb" 55 | }, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "Overwriting config.py\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "%%writefile config.py\n", 67 | "\n", 68 | "width = height = 112\n", 69 | "batch_size = 16\n", 70 | "epochs = 30" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 45, 76 | "metadata": { 77 | "id": "iRA0dhuZ7OZr" 78 | }, 79 | "outputs": [], 80 | "source": [ 81 | "import os\n", 82 | "import numpy as np\n", 83 | "import cv2\n", 84 | "import tensorflow as tf\n", 85 | "from tensorflow.keras.layers import Conv2D, Dense, Flatten, MaxPooling2D, Dropout\n", 86 | "from keras import Input\n", 87 | "import wandb\n", 88 | "from wandb.keras import WandbCallback\n", 89 | "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", 90 | "from tensorflow.keras.models import Sequential\n", 91 | "from tqdm import tqdm\n", 92 | "from config import *" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": { 98 | "id": "9KjYrEoX8uvY" 99 | }, 100 | "source": [ 101 | "### Preparing Dataset" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 4, 107 | "metadata": { 108 | "colab": { 109 | "base_uri": "https://localhost:8080/" 110 | }, 111 | "id": "qPChMFM58XVG", 112 | "outputId": "ac2ee9de-fe7e-4790-b9eb-9d4ffd6abb16" 113 | }, 114 | "outputs": [ 115 | { 116 | "name": "stdout", 117 | "output_type": "stream", 118 | "text": [ 119 | "Found 1091 images belonging to 14 classes.\n", 120 | "Found 268 images belonging to 14 classes.\n", 121 | "num_classes: 14\n", 122 | "[73 81 82 80 84 71 80 60 80 77 78 90 74 81]\n", 123 | "[18 20 20 20 21 17 19 15 20 19 19 22 18 20]\n" 124 | ] 125 | } 126 | ], 127 | "source": [ 128 | "data_path = '/content/drive/MyDrive/7-7 dataset'\n", 129 | "\n", 130 | "data_generator = ImageDataGenerator(\n", 131 | " rescale = 1./255,\n", 132 | " validation_split=0.2\n", 133 | ")\n", 134 | "\n", 135 | "train_data = data_generator.flow_from_directory(\n", 136 | " data_path,\n", 137 | " target_size=(width, height),\n", 138 | " batch_size = batch_size,\n", 139 | " class_mode='categorical',\n", 140 | " shuffle=True,\n", 141 | " subset='training'\n", 142 | ")\n", 143 | "\n", 144 | "\n", 145 | "val_data = data_generator.flow_from_directory(\n", 146 | " data_path,\n", 147 | " target_size=(width, height),\n", 148 | " batch_size = batch_size,\n", 149 | " class_mode='categorical',\n", 150 | " shuffle=False,\n", 151 | " subset='validation'\n", 152 | ")\n", 153 | "\n", 154 | "\n", 155 | "label_map = (train_data.class_indices)\n", 156 | "np.save('label_map.npy', label_map) \n", 157 | "num_classes = len(np.bincount(train_data.labels))\n", 158 | "\n", 159 | "print('num_classes: {}'.format(num_classes))\n", 160 | "print(np.bincount(train_data.labels))\n", 161 | "print(np.bincount(val_data.labels))" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": { 167 | "id": "yLna0gTjBY2i" 168 | }, 169 | "source": [ 170 | "### Define Model and wandb Initializing" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": { 176 | "id": "QDc2jfaTpGA3" 177 | }, 178 | "source": [ 179 | "1-1- My Model" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 46, 185 | "metadata": { 186 | "colab": { 187 | "base_uri": "https://localhost:8080/" 188 | }, 189 | "id": "JjVtC98g5IRG", 190 | "outputId": "61e24c86-4f2b-44df-b54e-ae7c862b6eba" 191 | }, 192 | "outputs": [ 193 | { 194 | "name": "stdout", 195 | "output_type": "stream", 196 | "text": [ 197 | "Model: \"model_3\"\n", 198 | "__________________________________________________________________________________________________\n", 199 | "Layer (type) Output Shape Param # Connected to \n", 200 | "==================================================================================================\n", 201 | "input_6 (InputLayer) [(None, 112, 112, 3) 0 \n", 202 | "__________________________________________________________________________________________________\n", 203 | "conv2d_40 (Conv2D) (None, 110, 110, 32) 896 input_6[0][0] \n", 204 | "__________________________________________________________________________________________________\n", 205 | "max_pooling2d_8 (MaxPooling2D) multiple 0 conv2d_40[0][0] \n", 206 | " conv2d_41[0][0] \n", 207 | " conv2d_42[0][0] \n", 208 | " conv2d_43[0][0] \n", 209 | "__________________________________________________________________________________________________\n", 210 | "conv2d_41 (Conv2D) (None, 53, 53, 64) 18496 max_pooling2d_8[0][0] \n", 211 | "__________________________________________________________________________________________________\n", 212 | "conv2d_42 (Conv2D) (None, 24, 24, 128) 73856 max_pooling2d_8[1][0] \n", 213 | "__________________________________________________________________________________________________\n", 214 | "conv2d_43 (Conv2D) (None, 10, 10, 256) 295168 max_pooling2d_8[2][0] \n", 215 | "__________________________________________________________________________________________________\n", 216 | "flatten_8 (Flatten) (None, 6400) 0 max_pooling2d_8[3][0] \n", 217 | "__________________________________________________________________________________________________\n", 218 | "dropout_8 (Dropout) multiple 0 flatten_8[0][0] \n", 219 | " dense_24[0][0] \n", 220 | " dense_25[0][0] \n", 221 | "__________________________________________________________________________________________________\n", 222 | "dense_24 (Dense) (None, 128) 819328 dropout_8[0][0] \n", 223 | "__________________________________________________________________________________________________\n", 224 | "dense_25 (Dense) (None, 64) 8256 dropout_8[1][0] \n", 225 | "__________________________________________________________________________________________________\n", 226 | "dense_26 (Dense) (None, 14) 910 dropout_8[2][0] \n", 227 | "==================================================================================================\n", 228 | "Total params: 1,216,910\n", 229 | "Trainable params: 1,216,910\n", 230 | "Non-trainable params: 0\n", 231 | "__________________________________________________________________________________________________\n" 232 | ] 233 | } 234 | ], 235 | "source": [ 236 | "class MyModel(tf.keras.Model):\n", 237 | " def __init__(self, num_class, input_shape):\n", 238 | " super().__init__()\n", 239 | " self.conv1 = Conv2D(32, (3, 3), activation='relu', input_shape = input_shape)\n", 240 | " self.conv2 = Conv2D(64, (3, 3), activation='relu')\n", 241 | " self.conv3 = Conv2D(128, (3, 3), activation='relu')\n", 242 | " self.conv4 = Conv2D(256, (3, 3), activation='relu')\n", 243 | " self.maxpool = MaxPooling2D()\n", 244 | " self.flatten = Flatten()\n", 245 | " self.dropout = Dropout(0.2)\n", 246 | " self.fc1 = Dense(128, activation='relu')\n", 247 | " self.fc2 = Dense(64, activation='relu')\n", 248 | " self.fc3= Dense(num_class, activation='softmax')\n", 249 | " self.dim = input_shape\n", 250 | "\n", 251 | "\n", 252 | " def call(self, x):\n", 253 | " conv1 = self.conv1(x)\n", 254 | " maxpool1 = self.maxpool(conv1)\n", 255 | "\n", 256 | " conv2 = self.conv2(maxpool1)\n", 257 | " maxpool2 = self.maxpool(conv2)\n", 258 | "\n", 259 | " conv3 = self.conv3(maxpool2)\n", 260 | " maxpool3 = self.maxpool(conv3)\n", 261 | "\n", 262 | " conv4 = self.conv4(maxpool3)\n", 263 | " maxpool4 = self.maxpool(conv4)\n", 264 | "\n", 265 | " flatten = self.flatten(maxpool4)\n", 266 | " dropout = self.dropout(flatten)\n", 267 | " \n", 268 | " fc1 = self.fc1(dropout)\n", 269 | " dropout = self.dropout(fc1)\n", 270 | " \n", 271 | " fc2 = self.fc2(dropout)\n", 272 | " dropout = self.dropout(fc2)\n", 273 | " \n", 274 | " output = self.fc3(dropout)\n", 275 | "\n", 276 | " return output\n", 277 | "\n", 278 | " def build_graph(self):\n", 279 | " x = Input(shape=(self.dim))\n", 280 | " return tf.keras.Model(inputs=[x], outputs=self.call(x))\n", 281 | "\n", 282 | "\n", 283 | "input_shape = (width ,height ,3)\n", 284 | "model = MyModel(num_classes, (input_shape))\n", 285 | "model.build((None, *input_shape))\n", 286 | "model.build_graph().summary()" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": null, 292 | "metadata": {}, 293 | "outputs": [], 294 | "source": [ 295 | "wandb.init(project='FaceRecognition-ExpertMode')\n", 296 | "config = wandb.config\n", 297 | "config.learning_rate=0.0001" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": { 303 | "id": "xpD4vdVyimt-" 304 | }, 305 | "source": [ 306 | "### Define Loss and Training Loop" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 48, 312 | "metadata": { 313 | "id": "F7ZZMSk_iCEp" 314 | }, 315 | "outputs": [], 316 | "source": [ 317 | "loss_function = tf.keras.losses.CategoricalCrossentropy()\n", 318 | "optimizer = tf.keras.optimizers.Adam(learning_rate=config.learning_rate)" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 49, 324 | "metadata": { 325 | "id": "aphJD-jliW-7" 326 | }, 327 | "outputs": [], 328 | "source": [ 329 | "train_loss = tf.keras.metrics.Mean(name='train_loss')\n", 330 | "train_acc = tf.keras.metrics.CategoricalAccuracy(name='train_acc')\n", 331 | "\n", 332 | "val_loss = tf.keras.metrics.Mean(name='val_loss')\n", 333 | "val_acc = tf.keras.metrics.CategoricalAccuracy(name='val_acc')" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 50, 339 | "metadata": { 340 | "id": "3iczFAAliYiM" 341 | }, 342 | "outputs": [], 343 | "source": [ 344 | "def train_step(images, y):\n", 345 | " with tf.GradientTape() as tape:\n", 346 | " y_pred = model(images, training = True)\n", 347 | " loss = loss_function(y, y_pred)\n", 348 | "\n", 349 | " # calculate gradient\n", 350 | " gradient = tape.gradient(loss, model.trainable_variables)\n", 351 | " \n", 352 | " # update\n", 353 | " optimizer.apply_gradients(zip(gradient, model.trainable_variables))\n", 354 | "\n", 355 | " train_loss(loss)\n", 356 | " train_acc(y, y_pred)" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": 51, 362 | "metadata": { 363 | "id": "fvgcoybQiabE" 364 | }, 365 | "outputs": [], 366 | "source": [ 367 | "def val_step(images, y):\n", 368 | " y_pred = model(images, training=False)\n", 369 | " loss = loss_function(y, y_pred)\n", 370 | "\n", 371 | " val_loss(loss)\n", 372 | " val_acc(y, y_pred)" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 52, 378 | "metadata": { 379 | "id": "j7WRdIZOid4a" 380 | }, 381 | "outputs": [], 382 | "source": [ 383 | "def train():\n", 384 | " \n", 385 | " train_step_per_epoch = train_data.samples // batch_size\n", 386 | " val_step_per_epoch = val_data.samples // batch_size\n", 387 | "\n", 388 | " for epoch in range(epochs):\n", 389 | " train_loss.reset_state()\n", 390 | " train_acc.reset_state()\n", 391 | " val_loss.reset_state()\n", 392 | " val_acc.reset_state()\n", 393 | "\n", 394 | " \n", 395 | " for _ in tqdm(range(train_step_per_epoch)):\n", 396 | " images, labels = next(train_data)\n", 397 | " train_step(images, labels)\n", 398 | " \n", 399 | " for _ in tqdm(range(val_step_per_epoch)):\n", 400 | " images, labels = next(val_data)\n", 401 | " val_step(images, labels)\n", 402 | "\n", 403 | " print('epoch: {}'.format(epoch + 1))\n", 404 | " print('loss: {}'.format(train_loss.result()))\n", 405 | " print('accuracy: {}'.format(train_acc.result()))\n", 406 | " print('val_loss: {}'.format(val_loss.result()))\n", 407 | " print('val_accuracy: {}'.format(val_acc.result()))\n", 408 | "\n", 409 | "\n", 410 | " # log metrics using wandb.log\n", 411 | " wandb.log({'epochs': epoch + 1,\n", 412 | " 'loss': np.mean(train_loss.result()),\n", 413 | " 'acc': float(train_acc.result()), \n", 414 | " 'val_loss': np.mean(val_loss.result()),\n", 415 | " 'val_acc':float(val_acc.result())})" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": 53, 421 | "metadata": { 422 | "colab": { 423 | "base_uri": "https://localhost:8080/" 424 | }, 425 | "id": "vO1-6Dn8ifik", 426 | "outputId": "0b0736cf-05da-41cd-9cb7-6dd1215b00fb" 427 | }, 428 | "outputs": [ 429 | { 430 | "metadata": { 431 | "tags": null 432 | }, 433 | "name": "stderr", 434 | "output_type": "stream", 435 | "text": [ 436 | "100%|██████████| 68/68 [00:19<00:00, 3.49it/s]\n", 437 | "100%|██████████| 16/16 [00:04<00:00, 3.83it/s]\n" 438 | ] 439 | }, 440 | { 441 | "metadata": { 442 | "tags": null 443 | }, 444 | "name": "stdout", 445 | "output_type": "stream", 446 | "text": [ 447 | "epoch: 1\n", 448 | "loss: 2.636568069458008\n", 449 | "accuracy: 0.07534883916378021\n", 450 | "val_loss: 2.611361503601074\n", 451 | "val_accuracy: 0.0833333358168602\n" 452 | ] 453 | }, 454 | { 455 | "metadata": { 456 | "tags": null 457 | }, 458 | "name": "stderr", 459 | "output_type": "stream", 460 | "text": [ 461 | "100%|██████████| 68/68 [00:19<00:00, 3.58it/s]\n", 462 | "100%|██████████| 16/16 [00:04<00:00, 3.90it/s]\n" 463 | ] 464 | }, 465 | { 466 | "metadata": { 467 | "tags": null 468 | }, 469 | "name": "stdout", 470 | "output_type": "stream", 471 | "text": [ 472 | "epoch: 2\n", 473 | "loss: 2.592245101928711\n", 474 | "accuracy: 0.13209302723407745\n", 475 | "val_loss: 2.482846975326538\n", 476 | "val_accuracy: 0.2976190447807312\n" 477 | ] 478 | }, 479 | { 480 | "metadata": { 481 | "tags": null 482 | }, 483 | "name": "stderr", 484 | "output_type": "stream", 485 | "text": [ 486 | "100%|██████████| 68/68 [00:19<00:00, 3.57it/s]\n", 487 | "100%|██████████| 16/16 [00:04<00:00, 3.90it/s]\n" 488 | ] 489 | }, 490 | { 491 | "metadata": { 492 | "tags": null 493 | }, 494 | "name": "stdout", 495 | "output_type": "stream", 496 | "text": [ 497 | "epoch: 3\n", 498 | "loss: 2.3394315242767334\n", 499 | "accuracy: 0.23720930516719818\n", 500 | "val_loss: 2.0370912551879883\n", 501 | "val_accuracy: 0.4126984179019928\n" 502 | ] 503 | }, 504 | { 505 | "metadata": { 506 | "tags": null 507 | }, 508 | "name": "stderr", 509 | "output_type": "stream", 510 | "text": [ 511 | "100%|██████████| 68/68 [00:19<00:00, 3.55it/s]\n", 512 | "100%|██████████| 16/16 [00:04<00:00, 3.91it/s]\n" 513 | ] 514 | }, 515 | { 516 | "metadata": { 517 | "tags": null 518 | }, 519 | "name": "stdout", 520 | "output_type": "stream", 521 | "text": [ 522 | "epoch: 4\n", 523 | "loss: 1.9643079042434692\n", 524 | "accuracy: 0.3488371968269348\n", 525 | "val_loss: 1.6303080320358276\n", 526 | "val_accuracy: 0.4722222089767456\n" 527 | ] 528 | }, 529 | { 530 | "metadata": { 531 | "tags": null 532 | }, 533 | "name": "stderr", 534 | "output_type": "stream", 535 | "text": [ 536 | "100%|██████████| 68/68 [00:19<00:00, 3.54it/s]\n", 537 | "100%|██████████| 16/16 [00:04<00:00, 3.83it/s]\n" 538 | ] 539 | }, 540 | { 541 | "metadata": { 542 | "tags": null 543 | }, 544 | "name": "stdout", 545 | "output_type": "stream", 546 | "text": [ 547 | "epoch: 5\n", 548 | "loss: 1.6475915908813477\n", 549 | "accuracy: 0.4502325654029846\n", 550 | "val_loss: 1.3865840435028076\n", 551 | "val_accuracy: 0.5714285969734192\n" 552 | ] 553 | }, 554 | { 555 | "metadata": { 556 | "tags": null 557 | }, 558 | "name": "stderr", 559 | "output_type": "stream", 560 | "text": [ 561 | "100%|██████████| 68/68 [00:19<00:00, 3.52it/s]\n", 562 | "100%|██████████| 16/16 [00:04<00:00, 3.90it/s]\n" 563 | ] 564 | }, 565 | { 566 | "metadata": { 567 | "tags": null 568 | }, 569 | "name": "stdout", 570 | "output_type": "stream", 571 | "text": [ 572 | "epoch: 6\n", 573 | "loss: 1.540524959564209\n", 574 | "accuracy: 0.5041860342025757\n", 575 | "val_loss: 1.3172893524169922\n", 576 | "val_accuracy: 0.6111111044883728\n" 577 | ] 578 | }, 579 | { 580 | "metadata": { 581 | "tags": null 582 | }, 583 | "name": "stderr", 584 | "output_type": "stream", 585 | "text": [ 586 | "100%|██████████| 68/68 [00:19<00:00, 3.51it/s]\n", 587 | "100%|██████████| 16/16 [00:04<00:00, 3.90it/s]\n" 588 | ] 589 | }, 590 | { 591 | "metadata": { 592 | "tags": null 593 | }, 594 | "name": "stdout", 595 | "output_type": "stream", 596 | "text": [ 597 | "epoch: 7\n", 598 | "loss: 1.3113093376159668\n", 599 | "accuracy: 0.5832558274269104\n", 600 | "val_loss: 1.2582142353057861\n", 601 | "val_accuracy: 0.6150793433189392\n" 602 | ] 603 | }, 604 | { 605 | "metadata": { 606 | "tags": null 607 | }, 608 | "name": "stderr", 609 | "output_type": "stream", 610 | "text": [ 611 | "100%|██████████| 68/68 [00:19<00:00, 3.53it/s]\n", 612 | "100%|██████████| 16/16 [00:04<00:00, 3.89it/s]\n" 613 | ] 614 | }, 615 | { 616 | "metadata": { 617 | "tags": null 618 | }, 619 | "name": "stdout", 620 | "output_type": "stream", 621 | "text": [ 622 | "epoch: 8\n", 623 | "loss: 1.1835156679153442\n", 624 | "accuracy: 0.6139534711837769\n", 625 | "val_loss: 1.1214003562927246\n", 626 | "val_accuracy: 0.6507936716079712\n" 627 | ] 628 | }, 629 | { 630 | "metadata": { 631 | "tags": null 632 | }, 633 | "name": "stderr", 634 | "output_type": "stream", 635 | "text": [ 636 | "100%|██████████| 68/68 [00:19<00:00, 3.54it/s]\n", 637 | "100%|██████████| 16/16 [00:04<00:00, 3.89it/s]\n" 638 | ] 639 | }, 640 | { 641 | "metadata": { 642 | "tags": null 643 | }, 644 | "name": "stdout", 645 | "output_type": "stream", 646 | "text": [ 647 | "epoch: 9\n", 648 | "loss: 1.1117478609085083\n", 649 | "accuracy: 0.6353488564491272\n", 650 | "val_loss: 1.059070348739624\n", 651 | "val_accuracy: 0.7142857313156128\n" 652 | ] 653 | }, 654 | { 655 | "metadata": { 656 | "tags": null 657 | }, 658 | "name": "stderr", 659 | "output_type": "stream", 660 | "text": [ 661 | "100%|██████████| 68/68 [00:19<00:00, 3.54it/s]\n", 662 | "100%|██████████| 16/16 [00:04<00:00, 3.92it/s]\n" 663 | ] 664 | }, 665 | { 666 | "metadata": { 667 | "tags": null 668 | }, 669 | "name": "stdout", 670 | "output_type": "stream", 671 | "text": [ 672 | "epoch: 10\n", 673 | "loss: 1.0067936182022095\n", 674 | "accuracy: 0.6790697574615479\n", 675 | "val_loss: 0.8845313787460327\n", 676 | "val_accuracy: 0.7341269850730896\n" 677 | ] 678 | }, 679 | { 680 | "metadata": { 681 | "tags": null 682 | }, 683 | "name": "stderr", 684 | "output_type": "stream", 685 | "text": [ 686 | "100%|██████████| 68/68 [00:19<00:00, 3.51it/s]\n", 687 | "100%|██████████| 16/16 [00:04<00:00, 3.88it/s]\n" 688 | ] 689 | }, 690 | { 691 | "metadata": { 692 | "tags": null 693 | }, 694 | "name": "stdout", 695 | "output_type": "stream", 696 | "text": [ 697 | "epoch: 11\n", 698 | "loss: 0.9133316874504089\n", 699 | "accuracy: 0.70790696144104\n", 700 | "val_loss: 0.9504942893981934\n", 701 | "val_accuracy: 0.7222222089767456\n" 702 | ] 703 | }, 704 | { 705 | "metadata": { 706 | "tags": null 707 | }, 708 | "name": "stderr", 709 | "output_type": "stream", 710 | "text": [ 711 | "100%|██████████| 68/68 [00:19<00:00, 3.53it/s]\n", 712 | "100%|██████████| 16/16 [00:04<00:00, 3.94it/s]\n" 713 | ] 714 | }, 715 | { 716 | "metadata": { 717 | "tags": null 718 | }, 719 | "name": "stdout", 720 | "output_type": "stream", 721 | "text": [ 722 | "epoch: 12\n", 723 | "loss: 0.830324113368988\n", 724 | "accuracy: 0.7339534759521484\n", 725 | "val_loss: 0.9632441997528076\n", 726 | "val_accuracy: 0.7142857313156128\n" 727 | ] 728 | }, 729 | { 730 | "metadata": { 731 | "tags": null 732 | }, 733 | "name": "stderr", 734 | "output_type": "stream", 735 | "text": [ 736 | "100%|██████████| 68/68 [00:19<00:00, 3.56it/s]\n", 737 | "100%|██████████| 16/16 [00:04<00:00, 3.82it/s]\n" 738 | ] 739 | }, 740 | { 741 | "metadata": { 742 | "tags": null 743 | }, 744 | "name": "stdout", 745 | "output_type": "stream", 746 | "text": [ 747 | "epoch: 13\n", 748 | "loss: 0.748000979423523\n", 749 | "accuracy: 0.7525581121444702\n", 750 | "val_loss: 0.8080567717552185\n", 751 | "val_accuracy: 0.76171875\n" 752 | ] 753 | }, 754 | { 755 | "metadata": { 756 | "tags": null 757 | }, 758 | "name": "stderr", 759 | "output_type": "stream", 760 | "text": [ 761 | "100%|██████████| 68/68 [00:19<00:00, 3.53it/s]\n", 762 | "100%|██████████| 16/16 [00:04<00:00, 3.91it/s]\n" 763 | ] 764 | }, 765 | { 766 | "metadata": { 767 | "tags": null 768 | }, 769 | "name": "stdout", 770 | "output_type": "stream", 771 | "text": [ 772 | "epoch: 14\n", 773 | "loss: 0.6928069591522217\n", 774 | "accuracy: 0.7674418687820435\n", 775 | "val_loss: 0.7819676995277405\n", 776 | "val_accuracy: 0.7817460298538208\n" 777 | ] 778 | }, 779 | { 780 | "metadata": { 781 | "tags": null 782 | }, 783 | "name": "stderr", 784 | "output_type": "stream", 785 | "text": [ 786 | "100%|██████████| 68/68 [00:19<00:00, 3.54it/s]\n", 787 | "100%|██████████| 16/16 [00:04<00:00, 3.88it/s]\n" 788 | ] 789 | }, 790 | { 791 | "metadata": { 792 | "tags": null 793 | }, 794 | "name": "stdout", 795 | "output_type": "stream", 796 | "text": [ 797 | "epoch: 15\n", 798 | "loss: 0.6234845519065857\n", 799 | "accuracy: 0.806511640548706\n", 800 | "val_loss: 0.8339870572090149\n", 801 | "val_accuracy: 0.7579365372657776\n" 802 | ] 803 | }, 804 | { 805 | "metadata": { 806 | "tags": null 807 | }, 808 | "name": "stderr", 809 | "output_type": "stream", 810 | "text": [ 811 | "100%|██████████| 68/68 [00:19<00:00, 3.54it/s]\n", 812 | "100%|██████████| 16/16 [00:04<00:00, 3.85it/s]\n" 813 | ] 814 | }, 815 | { 816 | "metadata": { 817 | "tags": null 818 | }, 819 | "name": "stdout", 820 | "output_type": "stream", 821 | "text": [ 822 | "epoch: 16\n", 823 | "loss: 0.5811430811882019\n", 824 | "accuracy: 0.8176743984222412\n", 825 | "val_loss: 0.8494935035705566\n", 826 | "val_accuracy: 0.7698412537574768\n" 827 | ] 828 | }, 829 | { 830 | "metadata": { 831 | "tags": null 832 | }, 833 | "name": "stderr", 834 | "output_type": "stream", 835 | "text": [ 836 | "100%|██████████| 68/68 [00:19<00:00, 3.53it/s]\n", 837 | "100%|██████████| 16/16 [00:04<00:00, 3.94it/s]\n" 838 | ] 839 | }, 840 | { 841 | "metadata": { 842 | "tags": null 843 | }, 844 | "name": "stdout", 845 | "output_type": "stream", 846 | "text": [ 847 | "epoch: 17\n", 848 | "loss: 0.5330876111984253\n", 849 | "accuracy: 0.8241860270500183\n", 850 | "val_loss: 0.7843586206436157\n", 851 | "val_accuracy: 0.7658730149269104\n" 852 | ] 853 | }, 854 | { 855 | "metadata": { 856 | "tags": null 857 | }, 858 | "name": "stderr", 859 | "output_type": "stream", 860 | "text": [ 861 | "100%|██████████| 68/68 [00:19<00:00, 3.54it/s]\n", 862 | "100%|██████████| 16/16 [00:04<00:00, 3.92it/s]\n" 863 | ] 864 | }, 865 | { 866 | "metadata": { 867 | "tags": null 868 | }, 869 | "name": "stdout", 870 | "output_type": "stream", 871 | "text": [ 872 | "epoch: 18\n", 873 | "loss: 0.5095170736312866\n", 874 | "accuracy: 0.8316279053688049\n", 875 | "val_loss: 0.7897982597351074\n", 876 | "val_accuracy: 0.761904776096344\n" 877 | ] 878 | }, 879 | { 880 | "metadata": { 881 | "tags": null 882 | }, 883 | "name": "stderr", 884 | "output_type": "stream", 885 | "text": [ 886 | "100%|██████████| 68/68 [00:19<00:00, 3.53it/s]\n", 887 | "100%|██████████| 16/16 [00:04<00:00, 3.90it/s]\n" 888 | ] 889 | }, 890 | { 891 | "metadata": { 892 | "tags": null 893 | }, 894 | "name": "stdout", 895 | "output_type": "stream", 896 | "text": [ 897 | "epoch: 19\n", 898 | "loss: 0.4013980031013489\n", 899 | "accuracy: 0.8762790560722351\n", 900 | "val_loss: 0.8090662360191345\n", 901 | "val_accuracy: 0.7817460298538208\n" 902 | ] 903 | }, 904 | { 905 | "metadata": { 906 | "tags": null 907 | }, 908 | "name": "stderr", 909 | "output_type": "stream", 910 | "text": [ 911 | "100%|██████████| 68/68 [00:19<00:00, 3.53it/s]\n", 912 | "100%|██████████| 16/16 [00:04<00:00, 3.85it/s]\n" 913 | ] 914 | }, 915 | { 916 | "metadata": { 917 | "tags": null 918 | }, 919 | "name": "stdout", 920 | "output_type": "stream", 921 | "text": [ 922 | "epoch: 20\n", 923 | "loss: 0.41479194164276123\n", 924 | "accuracy: 0.8651162981987\n", 925 | "val_loss: 0.8082689046859741\n", 926 | "val_accuracy: 0.7777777910232544\n" 927 | ] 928 | }, 929 | { 930 | "metadata": { 931 | "tags": null 932 | }, 933 | "name": "stderr", 934 | "output_type": "stream", 935 | "text": [ 936 | "100%|██████████| 68/68 [00:19<00:00, 3.54it/s]\n", 937 | "100%|██████████| 16/16 [00:04<00:00, 3.84it/s]\n" 938 | ] 939 | }, 940 | { 941 | "metadata": { 942 | "tags": null 943 | }, 944 | "name": "stdout", 945 | "output_type": "stream", 946 | "text": [ 947 | "epoch: 21\n", 948 | "loss: 0.3756784200668335\n", 949 | "accuracy: 0.8865116238594055\n", 950 | "val_loss: 0.6631747484207153\n", 951 | "val_accuracy: 0.8015872836112976\n" 952 | ] 953 | }, 954 | { 955 | "metadata": { 956 | "tags": null 957 | }, 958 | "name": "stderr", 959 | "output_type": "stream", 960 | "text": [ 961 | "100%|██████████| 68/68 [00:19<00:00, 3.41it/s]\n", 962 | "100%|██████████| 16/16 [00:04<00:00, 3.72it/s]\n" 963 | ] 964 | }, 965 | { 966 | "metadata": { 967 | "tags": null 968 | }, 969 | "name": "stdout", 970 | "output_type": "stream", 971 | "text": [ 972 | "epoch: 22\n", 973 | "loss: 0.36110758781433105\n", 974 | "accuracy: 0.8706976771354675\n", 975 | "val_loss: 0.7182731032371521\n", 976 | "val_accuracy: 0.8055555820465088\n" 977 | ] 978 | }, 979 | { 980 | "metadata": { 981 | "tags": null 982 | }, 983 | "name": "stderr", 984 | "output_type": "stream", 985 | "text": [ 986 | "100%|██████████| 68/68 [00:20<00:00, 3.40it/s]\n", 987 | "100%|██████████| 16/16 [00:04<00:00, 3.69it/s]\n" 988 | ] 989 | }, 990 | { 991 | "metadata": { 992 | "tags": null 993 | }, 994 | "name": "stdout", 995 | "output_type": "stream", 996 | "text": [ 997 | "epoch: 23\n", 998 | "loss: 0.33967792987823486\n", 999 | "accuracy: 0.8799999952316284\n", 1000 | "val_loss: 0.7838874459266663\n", 1001 | "val_accuracy: 0.7857142686843872\n" 1002 | ] 1003 | }, 1004 | { 1005 | "metadata": { 1006 | "tags": null 1007 | }, 1008 | "name": "stderr", 1009 | "output_type": "stream", 1010 | "text": [ 1011 | "100%|██████████| 68/68 [00:19<00:00, 3.43it/s]\n", 1012 | "100%|██████████| 16/16 [00:04<00:00, 3.82it/s]\n" 1013 | ] 1014 | }, 1015 | { 1016 | "metadata": { 1017 | "tags": null 1018 | }, 1019 | "name": "stdout", 1020 | "output_type": "stream", 1021 | "text": [ 1022 | "epoch: 24\n", 1023 | "loss: 0.3033677637577057\n", 1024 | "accuracy: 0.8976744413375854\n", 1025 | "val_loss: 0.6791784763336182\n", 1026 | "val_accuracy: 0.8333333134651184\n" 1027 | ] 1028 | }, 1029 | { 1030 | "metadata": { 1031 | "tags": null 1032 | }, 1033 | "name": "stderr", 1034 | "output_type": "stream", 1035 | "text": [ 1036 | "100%|██████████| 68/68 [00:19<00:00, 3.53it/s]\n", 1037 | "100%|██████████| 16/16 [00:04<00:00, 3.98it/s]\n" 1038 | ] 1039 | }, 1040 | { 1041 | "metadata": { 1042 | "tags": null 1043 | }, 1044 | "name": "stdout", 1045 | "output_type": "stream", 1046 | "text": [ 1047 | "epoch: 25\n", 1048 | "loss: 0.25388339161872864\n", 1049 | "accuracy: 0.9181395173072815\n", 1050 | "val_loss: 0.7957773208618164\n", 1051 | "val_accuracy: 0.817460298538208\n" 1052 | ] 1053 | }, 1054 | { 1055 | "metadata": { 1056 | "tags": null 1057 | }, 1058 | "name": "stderr", 1059 | "output_type": "stream", 1060 | "text": [ 1061 | "100%|██████████| 68/68 [00:18<00:00, 3.60it/s]\n", 1062 | "100%|██████████| 16/16 [00:04<00:00, 3.92it/s]\n" 1063 | ] 1064 | }, 1065 | { 1066 | "metadata": { 1067 | "tags": null 1068 | }, 1069 | "name": "stdout", 1070 | "output_type": "stream", 1071 | "text": [ 1072 | "epoch: 26\n", 1073 | "loss: 0.2476063072681427\n", 1074 | "accuracy: 0.9116278886795044\n", 1075 | "val_loss: 0.8154959678649902\n", 1076 | "val_accuracy: 0.8015872836112976\n" 1077 | ] 1078 | }, 1079 | { 1080 | "metadata": { 1081 | "tags": null 1082 | }, 1083 | "name": "stderr", 1084 | "output_type": "stream", 1085 | "text": [ 1086 | "100%|██████████| 68/68 [00:18<00:00, 3.60it/s]\n", 1087 | "100%|██████████| 16/16 [00:03<00:00, 4.04it/s]\n" 1088 | ] 1089 | }, 1090 | { 1091 | "metadata": { 1092 | "tags": null 1093 | }, 1094 | "name": "stdout", 1095 | "output_type": "stream", 1096 | "text": [ 1097 | "epoch: 27\n", 1098 | "loss: 0.2381187379360199\n", 1099 | "accuracy: 0.9265116453170776\n", 1100 | "val_loss: 0.6258754730224609\n", 1101 | "val_accuracy: 0.841269850730896\n" 1102 | ] 1103 | }, 1104 | { 1105 | "metadata": { 1106 | "tags": null 1107 | }, 1108 | "name": "stderr", 1109 | "output_type": "stream", 1110 | "text": [ 1111 | "100%|██████████| 68/68 [00:19<00:00, 3.50it/s]\n", 1112 | "100%|██████████| 16/16 [00:04<00:00, 3.67it/s]\n" 1113 | ] 1114 | }, 1115 | { 1116 | "metadata": { 1117 | "tags": null 1118 | }, 1119 | "name": "stdout", 1120 | "output_type": "stream", 1121 | "text": [ 1122 | "epoch: 28\n", 1123 | "loss: 0.22210989892482758\n", 1124 | "accuracy: 0.9311627745628357\n", 1125 | "val_loss: 0.7616292834281921\n", 1126 | "val_accuracy: 0.8095238208770752\n" 1127 | ] 1128 | }, 1129 | { 1130 | "metadata": { 1131 | "tags": null 1132 | }, 1133 | "name": "stderr", 1134 | "output_type": "stream", 1135 | "text": [ 1136 | "100%|██████████| 68/68 [00:20<00:00, 3.35it/s]\n", 1137 | "100%|██████████| 16/16 [00:04<00:00, 3.68it/s]\n" 1138 | ] 1139 | }, 1140 | { 1141 | "metadata": { 1142 | "tags": null 1143 | }, 1144 | "name": "stdout", 1145 | "output_type": "stream", 1146 | "text": [ 1147 | "epoch: 29\n", 1148 | "loss: 0.18624596297740936\n", 1149 | "accuracy: 0.9376744031906128\n", 1150 | "val_loss: 0.7430930733680725\n", 1151 | "val_accuracy: 0.817460298538208\n" 1152 | ] 1153 | }, 1154 | { 1155 | "metadata": { 1156 | "tags": null 1157 | }, 1158 | "name": "stderr", 1159 | "output_type": "stream", 1160 | "text": [ 1161 | "100%|██████████| 68/68 [00:20<00:00, 3.37it/s]\n", 1162 | "100%|██████████| 16/16 [00:04<00:00, 3.59it/s]\n" 1163 | ] 1164 | }, 1165 | { 1166 | "metadata": { 1167 | "tags": null 1168 | }, 1169 | "name": "stdout", 1170 | "output_type": "stream", 1171 | "text": [ 1172 | "epoch: 30\n", 1173 | "loss: 0.18340212106704712\n", 1174 | "accuracy: 0.9413953423500061\n", 1175 | "val_loss: 0.7341812252998352\n", 1176 | "val_accuracy: 0.81640625\n" 1177 | ] 1178 | }, 1179 | { 1180 | "name": "stderr", 1181 | "output_type": "stream", 1182 | "text": [ 1183 | "100%|██████████| 68/68 [00:20<00:00, 3.32it/s]\n", 1184 | "100%|██████████| 16/16 [00:04<00:00, 3.94it/s]\n" 1185 | ] 1186 | }, 1187 | { 1188 | "name": "stdout", 1189 | "output_type": "stream", 1190 | "text": [ 1191 | "epoch: 31\n", 1192 | "loss: 0.15785783529281616\n", 1193 | "accuracy: 0.9488372206687927\n", 1194 | "val_loss: 0.7166642546653748\n", 1195 | "val_accuracy: 0.8333333134651184\n" 1196 | ] 1197 | }, 1198 | { 1199 | "name": "stderr", 1200 | "output_type": "stream", 1201 | "text": [ 1202 | "100%|██████████| 68/68 [00:18<00:00, 3.61it/s]\n", 1203 | "100%|██████████| 16/16 [00:03<00:00, 4.01it/s]\n" 1204 | ] 1205 | }, 1206 | { 1207 | "name": "stdout", 1208 | "output_type": "stream", 1209 | "text": [ 1210 | "epoch: 32\n", 1211 | "loss: 0.19436943531036377\n", 1212 | "accuracy: 0.9358139634132385\n", 1213 | "val_loss: 0.8168303966522217\n", 1214 | "val_accuracy: 0.8134920597076416\n" 1215 | ] 1216 | }, 1217 | { 1218 | "name": "stderr", 1219 | "output_type": "stream", 1220 | "text": [ 1221 | "100%|██████████| 68/68 [00:18<00:00, 3.64it/s]\n", 1222 | "100%|██████████| 16/16 [00:04<00:00, 4.00it/s]\n" 1223 | ] 1224 | }, 1225 | { 1226 | "name": "stdout", 1227 | "output_type": "stream", 1228 | "text": [ 1229 | "epoch: 33\n", 1230 | "loss: 0.14992006123065948\n", 1231 | "accuracy: 0.952558159828186\n", 1232 | "val_loss: 0.889971911907196\n", 1233 | "val_accuracy: 0.8095238208770752\n" 1234 | ] 1235 | }, 1236 | { 1237 | "name": "stderr", 1238 | "output_type": "stream", 1239 | "text": [ 1240 | "100%|██████████| 68/68 [00:18<00:00, 3.65it/s]\n", 1241 | "100%|██████████| 16/16 [00:04<00:00, 3.94it/s]\n" 1242 | ] 1243 | }, 1244 | { 1245 | "name": "stdout", 1246 | "output_type": "stream", 1247 | "text": [ 1248 | "epoch: 34\n", 1249 | "loss: 0.18822398781776428\n", 1250 | "accuracy: 0.934883713722229\n", 1251 | "val_loss: 0.7917313575744629\n", 1252 | "val_accuracy: 0.8015872836112976\n" 1253 | ] 1254 | }, 1255 | { 1256 | "name": "stderr", 1257 | "output_type": "stream", 1258 | "text": [ 1259 | "100%|██████████| 68/68 [00:19<00:00, 3.57it/s]\n", 1260 | "100%|██████████| 16/16 [00:03<00:00, 4.03it/s]\n" 1261 | ] 1262 | }, 1263 | { 1264 | "name": "stdout", 1265 | "output_type": "stream", 1266 | "text": [ 1267 | "epoch: 35\n", 1268 | "loss: 0.14502890408039093\n", 1269 | "accuracy: 0.950697660446167\n", 1270 | "val_loss: 0.744623064994812\n", 1271 | "val_accuracy: 0.817460298538208\n" 1272 | ] 1273 | }, 1274 | { 1275 | "name": "stderr", 1276 | "output_type": "stream", 1277 | "text": [ 1278 | "100%|██████████| 68/68 [00:18<00:00, 3.66it/s]\n", 1279 | "100%|██████████| 16/16 [00:03<00:00, 4.03it/s]\n" 1280 | ] 1281 | }, 1282 | { 1283 | "name": "stdout", 1284 | "output_type": "stream", 1285 | "text": [ 1286 | "epoch: 36\n", 1287 | "loss: 0.16980038583278656\n", 1288 | "accuracy: 0.9516279101371765\n", 1289 | "val_loss: 0.9215313196182251\n", 1290 | "val_accuracy: 0.7857142686843872\n" 1291 | ] 1292 | }, 1293 | { 1294 | "name": "stderr", 1295 | "output_type": "stream", 1296 | "text": [ 1297 | "100%|██████████| 68/68 [00:18<00:00, 3.71it/s]\n", 1298 | "100%|██████████| 16/16 [00:03<00:00, 4.05it/s]\n" 1299 | ] 1300 | }, 1301 | { 1302 | "name": "stdout", 1303 | "output_type": "stream", 1304 | "text": [ 1305 | "epoch: 37\n", 1306 | "loss: 0.15086960792541504\n", 1307 | "accuracy: 0.9627906680107117\n", 1308 | "val_loss: 0.7807472944259644\n", 1309 | "val_accuracy: 0.8095238208770752\n" 1310 | ] 1311 | }, 1312 | { 1313 | "name": "stderr", 1314 | "output_type": "stream", 1315 | "text": [ 1316 | "100%|██████████| 68/68 [00:18<00:00, 3.61it/s]\n", 1317 | "100%|██████████| 16/16 [00:03<00:00, 4.02it/s]\n" 1318 | ] 1319 | }, 1320 | { 1321 | "name": "stdout", 1322 | "output_type": "stream", 1323 | "text": [ 1324 | "epoch: 38\n", 1325 | "loss: 0.14101479947566986\n", 1326 | "accuracy: 0.9609302282333374\n", 1327 | "val_loss: 0.6482131481170654\n", 1328 | "val_accuracy: 0.8253968358039856\n" 1329 | ] 1330 | }, 1331 | { 1332 | "name": "stderr", 1333 | "output_type": "stream", 1334 | "text": [ 1335 | "100%|██████████| 68/68 [00:18<00:00, 3.66it/s]\n", 1336 | "100%|██████████| 16/16 [00:03<00:00, 4.06it/s]\n" 1337 | ] 1338 | }, 1339 | { 1340 | "name": "stdout", 1341 | "output_type": "stream", 1342 | "text": [ 1343 | "epoch: 39\n", 1344 | "loss: 0.1397770792245865\n", 1345 | "accuracy: 0.9534883499145508\n", 1346 | "val_loss: 0.711073637008667\n", 1347 | "val_accuracy: 0.829365074634552\n" 1348 | ] 1349 | }, 1350 | { 1351 | "name": "stderr", 1352 | "output_type": "stream", 1353 | "text": [ 1354 | "100%|██████████| 68/68 [00:18<00:00, 3.64it/s]\n", 1355 | "100%|██████████| 16/16 [00:04<00:00, 3.92it/s]\n" 1356 | ] 1357 | }, 1358 | { 1359 | "name": "stdout", 1360 | "output_type": "stream", 1361 | "text": [ 1362 | "epoch: 40\n", 1363 | "loss: 0.09143207222223282\n", 1364 | "accuracy: 0.9776744246482849\n", 1365 | "val_loss: 0.7403008341789246\n", 1366 | "val_accuracy: 0.841269850730896\n" 1367 | ] 1368 | }, 1369 | { 1370 | "name": "stderr", 1371 | "output_type": "stream", 1372 | "text": [ 1373 | "100%|██████████| 68/68 [00:18<00:00, 3.62it/s]\n", 1374 | "100%|██████████| 16/16 [00:04<00:00, 3.97it/s]\n" 1375 | ] 1376 | }, 1377 | { 1378 | "name": "stdout", 1379 | "output_type": "stream", 1380 | "text": [ 1381 | "epoch: 41\n", 1382 | "loss: 0.08669579774141312\n", 1383 | "accuracy: 0.9758139252662659\n", 1384 | "val_loss: 0.8374333381652832\n", 1385 | "val_accuracy: 0.8214285969734192\n" 1386 | ] 1387 | }, 1388 | { 1389 | "name": "stderr", 1390 | "output_type": "stream", 1391 | "text": [ 1392 | "100%|██████████| 68/68 [00:18<00:00, 3.69it/s]\n", 1393 | "100%|██████████| 16/16 [00:03<00:00, 4.11it/s]\n" 1394 | ] 1395 | }, 1396 | { 1397 | "name": "stdout", 1398 | "output_type": "stream", 1399 | "text": [ 1400 | "epoch: 42\n", 1401 | "loss: 0.11547188460826874\n", 1402 | "accuracy: 0.9590697884559631\n", 1403 | "val_loss: 0.8481805920600891\n", 1404 | "val_accuracy: 0.8333333134651184\n" 1405 | ] 1406 | }, 1407 | { 1408 | "name": "stderr", 1409 | "output_type": "stream", 1410 | "text": [ 1411 | "100%|██████████| 68/68 [00:18<00:00, 3.69it/s]\n", 1412 | "100%|██████████| 16/16 [00:03<00:00, 4.06it/s]\n" 1413 | ] 1414 | }, 1415 | { 1416 | "name": "stdout", 1417 | "output_type": "stream", 1418 | "text": [ 1419 | "epoch: 43\n", 1420 | "loss: 0.1034543439745903\n", 1421 | "accuracy: 0.9693022966384888\n", 1422 | "val_loss: 0.788997232913971\n", 1423 | "val_accuracy: 0.8214285969734192\n" 1424 | ] 1425 | }, 1426 | { 1427 | "name": "stderr", 1428 | "output_type": "stream", 1429 | "text": [ 1430 | "100%|██████████| 68/68 [00:18<00:00, 3.66it/s]\n", 1431 | "100%|██████████| 16/16 [00:03<00:00, 4.06it/s]\n" 1432 | ] 1433 | }, 1434 | { 1435 | "name": "stdout", 1436 | "output_type": "stream", 1437 | "text": [ 1438 | "epoch: 44\n", 1439 | "loss: 0.07476228475570679\n", 1440 | "accuracy: 0.9804651141166687\n", 1441 | "val_loss: 0.7074779272079468\n", 1442 | "val_accuracy: 0.8492063283920288\n" 1443 | ] 1444 | }, 1445 | { 1446 | "name": "stderr", 1447 | "output_type": "stream", 1448 | "text": [ 1449 | "100%|██████████| 68/68 [00:18<00:00, 3.63it/s]\n", 1450 | "100%|██████████| 16/16 [00:03<00:00, 4.06it/s]\n" 1451 | ] 1452 | }, 1453 | { 1454 | "name": "stdout", 1455 | "output_type": "stream", 1456 | "text": [ 1457 | "epoch: 45\n", 1458 | "loss: 0.07153719663619995\n", 1459 | "accuracy: 0.9702325463294983\n", 1460 | "val_loss: 0.7347460389137268\n", 1461 | "val_accuracy: 0.8690476417541504\n" 1462 | ] 1463 | }, 1464 | { 1465 | "name": "stderr", 1466 | "output_type": "stream", 1467 | "text": [ 1468 | "100%|██████████| 68/68 [00:18<00:00, 3.67it/s]\n", 1469 | "100%|██████████| 16/16 [00:03<00:00, 4.02it/s]\n" 1470 | ] 1471 | }, 1472 | { 1473 | "name": "stdout", 1474 | "output_type": "stream", 1475 | "text": [ 1476 | "epoch: 46\n", 1477 | "loss: 0.08088342100381851\n", 1478 | "accuracy: 0.9776744246482849\n", 1479 | "val_loss: 0.9370545744895935\n", 1480 | "val_accuracy: 0.8015872836112976\n" 1481 | ] 1482 | }, 1483 | { 1484 | "name": "stderr", 1485 | "output_type": "stream", 1486 | "text": [ 1487 | "100%|██████████| 68/68 [00:18<00:00, 3.63it/s]\n", 1488 | "100%|██████████| 16/16 [00:04<00:00, 3.96it/s]\n" 1489 | ] 1490 | }, 1491 | { 1492 | "name": "stdout", 1493 | "output_type": "stream", 1494 | "text": [ 1495 | "epoch: 47\n", 1496 | "loss: 0.08347026258707047\n", 1497 | "accuracy: 0.9720930457115173\n", 1498 | "val_loss: 0.7446317076683044\n", 1499 | "val_accuracy: 0.84765625\n" 1500 | ] 1501 | }, 1502 | { 1503 | "name": "stderr", 1504 | "output_type": "stream", 1505 | "text": [ 1506 | "100%|██████████| 68/68 [00:18<00:00, 3.64it/s]\n", 1507 | "100%|██████████| 16/16 [00:03<00:00, 4.04it/s]\n" 1508 | ] 1509 | }, 1510 | { 1511 | "name": "stdout", 1512 | "output_type": "stream", 1513 | "text": [ 1514 | "epoch: 48\n", 1515 | "loss: 0.07747302204370499\n", 1516 | "accuracy: 0.968372106552124\n", 1517 | "val_loss: 0.8426114916801453\n", 1518 | "val_accuracy: 0.85317462682724\n" 1519 | ] 1520 | }, 1521 | { 1522 | "name": "stderr", 1523 | "output_type": "stream", 1524 | "text": [ 1525 | "100%|██████████| 68/68 [00:18<00:00, 3.59it/s]\n", 1526 | "100%|██████████| 16/16 [00:03<00:00, 4.05it/s]\n" 1527 | ] 1528 | }, 1529 | { 1530 | "name": "stdout", 1531 | "output_type": "stream", 1532 | "text": [ 1533 | "epoch: 49\n", 1534 | "loss: 0.0809091255068779\n", 1535 | "accuracy: 0.9770220518112183\n", 1536 | "val_loss: 0.8185986876487732\n", 1537 | "val_accuracy: 0.8492063283920288\n" 1538 | ] 1539 | }, 1540 | { 1541 | "name": "stderr", 1542 | "output_type": "stream", 1543 | "text": [ 1544 | "100%|██████████| 68/68 [00:18<00:00, 3.65it/s]\n", 1545 | "100%|██████████| 16/16 [00:03<00:00, 4.03it/s]" 1546 | ] 1547 | }, 1548 | { 1549 | "name": "stdout", 1550 | "output_type": "stream", 1551 | "text": [ 1552 | "epoch: 50\n", 1553 | "loss: 0.06746424734592438\n", 1554 | "accuracy: 0.9720930457115173\n", 1555 | "val_loss: 0.9579612612724304\n", 1556 | "val_accuracy: 0.8134920597076416\n" 1557 | ] 1558 | }, 1559 | { 1560 | "name": "stderr", 1561 | "output_type": "stream", 1562 | "text": [ 1563 | "\n" 1564 | ] 1565 | } 1566 | ], 1567 | "source": [ 1568 | "train()" 1569 | ] 1570 | }, 1571 | { 1572 | "cell_type": "code", 1573 | "execution_count": 24, 1574 | "metadata": { 1575 | "id": "1hmGMzy1QISu" 1576 | }, 1577 | "outputs": [], 1578 | "source": [ 1579 | "model.save_weights(filepath='checkpoint',save_format='HDF5')" 1580 | ] 1581 | }, 1582 | { 1583 | "cell_type": "code", 1584 | "execution_count": null, 1585 | "metadata": { 1586 | "id": "lOFQqYjKknlE" 1587 | }, 1588 | "outputs": [], 1589 | "source": [] 1590 | } 1591 | ], 1592 | "metadata": { 1593 | "accelerator": "GPU", 1594 | "colab": { 1595 | "collapsed_sections": [], 1596 | "name": "FaceRecognition-ExpertMode.ipynb", 1597 | "provenance": [] 1598 | }, 1599 | "kernelspec": { 1600 | "display_name": "Python 3 (ipykernel)", 1601 | "language": "python", 1602 | "name": "python3" 1603 | }, 1604 | "language_info": { 1605 | "codemirror_mode": { 1606 | "name": "ipython", 1607 | "version": 3 1608 | }, 1609 | "file_extension": ".py", 1610 | "mimetype": "text/x-python", 1611 | "name": "python", 1612 | "nbconvert_exporter": "python", 1613 | "pygments_lexer": "ipython3", 1614 | "version": "3.8.10" 1615 | }, 1616 | "widgets": { 1617 | "application/vnd.jupyter.widget-state+json": { 1618 | "1144456bbb6046a0962bfeacfc6c6fb6": { 1619 | "model_module": "@jupyter-widgets/controls", 1620 | "model_module_version": "1.5.0", 1621 | "model_name": "FloatProgressModel", 1622 | "state": { 1623 | "_dom_classes": [], 1624 | "_model_module": "@jupyter-widgets/controls", 1625 | "_model_module_version": "1.5.0", 1626 | "_model_name": "FloatProgressModel", 1627 | "_view_count": null, 1628 | "_view_module": "@jupyter-widgets/controls", 1629 | "_view_module_version": "1.5.0", 1630 | "_view_name": "ProgressView", 1631 | "bar_style": "", 1632 | "description": "", 1633 | "description_tooltip": null, 1634 | "layout": "IPY_MODEL_7926b278b65d49dfb66b12e2397b0795", 1635 | "max": 1, 1636 | "min": 0, 1637 | "orientation": "horizontal", 1638 | "style": "IPY_MODEL_228ce7348e344cff93fa649d342376f8", 1639 | "value": 1 1640 | } 1641 | }, 1642 | "228ce7348e344cff93fa649d342376f8": { 1643 | "model_module": "@jupyter-widgets/controls", 1644 | "model_module_version": "1.5.0", 1645 | "model_name": "ProgressStyleModel", 1646 | "state": { 1647 | "_model_module": "@jupyter-widgets/controls", 1648 | "_model_module_version": "1.5.0", 1649 | "_model_name": "ProgressStyleModel", 1650 | "_view_count": null, 1651 | "_view_module": "@jupyter-widgets/base", 1652 | "_view_module_version": "1.2.0", 1653 | "_view_name": "StyleView", 1654 | "bar_color": null, 1655 | "description_width": "" 1656 | } 1657 | }, 1658 | "250937d1762a438cb58c9ff919e3f640": { 1659 | "model_module": "@jupyter-widgets/base", 1660 | "model_module_version": "1.2.0", 1661 | "model_name": "LayoutModel", 1662 | "state": { 1663 | "_model_module": "@jupyter-widgets/base", 1664 | "_model_module_version": "1.2.0", 1665 | "_model_name": "LayoutModel", 1666 | "_view_count": null, 1667 | "_view_module": "@jupyter-widgets/base", 1668 | "_view_module_version": "1.2.0", 1669 | "_view_name": "LayoutView", 1670 | "align_content": null, 1671 | "align_items": null, 1672 | "align_self": null, 1673 | "border": null, 1674 | "bottom": null, 1675 | "display": null, 1676 | "flex": null, 1677 | "flex_flow": null, 1678 | "grid_area": null, 1679 | "grid_auto_columns": null, 1680 | "grid_auto_flow": null, 1681 | "grid_auto_rows": null, 1682 | "grid_column": null, 1683 | "grid_gap": null, 1684 | "grid_row": null, 1685 | "grid_template_areas": null, 1686 | "grid_template_columns": null, 1687 | "grid_template_rows": null, 1688 | "height": null, 1689 | "justify_content": null, 1690 | "justify_items": null, 1691 | "left": null, 1692 | "margin": null, 1693 | "max_height": null, 1694 | "max_width": null, 1695 | "min_height": null, 1696 | "min_width": null, 1697 | "object_fit": null, 1698 | "object_position": null, 1699 | "order": null, 1700 | "overflow": null, 1701 | "overflow_x": null, 1702 | "overflow_y": null, 1703 | "padding": null, 1704 | "right": null, 1705 | "top": null, 1706 | "visibility": null, 1707 | "width": null 1708 | } 1709 | }, 1710 | "771422eabbcd4d479d9dc6343f76039e": { 1711 | "model_module": "@jupyter-widgets/controls", 1712 | "model_module_version": "1.5.0", 1713 | "model_name": "LabelModel", 1714 | "state": { 1715 | "_dom_classes": [], 1716 | "_model_module": "@jupyter-widgets/controls", 1717 | "_model_module_version": "1.5.0", 1718 | "_model_name": "LabelModel", 1719 | "_view_count": null, 1720 | "_view_module": "@jupyter-widgets/controls", 1721 | "_view_module_version": "1.5.0", 1722 | "_view_name": "LabelView", 1723 | "description": "", 1724 | "description_tooltip": null, 1725 | "layout": "IPY_MODEL_250937d1762a438cb58c9ff919e3f640", 1726 | "placeholder": "​", 1727 | "style": "IPY_MODEL_ae5d2d731a714291aa205553e0430684", 1728 | "value": " 0.02MB of 0.02MB uploaded (0.00MB deduped)\r" 1729 | } 1730 | }, 1731 | "7926b278b65d49dfb66b12e2397b0795": { 1732 | "model_module": "@jupyter-widgets/base", 1733 | "model_module_version": "1.2.0", 1734 | "model_name": "LayoutModel", 1735 | "state": { 1736 | "_model_module": "@jupyter-widgets/base", 1737 | "_model_module_version": "1.2.0", 1738 | "_model_name": "LayoutModel", 1739 | "_view_count": null, 1740 | "_view_module": "@jupyter-widgets/base", 1741 | "_view_module_version": "1.2.0", 1742 | "_view_name": "LayoutView", 1743 | "align_content": null, 1744 | "align_items": null, 1745 | "align_self": null, 1746 | "border": null, 1747 | "bottom": null, 1748 | "display": null, 1749 | "flex": null, 1750 | "flex_flow": null, 1751 | "grid_area": null, 1752 | "grid_auto_columns": null, 1753 | "grid_auto_flow": null, 1754 | "grid_auto_rows": null, 1755 | "grid_column": null, 1756 | "grid_gap": null, 1757 | "grid_row": null, 1758 | "grid_template_areas": null, 1759 | "grid_template_columns": null, 1760 | "grid_template_rows": null, 1761 | "height": null, 1762 | "justify_content": null, 1763 | "justify_items": null, 1764 | "left": null, 1765 | "margin": null, 1766 | "max_height": null, 1767 | "max_width": null, 1768 | "min_height": null, 1769 | "min_width": null, 1770 | "object_fit": null, 1771 | "object_position": null, 1772 | "order": null, 1773 | "overflow": null, 1774 | "overflow_x": null, 1775 | "overflow_y": null, 1776 | "padding": null, 1777 | "right": null, 1778 | "top": null, 1779 | "visibility": null, 1780 | "width": null 1781 | } 1782 | }, 1783 | "ae5d2d731a714291aa205553e0430684": { 1784 | "model_module": "@jupyter-widgets/controls", 1785 | "model_module_version": "1.5.0", 1786 | "model_name": "DescriptionStyleModel", 1787 | "state": { 1788 | "_model_module": "@jupyter-widgets/controls", 1789 | "_model_module_version": "1.5.0", 1790 | "_model_name": "DescriptionStyleModel", 1791 | "_view_count": null, 1792 | "_view_module": "@jupyter-widgets/base", 1793 | "_view_module_version": "1.2.0", 1794 | "_view_name": "StyleView", 1795 | "description_width": "" 1796 | } 1797 | }, 1798 | "e6fcbbf7d62d44b8b817e074d610fe00": { 1799 | "model_module": "@jupyter-widgets/base", 1800 | "model_module_version": "1.2.0", 1801 | "model_name": "LayoutModel", 1802 | "state": { 1803 | "_model_module": "@jupyter-widgets/base", 1804 | "_model_module_version": "1.2.0", 1805 | "_model_name": "LayoutModel", 1806 | "_view_count": null, 1807 | "_view_module": "@jupyter-widgets/base", 1808 | "_view_module_version": "1.2.0", 1809 | "_view_name": "LayoutView", 1810 | "align_content": null, 1811 | "align_items": null, 1812 | "align_self": null, 1813 | "border": null, 1814 | "bottom": null, 1815 | "display": null, 1816 | "flex": null, 1817 | "flex_flow": null, 1818 | "grid_area": null, 1819 | "grid_auto_columns": null, 1820 | "grid_auto_flow": null, 1821 | "grid_auto_rows": null, 1822 | "grid_column": null, 1823 | "grid_gap": null, 1824 | "grid_row": null, 1825 | "grid_template_areas": null, 1826 | "grid_template_columns": null, 1827 | "grid_template_rows": null, 1828 | "height": null, 1829 | "justify_content": null, 1830 | "justify_items": null, 1831 | "left": null, 1832 | "margin": null, 1833 | "max_height": null, 1834 | "max_width": null, 1835 | "min_height": null, 1836 | "min_width": null, 1837 | "object_fit": null, 1838 | "object_position": null, 1839 | "order": null, 1840 | "overflow": null, 1841 | "overflow_x": null, 1842 | "overflow_y": null, 1843 | "padding": null, 1844 | "right": null, 1845 | "top": null, 1846 | "visibility": null, 1847 | "width": null 1848 | } 1849 | }, 1850 | "ff6f79ef22ab421c993e54eee9cdbf5b": { 1851 | "model_module": "@jupyter-widgets/controls", 1852 | "model_module_version": "1.5.0", 1853 | "model_name": "VBoxModel", 1854 | "state": { 1855 | "_dom_classes": [], 1856 | "_model_module": "@jupyter-widgets/controls", 1857 | "_model_module_version": "1.5.0", 1858 | "_model_name": "VBoxModel", 1859 | "_view_count": null, 1860 | "_view_module": "@jupyter-widgets/controls", 1861 | "_view_module_version": "1.5.0", 1862 | "_view_name": "VBoxView", 1863 | "box_style": "", 1864 | "children": [ 1865 | "IPY_MODEL_771422eabbcd4d479d9dc6343f76039e", 1866 | "IPY_MODEL_1144456bbb6046a0962bfeacfc6c6fb6" 1867 | ], 1868 | "layout": "IPY_MODEL_e6fcbbf7d62d44b8b817e074d610fe00" 1869 | } 1870 | } 1871 | } 1872 | } 1873 | }, 1874 | "nbformat": 4, 1875 | "nbformat_minor": 1 1876 | } 1877 | -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/README.md: -------------------------------------------------------------------------------- 1 | # Face Recognition 2 | 3 | Face Recognition on Image using tensorflow and keras 4 | 5 | 6 | 7 | 8 | | ![angelina-jolie](https://user-images.githubusercontent.com/82975802/137978311-bd050d06-7ad4-4a0d-b115-09aa607327b4.jpg) | ![queen-elizabet](https://user-images.githubusercontent.com/82975802/137979290-a87714cf-f086-4258-a02a-99b5c97d4147.jpg)| 9 | | :---: | :---: | 10 | | ![Scarlett-Johansson](https://user-images.githubusercontent.com/82975802/137978399-8b11247a-7357-4215-80d6-db41f52b9bbf.jpg) | ![behnam-bani](https://user-images.githubusercontent.com/82975802/137979087-157d0c5d-65b2-400a-a647-7ecd75a167d7.jpg)| 11 | 12 | 13 | 14 | 15 | - Train Neural Network on 7-7 dataset using tensorflow and keras 16 | 17 | - [x] train.ipynb 18 | 19 | - [x] inference-img.py 20 | 21 | - [ ] train.py 22 | 23 | # 24 | 25 | ### Dataset: 26 | 27 | Dataset contain images in 14 classes 28 | 29 | Dataset link: [7-7 dataset]( https://drive.google.com/drive/folders/1WGSotRtFPYGuxPEGkWWRsBPlVXFSvl7p?usp=sharing) 30 | 31 | # 32 | 33 | ### Train: 34 | 35 | For training you can run `FaceRecognition_ExpertMode.ipynb` 36 | 37 | ``` 38 | ``` 39 | 40 | # 41 | 42 | ### Inference: 43 | 44 | 45 | First download model from this link: [MyModel](https://drive.google.com/file/d/1akU4IbNxq0JnarxF_SrL63TQaGfIA7b_/view?usp=sharing) 46 | 47 | Then, put your images in `./input` directory and run the following command. output images will be saved on `output` folder. 48 | 49 | ``` 50 | python3 inference-img.py --input input/queen-elizabet.jpg 51 | 52 | ``` 53 | 54 | # 55 | 56 | ### Useful links: 57 | 58 | Face-Alignment preprocessing used in the inference step: https://github.com/SajjadAemmi/Face-Alignment 59 | 60 | -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/__pycache__/config.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/__pycache__/config.cpython-38.pyc -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/__pycache__/model.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/__pycache__/model.cpython-38.pyc -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/config.py: -------------------------------------------------------------------------------- 1 | 2 | width = height = 112 3 | batch_size = 16 4 | epochs = 30 -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/inference-img.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import cv2 4 | from config import * 5 | from FaceAlignment.align_image import main 6 | import argparse 7 | from model import MyModel 8 | 9 | input_shape = (width ,height ,3) 10 | model = MyModel(14, (input_shape)) 11 | model.build((None, *input_shape)) 12 | model.load_weights('checkpoint') 13 | 14 | def get_optimal_font_scale(text, width): 15 | 16 | for scale in reversed(range(0, 60, 1)): 17 | textSize = cv2.getTextSize(text, fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=scale/10, thickness=1) 18 | new_width = textSize[0][0] 19 | if (new_width <= width): 20 | return scale/10 21 | return 1 22 | 23 | parser = argparse.ArgumentParser() 24 | parser.add_argument("--input", default='angelina-jolie.jpg', type=str) 25 | args = parser.parse_args() 26 | 27 | file_name, file_ext = os.path.splitext(os.path.basename(args.input)) 28 | 29 | img = cv2.imread(args.input) 30 | img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 31 | face_BB = main(img) 32 | 33 | try: 34 | face, BB = next(face_BB) 35 | face_img = cv2.resize(face, (width, height)) 36 | face_img = face_img / 255.0 37 | face_img = face_img.reshape(1, width, height, 3) 38 | 39 | y_pred = model(face_img, Training=False) 40 | prediction = np.argmax(y_pred) 41 | 42 | label_map = np.load('label_map.npy', allow_pickle='TRUE').item() 43 | key_list = list(label_map.keys()) 44 | val_list = list(label_map.values()) 45 | 46 | position = val_list.index(prediction) 47 | label = key_list[position] 48 | print('predicted label: {}'.format(label)) 49 | 50 | BB = np.array(BB) 51 | font_size = get_optimal_font_scale(label, img_rgb.shape[1] // 4) 52 | cv2.rectangle(img, pt1=(BB[0], BB[1]), pt2=(BB[2], BB[3]), color=(0, 255, 0),thickness=2) 53 | cv2.putText(img, label, (img_rgb.shape[0] // 12, img_rgb.shape[1] // 12), cv2.FONT_HERSHEY_SIMPLEX, font_size, (0, 255, 0), 2, 54 | cv2.LINE_AA) 55 | 56 | except: 57 | y_pred = 'Face not Detected' 58 | print('Face not Detected') 59 | 60 | 61 | cv2.imshow('Face Recognition', img) 62 | cv2.imwrite(os.path.join('output/{}'.format(file_name)+ '.jpg'), img) 63 | cv2.waitKey() -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/input/Leila_Hatami.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/input/Leila_Hatami.jpg -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/input/Scarlett-Johansson.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/input/Scarlett-Johansson.jpg -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/input/angelina-jolie.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/input/angelina-jolie.jpg -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/input/behnam-bani.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/input/behnam-bani.jpg -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/input/morgan-freeman.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/input/morgan-freeman.jpeg -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/input/queen-elizabet.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/input/queen-elizabet.jpg -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/label_map.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/label_map.npy -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorflow.keras.layers import Conv2D, Dense, Flatten, MaxPooling2D, Dropout 3 | from config import * 4 | from keras import Input 5 | 6 | 7 | class MyModel(tf.keras.Model): 8 | def __init__(self, num_class, input_shape): 9 | super().__init__() 10 | self.conv1 = Conv2D(32, (3, 3), activation='relu', input_shape = input_shape) 11 | self.conv2 = Conv2D(64, (3, 3), activation='relu') 12 | self.conv3 = Conv2D(128, (3, 3), activation='relu') 13 | self.conv4 = Conv2D(256, (3, 3), activation='relu') 14 | self.conv5 = Conv2D(128, (3, 3), activation='relu') 15 | self.maxpool = MaxPooling2D() 16 | self.flatten = Flatten() 17 | self.dropout = Dropout(0.2) 18 | self.fc1 = Dense(128, activation='relu') 19 | self.fc2 = Dense(64, activation='relu') 20 | self.fc3= Dense(num_class, activation='softmax') 21 | self.dim = input_shape 22 | 23 | 24 | def call(self, x, *args, **kwargs): 25 | conv1 = self.conv1(x) 26 | maxpool1 = self.maxpool(conv1) 27 | 28 | conv2 = self.conv2(maxpool1) 29 | maxpool2 = self.maxpool(conv2) 30 | 31 | conv3 = self.conv3(maxpool2) 32 | maxpool3 = self.maxpool(conv3) 33 | 34 | conv4 = self.conv4(maxpool3) 35 | maxpool4 = self.maxpool(conv4) 36 | 37 | 38 | flatten = self.flatten(maxpool4) 39 | dropout = self.dropout(flatten) 40 | fc1 = self.fc1(dropout) 41 | dropout = self.dropout(fc1) 42 | fc2 = self.fc2(dropout) 43 | dropout = self.dropout(fc2) 44 | output = self.fc3(dropout) 45 | 46 | return output 47 | 48 | def build_graph(self): 49 | x = Input(shape=(self.dim)) 50 | return tf.keras.Model(inputs=[x], outputs=self.call(x)) 51 | 52 | -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/output/Leila_Hatami.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/output/Leila_Hatami.jpg -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/output/Scarlett-Johansson.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/output/Scarlett-Johansson.jpg -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/output/angelina-jolie.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/output/angelina-jolie.jpg -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/output/behnam-bani.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/output/behnam-bani.jpg -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/output/morgan-freeman.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/output/morgan-freeman.jpg -------------------------------------------------------------------------------- /13-2- FaceRecognition-ExpertMode/output/queen-elizabet.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NahidEbrahimian/Deep-Learning-Tensorflow/ad25fcac5daedd0900804013986c0ea270fdada9/13-2- FaceRecognition-ExpertMode/output/queen-elizabet.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deep-Learning-Course 2 | 3 | ## 01- CNN and MLP 4 | 5 | - Test accuracy comparison between MLP and Deep Learning 6 | 7 | | Dataset | MLP (Machine Learning) | CNN + MLP (Deep Learning) | 8 | | :--- | :---: | :---: | 9 | | Mnist | 0.97 | 0.98 | 10 | |Fashion Mnist | 0.86 | 0.89 | 11 | |Cfar 10 | 0.37 | 0.63 | 12 | |Cfar 100 | 0.13 | 0.28 | 13 | 14 | # 15 | 16 | ## 02- Sheikh-Recognition 17 | 18 | - Train Neural Network for normal person and sheikh images classification 19 | 20 | - [x] train.ipynb 21 | 22 | - [x] inference.py 23 | 24 | #### Dataset: 25 | 26 | contain images from two classes, normal person and sheikh 27 | 28 | For inference run the following command: 29 | 30 | ``` 31 | !python3 inference.py --input_image test/image3.jpg 32 | 33 | ``` 34 | 35 | # 36 | 37 | ## 03- Sheikh-Recognition-bot 38 | 39 | - Train Neural Network for normal person and sheikh images classification 40 | 41 | - [x] train.ipynb 42 | 43 | - [x] bot.py 44 | 45 | #### Dataset: 46 | 47 | contain images from two classes, normal person and sheikh 48 | 49 | #### Usage: 50 | 51 | 1. Click [here](https://t.me/SheikhRecognition_bot) to open the chat with the bot in the Telegram app 52 | 53 | 2. Start the bot and send him a photo 54 | 55 | ## 04- Persian Face Recognition 56 | 57 | - [ ] train.ipynb 58 | 59 | - [x] preprocess.py 60 | 61 | - [ ] inference.py 62 | 63 | #### Preprocessing 64 | 65 | Preprocess stage consists of 4 common stages: detect, align, represent and verify. link: [Github]( https://github.com/serengil/retinaface#:~:text=RetinaFace%20is%20a%20deep%20learning,is%20published%20by%20Stanislas%20Bertrand.) 66 | 67 | 1. You must first install retinaface: 68 | 69 | ``` 70 | !pip install retina-face 71 | ``` 72 | 73 | 2. Run the following command to apply preprocessing: 74 | 75 | ``` 76 | !python3 preprocess.py --input_images_dir "./input_images" --output_dir "./output_dir" 77 | ``` 78 | 79 | # 80 | 81 | ## 05- 17Flowers Classification 82 | 83 | #### Dataset: 84 | 85 | Contain images from 17 classes of flowers in two subset, train and test. 86 | 87 | Dataset link: [Flowers]( https://drive.google.com/drive/folders/1-7GcWubgmhIImiZUrghV3haBjIeLoEhf?usp=sharing) 88 | 89 | #### Result: 90 | 91 | Comparison accuracy of pretrained models that used in transfer learning on test data: 92 | 93 | | Model | Accuracy | 94 | | :--- | :---: | 95 | | Vgg16 | 0.67 | 96 | |Vgg19 | 0.70 | 97 | |ResNet50V2 | 0.82 | 98 | |MobileNetV2 | 0.37 | 99 | 100 | # 101 | 102 | ## 07- UTKFace-Age prediction-Regression 103 | 104 | - Train Neural Network on UTKFace dataset using tensorflow and keras 105 | 106 | - [x] train.ipynb 107 | 108 | - [x] inference.py 109 | 110 | #### Dataset: 111 | 112 | Dataset link: [UTKFace-dataset]( https://www.kaggle.com/jangedoo/utkface-new) 113 | 114 | 115 | #### inference: 116 | 117 | 1- First install retina-face 118 | ``` 119 | !pip install retina-face 120 | ``` 121 | 122 | 2- Run the following command: 123 | 124 | ``` 125 | !python3 inference.py --image_path 'input/08.jpg' 126 | ``` 127 | 128 | ## 08- Gender Recognition 129 | 130 | - Train Neural Network on gender-recognition dataset using tensorflow and keras 131 | 132 | - [x] train.ipynb 133 | 134 | - [ ] inference.py 135 | 136 | #### Dataset: 137 | 138 | Dataset link: [celeba-dataset]( https://www.kaggle.com/ashishjangra27/gender-recognition-200k-images-celeba) 139 | 140 | 141 | ## 10- Houses Price Image Regression 142 | 143 | - Train Neural Network for house price prediction using images 144 | 145 | - [x] train.ipynb 146 | 147 | - [x] inference.py 148 | 149 | Reference: [KerasRegressionandCNNs]( https://www.pyimagesearch.com/2019/01/28/keras-regression-and-cnns/) 150 | 151 | # 152 | 153 | ## 11- DCGAN_on_Mnist_dataset 154 | 155 | - Training DCGAN on Mnist dataset 156 | 157 | ![DCGAN-02](https://user-images.githubusercontent.com/82975802/135684094-e46ae7fd-4d25-401c-b91b-7fe30cef7164.gif) 158 | 159 | Reference: [tensorflow-tutorials]( https://www.tensorflow.org/tutorials/generative/dcgan) 160 | 161 | ## 12- DCGAN_on_Celeb_A_dataset 162 | 163 | - Face Generator, Training DCGAN on celeba dataset 164 | 165 | ![DCGAN-Celeb_A-02 (1)](https://user-images.githubusercontent.com/82975802/135740954-117c825d-d07e-4b8f-a100-74d61a72b933.gif) 166 | 167 | Dataset link: [celeba-dataset]( https://www.kaggle.com/ashishjangra27/gender-recognition-200k-images-celeba) 168 | 169 | --------------------------------------------------------------------------------