From a0af3c9fa95fd4042e177eb9c14a584c352b43d8 Mon Sep 17 00:00:00 2001 From: saiteja gande Date: Sat, 12 Aug 2023 22:45:43 +0200 Subject: [PATCH 1/2] Updated Model and fixed error --- Section #4 - Capstone/simpsons.ipynb | 379 +++++++++++++++------------ 1 file changed, 208 insertions(+), 171 deletions(-) diff --git a/Section #4 - Capstone/simpsons.ipynb b/Section #4 - Capstone/simpsons.ipynb index 3d8e127..0aca521 100644 --- a/Section #4 - Capstone/simpsons.ipynb +++ b/Section #4 - Capstone/simpsons.ipynb @@ -1,48 +1,30 @@ { - "nbformat": 4, - "nbformat_minor": 2, - "metadata": { - "kernelspec": { - "language": "python", - "display_name": "Python 3", - "name": "python3" - }, - "language_info": { - "pygments_lexer": "ipython3", - "nbconvert_exporter": "python", - "version": "3.6.4", - "file_extension": ".py", - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "name": "python", - "mimetype": "text/x-python" - }, - "colab": { - "name": "simpsons.ipynb", - "provenance": [] - } - }, "cells": [ { "cell_type": "code", "execution_count": null, - "source": [ - "# Installing `caer` and `canaro` since they don't come pre-installed\n", - "!pip install --upgrade caer canaro" - ], - "outputs": [], "metadata": { - "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5", "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", - "trusted": true, - "id": "MQoxjirY4S5o" - } + "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5", + "id": "MQoxjirY4S5o", + "trusted": true + }, + "outputs": [], + "source": [ + "# Installing `caer` and `canaro` since they don't come pre-installed\n", + "# !pip install --upgrade caer canaro" + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0", + "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a", + "id": "_yldS7bG4S58", + "trusted": true + }, + "outputs": [], "source": [ "import os\n", "import caer\n", @@ -51,32 +33,30 @@ "import cv2 as cv\n", "import gc\n", "#pylint:disable=no-member (Removes linting problems with cv)" - ], - "outputs": [], - "metadata": { - "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a", - "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0", - "trusted": true, - "id": "_yldS7bG4S58" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "TuC64gTq4S6K", + "trusted": true + }, + "outputs": [], "source": [ "IMG_SIZE = (80,80)\n", "channels = 1\n", "char_path = r'../input/the-simpsons-characters-dataset/simpsons_dataset'" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "TuC64gTq4S6K" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "RD5OHUE84S6U", + "trusted": true + }, + "outputs": [], "source": [ "# Creating a character dictionary, sorting it in descending order\n", "char_dict = {}\n", @@ -86,16 +66,16 @@ "# Sort in descending order\n", "char_dict = caer.sort_dict(char_dict, descending=True)\n", "char_dict" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "RD5OHUE84S6U" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "OQ09DqmI4S6g", + "trusted": true + }, + "outputs": [], "source": [ "# Getting the first 10 categories with the most number of images\n", "characters = []\n", @@ -106,71 +86,71 @@ " if count >= 10:\n", " break\n", "characters" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "OQ09DqmI4S6g" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "X4UnTWk74S6q", + "trusted": true + }, + "outputs": [], "source": [ "# Create the training data\n", "train = caer.preprocess_from_dir(char_path, characters, channels=channels, IMG_SIZE=IMG_SIZE, isShuffle=True)" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "X4UnTWk74S6q" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "ZaSuzC2J4S6z", + "trusted": true + }, + "outputs": [], "source": [ "# Number of training samples\n", "len(train)" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "ZaSuzC2J4S6z" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "hSw-V2H24S7A", + "trusted": true + }, + "outputs": [], "source": [ "# Visualizing the data (OpenCV doesn't display well in Jupyter notebooks)\n", "import matplotlib.pyplot as plt\n", "plt.figure(figsize=(30,30))\n", "plt.imshow(train[0][0], cmap='gray')\n", "plt.show()" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "hSw-V2H24S7A" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "arO-90034S7J", + "trusted": true + }, + "outputs": [], "source": [ "# Separating the array and corresponding labels\n", "featureSet, labels = caer.sep_train(train, IMG_SIZE=IMG_SIZE)" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "arO-90034S7J" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "Sl8VnLCY4S7O", + "trusted": true + }, + "outputs": [], "source": [ "from tensorflow.keras.utils import to_categorical\n", "\n", @@ -178,98 +158,135 @@ "featureSet = caer.normalize(featureSet)\n", "# Converting numerical labels to binary class vectors\n", "labels = to_categorical(labels, len(characters))" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "Sl8VnLCY4S7O" - } + ] }, { "cell_type": "code", "execution_count": null, - "source": [ - "x_train, x_val, y_train, y_val = caer.train_test_split(featureSet, labels, val_ratio=.2)" - ], - "outputs": [], "metadata": { - "trusted": true, - "id": "pzXXrqbt4S7S" - } + "id": "pzXXrqbt4S7S", + "trusted": true + }, + "outputs": [], + "source": [ + "x_train, x_val, y_train, y_val = caer.train_val_split(featureSet, labels, val_ratio=.2)" + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "emsrpYWZ4S7W", + "trusted": true + }, + "outputs": [], "source": [ "del train\n", "del featureSet\n", "del labels \n", "gc.collect()" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "emsrpYWZ4S7W" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "NkS1ceD94S7a", + "trusted": true + }, + "outputs": [], "source": [ "# Useful variables when training\n", "BATCH_SIZE = 32\n", "EPOCHS = 10" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "NkS1ceD94S7a" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "_atEyygG4S7g", + "trusted": true + }, + "outputs": [], "source": [ "# Image data generator (introduces randomness in network ==> better accuracy)\n", "datagen = canaro.generators.imageDataGenerator()\n", "train_gen = datagen.flow(x_train, y_train, batch_size=BATCH_SIZE)" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "_atEyygG4S7g" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "Y8fjXBuH4S7m", + "trusted": true + }, + "outputs": [], "source": [ "# Create our model (returns a compiled model)\n", - "model = canaro.models.createSimpsonsModel(IMG_SIZE=IMG_SIZE, channels=channels, output_dim=len(characters), \n", - " loss='binary_crossentropy', decay=1e-7, learning_rate=0.001, momentum=0.9,\n", - " nesterov=True)" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "Y8fjXBuH4S7m" - } + "from tensorflow.keras.models import Sequential\n", + "from tensorflow.keras.layers import Dense, Flatten, Dropout, Conv2D, MaxPooling2D\n", + "\n", + "output_dim=10\n", + "\n", + "w, h = IMG_SIZE[:2]\n", + "\n", + "model = Sequential()\n", + "model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(w, h,channels)))\n", + "model.add(Conv2D(32, (3, 3), activation='relu'))\n", + "model.add(MaxPooling2D(pool_size=(2, 2)))\n", + "model.add(Dropout(0.2))\n", + "\n", + "model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))\n", + "model.add(Conv2D(64, (3, 3), activation='relu'))\n", + "model.add(MaxPooling2D(pool_size=(2, 2)))\n", + "model.add(Dropout(0.2))\n", + "\n", + "model.add(Conv2D(256, (3, 3), padding='same', activation='relu')) \n", + "model.add(Conv2D(256, (3, 3), activation='relu'))\n", + "model.add(MaxPooling2D(pool_size=(2, 2)))\n", + "model.add(Dropout(0.2))\n", + "\n", + "model.add(Flatten())\n", + "model.add(Dropout(0.5))\n", + "model.add(Dense(1024, activation='relu'))\n", + "\n", + "# Output Layer\n", + "model.add(Dense(output_dim, activation='softmax'))\n" + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "CepcT54J4S7t", + "trusted": true + }, + "outputs": [], "source": [ "model.summary()" - ], + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, "outputs": [], - "metadata": { - "trusted": true, - "id": "CepcT54J4S7t" - } + "source": [ + "from tensorflow.keras.optimizers.legacy import SGD\n", + "optimizer = SGD(learning_rate=0.001, decay=1e-7, momentum=0.9, nesterov=True)\n", + "model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])" + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "AknG90ch4S7-", + "trusted": true + }, + "outputs": [], "source": [ "# Training the model\n", "\n", @@ -281,37 +298,37 @@ " validation_data=(x_val,y_val),\n", " validation_steps=len(y_val)//BATCH_SIZE,\n", " callbacks = callbacks_list)" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "AknG90ch4S7-" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "CUifBqOG4S8G", + "trusted": true + }, + "outputs": [], "source": [ "characters" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "CUifBqOG4S8G" - } + ] }, { "cell_type": "markdown", - "source": [ - "## Testing" - ], "metadata": { "id": "qiNY_zSy6Quj" - } + }, + "source": [ + "## Testing" + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "ETnmB3DC4S8M", + "trusted": true + }, + "outputs": [], "source": [ "test_path = r'../input/the-simpsons-characters-dataset/kaggle_simpson_testset/kaggle_simpson_testset/charles_montgomery_burns_0.jpg'\n", "\n", @@ -319,53 +336,73 @@ "\n", "plt.imshow(img)\n", "plt.show()" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "ETnmB3DC4S8M" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "yJjmMuvj4S8T", + "trusted": true + }, + "outputs": [], "source": [ "def prepare(image):\n", " image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)\n", " image = cv.resize(image, IMG_SIZE)\n", " image = caer.reshape(image, IMG_SIZE, 1)\n", " return image" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "yJjmMuvj4S8T" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "hZTjUKKn4S8a", + "trusted": true + }, + "outputs": [], "source": [ "predictions = model.predict(prepare(img))" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "hZTjUKKn4S8a" - } + ] }, { "cell_type": "code", "execution_count": null, + "metadata": { + "id": "3a4AW8qT4S8g", + "trusted": true + }, + "outputs": [], "source": [ "# Getting class with the highest probability\n", "print(characters[np.argmax(predictions[0])])" - ], - "outputs": [], - "metadata": { - "trusted": true, - "id": "3a4AW8qT4S8g" - } + ] } - ] -} \ No newline at end of file + ], + "metadata": { + "colab": { + "name": "simpsons.ipynb", + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "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.9.17" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From cf574a429af2de58119051ea68fde188c9d41480 Mon Sep 17 00:00:00 2001 From: saiteja gande Date: Sat, 12 Aug 2023 22:53:02 +0200 Subject: [PATCH 2/2] Updated Model and fixed error --- Section #4 - Capstone/simpsons.py | 37 ++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/Section #4 - Capstone/simpsons.py b/Section #4 - Capstone/simpsons.py index d96fce4..ce5ad5a 100644 --- a/Section #4 - Capstone/simpsons.py +++ b/Section #4 - Capstone/simpsons.py @@ -13,6 +13,9 @@ import matplotlib.pyplot as plt from tensorflow.keras.utils import to_categorical from tensorflow.keras.callbacks import LearningRateScheduler +from tensorflow.keras.models import Sequential +from tensorflow.keras.layers import Dense, Flatten, Dropout, Conv2D, MaxPooling2D +from tensorflow.keras.optimizers.legacy import SGD IMG_SIZE = (80,80) channels = 1 @@ -59,7 +62,7 @@ # Creating train and validation data -x_train, x_val, y_train, y_val = caer.train_test_split(featureSet, labels, val_ratio=.2) +x_train, x_val, y_train, y_val = caer.train_val_split(featureSet, labels, val_ratio=.2) # Deleting variables to save memory del train @@ -76,14 +79,38 @@ train_gen = datagen.flow(x_train, y_train, batch_size=BATCH_SIZE) # Create our model (returns the compiled model) -model = canaro.models.createSimpsonsModel(IMG_SIZE=IMG_SIZE, channels=channels, output_dim=len(characters), - loss='binary_crossentropy', decay=1e-7, learning_rate=0.001, momentum=0.9, - nesterov=True) +output_dim=10 + +w, h = IMG_SIZE[:2] + +model = Sequential() +model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(w, h,channels))) +model.add(Conv2D(32, (3, 3), activation='relu')) +model.add(MaxPooling2D(pool_size=(2, 2))) +model.add(Dropout(0.2)) + +model.add(Conv2D(64, (3, 3), padding='same', activation='relu')) +model.add(Conv2D(64, (3, 3), activation='relu')) +model.add(MaxPooling2D(pool_size=(2, 2))) +model.add(Dropout(0.2)) + +model.add(Conv2D(256, (3, 3), padding='same', activation='relu')) +model.add(Conv2D(256, (3, 3), activation='relu')) +model.add(MaxPooling2D(pool_size=(2, 2))) +model.add(Dropout(0.2)) + +model.add(Flatten()) +model.add(Dropout(0.5)) +model.add(Dense(1024, activation='relu')) + +# Output Layer +model.add(Dense(output_dim, activation='softmax')) model.summary() # Training the model - +optimizer = SGD(learning_rate=0.001, decay=1e-7, momentum=0.9, nesterov=True) +model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy']) callbacks_list = [LearningRateScheduler(canaro.lr_schedule)] training = model.fit(train_gen, steps_per_epoch=len(x_train)//BATCH_SIZE,