{"cells":[{"metadata":{"trusted":true},"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 ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"# install keras neural-network library\n!pip install keras","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# loading the dataset \nfrom keras.datasets import cifar10\n(X_train, y_train) , (X_test, y_test) = cifar10.load_data()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Storing the input shape\nInput_shape = X_train.shape[1:]\nInput_shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cnn_model.compile(loss = 'categorical_crossentropy', optimizer = keras.optimizers.Adam(lr = 0.001), metrics = ['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cnn_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Evaluating the model performance\nevaluation = cnn_model.evaluate(X_test, y_test)\nprint('Test Accuracy: {}'.format(evaluation[1]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predicted_classes = cnn_model.predict_classes(X_test) \npredicted_classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_test = y_test.argmax(1)\ny_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_test ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"MODEL TRAINING USING AUGEMENTED DATASET","execution_count":null},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# retraining the model\n\ncnn_model.fit_generator(datagen.flow(X_train, y_train, batch_size = 32), epochs = 5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# encoding the Predictor variable\ny_test = keras.utils.to_categorical(y_test, 10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# printing the accuracy\nscore = cnn_model.evaluate(X_test, y_test)\nprint('Test accuracy', score[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}