{"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# 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-05-30T12:42:33.706592Z","iopub.execute_input":"2022-05-30T12:42:33.706959Z","iopub.status.idle":"2022-05-30T12:42:33.715297Z","shell.execute_reply.started":"2022-05-30T12:42:33.706930Z","shell.execute_reply":"2022-05-30T12:42:33.714508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow\nimport matplotlib.pyplot as plt\nfrom keras.models import Sequential\nfrom keras import layers\nfrom keras.layers import Dense, Dropout, Activation, Flatten","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:42:34.998366Z","iopub.execute_input":"2022-05-30T12:42:34.998841Z","iopub.status.idle":"2022-05-30T12:42:40.056197Z","shell.execute_reply.started":"2022-05-30T12:42:34.998810Z","shell.execute_reply":"2022-05-30T12:42:40.055354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.datasets import cifar10\n(x_train, y_train), (x_test, y_test) = cifar10.load_data()\nprint('x_train shape:', x_train.shape)\nprint('y_train shape:', y_train.shape)\nprint(x_train.shape[0], 'train samples')\nprint(x_test.shape[0], 'test samples')","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:42:52.631905Z","iopub.execute_input":"2022-05-30T12:42:52.632956Z","iopub.status.idle":"2022-05-30T12:42:58.309718Z","shell.execute_reply.started":"2022-05-30T12:42:52.632921Z","shell.execute_reply":"2022-05-30T12:42:58.308949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[:5]","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:42:58.311178Z","iopub.execute_input":"2022-05-30T12:42:58.311544Z","iopub.status.idle":"2022-05-30T12:42:58.321389Z","shell.execute_reply.started":"2022-05-30T12:42:58.311509Z","shell.execute_reply":"2022-05-30T12:42:58.320406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = y_train.reshape(-1,)\ny_train[:5]","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:42:59.463190Z","iopub.execute_input":"2022-05-30T12:42:59.463765Z","iopub.status.idle":"2022-05-30T12:42:59.469524Z","shell.execute_reply.started":"2022-05-30T12:42:59.463732Z","shell.execute_reply":"2022-05-30T12:42:59.468725Z"},"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":"2022-05-30T12:43:00.490229Z","iopub.execute_input":"2022-05-30T12:43:00.491142Z","iopub.status.idle":"2022-05-30T12:43:00.496898Z","shell.execute_reply.started":"2022-05-30T12:43:00.491092Z","shell.execute_reply":"2022-05-30T12:43:00.495452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_sample(X, y, index):\n    plt.figure(figsize = (16,3))\n    plt.imshow(X[index])\n    plt.xlabel(classes[y[index]])","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:43:01.792308Z","iopub.execute_input":"2022-05-30T12:43:01.792645Z","iopub.status.idle":"2022-05-30T12:43:01.799221Z","shell.execute_reply.started":"2022-05-30T12:43:01.792618Z","shell.execute_reply":"2022-05-30T12:43:01.796657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(x_train, y_train, 0)\nplot_sample(x_train, y_train, 1)\nplot_sample(x_train, y_train, 2)","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:43:12.682709Z","iopub.execute_input":"2022-05-30T12:43:12.683073Z","iopub.status.idle":"2022-05-30T12:43:13.383802Z","shell.execute_reply.started":"2022-05-30T12:43:12.683044Z","shell.execute_reply":"2022-05-30T12:43:13.382826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalizing the values\nx_train = x_train/255\nx_test = x_test/255","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:43:13.740517Z","iopub.execute_input":"2022-05-30T12:43:13.741172Z","iopub.status.idle":"2022-05-30T12:43:14.247927Z","shell.execute_reply.started":"2022-05-30T12:43:13.741137Z","shell.execute_reply":"2022-05-30T12:43:14.246993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#building ANN model first and then comparing it with CNN\n\nann = Sequential([\n    layers.Flatten(input_shape=(32,32,3)),\n    layers.Dense(3000, activation='relu'),\n    layers.Dense(1000, activation='relu'),\n    layers.Dense(10, activation='sigmoid')\n])\n\nann.compile(optimizer='SGD',\n           loss='sparse_categorical_crossentropy',\n           metrics=['accuracy'])\n\nann.fit(x_train, y_train, epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:43:14.623333Z","iopub.execute_input":"2022-05-30T12:43:14.623688Z","iopub.status.idle":"2022-05-30T12:43:56.632758Z","shell.execute_reply.started":"2022-05-30T12:43:14.623659Z","shell.execute_reply":"2022-05-30T12:43:56.631922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:43:56.634476Z","iopub.execute_input":"2022-05-30T12:43:56.634858Z","iopub.status.idle":"2022-05-30T12:43:57.700655Z","shell.execute_reply.started":"2022-05-30T12:43:56.634821Z","shell.execute_reply":"2022-05-30T12:43:57.699858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report\ny_pred = ann.predict(x_test)\ny_predict_classes = [np.argmax(element) for element in y_pred]\nprint(\"Classification Report: \\n\",classification_report(y_test, y_predict_classes) )","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:43:57.701925Z","iopub.execute_input":"2022-05-30T12:43:57.702292Z","iopub.status.idle":"2022-05-30T12:43:59.570638Z","shell.execute_reply.started":"2022-05-30T12:43:57.702257Z","shell.execute_reply":"2022-05-30T12:43:59.569259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#CNN model\n\n#building CNN\n\ncnn = Sequential([\n    #cnn\n    layers.Conv2D(filters=32, activation='relu', kernel_size=(3,3), input_shape=(32,32,3)),\n    layers.MaxPooling2D((2,2)),\n    \n    #dense\n    layers.Flatten(),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(10, activation='softmax'),\n    \n    \n])\n\ncnn.compile(optimizer='adam',\n           loss='sparse_categorical_crossentropy',\n           metrics=['accuracy'])\n\ncnn.fit(x_train, y_train, epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:43:59.575452Z","iopub.execute_input":"2022-05-30T12:43:59.575836Z","iopub.status.idle":"2022-05-30T12:45:23.610554Z","shell.execute_reply.started":"2022-05-30T12:43:59.575799Z","shell.execute_reply":"2022-05-30T12:45:23.609717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:45:23.611737Z","iopub.execute_input":"2022-05-30T12:45:23.613394Z","iopub.status.idle":"2022-05-30T12:45:24.659633Z","shell.execute_reply.started":"2022-05-30T12:45:23.613353Z","shell.execute_reply":"2022-05-30T12:45:24.658893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=cnn.predict(x_test)\ny_classes = [np.argmax(element) for element in y_pred]\ny_classes[:5]","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:45:24.660705Z","iopub.execute_input":"2022-05-30T12:45:24.661026Z","iopub.status.idle":"2022-05-30T12:45:25.388430Z","shell.execute_reply.started":"2022-05-30T12:45:24.660988Z","shell.execute_reply":"2022-05-30T12:45:25.387684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = y_test.reshape(-1,)\ny_test","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:45:25.389587Z","iopub.execute_input":"2022-05-30T12:45:25.389926Z","iopub.status.idle":"2022-05-30T12:45:25.395683Z","shell.execute_reply.started":"2022-05-30T12:45:25.389892Z","shell.execute_reply":"2022-05-30T12:45:25.394978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes[y_classes[1000]]","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:45:25.396827Z","iopub.execute_input":"2022-05-30T12:45:25.397784Z","iopub.status.idle":"2022-05-30T12:45:25.409056Z","shell.execute_reply.started":"2022-05-30T12:45:25.397748Z","shell.execute_reply":"2022-05-30T12:45:25.408371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes[y_test[1000]]","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:45:25.411748Z","iopub.execute_input":"2022-05-30T12:45:25.412093Z","iopub.status.idle":"2022-05-30T12:45:25.419329Z","shell.execute_reply.started":"2022-05-30T12:45:25.412069Z","shell.execute_reply":"2022-05-30T12:45:25.418443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:45:25.421940Z","iopub.execute_input":"2022-05-30T12:45:25.422510Z","iopub.status.idle":"2022-05-30T12:45:25.430399Z","shell.execute_reply.started":"2022-05-30T12:45:25.422474Z","shell.execute_reply":"2022-05-30T12:45:25.429437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"incorrect_cnt=0\nfor i in range(1000):\n    if (classes[y_test[i]]!=classes[y_classes[i]]):\n        incorrect_cnt=incorrect_cnt+1\n        print(\"Obeserved value: \",classes[y_test[i]], \", Predicted value: \",classes[y_classes[i]]) \n        \nincorrect_cnt        ","metadata":{"execution":{"iopub.status.busy":"2022-05-30T12:45:25.431609Z","iopub.execute_input":"2022-05-30T12:45:25.432050Z","iopub.status.idle":"2022-05-30T12:45:25.453149Z","shell.execute_reply.started":"2022-05-30T12:45:25.431993Z","shell.execute_reply":"2022-05-30T12:45:25.452326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Out of 1000 test cases, CNN predicted 361 incorrect","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}