{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing import image\nimport os\nimport random\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:01.735583Z","iopub.execute_input":"2023-12-05T19:04:01.735960Z","iopub.status.idle":"2023-12-05T19:04:05.571960Z","shell.execute_reply.started":"2023-12-05T19:04:01.735912Z","shell.execute_reply":"2023-12-05T19:04:05.570958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:05.573917Z","iopub.execute_input":"2023-12-05T19:04:05.574628Z","iopub.status.idle":"2023-12-05T19:04:05.594370Z","shell.execute_reply.started":"2023-12-05T19:04:05.574591Z","shell.execute_reply":"2023-12-05T19:04:05.593587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:05.595477Z","iopub.execute_input":"2023-12-05T19:04:05.595837Z","iopub.status.idle":"2023-12-05T19:04:05.607687Z","shell.execute_reply.started":"2023-12-05T19:04:05.595805Z","shell.execute_reply":"2023-12-05T19:04:05.606749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels.shape)\nprint(len(labels['breed'].unique())) # Number of classes\nprint(labels['breed'].unique())","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:05.611014Z","iopub.execute_input":"2023-12-05T19:04:05.611366Z","iopub.status.idle":"2023-12-05T19:04:05.619565Z","shell.execute_reply.started":"2023-12-05T19:04:05.611334Z","shell.execute_reply":"2023-12-05T19:04:05.618716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels[\"breed\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:05.620831Z","iopub.execute_input":"2023-12-05T19:04:05.621151Z","iopub.status.idle":"2023-12-05T19:04:05.631433Z","shell.execute_reply.started":"2023-12-05T19:04:05.621117Z","shell.execute_reply":"2023-12-05T19:04:05.630215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:05.633022Z","iopub.execute_input":"2023-12-05T19:04:05.633474Z","iopub.status.idle":"2023-12-05T19:04:05.643915Z","shell.execute_reply.started":"2023-12-05T19:04:05.633440Z","shell.execute_reply":"2023-12-05T19:04:05.643139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:05.645146Z","iopub.execute_input":"2023-12-05T19:04:05.645731Z","iopub.status.idle":"2023-12-05T19:04:05.662300Z","shell.execute_reply.started":"2023-12-05T19:04:05.645698Z","shell.execute_reply":"2023-12-05T19:04:05.661401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Image resizing and pixel value normalization.","metadata":{}},{"cell_type":"code","source":"dataset_path = '/kaggle/input/dog-breed-identification/train'\nnew_size = (128, 128)\nresized_img_list = []\n\nfor image_id in os.listdir(dataset_path):\n    image_path = os.path.join(dataset_path, image_id)\n    img = image.load_img(image_path)\n    img_array = image.img_to_array(img)\n    resized_img_array = tf.image.resize(img_array, new_size)\n    resized_img_array = resized_img_array / 255.0\n    resized_img_list.append(resized_img_array)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:05.663626Z","iopub.execute_input":"2023-12-05T19:04:05.664014Z","iopub.status.idle":"2023-12-05T19:04:50.595728Z","shell.execute_reply.started":"2023-12-05T19:04:05.663980Z","shell.execute_reply":"2023-12-05T19:04:50.594691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_images = random.sample(resized_img_list, 20)\n\nplt.figure(figsize=(15, 8))\nfor i, img_array in enumerate(random_images, 1):\n    plt.subplot(4, 5, i)\n    plt.imshow(img_array)\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:50.596876Z","iopub.execute_input":"2023-12-05T19:04:50.597159Z","iopub.status.idle":"2023-12-05T19:04:51.630967Z","shell.execute_reply.started":"2023-12-05T19:04:50.597136Z","shell.execute_reply":"2023-12-05T19:04:51.630147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Preprocessing","metadata":{}},{"cell_type":"code","source":"X = np.array(resized_img_list)\ny = labels.loc[:, 'breed']\ny = np.array(y)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:51.634733Z","iopub.execute_input":"2023-12-05T19:04:51.635470Z","iopub.status.idle":"2023-12-05T19:04:54.721568Z","shell.execute_reply.started":"2023-12-05T19:04:51.635412Z","shell.execute_reply":"2023-12-05T19:04:54.720608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_temporary, y_train, y_temporary = train_test_split(X, y, train_size=0.8, random_state=1)\n\nX_val, X_test, y_val, y_test = train_test_split(X_temporary, y_temporary, train_size=0.5, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:54.723034Z","iopub.execute_input":"2023-12-05T19:04:54.723430Z","iopub.status.idle":"2023-12-05T19:04:55.427091Z","shell.execute_reply.started":"2023-12-05T19:04:54.723394Z","shell.execute_reply":"2023-12-05T19:04:55.426218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoder = LabelEncoder()\ny_train = label_encoder.fit_transform(y_train)\ny_val = label_encoder.transform(y_val)\ny_test = label_encoder.transform(y_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:55.428278Z","iopub.execute_input":"2023-12-05T19:04:55.428593Z","iopub.status.idle":"2023-12-05T19:04:55.436852Z","shell.execute_reply.started":"2023-12-05T19:04:55.428567Z","shell.execute_reply":"2023-12-05T19:04:55.435950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Length of the dataset: {len(X)}')\nprint(f'Lenght of the training dataset: {len(X_train)}')\nprint(f'Lenght of the validation dataset: {len(X_val)}')\nprint(f'Lenght of the test dataset: {len(X_test)}')","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:55.438404Z","iopub.execute_input":"2023-12-05T19:04:55.438746Z","iopub.status.idle":"2023-12-05T19:04:55.447439Z","shell.execute_reply.started":"2023-12-05T19:04:55.438715Z","shell.execute_reply":"2023-12-05T19:04:55.446517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Building the Model","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential()\nmodel.add(tf.keras.layers.Conv2D(16,\n                                 (3,3),\n                                 padding=\"same\",\n                                 activation=\"relu\",\n                                 input_shape=(128,128,3)))\nmodel.add(tf.keras.layers.MaxPooling2D((2,2)))\nmodel.add(tf.keras.layers.Conv2D(32, (3, 3), padding=\"same\", activation=\"relu\"))\nmodel.add(tf.keras.layers.MaxPooling2D((2, 2)))\nmodel.add(tf.keras.layers.Conv2D(64, (3, 3), padding=\"same\", activation=\"relu\"))\nmodel.add(tf.keras.layers.MaxPooling2D((2, 2)))\n\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(64, activation=\"relu\"))\nmodel.add(tf.keras.layers.Dropout(0.5))\nmodel.add(tf.keras.layers.Dense(64, activation=\"relu\"))\nmodel.add(tf.keras.layers.Dropout(0.5))\nmodel.add(tf.keras.layers.Dense(120, activation=\"softmax\"))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:55.448846Z","iopub.execute_input":"2023-12-05T19:04:55.449179Z","iopub.status.idle":"2023-12-05T19:04:55.613143Z","shell.execute_reply.started":"2023-12-05T19:04:55.449145Z","shell.execute_reply":"2023-12-05T19:04:55.612243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:55.614347Z","iopub.execute_input":"2023-12-05T19:04:55.614701Z","iopub.status.idle":"2023-12-05T19:04:55.634259Z","shell.execute_reply.started":"2023-12-05T19:04:55.614666Z","shell.execute_reply":"2023-12-05T19:04:55.633283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.fit(X_train, y_train, epochs=10, validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:04:55.635300Z","iopub.execute_input":"2023-12-05T19:04:55.635561Z","iopub.status.idle":"2023-12-05T19:05:31.450313Z","shell.execute_reply.started":"2023-12-05T19:04:55.635538Z","shell.execute_reply":"2023-12-05T19:05:31.449315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### We will run the model again by changing the number of convolutional layers, number of nodes, dropout and batch size.","metadata":{}},{"cell_type":"code","source":"model2 = tf.keras.Sequential()\nmodel2.add(tf.keras.layers.Conv2D(8,\n                                 (3,3),\n                                 padding=\"same\",\n                                 activation=\"relu\",\n                                 input_shape=(128,128,3)))\nmodel2.add(tf.keras.layers.MaxPooling2D((2,2)))\nmodel2.add(tf.keras.layers.Conv2D(8, (3, 3), padding=\"same\", activation=\"relu\"))\nmodel2.add(tf.keras.layers.MaxPooling2D((2, 2)))\n\nmodel2.add(tf.keras.layers.Flatten())\nmodel2.add(tf.keras.layers.Dense(16, activation=\"relu\"))\nmodel2.add(tf.keras.layers.Dropout(0.2))\nmodel2.add(tf.keras.layers.Dense(120, activation=\"softmax\"))\n\nmodel2.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:05:31.451643Z","iopub.execute_input":"2023-12-05T19:05:31.451973Z","iopub.status.idle":"2023-12-05T19:05:31.549426Z","shell.execute_reply.started":"2023-12-05T19:05:31.451913Z","shell.execute_reply":"2023-12-05T19:05:31.547842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.compile('adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:05:31.550487Z","iopub.execute_input":"2023-12-05T19:05:31.550749Z","iopub.status.idle":"2023-12-05T19:05:31.563073Z","shell.execute_reply.started":"2023-12-05T19:05:31.550728Z","shell.execute_reply":"2023-12-05T19:05:31.562072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results2 = model2.fit(X_train, y_train, batch_size=128, epochs=10, validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:05:31.564347Z","iopub.execute_input":"2023-12-05T19:05:31.564693Z","iopub.status.idle":"2023-12-05T19:05:52.474132Z","shell.execute_reply.started":"2023-12-05T19:05:31.564662Z","shell.execute_reply":"2023-12-05T19:05:52.473145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(results2.history[\"accuracy\"], label=\"accuracy\")\n\nplt.plot(results2.history[\"val_accuracy\"], label=\"val_accuracy\")\n\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Accuracy\")\n\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:05:52.475416Z","iopub.execute_input":"2023-12-05T19:05:52.475721Z","iopub.status.idle":"2023-12-05T19:05:52.721598Z","shell.execute_reply.started":"2023-12-05T19:05:52.475695Z","shell.execute_reply":"2023-12-05T19:05:52.720647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(results2.history[\"loss\"], label=\"loss\")\n\nplt.plot(results2.history[\"val_loss\"], label=\"val_loss\")\n\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\n\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-05T19:05:52.722802Z","iopub.execute_input":"2023-12-05T19:05:52.723188Z","iopub.status.idle":"2023-12-05T19:05:52.978211Z","shell.execute_reply.started":"2023-12-05T19:05:52.723162Z","shell.execute_reply":"2023-12-05T19:05:52.977286Z"},"trusted":true},"execution_count":null,"outputs":[]}]}