You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ResNet50 _hyperparam.ipynb 17 KiB

2 days ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stdout",
  10. "output_type": "stream",
  11. "text": [
  12. "Found 2199 images belonging to 2 classes.\n",
  13. "Found 549 images belonging to 2 classes.\n",
  14. "Training samples: 2199\n",
  15. "Validation samples: 549\n"
  16. ]
  17. }
  18. ],
  19. "source": [
  20. "import tensorflow as tf\n",
  21. "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
  22. "\n",
  23. "dataset_dir = 'C:/Users/vsavelev/GITHUB/DS_projet/jan24_cds_mushrooms/data'\n",
  24. "\n",
  25. "# Create ImageDataGenerator with validation split\n",
  26. "datagen = ImageDataGenerator(rescale=1.0/255, validation_split=0.2)\n",
  27. "\n",
  28. "train_generator = datagen.flow_from_directory(\n",
  29. " dataset_dir,\n",
  30. " target_size=(224, 224),\n",
  31. " batch_size=32,\n",
  32. " class_mode='categorical',\n",
  33. " subset='training' # Set as training data\n",
  34. ")\n",
  35. "\n",
  36. "validation_generator = datagen.flow_from_directory(\n",
  37. " dataset_dir,\n",
  38. " target_size=(224, 224),\n",
  39. " batch_size=32,\n",
  40. " class_mode='categorical',\n",
  41. " subset='validation' # Set as validation data\n",
  42. ")\n",
  43. "\n",
  44. "print(f'Training samples: {train_generator.samples}')\n",
  45. "print(f'Validation samples: {validation_generator.samples}')"
  46. ]
  47. },
  48. {
  49. "cell_type": "code",
  50. "execution_count": 2,
  51. "metadata": {},
  52. "outputs": [],
  53. "source": [
  54. "from tensorflow.keras.applications import ResNet50\n",
  55. "from tensorflow.keras import layers, models\n",
  56. "import keras_tuner as kt\n",
  57. "\n",
  58. "def build_model(hp):\n",
  59. " base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n",
  60. " base_model.trainable = False\n",
  61. "\n",
  62. " model = models.Sequential()\n",
  63. " model.add(base_model)\n",
  64. " model.add(layers.GlobalAveragePooling2D())\n",
  65. " model.add(layers.Dense(hp.Int('units', min_value=256, max_value=1024, step=256), activation='relu'))\n",
  66. " model.add(layers.Dropout(hp.Float('dropout', min_value=0.2, max_value=0.5, step=0.1)))\n",
  67. " model.add(layers.Dense(train_generator.num_classes, activation='softmax'))\n",
  68. " \n",
  69. " model.compile(optimizer=tf.keras.optimizers.Adam(hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])),\n",
  70. " loss='categorical_crossentropy',\n",
  71. " metrics=['accuracy'])\n",
  72. " \n",
  73. " return model\n"
  74. ]
  75. },
  76. {
  77. "cell_type": "code",
  78. "execution_count": 3,
  79. "metadata": {},
  80. "outputs": [],
  81. "source": [
  82. "#Set Up the Tuner\n",
  83. "\n",
  84. "tuner = kt.RandomSearch(\n",
  85. " build_model,\n",
  86. " objective='val_accuracy',\n",
  87. " max_trials=5,\n",
  88. " executions_per_trial=3,\n",
  89. " directory='my_dir',\n",
  90. " project_name='mushroom_classification'\n",
  91. ")\n"
  92. ]
  93. },
  94. {
  95. "cell_type": "code",
  96. "execution_count": 4,
  97. "metadata": {},
  98. "outputs": [
  99. {
  100. "name": "stdout",
  101. "output_type": "stream",
  102. "text": [
  103. "Trial 5 Complete [05h 32m 33s]\n",
  104. "val_accuracy: 0.8870673775672913\n",
  105. "\n",
  106. "Best val_accuracy So Far: 0.8870673775672913\n",
  107. "Total elapsed time: 11h 14m 56s\n"
  108. ]
  109. }
  110. ],
  111. "source": [
  112. "tuner.search(train_generator, epochs=10, validation_data=validation_generator)"
  113. ]
  114. },
  115. {
  116. "cell_type": "code",
  117. "execution_count": 5,
  118. "metadata": {},
  119. "outputs": [
  120. {
  121. "name": "stdout",
  122. "output_type": "stream",
  123. "text": [
  124. "\n",
  125. "The hyperparameter search is complete. The optimal number of units in the dense layer is 768.\n",
  126. "The optimal learning rate for the optimizer is 0.001.\n",
  127. "\n"
  128. ]
  129. },
  130. {
  131. "data": {
  132. "text/html": [
  133. "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">Model: \"sequential_1\"</span>\n",
  134. "</pre>\n"
  135. ],
  136. "text/plain": [
  137. "\u001b[1mModel: \"sequential_1\"\u001b[0m\n"
  138. ]
  139. },
  140. "metadata": {},
  141. "output_type": "display_data"
  142. },
  143. {
  144. "data": {
  145. "text/html": [
  146. "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
  147. "┃<span style=\"font-weight: bold\"> Layer (type) </span>┃<span style=\"font-weight: bold\"> Output Shape </span>┃<span style=\"font-weight: bold\"> Param # </span>┃\n",
  148. "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
  149. "│ resnet50 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Functional</span>) │ ? │ <span style=\"color: #00af00; text-decoration-color: #00af00\">23,587,712</span> │\n",
  150. "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
  151. "│ global_average_pooling2d_1 │ ? │ <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (unbuilt) │\n",
  152. "│ (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">GlobalAveragePooling2D</span>) │ │ │\n",
  153. "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
  154. "│ dense_2 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dense</span>) │ ? │ <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (unbuilt) │\n",
  155. "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
  156. "│ dropout_1 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dropout</span>) │ ? │ <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (unbuilt) │\n",
  157. "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
  158. "│ dense_3 (<span style=\"color: #0087ff; text-decoration-color: #0087ff\">Dense</span>) │ ? │ <span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (unbuilt) │\n",
  159. "└─────────────────────────────────┴────────────────────────┴───────────────┘\n",
  160. "</pre>\n"
  161. ],
  162. "text/plain": [
  163. "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓\n",
  164. "┃\u001b[1m \u001b[0m\u001b[1mLayer (type) \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mOutput Shape \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Param #\u001b[0m\u001b[1m \u001b[0m┃\n",
  165. "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩\n",
  166. "│ resnet50 (\u001b[38;5;33mFunctional\u001b[0m) │ ? │ \u001b[38;5;34m23,587,712\u001b[0m │\n",
  167. "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
  168. "│ global_average_pooling2d_1 │ ? │ \u001b[38;5;34m0\u001b[0m (unbuilt) │\n",
  169. "│ (\u001b[38;5;33mGlobalAveragePooling2D\u001b[0m) │ │ │\n",
  170. "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
  171. "│ dense_2 (\u001b[38;5;33mDense\u001b[0m) │ ? │ \u001b[38;5;34m0\u001b[0m (unbuilt) │\n",
  172. "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
  173. "│ dropout_1 (\u001b[38;5;33mDropout\u001b[0m) │ ? │ \u001b[38;5;34m0\u001b[0m (unbuilt) │\n",
  174. "├─────────────────────────────────┼────────────────────────┼───────────────┤\n",
  175. "│ dense_3 (\u001b[38;5;33mDense\u001b[0m) │ ? │ \u001b[38;5;34m0\u001b[0m (unbuilt) │\n",
  176. "└─────────────────────────────────┴────────────────────────┴───────────────┘\n"
  177. ]
  178. },
  179. "metadata": {},
  180. "output_type": "display_data"
  181. },
  182. {
  183. "data": {
  184. "text/html": [
  185. "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Total params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">23,587,712</span> (89.98 MB)\n",
  186. "</pre>\n"
  187. ],
  188. "text/plain": [
  189. "\u001b[1m Total params: \u001b[0m\u001b[38;5;34m23,587,712\u001b[0m (89.98 MB)\n"
  190. ]
  191. },
  192. "metadata": {},
  193. "output_type": "display_data"
  194. },
  195. {
  196. "data": {
  197. "text/html": [
  198. "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">0</span> (0.00 B)\n",
  199. "</pre>\n"
  200. ],
  201. "text/plain": [
  202. "\u001b[1m Trainable params: \u001b[0m\u001b[38;5;34m0\u001b[0m (0.00 B)\n"
  203. ]
  204. },
  205. "metadata": {},
  206. "output_type": "display_data"
  207. },
  208. {
  209. "data": {
  210. "text/html": [
  211. "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\"> Non-trainable params: </span><span style=\"color: #00af00; text-decoration-color: #00af00\">23,587,712</span> (89.98 MB)\n",
  212. "</pre>\n"
  213. ],
  214. "text/plain": [
  215. "\u001b[1m Non-trainable params: \u001b[0m\u001b[38;5;34m23,587,712\u001b[0m (89.98 MB)\n"
  216. ]
  217. },
  218. "metadata": {},
  219. "output_type": "display_data"
  220. }
  221. ],
  222. "source": [
  223. "best_hps = tuner.get_best_hyperparameters(num_trials=1)[0]\n",
  224. "\n",
  225. "print(f\"\"\"\n",
  226. "The hyperparameter search is complete. The optimal number of units in the dense layer is {best_hps.get('units')}.\n",
  227. "The optimal learning rate for the optimizer is {best_hps.get('learning_rate')}.\n",
  228. "\"\"\")\n",
  229. "\n",
  230. "# Build the best model\n",
  231. "model = tuner.hypermodel.build(best_hps)\n",
  232. "model.summary()"
  233. ]
  234. },
  235. {
  236. "cell_type": "code",
  237. "execution_count": 6,
  238. "metadata": {},
  239. "outputs": [
  240. {
  241. "name": "stdout",
  242. "output_type": "stream",
  243. "text": [
  244. "Epoch 1/10\n",
  245. "\u001b[1m68/68\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m164s\u001b[0m 2s/step - accuracy: 0.8856 - loss: 0.4458 - val_accuracy: 0.8860 - val_loss: 0.3645\n",
  246. "Epoch 2/10\n",
  247. "\u001b[1m 1/68\u001b[0m \u001b[37m━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[1m1:57\u001b[0m 2s/step - accuracy: 0.9062 - loss: 0.3521"
  248. ]
  249. },
  250. {
  251. "name": "stderr",
  252. "output_type": "stream",
  253. "text": [
  254. "c:\\Users\\vsavelev\\AppData\\Local\\anaconda3\\Lib\\contextlib.py:158: UserWarning: Your input ran out of data; interrupting training. Make sure that your dataset or generator can generate at least `steps_per_epoch * epochs` batches. You may need to use the `.repeat()` function when building your dataset.\n",
  255. " self.gen.throw(typ, value, traceback)\n"
  256. ]
  257. },
  258. {
  259. "name": "stdout",
  260. "output_type": "stream",
  261. "text": [
  262. "\u001b[1m68/68\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m2s\u001b[0m 11ms/step - accuracy: 0.9062 - loss: 0.3521 - val_accuracy: 1.0000 - val_loss: 0.0784\n",
  263. "Epoch 3/10\n",
  264. "\u001b[1m68/68\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m153s\u001b[0m 2s/step - accuracy: 0.8923 - loss: 0.3465 - val_accuracy: 0.8879 - val_loss: 0.3535\n",
  265. "Epoch 4/10\n",
  266. "\u001b[1m68/68\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m2s\u001b[0m 6ms/step - accuracy: 0.8750 - loss: 0.4432 - val_accuracy: 0.8000 - val_loss: 0.5806\n",
  267. "Epoch 5/10\n",
  268. "\u001b[1m68/68\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m151s\u001b[0m 2s/step - accuracy: 0.8817 - loss: 0.3742 - val_accuracy: 0.8860 - val_loss: 0.3608\n",
  269. "Epoch 6/10\n",
  270. "\u001b[1m68/68\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m2s\u001b[0m 8ms/step - accuracy: 0.9375 - loss: 0.2319 - val_accuracy: 1.0000 - val_loss: 0.0935\n",
  271. "Epoch 7/10\n",
  272. "\u001b[1m68/68\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m156s\u001b[0m 2s/step - accuracy: 0.8882 - loss: 0.3576 - val_accuracy: 0.8860 - val_loss: 0.3543\n",
  273. "Epoch 8/10\n",
  274. "\u001b[1m68/68\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m2s\u001b[0m 7ms/step - accuracy: 0.8438 - loss: 0.4161 - val_accuracy: 1.0000 - val_loss: 0.1267\n",
  275. "Epoch 9/10\n",
  276. "\u001b[1m68/68\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m154s\u001b[0m 2s/step - accuracy: 0.8969 - loss: 0.3322 - val_accuracy: 0.8860 - val_loss: 0.3748\n",
  277. "Epoch 10/10\n",
  278. "\u001b[1m68/68\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m2s\u001b[0m 6ms/step - accuracy: 0.9375 - loss: 0.2687 - val_accuracy: 1.0000 - val_loss: 0.1681\n"
  279. ]
  280. }
  281. ],
  282. "source": [
  283. "history = model.fit(\n",
  284. " train_generator,\n",
  285. " steps_per_epoch=train_generator.samples // train_generator.batch_size,\n",
  286. " validation_data=validation_generator,\n",
  287. " validation_steps=validation_generator.samples // validation_generator.batch_size,\n",
  288. " epochs=10\n",
  289. ")\n",
  290. "\n",
  291. "#This specifies the number of complete passes through the training dataset. Here, the model will train for 10 epochs."
  292. ]
  293. },
  294. {
  295. "cell_type": "code",
  296. "execution_count": null,
  297. "metadata": {},
  298. "outputs": [],
  299. "source": []
  300. },
  301. {
  302. "cell_type": "code",
  303. "execution_count": 7,
  304. "metadata": {},
  305. "outputs": [
  306. {
  307. "name": "stdout",
  308. "output_type": "stream",
  309. "text": [
  310. "\u001b[1m18/18\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m30s\u001b[0m 2s/step - accuracy: 0.8850 - loss: 0.3732\n",
  311. "Validation loss: 0.36864525079727173\n",
  312. "Validation accuracy: 0.8870673775672913\n"
  313. ]
  314. }
  315. ],
  316. "source": [
  317. "loss, accuracy = model.evaluate(validation_generator)\n",
  318. "print(f'Validation loss: {loss}')\n",
  319. "print(f'Validation accuracy: {accuracy}')\n"
  320. ]
  321. }
  322. ],
  323. "metadata": {
  324. "kernelspec": {
  325. "display_name": "base",
  326. "language": "python",
  327. "name": "python3"
  328. },
  329. "language_info": {
  330. "name": "python",
  331. "version": "3.11.7"
  332. }
  333. },
  334. "nbformat": 4,
  335. "nbformat_minor": 2
  336. }