{ "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 }