{"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":{"id":"TMq5gV7Ou8sx","execution":{"iopub.status.busy":"2021-10-31T04:49:44.327537Z","iopub.execute_input":"2021-10-31T04:49:44.328130Z","iopub.status.idle":"2021-10-31T04:49:48.785880Z","shell.execute_reply.started":"2021-10-31T04:49:44.328036Z","shell.execute_reply":"2021-10-31T04:49:48.784985Z"},"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()","metadata":{"id":"Hl48yKSNL6oM","outputId":"4664215d-bbd8-4cdc-e753-a2fccad916e6","execution":{"iopub.status.busy":"2021-10-31T04:49:48.787504Z","iopub.execute_input":"2021-10-31T04:49:48.787757Z","iopub.status.idle":"2021-10-31T04:49:54.431395Z","shell.execute_reply.started":"2021-10-31T04:49:48.787720Z","shell.execute_reply":"2021-10-31T04:49:54.430635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"id":"W82EEYNhCrrv","outputId":"e937cd96-8612-4c64-e91d-d5026db9b3de","execution":{"iopub.status.busy":"2021-10-31T04:49:54.432489Z","iopub.execute_input":"2021-10-31T04:49:54.432729Z","iopub.status.idle":"2021-10-31T04:49:54.442815Z","shell.execute_reply.started":"2021-10-31T04:49:54.432697Z","shell.execute_reply":"2021-10-31T04:49:54.441849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"id":"WixG95NVCzlb","outputId":"9f506d2f-7eb1-4b95-fbfc-b6be7c9e3dfe","execution":{"iopub.status.busy":"2021-10-31T04:49:54.445348Z","iopub.execute_input":"2021-10-31T04:49:54.446293Z","iopub.status.idle":"2021-10-31T04:49:54.451837Z","shell.execute_reply.started":"2021-10-31T04:49:54.446252Z","shell.execute_reply":"2021-10-31T04:49:54.451010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train[0])","metadata":{"id":"JyRH8p4UC2YG","outputId":"a8b340c5-dda0-450b-c5b3-f6236fcb8a7a","execution":{"iopub.status.busy":"2021-10-31T04:49:54.453431Z","iopub.execute_input":"2021-10-31T04:49:54.453952Z","iopub.status.idle":"2021-10-31T04:49:54.462471Z","shell.execute_reply.started":"2021-10-31T04:49:54.453886Z","shell.execute_reply":"2021-10-31T04:49:54.461656Z"},"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":{"id":"qBAaLqGgC68_","execution":{"iopub.status.busy":"2021-10-31T04:49:54.463494Z","iopub.execute_input":"2021-10-31T04:49:54.464591Z","iopub.status.idle":"2021-10-31T04:49:54.468744Z","shell.execute_reply.started":"2021-10-31T04:49:54.464552Z","shell.execute_reply":"2021-10-31T04:49:54.467905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[:5]","metadata":{"id":"OMaSZkFwDESl","outputId":"38ce88cc-a9b5-45a0-f99b-8fa2fb4911dc","execution":{"iopub.status.busy":"2021-10-31T04:49:54.470222Z","iopub.execute_input":"2021-10-31T04:49:54.471149Z","iopub.status.idle":"2021-10-31T04:49:54.478955Z","shell.execute_reply.started":"2021-10-31T04:49:54.471111Z","shell.execute_reply":"2021-10-31T04:49:54.478174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = y_train.reshape(-1,)\ny_train[:5]","metadata":{"id":"cw6i1DZMCX9U","outputId":"cf188405-2178-4033-c506-03595f9f7901","execution":{"iopub.status.busy":"2021-10-31T04:49:54.480280Z","iopub.execute_input":"2021-10-31T04:49:54.480715Z","iopub.status.idle":"2021-10-31T04:49:54.487693Z","shell.execute_reply.started":"2021-10-31T04:49:54.480675Z","shell.execute_reply":"2021-10-31T04:49:54.486965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = [\"airplane\",\"automobile\",\"bird\",\"cat\",\"deer\",\"dog\",\"frog\",\"horse\",\"ship\",\"truck\"]","metadata":{"id":"OfWAbV6OCagx","execution":{"iopub.status.busy":"2021-10-31T04:49:54.489068Z","iopub.execute_input":"2021-10-31T04:49:54.489890Z","iopub.status.idle":"2021-10-31T04:49:54.495749Z","shell.execute_reply.started":"2021-10-31T04:49:54.489852Z","shell.execute_reply":"2021-10-31T04:49:54.494818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes[5]","metadata":{"id":"vxuTnQ1bCitF","outputId":"ef7138ca-bd8e-4bae-d776-d0c4c0295be8","execution":{"iopub.status.busy":"2021-10-31T04:49:54.498656Z","iopub.execute_input":"2021-10-31T04:49:54.499627Z","iopub.status.idle":"2021-10-31T04:49:54.505630Z","shell.execute_reply.started":"2021-10-31T04:49:54.499588Z","shell.execute_reply":"2021-10-31T04:49:54.504730Z"},"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-10-31T04:49:54.506896Z","iopub.execute_input":"2021-10-31T04:49:54.507759Z","iopub.status.idle":"2021-10-31T04:50:04.840734Z","shell.execute_reply.started":"2021-10-31T04:49:54.507719Z","shell.execute_reply":"2021-10-31T04:50:04.839437Z"},"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":{"id":"F7WG8mXlC5gG","outputId":"f65368de-53e4-45ec-d873-60bbadd28b4d","execution":{"iopub.status.busy":"2021-10-31T04:50:04.841664Z","iopub.execute_input":"2021-10-31T04:50:04.841877Z","iopub.status.idle":"2021-10-31T04:50:05.330510Z","shell.execute_reply.started":"2021-10-31T04:50:04.841849Z","shell.execute_reply":"2021-10-31T04:50:05.329713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **ANN Model :-**","metadata":{"id":"Db6UlN_QDHML"}},{"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":{"id":"zFepe92wDKlS","outputId":"829ef29e-c36c-42ca-ddf4-f2f121764537","execution":{"iopub.status.busy":"2021-10-31T04:50:05.332212Z","iopub.execute_input":"2021-10-31T04:50:05.332785Z","iopub.status.idle":"2021-10-31T04:51:06.465761Z","shell.execute_reply.started":"2021-10-31T04:50:05.332724Z","shell.execute_reply":"2021-10-31T04:51:06.465093Z"},"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":{"id":"Kddlontvm5-Y","outputId":"21672523-5f13-4f5b-8fb2-15c2a65018e2","execution":{"iopub.status.busy":"2021-10-31T04:51:06.467021Z","iopub.execute_input":"2021-10-31T04:51:06.467334Z","iopub.status.idle":"2021-10-31T04:51:08.054342Z","shell.execute_reply.started":"2021-10-31T04:51:06.467305Z","shell.execute_reply":"2021-10-31T04:51:08.053609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Accuracy of ANN model is = 0.48**","metadata":{"id":"9qkaNHfowp52"}},{"cell_type":"markdown","source":"# **CNN Model:-**","metadata":{"id":"9_MyT3uOw2U6"}},{"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":{"id":"XJEyHOZaw0Jy","execution":{"iopub.status.busy":"2021-10-31T04:51:25.513677Z","iopub.execute_input":"2021-10-31T04:51:25.513966Z","iopub.status.idle":"2021-10-31T04:51:25.576344Z","shell.execute_reply.started":"2021-10-31T04:51:25.513907Z","shell.execute_reply":"2021-10-31T04:51:25.575688Z"},"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":{"id":"j4Qre2-bxBcn","outputId":"d64725b7-d45f-4564-814b-d78aeaf64a0c","execution":{"iopub.status.busy":"2021-10-31T04:51:27.748811Z","iopub.execute_input":"2021-10-31T04:51:27.749664Z","iopub.status.idle":"2021-10-31T04:53:51.578587Z","shell.execute_reply.started":"2021-10-31T04:51:27.749609Z","shell.execute_reply":"2021-10-31T04:53:51.577412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_cnn.evaluate(X_test,y_test)","metadata":{"id":"yq18viEFxEvv","outputId":"2b1975e6-e5f5-48d3-8f45-d4da7267bd09","execution":{"iopub.status.busy":"2021-10-31T04:53:51.580477Z","iopub.execute_input":"2021-10-31T04:53:51.580755Z","iopub.status.idle":"2021-10-31T04:53:53.305562Z","shell.execute_reply.started":"2021-10-31T04:53:51.580721Z","shell.execute_reply":"2021-10-31T04:53:53.304892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Accuracy of cNN model is = 0.7406**","metadata":{"id":"4kBF8mnz4GTh"}},{"cell_type":"code","source":"y_pred = model_cnn.predict(X_test)\ny_pred[:5]","metadata":{"id":"G41Z6zAK4DKQ","outputId":"87fc65f8-fdf8-4060-fd2f-93add3ed34e5","execution":{"iopub.status.busy":"2021-10-31T04:54:49.545284Z","iopub.execute_input":"2021-10-31T04:54:49.545552Z","iopub.status.idle":"2021-10-31T04:54:51.226675Z","shell.execute_reply.started":"2021-10-31T04:54:49.545522Z","shell.execute_reply":"2021-10-31T04:54:51.225853Z"},"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":{"id":"ZFRjSVX44fzz","outputId":"46128a0d-1319-4674-c3f1-cb30be0c7088","execution":{"iopub.status.busy":"2021-10-31T04:54:51.228267Z","iopub.execute_input":"2021-10-31T04:54:51.228620Z","iopub.status.idle":"2021-10-31T04:54:51.966976Z","shell.execute_reply.started":"2021-10-31T04:54:51.228580Z","shell.execute_reply":"2021-10-31T04:54:51.966313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test[:5]","metadata":{"id":"boi11_VW4r2h","outputId":"07c36b05-a0e9-4d0f-a899-df1ee1b096f3","execution":{"iopub.status.busy":"2021-10-31T04:54:56.656667Z","iopub.execute_input":"2021-10-31T04:54:56.656935Z","iopub.status.idle":"2021-10-31T04:54:56.664265Z","shell.execute_reply.started":"2021-10-31T04:54:56.656890Z","shell.execute_reply":"2021-10-31T04:54:56.663222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes[y_classes[5]]","metadata":{"id":"oGEuADuV4wQj","outputId":"7325f83b-a18c-4023-e354-605129a1bbb9","execution":{"iopub.status.busy":"2021-10-31T04:55:26.707052Z","iopub.execute_input":"2021-10-31T04:55:26.707596Z","iopub.status.idle":"2021-10-31T04:55:26.716640Z","shell.execute_reply.started":"2021-10-31T04:55:26.707558Z","shell.execute_reply":"2021-10-31T04:55:26.715833Z"},"trusted":true},"execution_count":null,"outputs":[]}]}