File size: 11,747 Bytes
f275200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85dcc51c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import os\n",
    "import cv2\n",
    "import imghdr\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "945cdd4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#To create augmented dataset and store it locally\n",
    "#DONOT RUN THIS NOW THAT THE AUGMENTED DATASET HAS ALREADY BEEN CREATED\n",
    "ans=input(\"Are you sure you want to run this(y/n): \")\n",
    "if(ans==\"y\"): \n",
    "    from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
    "    datagen = ImageDataGenerator(\n",
    "            rotation_range=45,     \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",
    "    i = 0\n",
    "    for batch in datagen.flow_from_directory(directory=\"pepe_folder\", \n",
    "                                             batch_size=20,  \n",
    "                                             target_size=(256, 256),\n",
    "                                             color_mode=\"rgb\",\n",
    "                                             save_to_dir=r'data\\aug_pepe', \n",
    "                                             save_prefix='aug', \n",
    "                                             save_format='png'):\n",
    "        i += 1\n",
    "        if i > 60:\n",
    "            break\n",
    "\n",
    "    i = 0\n",
    "    for batch in datagen.flow_from_directory(directory=\"notpepe_folder\", \n",
    "                                             batch_size=20,  \n",
    "                                             target_size=(256, 256),\n",
    "                                             color_mode=\"rgb\",\n",
    "                                             save_to_dir=r'data\\aug_notpepe', \n",
    "                                             save_prefix='aug', \n",
    "                                             save_format='png'):\n",
    "        i += 1\n",
    "        if i > 60:\n",
    "            break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b27e514f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#to retrieve augmented dataset and remove corrupted files from it\n",
    "data_dir = 'data' \n",
    "image_exts = ['jpeg','jpg', 'bmp', 'png']\n",
    "for image_class in os.listdir(data_dir): \n",
    "    for image in os.listdir(os.path.join(data_dir, image_class)):\n",
    "        image_path = os.path.join(data_dir, image_class, image)\n",
    "        try: \n",
    "            img = cv2.imread(image_path)\n",
    "            tip = imghdr.what(image_path)\n",
    "            if tip not in image_exts: \n",
    "                print('Image not in ext list {}'.format(image_path))\n",
    "                os.remove(image_path)\n",
    "        except Exception as e: \n",
    "            print('Issue with image {}'.format(image_path))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75b52f06",
   "metadata": {},
   "outputs": [],
   "source": [
    "predata = tf.keras.utils.image_dataset_from_directory('data')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1d8486d",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_iterator = predata.as_numpy_iterator()\n",
    "batch = data_iterator.next()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81675bd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#to view a sample batch and verify the labels\n",
    "fig, ax = plt.subplots(ncols=4, figsize=(20,20))\n",
    "for idx, img in enumerate(batch[0][:4]):\n",
    "    ax[idx].imshow(img.astype(int))\n",
    "    ax[idx].title.set_text(batch[1][idx])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e55af779",
   "metadata": {},
   "outputs": [],
   "source": [
    "#scaling the images\n",
    "data = predata.map(lambda x,y: (x/255, y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91ca596a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#splitting the dataset into training, validation and testing\n",
    "train_size = int(len(data)*.7)\n",
    "val_size = int(len(data)*.2)+1\n",
    "test_size = int(len(data)*.1)\n",
    "train = data.take(train_size)\n",
    "val = data.skip(train_size).take(val_size)\n",
    "test = data.skip(train_size+val_size).take(test_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75fd8b96",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten,BatchNormalization,Input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2eb4c6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#building the model architecture\n",
    "model = Sequential()\n",
    "model.add(Input(shape=(256, 256, 3)))\n",
    "model.add(Conv2D(16, (3, 3), strides=1, activation='relu'))\n",
    "model.add(BatchNormalization())\n",
    "model.add(MaxPooling2D())\n",
    "\n",
    "model.add(Conv2D(32, (3, 3), strides=1, activation='relu'))\n",
    "model.add(BatchNormalization())\n",
    "model.add(MaxPooling2D())\n",
    "\n",
    "model.add(Conv2D(16, (3, 3), strides=1, activation='relu'))\n",
    "model.add(MaxPooling2D())\n",
    "\n",
    "model.add(Flatten())\n",
    "model.add(Dense(256, activation='relu'))\n",
    "model.add(Dense(1, activation='sigmoid'))\n",
    "\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "\n",
    "learning_rate = 0.0006\n",
    "optimizer = Adam(learning_rate=learning_rate)\n",
    "\n",
    "model.compile(optimizer=optimizer, loss=tf.losses.BinaryCrossentropy(), metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c639b2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#to view the model summary\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99a49621",
   "metadata": {},
   "outputs": [],
   "source": [
    "#saving logs\n",
    "logdir='logs'\n",
    "tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=logdir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c550f6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "hist = model.fit(train, epochs=10, validation_data=val, callbacks=[tensorboard_callback])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d19ccb13",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.metrics import Precision, Recall, BinaryAccuracy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6237387",
   "metadata": {},
   "outputs": [],
   "source": [
    "#calculating precision, recall and binary accuracy\n",
    "pre = Precision()\n",
    "re = Recall()\n",
    "acc = BinaryAccuracy()\n",
    "for batch in test.as_numpy_iterator(): \n",
    "    X, y = batch\n",
    "    yhat = model.predict(X)\n",
    "    pre.update_state(y, yhat)\n",
    "    re.update_state(y, yhat)\n",
    "    acc.update_state(y, yhat)\n",
    "print(pre.result(), re.result(), acc.result())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2db17093",
   "metadata": {},
   "outputs": [],
   "source": [
    "#calculating the F1 score and test accuracy\n",
    "from sklearn.metrics import f1_score\n",
    "test_images = []\n",
    "test_labels = []\n",
    "\n",
    "for image, label in test:\n",
    "    test_images.append(image)\n",
    "    test_labels.append(label)\n",
    "\n",
    "test_images = tf.concat(test_images, axis=0)\n",
    "test_labels = tf.concat(test_labels, axis=0)\n",
    "\n",
    "\n",
    "y_pred = model.predict(test_images)\n",
    "y_pred_binary = (y_pred > 0.5).astype(int)  \n",
    "y_true = test_labels  \n",
    "\n",
    "\n",
    "f1 = f1_score(y_true, y_pred_binary)\n",
    "\n",
    "print(\"F1 Score:\", f1)\n",
    "test_loss = model.evaluate(test)\n",
    "print(\"Test Loss:\", test_loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b937369d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#plotting the losses\n",
    "fig = plt.figure()\n",
    "plt.plot(hist.history['loss'], color='teal', label='Training Loss')\n",
    "plt.plot(hist.history['val_loss'], color='orange', label='Validation Loss')\n",
    "\n",
    "\n",
    "plt.axhline(y=test_loss[0], color='red', linestyle='--', label='Test Loss')\n",
    "\n",
    "fig.suptitle('Loss', fontsize=20)\n",
    "plt.legend(loc=\"upper left\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "618e8ed4",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(13):\n",
    "    img = cv2.imread(f\"C:/Users/91896/Desktop/not/OIP ({i}).jpeg\")\n",
    "    plt.imshow(img)\n",
    "    resize = tf.image.resize(img, (256,256))\n",
    "    plt.imshow(resize.numpy().astype(int))\n",
    "    plt.show()\n",
    "    yhat = model.predict(np.expand_dims(resize/255, 0))\n",
    "    yhat\n",
    "    if yhat > 0.5: \n",
    "        print(f'Pepe Found')\n",
    "    else:\n",
    "        print(f'Pepe Not Found')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a453a1ec",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'model' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 4\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtensorflow\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mkeras\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodels\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m load_model\n\u001b[0;32m      3\u001b[0m \u001b[38;5;66;03m# Saving the model\u001b[39;00m\n\u001b[1;32m----> 4\u001b[0m model\u001b[38;5;241m.\u001b[39msave(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mimageclassifier.keras\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'model' is not defined"
     ]
    }
   ],
   "source": [
    "from tensorflow.keras.models import load_model\n",
    "\n",
    "# Saving the model\n",
    "model.save('imageclassifier.keras')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9df6180d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aaac6285",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}