{"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":"import os \nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport zipfile\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import RMSprop, Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import datasets,layers,models,Model","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-06T10:25:26.759788Z","iopub.execute_input":"2021-11-06T10:25:26.760439Z","iopub.status.idle":"2021-11-06T10:25:32.583734Z","shell.execute_reply.started":"2021-11-06T10:25:26.760344Z","shell.execute_reply":"2021-11-06T10:25:32.582964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#load data\n(X_train,y_train),(X_test,y_test)=datasets.cifar10.load_data()\nX_train.shape,X_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:32:08.711468Z","iopub.execute_input":"2021-11-06T10:32:08.711771Z","iopub.status.idle":"2021-11-06T10:32:09.496332Z","shell.execute_reply.started":"2021-11-06T10:32:08.711738Z","shell.execute_reply":"2021-11-06T10:32:09.495524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train[0])","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:32:09.498446Z","iopub.execute_input":"2021-11-06T10:32:09.498685Z","iopub.status.idle":"2021-11-06T10:32:09.507053Z","shell.execute_reply.started":"2021-11-06T10:32:09.498657Z","shell.execute_reply":"2021-11-06T10:32:09.506036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape converting 2D to 1D\ny_train = y_train.reshape(-1,)\ny_test = y_test.reshape(-1,)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:32:09.508628Z","iopub.execute_input":"2021-11-06T10:32:09.50895Z","iopub.status.idle":"2021-11-06T10:32:09.516371Z","shell.execute_reply.started":"2021-11-06T10:32:09.508893Z","shell.execute_reply":"2021-11-06T10:32:09.515793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[:5]","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:32:09.517789Z","iopub.execute_input":"2021-11-06T10:32:09.51818Z","iopub.status.idle":"2021-11-06T10:32:09.533033Z","shell.execute_reply.started":"2021-11-06T10:32:09.518138Z","shell.execute_reply":"2021-11-06T10:32:09.532162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = y_train.reshape(-1,)\ny_train[:5]","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:32:09.535244Z","iopub.execute_input":"2021-11-06T10:32:09.535725Z","iopub.status.idle":"2021-11-06T10:32:09.546879Z","shell.execute_reply.started":"2021-11-06T10:32:09.535688Z","shell.execute_reply":"2021-11-06T10:32:09.546014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = [\"airplane\",\"automobile\",\"bird\",\"cat\",\"deer\",\"dog\",\"frog\",\"horse\",\"ship\",\"truck\"]","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:32:09.548616Z","iopub.execute_input":"2021-11-06T10:32:09.549096Z","iopub.status.idle":"2021-11-06T10:32:09.557125Z","shell.execute_reply.started":"2021-11-06T10:32:09.549055Z","shell.execute_reply":"2021-11-06T10:32:09.556213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes[5]","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:32:09.558327Z","iopub.execute_input":"2021-11-06T10:32:09.558538Z","iopub.status.idle":"2021-11-06T10:32:09.571351Z","shell.execute_reply.started":"2021-11-06T10:32:09.558512Z","shell.execute_reply":"2021-11-06T10:32:09.570492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize= (12,12))\nfor i in range(256):\n  plt.subplot(16,16,i+1)\n  plt.axis('off')\n  plt.imshow(X_train[i])","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:32:09.573317Z","iopub.execute_input":"2021-11-06T10:32:09.573944Z","iopub.status.idle":"2021-11-06T10:32:20.987975Z","shell.execute_reply.started":"2021-11-06T10:32:09.573896Z","shell.execute_reply":"2021-11-06T10:32:20.987095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This code normalazation\nX_train = X_train / 255\nX_test = X_test / 255\nX_train[:1]","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:32:20.989117Z","iopub.execute_input":"2021-11-06T10:32:20.989327Z","iopub.status.idle":"2021-11-06T10:32:21.6031Z","shell.execute_reply.started":"2021-11-06T10:32:20.989302Z","shell.execute_reply":"2021-11-06T10:32:21.602485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **ANN Model fitting**","metadata":{}},{"cell_type":"code","source":"model_ann = models.Sequential([\n        layers.Flatten(input_shape=(32,32,3)),\n        layers.Dense(3000, activation='relu'),\n        layers.Dense(2000, activation='relu'),\n        layers.Dense(1000, activation='relu'),\n        layers.Dense(10, activation='softmax')    \n    ])\n\nmodel_ann.compile(optimizer='Adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\nMODEL_1 = model_ann.fit(\n    X_train, y_train, epochs=10\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:32:21.604018Z","iopub.execute_input":"2021-11-06T10:32:21.604528Z","iopub.status.idle":"2021-11-06T10:47:26.048154Z","shell.execute_reply.started":"2021-11-06T10:32:21.604497Z","shell.execute_reply":"2021-11-06T10:47:26.047284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix , classification_report\nimport numpy as np\ny_pred = model_ann.predict(X_test)\ny_pred_classes = [np.argmax(element) for element in y_pred]\n\nprint(\"Classification Report: \\n\", classification_report(y_test, y_pred_classes))","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:47:26.050718Z","iopub.execute_input":"2021-11-06T10:47:26.051276Z","iopub.status.idle":"2021-11-06T10:47:31.69061Z","shell.execute_reply.started":"2021-11-06T10:47:26.051231Z","shell.execute_reply":"2021-11-06T10:47:31.689982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Accuracy of ANN model is = 0.48**","metadata":{}},{"cell_type":"markdown","source":"## **CNN Model fittig**","metadata":{}},{"cell_type":"code","source":"model_cnn = models.Sequential([\n        layers.Conv2D(filters=32,kernel_size=(3,3),activation='relu',input_shape=(32,32,3)),\n        layers.MaxPooling2D((2,2)),\n        \n        layers.Conv2D(filters=64,kernel_size=(3,3),activation='relu'),\n        layers.MaxPooling2D((2,2)),\n        layers.Dropout(0.5),\n    \n        layers.Conv2D(filters=128,kernel_size=(3,3),activation='relu'),\n        layers.MaxPooling2D((2,2)),\n        layers.Dropout(0.5),\n        \n        layers.Flatten(),\n        layers.Dense(512, activation='relu'),\n        layers.Dense(10, activation='softmax')    \n    ])","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:47:31.691909Z","iopub.execute_input":"2021-11-06T10:47:31.692295Z","iopub.status.idle":"2021-11-06T10:47:31.769139Z","shell.execute_reply.started":"2021-11-06T10:47:31.692241Z","shell.execute_reply":"2021-11-06T10:47:31.76851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_cnn.compile(\n                loss = 'sparse_categorical_crossentropy',\n               optimizer = 'Adam',\n               metrics = ['accuracy'])\n\n\nMODEL_2=model_cnn.fit(X_train,y_train,\n                        epochs=20,\n                      verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:47:31.770339Z","iopub.execute_input":"2021-11-06T10:47:31.771Z","iopub.status.idle":"2021-11-06T10:57:42.193538Z","shell.execute_reply.started":"2021-11-06T10:47:31.770949Z","shell.execute_reply":"2021-11-06T10:57:42.192818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_cnn.evaluate(X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:57:42.196344Z","iopub.execute_input":"2021-11-06T10:57:42.19659Z","iopub.status.idle":"2021-11-06T10:57:44.418073Z","shell.execute_reply.started":"2021-11-06T10:57:42.196556Z","shell.execute_reply":"2021-11-06T10:57:44.417264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Accuracy of cNN model is = 0.7406**","metadata":{}},{"cell_type":"code","source":"y_pred = model_cnn.predict(X_test)\ny_pred[:5]","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:57:44.419318Z","iopub.execute_input":"2021-11-06T10:57:44.419546Z","iopub.status.idle":"2021-11-06T10:57:46.450278Z","shell.execute_reply.started":"2021-11-06T10:57:44.419519Z","shell.execute_reply":"2021-11-06T10:57:46.449695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_cnn.predict(X_test)\n\ny_classes = [np.argmax(element) for element in y_pred]\ny_classes[:5]","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:57:46.451491Z","iopub.execute_input":"2021-11-06T10:57:46.451692Z","iopub.status.idle":"2021-11-06T10:57:48.539536Z","shell.execute_reply.started":"2021-11-06T10:57:46.451668Z","shell.execute_reply":"2021-11-06T10:57:48.53873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test[:5]","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:57:48.540652Z","iopub.execute_input":"2021-11-06T10:57:48.540897Z","iopub.status.idle":"2021-11-06T10:57:48.546545Z","shell.execute_reply.started":"2021-11-06T10:57:48.540869Z","shell.execute_reply":"2021-11-06T10:57:48.545786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes[y_classes[5]]","metadata":{"execution":{"iopub.status.busy":"2021-11-06T10:57:48.547531Z","iopub.execute_input":"2021-11-06T10:57:48.54774Z","iopub.status.idle":"2021-11-06T10:57:48.557866Z","shell.execute_reply.started":"2021-11-06T10:57:48.547716Z","shell.execute_reply":"2021-11-06T10:57:48.557317Z"},"trusted":true},"execution_count":null,"outputs":[]}]}