{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Import TensorFlow\nimport tensorflow as tf\n\n# Import Numpy and Matplotlib\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom keras.preprocessing.image import ImageDataGenerator\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-25T22:21:44.264639Z","iopub.execute_input":"2022-09-25T22:21:44.265331Z","iopub.status.idle":"2022-09-25T22:21:44.274830Z","shell.execute_reply.started":"2022-09-25T22:21:44.265295Z","shell.execute_reply":"2022-09-25T22:21:44.273584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import ssl\nssl._create_default_https_context = ssl._create_unverified_context\n# Importing the CIFAR-10 dataset from Keras \nfrom tensorflow.keras.datasets import cifar10\n(X_train, y_train), (X_test, y_test) = cifar10.load_data()","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:21:44.281763Z","iopub.execute_input":"2022-09-25T22:21:44.282141Z","iopub.status.idle":"2022-09-25T22:21:45.120451Z","shell.execute_reply.started":"2022-09-25T22:21:44.282105Z","shell.execute_reply":"2022-09-25T22:21:45.119482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Now, we can print the the number of samples in the X_train and X_test datasets to see how the dataset is distributed.","metadata":{}},{"cell_type":"code","source":"print(f\"X Train Size: {len(X_train)}\\n X Test Size: {len(X_test)}\" )","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:21:45.122729Z","iopub.execute_input":"2022-09-25T22:21:45.123085Z","iopub.status.idle":"2022-09-25T22:21:45.129186Z","shell.execute_reply.started":"2022-09-25T22:21:45.123049Z","shell.execute_reply":"2022-09-25T22:21:45.127993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Before we move on with the preprocessing of the data, we can print a random sample to see what the data looks like.","metadata":{}},{"cell_type":"code","source":"# Print a sample from X_test dataset\nprint(X_test[42]) #Pixel Values","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:21:45.133406Z","iopub.execute_input":"2022-09-25T22:21:45.137810Z","iopub.status.idle":"2022-09-25T22:21:45.146490Z","shell.execute_reply.started":"2022-09-25T22:21:45.137771Z","shell.execute_reply":"2022-09-25T22:21:45.145544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### The numbers we see here are pixel values. This is how computers see images. But like this, it doesn’t mean much to us. Let’s visualize this sample.","metadata":{}},{"cell_type":"code","source":"# Use the .imshow() function and show the plot\nplt.imshow(X_test[42])\nplt.show()\n# Print the shape of the sample image\nX_test[42].shape # RGB images have 3 channels ","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:21:45.147532Z","iopub.execute_input":"2022-09-25T22:21:45.148168Z","iopub.status.idle":"2022-09-25T22:21:45.338845Z","shell.execute_reply.started":"2022-09-25T22:21:45.148142Z","shell.execute_reply":"2022-09-25T22:21:45.337873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Preprocessing**\nGreat! With this knowledge, we can adjust our dataset for further use.\n\nWe need three datasets: train, validate, and test. We can separate 10000 images from the train dataset, and use it as the validation dataset","metadata":{}},{"cell_type":"code","source":"# Create the validation datasets \n# and assign the last 10000 images of X_train and y_train\nX_val = X_train[40000:]\ny_val = y_train[40000:]\n# Create new train datasets\n# and assign the first 40000 images of X_train and y_train\nX_train = X_train[:40000]\ny_train = y_train[:40000]","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:21:45.342146Z","iopub.execute_input":"2022-09-25T22:21:45.342465Z","iopub.status.idle":"2022-09-25T22:21:45.347360Z","shell.execute_reply.started":"2022-09-25T22:21:45.342436Z","shell.execute_reply":"2022-09-25T22:21:45.346270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Let's check the number of samples in each dataset to see the final results.","metadata":{}},{"cell_type":"code","source":"# Print the lengths of the each dataset\nprint(\"X Train Length:\", len(X_train))\nprint(\"X Test Length:\", len(X_test))\nprint(\"X Val Length:\", len(X_val))\nprint(\"Y Train Length:\", len(y_train))\nprint(\"Y Test Length:\", len(y_test))\nprint(\"Y Val Length:\", len(y_val))","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:21:45.349119Z","iopub.execute_input":"2022-09-25T22:21:45.349489Z","iopub.status.idle":"2022-09-25T22:21:45.358515Z","shell.execute_reply.started":"2022-09-25T22:21:45.349451Z","shell.execute_reply":"2022-09-25T22:21:45.357621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Normalization**\nSince pixel values are between 0 and 255, dividing these values by 255 will scale each of them to values between 0 and 1.","metadata":{}},{"cell_type":"code","source":"# Divide each dataset by 255\nX_train = X_train / 255\nX_test = X_test / 255\nX_val = X_val / 255","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:21:45.359446Z","iopub.execute_input":"2022-09-25T22:21:45.360669Z","iopub.status.idle":"2022-09-25T22:21:45.839769Z","shell.execute_reply.started":"2022-09-25T22:21:45.360634Z","shell.execute_reply":"2022-09-25T22:21:45.838763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Constructing the neural network**\nThat was it for the preprocessing of the data!\n\nNow we can create our model. First, we start by creating a model object using the Sequential API of Keras.","metadata":{}},{"cell_type":"code","source":"# Create a model object\nmodel = tf.keras.Sequential()","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:51:53.858120Z","iopub.execute_input":"2022-09-25T22:51:53.858475Z","iopub.status.idle":"2022-09-25T22:51:53.868132Z","shell.execute_reply.started":"2022-09-25T22:51:53.858441Z","shell.execute_reply":"2022-09-25T22:51:53.867061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Feature extraction layers**\nFor the first two layers, we add a convolution and max pooling layer.","metadata":{}},{"cell_type":"code","source":"model.add(tf.keras.layers.Conv2D(64,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\",\n                                 input_shape=(32,32,3)))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.Conv2D(64,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.Conv2D(64,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.MaxPooling2D((2,2)))\nmodel.add( tf.keras.layers.Dropout(0.20))\n\nmodel.add(tf.keras.layers.Conv2D(128,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.Conv2D(128,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.Conv2D(128,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add( tf.keras.layers.Dropout(0.25))\nmodel.add(tf.keras.layers.MaxPooling2D((2,2)))\n\nmodel.add(tf.keras.layers.Conv2D(256,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.Conv2D(256,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\nmodel.add(tf.keras.layers.Conv2D(256,\n                                 kernel_size=(3,3),\n                                 strides=(1,1),\n                                 padding=\"same\",\n                                 activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\n\n\nmodel.add(tf.keras.layers.MaxPooling2D((2,2)))\nmodel.add( tf.keras.layers.Dropout(0.35))","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:52:01.234118Z","iopub.execute_input":"2022-09-25T22:52:01.234478Z","iopub.status.idle":"2022-09-25T22:52:01.419284Z","shell.execute_reply.started":"2022-09-25T22:52:01.234444Z","shell.execute_reply":"2022-09-25T22:52:01.418358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Flatten**\nTo connect the 2D convolution and 1D dense layers, we have to \"flatten\" the convolution layer.","metadata":{}},{"cell_type":"markdown","source":"## **Classification layers**\nNow that we have the features extracted, we can move on to the classification part.","metadata":{}},{"cell_type":"code","source":"# Add the dense layer and dropout layer\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(512, activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\nmodel.add( tf.keras.layers.Dropout(0.5))\n\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(1028, activation=\"relu\"))\nmodel.add(tf.keras.layers.BatchNormalization())\nmodel.add( tf.keras.layers.Dropout(0.5))","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:52:08.156133Z","iopub.execute_input":"2022-09-25T22:52:08.156493Z","iopub.status.idle":"2022-09-25T22:52:08.218929Z","shell.execute_reply.started":"2022-09-25T22:52:08.156461Z","shell.execute_reply":"2022-09-25T22:52:08.217965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Output layer**\nAs the last part of our neural network, we add the output layer. The number of nodes will be equal to the number of target classes which is 10 in our case. We'll use the softmax activation function in the output layer.","metadata":{}},{"cell_type":"code","source":"# Add the output layer\nmodel.add(tf.keras.layers.Dense(10, activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:52:10.535761Z","iopub.execute_input":"2022-09-25T22:52:10.536129Z","iopub.status.idle":"2022-09-25T22:52:10.552662Z","shell.execute_reply.started":"2022-09-25T22:52:10.536098Z","shell.execute_reply":"2022-09-25T22:52:10.551743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Optimizer**\nNow we have the structure of our model. To configure the model for training, we'll use the .compile() method. Inside the compile method, we have to define the following:\n\n* \"Adam\" for optimizer\n* \"Sparse Categorical Crossentropy\" for the loss function","metadata":{}},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer=\"adam\",\n              loss=\"sparse_categorical_crossentropy\",\n              metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:52:12.521920Z","iopub.execute_input":"2022-09-25T22:52:12.522650Z","iopub.status.idle":"2022-09-25T22:52:12.535641Z","shell.execute_reply.started":"2022-09-25T22:52:12.522602Z","shell.execute_reply":"2022-09-25T22:52:12.534394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Training the model**\nIt's time to train the model. We'll give the X_train and y_train datasets as the first two arguments. These will be used for training. And with the validation_data parameter, we'll give the X_val and y_val as a tuple.","metadata":{}},{"cell_type":"code","source":"# Train the model for 50 epochs \nresults = model.fit(X_train, y_train, epochs=50,\n                    validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-09-25T22:52:15.810406Z","iopub.execute_input":"2022-09-25T22:52:15.810785Z","iopub.status.idle":"2022-09-25T23:04:01.795383Z","shell.execute_reply.started":"2022-09-25T22:52:15.810751Z","shell.execute_reply":"2022-09-25T23:04:01.794420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the the training loss\nplt.plot(results.history[\"loss\"], label=\"loss\")\n\n# Plot the the validation loss\nplt.plot(results.history[\"val_loss\"], label=\"val_loss\")\n\n# Name the x and y axises\nplt.xlabel(\"epochs\")\nplt.ylabel(\"Loss\")\n# Put legend table\nplt.legend()\nplt.grid()\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-25T23:04:01.797534Z","iopub.execute_input":"2022-09-25T23:04:01.798391Z","iopub.status.idle":"2022-09-25T23:04:02.016320Z","shell.execute_reply.started":"2022-09-25T23:04:01.798353Z","shell.execute_reply":"2022-09-25T23:04:02.015424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the the training accuracy\nplt.plot(results.history[\"accuracy\"], label=\"accuracy\")\n\n# Plot the the validation accuracy\nplt.plot(results.history[\"val_accuracy\"], label=\"val_accuracy\")\n\n# Name the x and y axises\nplt.xlabel(\"epochs\")\nplt.ylabel(\"Accuracy\")\n# Put legend table\nplt.legend()\nplt.grid()\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-25T23:04:02.017631Z","iopub.execute_input":"2022-09-25T23:04:02.018512Z","iopub.status.idle":"2022-09-25T23:04:02.227459Z","shell.execute_reply.started":"2022-09-25T23:04:02.018473Z","shell.execute_reply":"2022-09-25T23:04:02.226555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the performance\nmodel.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-25T23:04:02.229463Z","iopub.execute_input":"2022-09-25T23:04:02.229860Z","iopub.status.idle":"2022-09-25T23:04:05.176205Z","shell.execute_reply.started":"2022-09-25T23:04:02.229824Z","shell.execute_reply":"2022-09-25T23:04:05.175023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make prediction on the reshaped sample\npredict_result = model.predict(X_test[42].reshape(1,32,32,3))\n# Print the prediction result\nprint(predict_result)\n# Find the predicted class\npredict_class = predict_result.argmax()\n# Find the prediction probability\npredict_prob = predict_result.max()","metadata":{"execution":{"iopub.status.busy":"2022-09-25T23:05:04.501343Z","iopub.execute_input":"2022-09-25T23:05:04.501983Z","iopub.status.idle":"2022-09-25T23:05:04.785495Z","shell.execute_reply.started":"2022-09-25T23:05:04.501949Z","shell.execute_reply":"2022-09-25T23:05:04.784540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Print the results\nprint(f\"Image belongs to class: {predict_class}\\n Prob is: {predict_prob}\")","metadata":{"execution":{"iopub.status.busy":"2022-09-25T23:05:12.501371Z","iopub.execute_input":"2022-09-25T23:05:12.502268Z","iopub.status.idle":"2022-09-25T23:05:12.508180Z","shell.execute_reply.started":"2022-09-25T23:05:12.502222Z","shell.execute_reply":"2022-09-25T23:05:12.507142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\nclass_names = [\"airplane\", \"automobile\", \"bird\", \"cat\", \"deer\", \"dog\", \"frog\", \"horse\", \"ship\", \"truck\"]\n\ny_preds = model.predict(X_test)\ny_pred=y_preds.argmax(axis=1)\ncm=confusion_matrix(y_test,y_pred)\ncm_df=pd.DataFrame(cm,index=class_names,\n       columns=class_names)\nplt.figure(figsize=(20,10))\nsns.heatmap(cm_df, annot=True,cmap='BuPu')\nplt.title('Confusion Matrix')\nplt.ylabel('Actal Values')\nplt.xlabel('Predicted Values')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-25T23:12:18.372645Z","iopub.execute_input":"2022-09-25T23:12:18.373007Z","iopub.status.idle":"2022-09-25T23:12:20.286384Z","shell.execute_reply.started":"2022-09-25T23:12:18.372977Z","shell.execute_reply":"2022-09-25T23:12:20.285477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}