{"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":"# loading the required libraries\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn ","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:10.984385Z","iopub.execute_input":"2021-08-03T12:48:10.984751Z","iopub.status.idle":"2021-08-03T12:48:10.989478Z","shell.execute_reply.started":"2021-08-03T12:48:10.984721Z","shell.execute_reply":"2021-08-03T12:48:10.988311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install keras","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-03T12:48:10.991358Z","iopub.execute_input":"2021-08-03T12:48:10.991685Z","iopub.status.idle":"2021-08-03T12:48:17.799655Z","shell.execute_reply.started":"2021-08-03T12:48:10.991656Z","shell.execute_reply":"2021-08-03T12:48:17.798576Z"},"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()","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:17.802024Z","iopub.execute_input":"2021-08-03T12:48:17.802342Z","iopub.status.idle":"2021-08-03T12:48:18.641399Z","shell.execute_reply.started":"2021-08-03T12:48:17.802314Z","shell.execute_reply":"2021-08-03T12:48:18.640139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking the shape\nprint('X_train shape - ',X_train.shape)\nprint('X_test shape - ',X_test.shape)\nprint('y_train shape - ',y_train.shape)\nprint('y_test shape - ',y_test.shape)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:18.643306Z","iopub.execute_input":"2021-08-03T12:48:18.643661Z","iopub.status.idle":"2021-08-03T12:48:18.649226Z","shell.execute_reply.started":"2021-08-03T12:48:18.643631Z","shell.execute_reply":"2021-08-03T12:48:18.648594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualizing few samples\n\nlabels = ['Airplane', 'Automobile', 'Bird', 'Cat', 'Deer', 'Dog', 'Frog', 'Horse', 'Ship', 'Truck']\n\nW_grid = 10\nL_grid = 10\n\nfig, axes = plt.subplots(L_grid, W_grid, figsize = (25, 25))\naxes = axes.ravel()\n\nn_training = len(X_train)\n\nfor i in range(0,L_grid*W_grid):\n    index = np.random.randint(0,n_training) # pick a random number\n    axes[i].imshow(X_train[index])\n    index = y_train[index]\n    axes[i].set_title(labels[int(index)])\n    axes[i].axis('off')\n    \nplt.subplots_adjust(hspace = 0.4)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:18.650078Z","iopub.execute_input":"2021-08-03T12:48:18.650310Z","iopub.status.idle":"2021-08-03T12:48:23.302871Z","shell.execute_reply.started":"2021-08-03T12:48:18.650288Z","shell.execute_reply":"2021-08-03T12:48:23.301916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# converting the values into float and normalizing\nX_train = X_train.astype('float32')\nX_test = X_test.astype('float32')\nX_train = X_train/255\nX_test = X_test/255\n","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:23.304244Z","iopub.execute_input":"2021-08-03T12:48:23.304676Z","iopub.status.idle":"2021-08-03T12:48:23.754669Z","shell.execute_reply.started":"2021-08-03T12:48:23.304631Z","shell.execute_reply":"2021-08-03T12:48:23.753758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# encoding the Predictor variable\nimport keras\ny_train = keras.utils.to_categorical(y_train, 10)\ny_test = keras.utils.to_categorical(y_test, 10)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:23.756164Z","iopub.execute_input":"2021-08-03T12:48:23.756618Z","iopub.status.idle":"2021-08-03T12:48:23.763183Z","shell.execute_reply.started":"2021-08-03T12:48:23.756579Z","shell.execute_reply":"2021-08-03T12:48:23.762415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Storing the input shape\nInput_shape = X_train.shape[1:]\nInput_shape","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:23.764540Z","iopub.execute_input":"2021-08-03T12:48:23.764921Z","iopub.status.idle":"2021-08-03T12:48:23.774913Z","shell.execute_reply.started":"2021-08-03T12:48:23.764883Z","shell.execute_reply":"2021-08-03T12:48:23.773990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D,Dense,AveragePooling2D,MaxPooling2D, Flatten, Dropout\nfrom keras.optimizers import Adam\nfrom keras.callbacks import TensorBoard","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:23.778030Z","iopub.execute_input":"2021-08-03T12:48:23.778349Z","iopub.status.idle":"2021-08-03T12:48:23.784100Z","shell.execute_reply.started":"2021-08-03T12:48:23.778322Z","shell.execute_reply":"2021-08-03T12:48:23.783328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model = Sequential()\ncnn_model.add(Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu', input_shape = Input_shape))\ncnn_model.add(Conv2D(filters = 64, kernel_size = (3,3), activation = 'relu'))\ncnn_model.add(MaxPooling2D(2,2))\ncnn_model.add(Dropout(0.4))\n\n\ncnn_model.add(Conv2D(filters = 128, kernel_size = (3,3), activation = 'relu'))\ncnn_model.add(Conv2D(filters = 128, kernel_size = (3,3), activation = 'relu'))\ncnn_model.add(AveragePooling2D(2,2))\ncnn_model.add(Dropout(0.4))\n\ncnn_model.add(Flatten())\n\ncnn_model.add(Dense(units = 1024, activation = 'relu'))\n\ncnn_model.add(Dense(units = 1024, activation = 'relu'))\n\ncnn_model.add(Dense(units = 10, activation = 'softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:23.785472Z","iopub.execute_input":"2021-08-03T12:48:23.785748Z","iopub.status.idle":"2021-08-03T12:48:23.896945Z","shell.execute_reply.started":"2021-08-03T12:48:23.785724Z","shell.execute_reply":"2021-08-03T12:48:23.896200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.compile(loss = 'categorical_crossentropy', optimizer = keras.optimizers.Adam(lr = 0.001), metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:23.897922Z","iopub.execute_input":"2021-08-03T12:48:23.898319Z","iopub.status.idle":"2021-08-03T12:48:23.913884Z","shell.execute_reply.started":"2021-08-03T12:48:23.898292Z","shell.execute_reply":"2021-08-03T12:48:23.912981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:23.915003Z","iopub.execute_input":"2021-08-03T12:48:23.915610Z","iopub.status.idle":"2021-08-03T12:48:23.926772Z","shell.execute_reply.started":"2021-08-03T12:48:23.915568Z","shell.execute_reply":"2021-08-03T12:48:23.925094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fitting the train data into the model\nhistory = cnn_model.fit(X_train, y_train, batch_size = 32, epochs = 10, shuffle = True,validation_data=(X_test,y_test))","metadata":{"execution":{"iopub.status.busy":"2021-08-03T12:48:23.927817Z","iopub.execute_input":"2021-08-03T12:48:23.928072Z","iopub.status.idle":"2021-08-03T13:35:19.441732Z","shell.execute_reply.started":"2021-08-03T12:48:23.928048Z","shell.execute_reply":"2021-08-03T13:35:19.440515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluating the model performance\nevaluation = cnn_model.evaluate(X_test, y_test)\nprint('Test Accuracy: {}'.format(evaluation[1]))","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:35:19.444224Z","iopub.execute_input":"2021-08-03T13:35:19.444587Z","iopub.status.idle":"2021-08-03T13:35:32.246327Z","shell.execute_reply.started":"2021-08-03T13:35:19.444558Z","shell.execute_reply":"2021-08-03T13:35:32.245308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes = cnn_model.predict_classes(X_test) \npredicted_classes","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:35:32.247580Z","iopub.execute_input":"2021-08-03T13:35:32.247880Z","iopub.status.idle":"2021-08-03T13:35:45.208025Z","shell.execute_reply.started":"2021-08-03T13:35:32.247845Z","shell.execute_reply":"2021-08-03T13:35:45.207304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = y_test.argmax(1)\ny_test","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:35:45.209132Z","iopub.execute_input":"2021-08-03T13:35:45.209633Z","iopub.status.idle":"2021-08-03T13:35:45.215195Z","shell.execute_reply.started":"2021-08-03T13:35:45.209592Z","shell.execute_reply":"2021-08-03T13:35:45.214403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test ","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:35:45.216399Z","iopub.execute_input":"2021-08-03T13:35:45.216891Z","iopub.status.idle":"2021-08-03T13:35:45.226279Z","shell.execute_reply.started":"2021-08-03T13:35:45.216862Z","shell.execute_reply":"2021-08-03T13:35:45.225372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plotting Confusion Matrix\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns\n\ncm = confusion_matrix(y_test, predicted_classes)\ncm\nplt.figure(figsize = (10, 10))\nsns.heatmap(cm, annot = True)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:35:45.227667Z","iopub.execute_input":"2021-08-03T13:35:45.227996Z","iopub.status.idle":"2021-08-03T13:35:46.162967Z","shell.execute_reply.started":"2021-08-03T13:35:45.227968Z","shell.execute_reply":"2021-08-03T13:35:46.162019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"MODEL TRAINING USING AUGEMENTED DATASET","metadata":{}},{"cell_type":"code","source":"# generating more data using the existing data\nfrom keras.preprocessing.image import ImageDataGenerator\n\ndatagen = ImageDataGenerator(\n                            #width_shift_range = 0.1,\n                            horizontal_flip = True,\n                             )\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:35:46.164282Z","iopub.execute_input":"2021-08-03T13:35:46.164615Z","iopub.status.idle":"2021-08-03T13:35:46.525144Z","shell.execute_reply.started":"2021-08-03T13:35:46.164586Z","shell.execute_reply":"2021-08-03T13:35:46.524103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# retraining the model\n\ncnn_model.fit_generator(datagen.flow(X_train, y_train, batch_size = 32), epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:35:46.526415Z","iopub.execute_input":"2021-08-03T13:35:46.526769Z","iopub.status.idle":"2021-08-03T13:58:16.703474Z","shell.execute_reply.started":"2021-08-03T13:35:46.526738Z","shell.execute_reply":"2021-08-03T13:58:16.702473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# encoding the Predictor variable\ny_test = keras.utils.to_categorical(y_test, 10)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:58:16.705017Z","iopub.execute_input":"2021-08-03T13:58:16.705695Z","iopub.status.idle":"2021-08-03T13:58:16.709870Z","shell.execute_reply.started":"2021-08-03T13:58:16.705655Z","shell.execute_reply":"2021-08-03T13:58:16.709191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# printing the accuracy\nscore = cnn_model.evaluate(X_test, y_test)\nprint('Test accuracy', score[1])","metadata":{"execution":{"iopub.status.busy":"2021-08-03T13:58:16.710945Z","iopub.execute_input":"2021-08-03T13:58:16.711416Z","iopub.status.idle":"2021-08-03T13:58:29.815517Z","shell.execute_reply.started":"2021-08-03T13:58:16.711384Z","shell.execute_reply":"2021-08-03T13:58:29.814807Z"},"trusted":true},"execution_count":null,"outputs":[]}]}