{"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":"# Here in this notebook we will train a CNN model from scratch and then using transfer learning ","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:20.173566Z","iopub.execute_input":"2023-01-22T17:25:20.173943Z","iopub.status.idle":"2023-01-22T17:25:20.178699Z","shell.execute_reply.started":"2023-01-22T17:25:20.173912Z","shell.execute_reply":"2023-01-22T17:25:20.177665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:20.364018Z","iopub.execute_input":"2023-01-22T17:25:20.364364Z","iopub.status.idle":"2023-01-22T17:25:20.369112Z","shell.execute_reply.started":"2023-01-22T17:25:20.364335Z","shell.execute_reply":"2023-01-22T17:25:20.368047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(r\"/kaggle/input/digit-recognizer/train.csv\")\ndf_test = pd.read_csv(r\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:20.566158Z","iopub.execute_input":"2023-01-22T17:25:20.566454Z","iopub.status.idle":"2023-01-22T17:25:25.492870Z","shell.execute_reply.started":"2023-01-22T17:25:20.566428Z","shell.execute_reply":"2023-01-22T17:25:25.491855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = df_train['label']\ndf_train = df_train.drop('label', axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:25.494762Z","iopub.execute_input":"2023-01-22T17:25:25.495132Z","iopub.status.idle":"2023-01-22T17:25:25.587264Z","shell.execute_reply.started":"2023-01-22T17:25:25.495097Z","shell.execute_reply":"2023-01-22T17:25:25.586290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.values.reshape(-1,28,28,1)\ndf_test = df_test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:25.588895Z","iopub.execute_input":"2023-01-22T17:25:25.589330Z","iopub.status.idle":"2023-01-22T17:25:25.594798Z","shell.execute_reply.started":"2023-01-22T17:25:25.589291Z","shell.execute_reply":"2023-01-22T17:25:25.593864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train/255.0  \ndf_test = df_test/255.0","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:25.597462Z","iopub.execute_input":"2023-01-22T17:25:25.598058Z","iopub.status.idle":"2023-01-22T17:25:25.772854Z","shell.execute_reply.started":"2023-01-22T17:25:25.598014Z","shell.execute_reply":"2023-01-22T17:25:25.771682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare the dataset for CNN from scrach","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:25.774372Z","iopub.execute_input":"2023-01-22T17:25:25.775202Z","iopub.status.idle":"2023-01-22T17:25:25.781057Z","shell.execute_reply.started":"2023-01-22T17:25:25.775162Z","shell.execute_reply":"2023-01-22T17:25:25.779811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_1, X_valid_1, y_train_1, y_valid_1 = train_test_split(df_train, label, test_size= 0.3)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:25.783059Z","iopub.execute_input":"2023-01-22T17:25:25.783536Z","iopub.status.idle":"2023-01-22T17:25:26.167968Z","shell.execute_reply.started":"2023-01-22T17:25:25.783483Z","shell.execute_reply":"2023-01-22T17:25:26.166886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'X_train shape is: {X_train_1.shape}')\nprint(f'X_valid shape is: {X_valid_1.shape}')\nprint(f'y_train shape is: {y_train_1.shape}')\nprint(f'y_valid shape is: {y_valid_1.shape}')","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:26.169492Z","iopub.execute_input":"2023-01-22T17:25:26.169875Z","iopub.status.idle":"2023-01-22T17:25:26.176690Z","shell.execute_reply.started":"2023-01-22T17:25:26.169836Z","shell.execute_reply":"2023-01-22T17:25:26.175695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preview some images\nfig, axes = plt.subplots(5, 5, figsize=(10,10))\naxes = axes.ravel()\nfor i in np.arange(0, 25):\n    axes[i].imshow(X_train_1[i].reshape(28, 28), cmap = 'gray')\n    axes[i].axis('off')\nplt.subplots_adjust(hspace=0.5)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:26.178426Z","iopub.execute_input":"2023-01-22T17:25:26.179205Z","iopub.status.idle":"2023-01-22T17:25:27.060858Z","shell.execute_reply.started":"2023-01-22T17:25:26.179168Z","shell.execute_reply":"2023-01-22T17:25:27.059917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data augmentation\n### Creating 5X training data ","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:27.061813Z","iopub.execute_input":"2023-01-22T17:25:27.062209Z","iopub.status.idle":"2023-01-22T17:25:27.068777Z","shell.execute_reply.started":"2023-01-22T17:25:27.062158Z","shell.execute_reply":"2023-01-22T17:25:27.067819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_gen = ImageDataGenerator(\n           rotation_range=60,\n           width_shift_range=0.2,\n           height_shift_range=0.2,\n           shear_range=0.1,\n           zoom_range=0.2,\n           horizontal_flip=False,\n           vertical_flip=False,\n)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:27.073306Z","iopub.execute_input":"2023-01-22T17:25:27.074211Z","iopub.status.idle":"2023-01-22T17:25:27.087638Z","shell.execute_reply.started":"2023-01-22T17:25:27.074167Z","shell.execute_reply":"2023-01-22T17:25:27.086804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_augmented, y_augmented = [], []\nfor x, y in data_gen.flow(X_train_1, y_train_1, batch_size= X_train_1.shape[0], shuffle = False):\n    X_augmented.append(x)\n    y_augmented.append(y)\n    if len(X_augmented) == 5:\n        break","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:27.090779Z","iopub.execute_input":"2023-01-22T17:25:27.091521Z","iopub.status.idle":"2023-01-22T17:25:59.753373Z","shell.execute_reply.started":"2023-01-22T17:25:27.091495Z","shell.execute_reply":"2023-01-22T17:25:59.752300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_augmented = np.concatenate(X_augmented)\ny_augmented = np.concatenate(y_augmented)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:59.754802Z","iopub.execute_input":"2023-01-22T17:25:59.755869Z","iopub.status.idle":"2023-01-22T17:25:59.951290Z","shell.execute_reply.started":"2023-01-22T17:25:59.755829Z","shell.execute_reply":"2023-01-22T17:25:59.950288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_augmented = np.asarray(X_augmented)\ny_augmented = np.asarray(y_augmented)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:59.952640Z","iopub.execute_input":"2023-01-22T17:25:59.953402Z","iopub.status.idle":"2023-01-22T17:25:59.958862Z","shell.execute_reply.started":"2023-01-22T17:25:59.953364Z","shell.execute_reply":"2023-01-22T17:25:59.957626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_1 = np.concatenate((X_train_1, X_augmented), axis =0)\ny_train_1 = np.concatenate((y_train_1, y_augmented), axis = 0)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:25:59.960316Z","iopub.execute_input":"2023-01-22T17:25:59.961212Z","iopub.status.idle":"2023-01-22T17:26:00.277665Z","shell.execute_reply.started":"2023-01-22T17:25:59.961073Z","shell.execute_reply":"2023-01-22T17:26:00.276609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preview some images\nfig, axes = plt.subplots(5, 5, figsize=(10,10))\naxes = axes.ravel()\nfor i in np.arange(0, 25):\n    plt.subplot(5, 5, i+1)\n    axes[i].imshow(X_augmented[i].reshape(28, 28), cmap = 'gray')\n    plt.title(str('label is: '+ str(y_augmented[i])), fontsize=15, color='black')\n    axes[i].axis('off') \nplt.subplots_adjust(hspace=0.5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:26:00.279327Z","iopub.execute_input":"2023-01-22T17:26:00.279740Z","iopub.status.idle":"2023-01-22T17:26:01.340167Z","shell.execute_reply.started":"2023-01-22T17:26:00.279701Z","shell.execute_reply":"2023-01-22T17:26:01.339216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:26:01.341702Z","iopub.execute_input":"2023-01-22T17:26:01.342081Z","iopub.status.idle":"2023-01-22T17:26:01.346781Z","shell.execute_reply.started":"2023-01-22T17:26:01.342036Z","shell.execute_reply":"2023-01-22T17:26:01.345614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_1 = to_categorical(y_train_1, num_classes = 10)\ny_valid_1 = to_categorical(y_valid_1, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:26:01.348607Z","iopub.execute_input":"2023-01-22T17:26:01.349237Z","iopub.status.idle":"2023-01-22T17:26:01.361086Z","shell.execute_reply.started":"2023-01-22T17:26:01.349201Z","shell.execute_reply":"2023-01-22T17:26:01.360161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'X_train shape is: {X_train_1.shape}')\nprint(f'X_valid shape is: {X_valid_1.shape}')\nprint(f'y_train shape is: {y_train_1.shape}')\nprint(f'y_valid shape is: {y_valid_1.shape}')","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:26:01.362046Z","iopub.execute_input":"2023-01-22T17:26:01.362935Z","iopub.status.idle":"2023-01-22T17:26:01.370413Z","shell.execute_reply.started":"2023-01-22T17:26:01.362900Z","shell.execute_reply":"2023-01-22T17:26:01.369455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout, Input","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:26:01.371611Z","iopub.execute_input":"2023-01-22T17:26:01.372456Z","iopub.status.idle":"2023-01-22T17:26:01.381670Z","shell.execute_reply.started":"2023-01-22T17:26:01.372421Z","shell.execute_reply":"2023-01-22T17:26:01.380756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(64, kernel_size = (3,3), activation = 'relu', input_shape = (28,28,1)))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Conv2D(64, kernel_size = (3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Flatten())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(512, activation = 'relu'))\nmodel.add(Dense(10, activation = 'softmax'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:26:01.383024Z","iopub.execute_input":"2023-01-22T17:26:01.384458Z","iopub.status.idle":"2023-01-22T17:26:01.439366Z","shell.execute_reply.started":"2023-01-22T17:26:01.384423Z","shell.execute_reply":"2023-01-22T17:26:01.438520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:26:01.440806Z","iopub.execute_input":"2023-01-22T17:26:01.441081Z","iopub.status.idle":"2023-01-22T17:26:01.454321Z","shell.execute_reply.started":"2023-01-22T17:26:01.441050Z","shell.execute_reply":"2023-01-22T17:26:01.453281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train_1, y_train_1, epochs=10, batch_size=64, validation_data=(X_valid_1, y_valid_1))","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:26:01.456754Z","iopub.execute_input":"2023-01-22T17:26:01.457432Z","iopub.status.idle":"2023-01-22T17:28:03.607908Z","shell.execute_reply.started":"2023-01-22T17:26:01.457406Z","shell.execute_reply":"2023-01-22T17:28:03.606868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(df_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:03.610954Z","iopub.execute_input":"2023-01-22T17:28:03.611256Z","iopub.status.idle":"2023-01-22T17:28:05.041335Z","shell.execute_reply.started":"2023-01-22T17:28:03.611215Z","shell.execute_reply":"2023-01-22T17:28:05.040225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = predictions.argmax(axis = -1)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:05.042839Z","iopub.execute_input":"2023-01-22T17:28:05.043264Z","iopub.status.idle":"2023-01-22T17:28:05.051488Z","shell.execute_reply.started":"2023-01-22T17:28:05.043201Z","shell.execute_reply":"2023-01-22T17:28:05.050545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preview some images\nfig, axes = plt.subplots(5, 5, figsize=(10,10))\naxes = axes.ravel()\nfor i in np.arange(0, 25):\n    plt.subplot(5, 5, i+1)\n    axes[i].imshow(df_test[i].reshape(28, 28,1), cmap = 'gray')\n    plt.title(str('prediction is: '+ str(y_test[i])), fontsize=15, color='black')\n    axes[i].axis('off') \nplt.subplots_adjust(hspace=0.5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:05.053051Z","iopub.execute_input":"2023-01-22T17:28:05.053511Z","iopub.status.idle":"2023-01-22T17:28:06.237717Z","shell.execute_reply.started":"2023-01-22T17:28:05.053469Z","shell.execute_reply":"2023-01-22T17:28:06.236659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_1 = pd.DataFrame({\n    'ImageId': range(1, len(df_test)+1),\n    'Label': y_test\n}).set_index('ImageId')","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:06.244296Z","iopub.execute_input":"2023-01-22T17:28:06.244577Z","iopub.status.idle":"2023-01-22T17:28:06.251639Z","shell.execute_reply.started":"2023-01-22T17:28:06.244550Z","shell.execute_reply":"2023-01-22T17:28:06.250519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_1","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:06.253063Z","iopub.execute_input":"2023-01-22T17:28:06.253494Z","iopub.status.idle":"2023-01-22T17:28:06.270471Z","shell.execute_reply.started":"2023-01-22T17:28:06.253459Z","shell.execute_reply":"2023-01-22T17:28:06.269451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_1.to_csv('sub_1.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:06.271785Z","iopub.execute_input":"2023-01-22T17:28:06.272116Z","iopub.status.idle":"2023-01-22T17:28:06.307075Z","shell.execute_reply.started":"2023-01-22T17:28:06.272073Z","shell.execute_reply":"2023-01-22T17:28:06.306272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Applying transfer learning","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg16 import VGG16","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:06.308466Z","iopub.execute_input":"2023-01-22T17:28:06.308823Z","iopub.status.idle":"2023-01-22T17:28:06.314309Z","shell.execute_reply.started":"2023-01-22T17:28:06.308790Z","shell.execute_reply":"2023-01-22T17:28:06.312249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The lowest input shape for the VGG16 model is (32,32,3)\nvgg = VGG16(weights='imagenet', include_top=False, input_shape= (32,32,3))","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:06.315471Z","iopub.execute_input":"2023-01-22T17:28:06.316217Z","iopub.status.idle":"2023-01-22T17:28:06.579443Z","shell.execute_reply.started":"2023-01-22T17:28:06.316184Z","shell.execute_reply":"2023-01-22T17:28:06.578448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:06.581471Z","iopub.execute_input":"2023-01-22T17:28:06.582183Z","iopub.status.idle":"2023-01-22T17:28:06.591263Z","shell.execute_reply.started":"2023-01-22T17:28:06.582144Z","shell.execute_reply":"2023-01-22T17:28:06.590113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in vgg.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:06.593063Z","iopub.execute_input":"2023-01-22T17:28:06.593606Z","iopub.status.idle":"2023-01-22T17:28:06.600430Z","shell.execute_reply.started":"2023-01-22T17:28:06.593570Z","shell.execute_reply":"2023-01-22T17:28:06.599352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model = Sequential()\nvgg_model.add(vgg)\nvgg_model.add(Flatten())\nvgg_model.add(Dense(1024, activation = 'relu'))\nvgg_model.add(Dropout(0.25))\nvgg_model.add(Dense(10, activation = 'softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:06.601776Z","iopub.execute_input":"2023-01-22T17:28:06.602199Z","iopub.status.idle":"2023-01-22T17:28:06.675691Z","shell.execute_reply.started":"2023-01-22T17:28:06.602164Z","shell.execute_reply":"2023-01-22T17:28:06.674676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:06.676868Z","iopub.execute_input":"2023-01-22T17:28:06.677188Z","iopub.status.idle":"2023-01-22T17:28:06.682691Z","shell.execute_reply.started":"2023-01-22T17:28:06.677163Z","shell.execute_reply":"2023-01-22T17:28:06.681684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### To use VGG16 as a base model, we should first convert the input shape to (32,32,3)\n- We will pad our original images by adding zeros to get final (32 * 32) pixels\n- Then we will add the three channels.","metadata":{}},{"cell_type":"code","source":"X_train_2, X_valid_2, y_train_2, y_valid_2 = train_test_split(df_train, label, test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:06.684094Z","iopub.execute_input":"2023-01-22T17:28:06.684704Z","iopub.status.idle":"2023-01-22T17:28:07.065066Z","shell.execute_reply.started":"2023-01-22T17:28:06.684671Z","shell.execute_reply":"2023-01-22T17:28:07.064041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_2 = np.pad(X_train_2, [(0,0), (2,2), (2,2), (0,0)], mode='constant')\nX_valid_2 = np.pad(X_valid_2, [(0,0), (2,2), (2,2), (0,0)], mode='constant')","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:07.066514Z","iopub.execute_input":"2023-01-22T17:28:07.066895Z","iopub.status.idle":"2023-01-22T17:28:07.248501Z","shell.execute_reply.started":"2023-01-22T17:28:07.066856Z","shell.execute_reply":"2023-01-22T17:28:07.247171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_2 = np.pad(df_test, [(0,0), (2,2), (2,2), (0,0)], mode='constant')","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:07.250120Z","iopub.execute_input":"2023-01-22T17:28:07.250514Z","iopub.status.idle":"2023-01-22T17:28:07.392679Z","shell.execute_reply.started":"2023-01-22T17:28:07.250478Z","shell.execute_reply":"2023-01-22T17:28:07.391633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_2 = np.repeat(X_train_2, 3, axis=-1)\nX_valid_2 = np.repeat(X_valid_2, 3, axis = -1)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:07.394108Z","iopub.execute_input":"2023-01-22T17:28:07.394711Z","iopub.status.idle":"2023-01-22T17:28:07.997912Z","shell.execute_reply.started":"2023-01-22T17:28:07.394668Z","shell.execute_reply":"2023-01-22T17:28:07.996805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_2 = np.repeat(X_test_2, 3, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:07.999320Z","iopub.execute_input":"2023-01-22T17:28:08.000064Z","iopub.status.idle":"2023-01-22T17:28:08.521719Z","shell.execute_reply.started":"2023-01-22T17:28:08.000024Z","shell.execute_reply":"2023-01-22T17:28:08.520625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_2 = to_categorical(y_train_2, num_classes=10)\ny_valid_2 = to_categorical(y_valid_2, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:08.523177Z","iopub.execute_input":"2023-01-22T17:28:08.523850Z","iopub.status.idle":"2023-01-22T17:28:08.530293Z","shell.execute_reply.started":"2023-01-22T17:28:08.523810Z","shell.execute_reply":"2023-01-22T17:28:08.529309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_2.shape, X_valid_2.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:08.532038Z","iopub.execute_input":"2023-01-22T17:28:08.532838Z","iopub.status.idle":"2023-01-22T17:28:08.550366Z","shell.execute_reply.started":"2023-01-22T17:28:08.532805Z","shell.execute_reply":"2023-01-22T17:28:08.549479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_2.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:08.559318Z","iopub.execute_input":"2023-01-22T17:28:08.560059Z","iopub.status.idle":"2023-01-22T17:28:08.570341Z","shell.execute_reply.started":"2023-01-22T17:28:08.560025Z","shell.execute_reply":"2023-01-22T17:28:08.569470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_2.shape, y_valid_2.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:08.571838Z","iopub.execute_input":"2023-01-22T17:28:08.573235Z","iopub.status.idle":"2023-01-22T17:28:08.585138Z","shell.execute_reply.started":"2023-01-22T17:28:08.573198Z","shell.execute_reply":"2023-01-22T17:28:08.583533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:08.587683Z","iopub.execute_input":"2023-01-22T17:28:08.588323Z","iopub.status.idle":"2023-01-22T17:28:08.609227Z","shell.execute_reply.started":"2023-01-22T17:28:08.588288Z","shell.execute_reply":"2023-01-22T17:28:08.608209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model.fit(X_train_2, y_train_2, epochs=15, batch_size = 64, validation_data= (X_valid_2, y_valid_2))","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:28:08.622706Z","iopub.execute_input":"2023-01-22T17:28:08.624836Z","iopub.status.idle":"2023-01-22T17:30:32.300202Z","shell.execute_reply.started":"2023-01-22T17:28:08.624802Z","shell.execute_reply":"2023-01-22T17:30:32.298800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_2 = vgg_model.predict(X_test_2)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:30:32.304470Z","iopub.execute_input":"2023-01-22T17:30:32.305843Z","iopub.status.idle":"2023-01-22T17:30:40.420139Z","shell.execute_reply.started":"2023-01-22T17:30:32.305778Z","shell.execute_reply":"2023-01-22T17:30:40.419127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_2 = predictions_2.argmax(axis = -1)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:30:40.421704Z","iopub.execute_input":"2023-01-22T17:30:40.422060Z","iopub.status.idle":"2023-01-22T17:30:40.428400Z","shell.execute_reply.started":"2023-01-22T17:30:40.422024Z","shell.execute_reply":"2023-01-22T17:30:40.427124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preview some images\nfig, axes = plt.subplots(5, 5, figsize=(10,10))\naxes = axes.ravel()\nfor i in np.arange(0, 25):\n    plt.subplot(5, 5, i+1)\n    axes[i].imshow(X_test_2[i].reshape(32, 32, 3), cmap = 'gray')\n    plt.title(str('prediction is: '+ str(y_test_2[i])), fontsize=15, color='black')\n    axes[i].axis('off') \nplt.subplots_adjust(hspace=0.5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:30:40.429939Z","iopub.execute_input":"2023-01-22T17:30:40.431166Z","iopub.status.idle":"2023-01-22T17:30:41.222050Z","shell.execute_reply.started":"2023-01-22T17:30:40.431127Z","shell.execute_reply":"2023-01-22T17:30:41.221149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_2 = pd.DataFrame({\n    'ImageId': range(1, len(df_test)+1),\n    'Label': y_test_2\n}).set_index('ImageId')\nsub_2","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:30:41.223464Z","iopub.execute_input":"2023-01-22T17:30:41.224521Z","iopub.status.idle":"2023-01-22T17:30:41.237873Z","shell.execute_reply.started":"2023-01-22T17:30:41.224482Z","shell.execute_reply":"2023-01-22T17:30:41.236650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_2.to_csv('sub_2.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-22T17:30:41.239671Z","iopub.execute_input":"2023-01-22T17:30:41.240417Z","iopub.status.idle":"2023-01-22T17:30:41.275323Z","shell.execute_reply.started":"2023-01-22T17:30:41.240378Z","shell.execute_reply":"2023-01-22T17:30:41.274491Z"},"trusted":true},"execution_count":null,"outputs":[]}]}