{"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 tensorflow as tf \nfrom tensorflow.keras import datasets,layers,models\nfrom keras.utils.np_utils import to_categorical\nimport numpy as py\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras import optimizers\n\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import to_categorical\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D,MaxPool2D,Dense,Flatten,Dropout,Input, AveragePooling2D, Activation,Conv2D, MaxPooling2D, BatchNormalization,Concatenate\nfrom tensorflow.keras.callbacks import EarlyStopping, TensorBoard\nfrom tensorflow.keras import regularizers, optimizers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-27T20:42:08.856935Z","iopub.execute_input":"2022-01-27T20:42:08.857546Z","iopub.status.idle":"2022-01-27T20:42:08.870199Z","shell.execute_reply.started":"2022-01-27T20:42:08.857507Z","shell.execute_reply":"2022-01-27T20:42:08.869229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(x_train , y_train),(x_test,y_test) = datasets.cifar10.load_data() ","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:38:45.706851Z","iopub.execute_input":"2022-01-27T19:38:45.707392Z","iopub.status.idle":"2022-01-27T19:38:46.567306Z","shell.execute_reply.started":"2022-01-27T19:38:45.707355Z","shell.execute_reply":"2022-01-27T19:38:46.566433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nprint('Shape of x_train is {}'.format(x_train.shape))\nprint('Shape of x_test is {}'.format(x_test.shape))\nprint('Shape of y_train is {}'.format(y_train.shape))\nprint('Shape of y_test is {}'.format(y_test.shape))","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:39:36.896408Z","iopub.execute_input":"2022-01-27T20:39:36.896896Z","iopub.status.idle":"2022-01-27T20:39:36.906125Z","shell.execute_reply.started":"2022-01-27T20:39:36.896855Z","shell.execute_reply":"2022-01-27T20:39:36.905242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:39:44.814897Z","iopub.execute_input":"2022-01-27T20:39:44.815208Z","iopub.status.idle":"2022-01-27T20:39:44.821453Z","shell.execute_reply.started":"2022-01-27T20:39:44.815173Z","shell.execute_reply":"2022-01-27T20:39:44.820824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:39:55.89484Z","iopub.execute_input":"2022-01-27T20:39:55.895121Z","iopub.status.idle":"2022-01-27T20:39:55.901432Z","shell.execute_reply.started":"2022-01-27T20:39:55.895086Z","shell.execute_reply":"2022-01-27T20:39:55.900258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:40:04.735778Z","iopub.execute_input":"2022-01-27T20:40:04.736086Z","iopub.status.idle":"2022-01-27T20:40:04.742793Z","shell.execute_reply.started":"2022-01-27T20:40:04.736051Z","shell.execute_reply":"2022-01-27T20:40:04.741825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:31:29.093479Z","iopub.execute_input":"2022-01-27T19:31:29.094107Z","iopub.status.idle":"2022-01-27T19:31:29.100064Z","shell.execute_reply.started":"2022-01-27T19:31:29.094066Z","shell.execute_reply":"2022-01-27T19:31:29.099012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[:5]","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:40:14.674674Z","iopub.execute_input":"2022-01-27T20:40:14.674989Z","iopub.status.idle":"2022-01-27T20:40:14.681023Z","shell.execute_reply.started":"2022-01-27T20:40:14.674955Z","shell.execute_reply":"2022-01-27T20:40:14.680233Z"},"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-01-27T20:00:31.706961Z","iopub.execute_input":"2022-01-27T20:00:31.707255Z","iopub.status.idle":"2022-01-27T20:00:31.712255Z","shell.execute_reply.started":"2022-01-27T20:00:31.707225Z","shell.execute_reply":"2022-01-27T20:00:31.711218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_sample(X, y, index):\n    plt.figure(figsize = (15,2))\n    plt.imshow(X[index])\n    plt.xlabel(classes[y[index]])","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:00:36.506683Z","iopub.execute_input":"2022-01-27T20:00:36.506993Z","iopub.status.idle":"2022-01-27T20:00:36.512015Z","shell.execute_reply.started":"2022-01-27T20:00:36.506962Z","shell.execute_reply":"2022-01-27T20:00:36.511089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(x_train, y_train, 0)","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:34:12.743501Z","iopub.execute_input":"2022-01-27T19:34:12.744041Z","iopub.status.idle":"2022-01-27T19:34:12.961057Z","shell.execute_reply.started":"2022-01-27T19:34:12.744006Z","shell.execute_reply":"2022-01-27T19:34:12.959862Z"},"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-01-27T19:37:29.28756Z","iopub.execute_input":"2022-01-27T19:37:29.28795Z","iopub.status.idle":"2022-01-27T19:37:29.296526Z","shell.execute_reply.started":"2022-01-27T19:37:29.28791Z","shell.execute_reply":"2022-01-27T19:37:29.295857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = y_test.reshape(-1,)","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:37:45.46708Z","iopub.execute_input":"2022-01-27T19:37:45.468052Z","iopub.status.idle":"2022-01-27T19:37:45.4726Z","shell.execute_reply.started":"2022-01-27T19:37:45.468005Z","shell.execute_reply":"2022-01-27T19:37:45.471563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_sample(X, y, index):\n    plt.figure(figsize = (15,2))\n    plt.imshow(X[index])\n    plt.xlabel(classes[y[index]])","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:40:16.066297Z","iopub.execute_input":"2022-01-27T19:40:16.067272Z","iopub.status.idle":"2022-01-27T19:40:16.073616Z","shell.execute_reply.started":"2022-01-27T19:40:16.067212Z","shell.execute_reply":"2022-01-27T19:40:16.072548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(x_train, y_train, 9)","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:39:18.648193Z","iopub.execute_input":"2022-01-27T19:39:18.648687Z","iopub.status.idle":"2022-01-27T19:39:18.902609Z","shell.execute_reply.started":"2022-01-27T19:39:18.648633Z","shell.execute_reply":"2022-01-27T19:39:18.901443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):\n  plot_sample(x_train, y_train, i)","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:40:31.106558Z","iopub.execute_input":"2022-01-27T19:40:31.106868Z","iopub.status.idle":"2022-01-27T19:40:31.358695Z","shell.execute_reply.started":"2022-01-27T19:40:31.10683Z","shell.execute_reply":"2022-01-27T19:40:31.357524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train[0]","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:42:38.938587Z","iopub.execute_input":"2022-01-27T19:42:38.939617Z","iopub.status.idle":"2022-01-27T19:42:38.948691Z","shell.execute_reply.started":"2022-01-27T19:42:38.939557Z","shell.execute_reply":"2022-01-27T19:42:38.947653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train/255\nx_test=x_test/255","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:43:09.179063Z","iopub.execute_input":"2022-01-27T19:43:09.179773Z","iopub.status.idle":"2022-01-27T19:43:09.87302Z","shell.execute_reply.started":"2022-01-27T19:43:09.179701Z","shell.execute_reply":"2022-01-27T19:43:09.871861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann=models.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='softmax')\n])\n\nann.compile(optimizer='SGD',\n            loss='sparse_categorical_crossentropy',\n            metrics=['accuracy']\n            )\nann.fit(x_train,y_train,epochs=5)","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:44:05.72064Z","iopub.execute_input":"2022-01-27T19:44:05.721438Z","iopub.status.idle":"2022-01-27T19:47:55.829846Z","shell.execute_reply.started":"2022-01-27T19:44:05.721394Z","shell.execute_reply":"2022-01-27T19:47:55.828661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix , classification_report\nimport numpy as np\ny_pred = 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":"2022-01-27T19:48:35.260191Z","iopub.execute_input":"2022-01-27T19:48:35.2605Z","iopub.status.idle":"2022-01-27T19:48:39.144301Z","shell.execute_reply.started":"2022-01-27T19:48:35.260466Z","shell.execute_reply":"2022-01-27T19:48:39.143196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn=models.Sequential([\n                       #cnn\n                      layers.Conv2D(filters=32,kernel_size=(3,3),activation='relu',input_shape=(32,32,3)),\n                      layers.MaxPool2D((2,2)),\n                       \n                      layers.Conv2D(filters=64,kernel_size=(3,3),activation='relu',input_shape=(32,32,3)),\n                      layers.MaxPool2D((2,2)),\n\n\n                      layers.Flatten(),\n                      layers.Dense(64,activation='relu'),\n                      layers.Dense(10,activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:04:19.806572Z","iopub.execute_input":"2022-01-27T20:04:19.807392Z","iopub.status.idle":"2022-01-27T20:04:19.863207Z","shell.execute_reply.started":"2022-01-27T20:04:19.807346Z","shell.execute_reply":"2022-01-27T20:04:19.862349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.compile(optimizer='adam',\n            loss='sparse_categorical_crossentropy',\n            metrics=['accuracy']\n            )\ncnn.fit(x_train,y_train,epochs=15)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:56:25.579542Z","iopub.execute_input":"2022-01-27T19:56:25.579877Z","iopub.status.idle":"2022-01-27T19:56:27.754595Z","shell.execute_reply.started":"2022-01-27T19:56:25.579839Z","shell.execute_reply":"2022-01-27T19:56:27.753905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = cnn.predict(x_test)\ny_pred[:5]","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:56:54.827069Z","iopub.execute_input":"2022-01-27T19:56:54.827412Z","iopub.status.idle":"2022-01-27T19:56:56.592332Z","shell.execute_reply.started":"2022-01-27T19:56:54.827357Z","shell.execute_reply":"2022-01-27T19:56:56.591487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_classes = [np.argmax(element) for element in y_pred]\ny_classes[:5]","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:57:24.708399Z","iopub.execute_input":"2022-01-27T19:57:24.709256Z","iopub.status.idle":"2022-01-27T19:57:24.746529Z","shell.execute_reply.started":"2022-01-27T19:57:24.709199Z","shell.execute_reply":"2022-01-27T19:57:24.74548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test[:5]","metadata":{"execution":{"iopub.status.busy":"2022-01-27T19:58:00.387111Z","iopub.execute_input":"2022-01-27T19:58:00.387424Z","iopub.status.idle":"2022-01-27T19:58:00.393692Z","shell.execute_reply.started":"2022-01-27T19:58:00.387387Z","shell.execute_reply":"2022-01-27T19:58:00.392835Z"},"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-01-27T20:02:41.147364Z","iopub.execute_input":"2022-01-27T20:02:41.14765Z","iopub.status.idle":"2022-01-27T20:02:41.15337Z","shell.execute_reply.started":"2022-01-27T20:02:41.14762Z","shell.execute_reply":"2022-01-27T20:02:41.152822Z"},"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-01-27T20:02:58.587675Z","iopub.execute_input":"2022-01-27T20:02:58.58829Z","iopub.status.idle":"2022-01-27T20:02:58.593011Z","shell.execute_reply.started":"2022-01-27T20:02:58.588246Z","shell.execute_reply":"2022-01-27T20:02:58.592025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_sample(X, y, index):\n    plt.figure(figsize = (15,2))\n    plt.imshow(X[index])\n    plt.xlabel(classes[y[index]])","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:03:23.567926Z","iopub.execute_input":"2022-01-27T20:03:23.568248Z","iopub.status.idle":"2022-01-27T20:03:23.574728Z","shell.execute_reply.started":"2022-01-27T20:03:23.568206Z","shell.execute_reply":"2022-01-27T20:03:23.574021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(x_train, y_train, 0)","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:26:02.914467Z","iopub.execute_input":"2022-01-27T20:26:02.914801Z","iopub.status.idle":"2022-01-27T20:26:03.136347Z","shell.execute_reply.started":"2022-01-27T20:26:02.914751Z","shell.execute_reply":"2022-01-27T20:26:03.13533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix , classification_report\nimport numpy as np\ny_pred = cnn.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":"2022-01-27T20:27:21.134242Z","iopub.execute_input":"2022-01-27T20:27:21.134534Z","iopub.status.idle":"2022-01-27T20:27:22.826341Z","shell.execute_reply.started":"2022-01-27T20:27:21.134506Z","shell.execute_reply":"2022-01-27T20:27:22.825317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train/255\nx_test=x_test/255\n\n#One hot encoding\ny_train_categorical=to_categorical(y_train,10)\ny_test_categorical=to_categorical(y_test,10)","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:54:32.29631Z","iopub.execute_input":"2022-01-27T20:54:32.296694Z","iopub.status.idle":"2022-01-27T20:54:32.804894Z","shell.execute_reply.started":"2022-01-27T20:54:32.296652Z","shell.execute_reply":"2022-01-27T20:54:32.804035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1=Sequential()\nmodel1.add(Conv2D(filters=32,kernel_size=(4,4),input_shape=(32,32,3),activation='relu'))\nmodel1.add(MaxPool2D(pool_size=(2,2)))\nmodel1.add(Conv2D(filters=32,kernel_size=(4,4),input_shape=(32,32,3),activation='relu'))\nmodel1.add(MaxPool2D(pool_size=(2,2)))\nmodel1.add(Flatten())\nmodel1.add(Dense(256,activation='relu'))\nmodel1.add(Dense(10,activation='softmax'))\nmodel1.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:54:41.655377Z","iopub.execute_input":"2022-01-27T20:54:41.65569Z","iopub.status.idle":"2022-01-27T20:54:41.725218Z","shell.execute_reply.started":"2022-01-27T20:54:41.65566Z","shell.execute_reply":"2022-01-27T20:54:41.72427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.summary()","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:43:17.79437Z","iopub.execute_input":"2022-01-27T20:43:17.794882Z","iopub.status.idle":"2022-01-27T20:43:17.804641Z","shell.execute_reply.started":"2022-01-27T20:43:17.794829Z","shell.execute_reply":"2022-01-27T20:43:17.803807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history1=model1.fit(x_train,y_train_categorical,epochs=20,validation_data=(x_test,y_test_categorical))","metadata":{"execution":{"iopub.status.busy":"2022-01-27T21:03:13.230689Z","iopub.execute_input":"2022-01-27T21:03:13.231026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluation = model1.evaluate(x_test, y_test_categorical)\nprint('Test Accuracy: {}'.format(evaluation[1]))","metadata":{"execution":{"iopub.status.busy":"2022-01-27T20:53:56.254182Z","iopub.execute_input":"2022-01-27T20:53:56.254767Z","iopub.status.idle":"2022-01-27T20:53:58.150149Z","shell.execute_reply.started":"2022-01-27T20:53:56.254716Z","shell.execute_reply":"2022-01-27T20:53:58.149232Z"},"trusted":true},"execution_count":null,"outputs":[]}]}