File size: 11,486 Bytes
9c69f85 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
{
"cells": [
{
"cell_type": "markdown",
"id": "dedc2602",
"metadata": {},
"source": [
"# Creating a convolutional network"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "701fb5bd",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
" Layer (type) Output Shape Param # \n",
"=================================================================\n",
" conv2d (Conv2D) (None, 228, 150, 20) 1520 \n",
" \n",
" dropout (Dropout) (None, 228, 150, 20) 0 \n",
" \n",
" conv2d_1 (Conv2D) (None, 224, 146, 20) 10020 \n",
" \n",
" dropout_1 (Dropout) (None, 224, 146, 20) 0 \n",
" \n",
" max_pooling2d (MaxPooling2D (None, 74, 48, 20) 0 \n",
" ) \n",
" \n",
" conv2d_2 (Conv2D) (None, 70, 44, 20) 10020 \n",
" \n",
" dropout_2 (Dropout) (None, 70, 44, 20) 0 \n",
" \n",
" conv2d_3 (Conv2D) (None, 66, 40, 10) 5010 \n",
" \n",
" dropout_3 (Dropout) (None, 66, 40, 10) 0 \n",
" \n",
" max_pooling2d_1 (MaxPooling (None, 22, 13, 10) 0 \n",
" 2D) \n",
" \n",
" flatten (Flatten) (None, 2860) 0 \n",
" \n",
" dense (Dense) (None, 4) 11444 \n",
" \n",
"=================================================================\n",
"Total params: 38,014\n",
"Trainable params: 38,014\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"from tensorflow.keras import models, layers\n",
"\n",
"conv_network = models.Sequential()\n",
"conv_network.add(layers.Conv2D(20, (5,5), activation='relu', input_shape=(232, 154, 3)))\n",
"conv_network.add(layers.Dropout(0.2))\n",
"conv_network.add(layers.Conv2D(20, (5,5), activation='relu'))\n",
"conv_network.add(layers.Dropout(0.2))\n",
"conv_network.add(layers.MaxPooling2D(3,3))\n",
"conv_network.add(layers.Conv2D(20, (5,5), activation='relu'))\n",
"conv_network.add(layers.Dropout(0.2))\n",
"conv_network.add(layers.Conv2D(10, (5,5), activation='relu'))\n",
"conv_network.add(layers.Dropout(0.2))\n",
"conv_network.add(layers.MaxPooling2D(3,3))\n",
"conv_network.add(layers.Flatten())\n",
"conv_network.add(layers.Dense(4, activation='softmax'))\n",
"\n",
"optimizer=tf.keras.optimizers.Adam(learning_rate=0.02)\n",
"\n",
"conv_network.compile(optimizer=optimizer, loss='mse', metrics=['accuracy'])\n",
"\n",
"conv_network.summary()"
]
},
{
"cell_type": "markdown",
"id": "4ab96d93",
"metadata": {},
"source": [
"# Loading in the data"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "2a6353d7",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 2008 files belonging to 4 classes.\n",
"Using 1607 files for training.\n",
"Found 2008 files belonging to 4 classes.\n",
"Using 401 files for validation.\n"
]
}
],
"source": [
"data_dir = \"/Users/kerickwalker/src/dis/deep_learning/bat_data\"\n",
"\n",
"img_width = 154\n",
"img_height = 232\n",
"batch_size = 128\n",
"\n",
"# Load in the training data\n",
"training_data = tf.keras.utils.image_dataset_from_directory(\n",
" data_dir,\n",
" validation_split=0.2,\n",
" subset=\"training\",\n",
" seed=123,\n",
" image_size=(img_height, img_width),\n",
" batch_size=batch_size)\n",
"\n",
"# Load in validation data\n",
"validation_data = tf.keras.utils.image_dataset_from_directory(\n",
" data_dir,\n",
" validation_split=0.2,\n",
" subset=\"validation\",\n",
" seed=123,\n",
" image_size=(img_height, img_width),\n",
" batch_size=batch_size)"
]
},
{
"cell_type": "markdown",
"id": "cd4adeaa",
"metadata": {},
"source": [
"# Training convolutional network"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c1d53cef",
"metadata": {},
"outputs": [],
"source": [
"conv_network.fit(training_data, validation_data=validation_data, epochs=10)"
]
},
{
"cell_type": "markdown",
"id": "8a22d520",
"metadata": {},
"source": [
"# Transfer Learning with MobileNetV2"
]
},
{
"cell_type": "markdown",
"id": "7451e896",
"metadata": {},
"source": [
"#### Convert dataset to numpy array for preprocessing"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "32c2dd65",
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"from tensorflow.keras.applications import MobileNetV2\n",
"from tensorflow.keras import layers, models"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "bcff2372",
"metadata": {},
"outputs": [],
"source": [
"img_size = (232, 154) # MobileNetV2 input size\n",
"batch_size = 32\n",
"data_dir = \"/Users/kerickwalker/src/dis/deep_learning/bat_data\""
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "26d31c9f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 2008 images belonging to 4 classes.\n"
]
}
],
"source": [
"train_datagen = ImageDataGenerator(\n",
" rescale=1./255,\n",
" rotation_range=20,\n",
" width_shift_range=0.2,\n",
" height_shift_range=0.2,\n",
" shear_range=0.2,\n",
" zoom_range=0.2,\n",
" horizontal_flip=True,\n",
" fill_mode='nearest'\n",
")\n",
"\n",
"train_generator = train_datagen.flow_from_directory(\n",
" data_dir,\n",
" target_size=img_size,\n",
" batch_size=batch_size,\n",
" class_mode='categorical',\n",
" shuffle=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "cf420374",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:`input_shape` is undefined or non-square, or `rows` is not in [96, 128, 160, 192, 224]. Weights for input shape (224, 224) will be loaded as the default.\n"
]
}
],
"source": [
"base_model = MobileNetV2(\n",
" input_shape=(232, 154, 3),\n",
" include_top=False,\n",
" weights='imagenet'\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "e7e027fb",
"metadata": {},
"outputs": [],
"source": [
"for layer in base_model.layers:\n",
" layer.trainable = False"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "2bd9014d",
"metadata": {},
"outputs": [],
"source": [
"model = models.Sequential()\n",
"model.add(base_model)\n",
"model.add(layers.GlobalAveragePooling2D())\n",
"model.add(layers.Dense(256, activation='relu'))\n",
"model.add(layers.Dropout(0.5))\n",
"model.add(layers.Dense(4, activation='softmax'))"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "04aef745",
"metadata": {},
"outputs": [],
"source": [
"model.compile(\n",
" optimizer='adam',\n",
" loss='categorical_crossentropy',\n",
" metrics=['accuracy']\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "4f624f89",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/10\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2023-11-30 18:29:04.053048: I tensorflow/core/common_runtime/executor.cc:1197] [/device:CPU:0] (DEBUG INFO) Executor start aborting (this does not indicate an error and you can ignore this message): INVALID_ARGUMENT: You must feed a value for placeholder tensor 'Placeholder/_0' with dtype int32\n",
"\t [[{{node Placeholder/_0}}]]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"62/62 [==============================] - 38s 560ms/step - loss: 0.6074 - accuracy: 0.7657\n",
"Epoch 2/10\n",
"62/62 [==============================] - 44s 715ms/step - loss: 0.2596 - accuracy: 0.9018\n",
"Epoch 3/10\n",
"62/62 [==============================] - 50s 809ms/step - loss: 0.2202 - accuracy: 0.9165\n",
"Epoch 4/10\n",
"62/62 [==============================] - 52s 833ms/step - loss: 0.1985 - accuracy: 0.9276\n",
"Epoch 5/10\n",
"62/62 [==============================] - 51s 822ms/step - loss: 0.1963 - accuracy: 0.9276\n",
"Epoch 6/10\n",
"62/62 [==============================] - 57s 922ms/step - loss: 0.2040 - accuracy: 0.9236\n",
"Epoch 7/10\n",
"62/62 [==============================] - 57s 912ms/step - loss: 0.1698 - accuracy: 0.9357\n",
"Epoch 8/10\n",
"62/62 [==============================] - 52s 834ms/step - loss: 0.1672 - accuracy: 0.9332\n",
"Epoch 9/10\n",
"62/62 [==============================] - 50s 795ms/step - loss: 0.1603 - accuracy: 0.9408\n",
"Epoch 10/10\n",
"62/62 [==============================] - 48s 778ms/step - loss: 0.1711 - accuracy: 0.9332\n"
]
}
],
"source": [
"history = model.fit(\n",
" train_generator,\n",
" steps_per_epoch=train_generator.samples // batch_size,\n",
" epochs=10\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "disdl",
"language": "python",
"name": "disdl"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|