{"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 numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom PIL import Image\nfrom skimage import transform\nfrom tqdm import tqdm\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-22T15:04:20.412245Z","iopub.execute_input":"2022-02-22T15:04:20.412755Z","iopub.status.idle":"2022-02-22T15:04:25.448577Z","shell.execute_reply.started":"2022-02-22T15:04:20.412671Z","shell.execute_reply":"2022-02-22T15:04:25.447826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cifar = tf.keras.datasets.cifar10\n(x_train, y_train), (x_test, y_test) = cifar.load_data()\n\nX_train_r = np.asarray(x_train)/255.0\nX_test_r = np.asarray(x_test)/255.0\n\ndel x_train\ndel x_test","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:25.450124Z","iopub.execute_input":"2022-02-22T15:04:25.450382Z","iopub.status.idle":"2022-02-22T15:04:32.427968Z","shell.execute_reply.started":"2022-02-22T15:04:25.450345Z","shell.execute_reply":"2022-02-22T15:04:32.427222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:32.429225Z","iopub.execute_input":"2022-02-22T15:04:32.429481Z","iopub.status.idle":"2022-02-22T15:04:32.437935Z","shell.execute_reply.started":"2022-02-22T15:04:32.429447Z","shell.execute_reply":"2022-02-22T15:04:32.437232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Shape of X Train : \", X_train_r.shape)\nprint(\"Shape of X Test : \", X_test_r.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:32.440252Z","iopub.execute_input":"2022-02-22T15:04:32.440569Z","iopub.status.idle":"2022-02-22T15:04:32.584419Z","shell.execute_reply.started":"2022-02-22T15:04:32.440533Z","shell.execute_reply":"2022-02-22T15:04:32.583623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10, 10))\ncount = 1\nidd = 0\nfor i in range(5):\n    for j in range(5):\n        plt.subplot(5, 5, count)\n        plt.imshow(X_train_r[idd])\n        plt.axis('off')\n        idd += 1\n        count += 1\n\nprint(y_train[0:10])","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:32.586153Z","iopub.execute_input":"2022-02-22T15:04:32.586785Z","iopub.status.idle":"2022-02-22T15:04:33.715403Z","shell.execute_reply.started":"2022-02-22T15:04:32.586731Z","shell.execute_reply":"2022-02-22T15:04:33.714621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\n\n#For training data\ny_trainHot = np.uint8(to_categorical(y_train, num_classes = 10))","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:33.716421Z","iopub.execute_input":"2022-02-22T15:04:33.716627Z","iopub.status.idle":"2022-02-22T15:04:33.723009Z","shell.execute_reply.started":"2022-02-22T15:04:33.716600Z","shell.execute_reply":"2022-02-22T15:04:33.722133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The class division and number of images in each class\nunique, counts = np.unique(y_train, return_counts=True)\ndict(zip(unique, counts))","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:33.724802Z","iopub.execute_input":"2022-02-22T15:04:33.725364Z","iopub.status.idle":"2022-02-22T15:04:33.735850Z","shell.execute_reply.started":"2022-02-22T15:04:33.725324Z","shell.execute_reply":"2022-02-22T15:04:33.735105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n\n#Splitting the data for training and testing\nX_train_s, X_valid_s, y_train, y_valid = train_test_split(X_train_r, y_trainHot, test_size=0.2, random_state=42)\n\n#del X_train_r","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:33.737194Z","iopub.execute_input":"2022-02-22T15:04:33.737665Z","iopub.status.idle":"2022-02-22T15:04:34.224452Z","shell.execute_reply.started":"2022-02-22T15:04:33.737630Z","shell.execute_reply":"2022-02-22T15:04:34.223694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Initializing MobileNet as Base Model for Transfer Learning\nfrom tensorflow.keras.applications import MobileNetV2\n\nbase_model = MobileNetV2(include_top=False, weights='imagenet',\n            input_shape=(32,32,3), classes=y_train.shape[1])","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:34.225973Z","iopub.execute_input":"2022-02-22T15:04:34.226267Z","iopub.status.idle":"2022-02-22T15:04:37.985029Z","shell.execute_reply.started":"2022-02-22T15:04:34.226219Z","shell.execute_reply":"2022-02-22T15:04:37.984345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Flatten, Dropout\n\n#Adding layers to base model of MobileNet\n\nmodel = Sequential()\n\n#Creating base layer of VGG19\nmodel.add(base_model)\n\nmodel.add(Flatten())\n\n#Adding the Dense Layers and Dropout\nmodel.add(Dense(512,activation=('relu'))) \n\nmodel.add(Dense(256,activation=('relu'))) \n\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(128,activation=('relu')))\n\nmodel.add(Dropout(0.2))\n\nmodel.add(Dense(10,activation=('softmax')))","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:37.987969Z","iopub.execute_input":"2022-02-22T15:04:37.988230Z","iopub.status.idle":"2022-02-22T15:04:38.336104Z","shell.execute_reply.started":"2022-02-22T15:04:37.988194Z","shell.execute_reply":"2022-02-22T15:04:38.335345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualizing Model Summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:38.337276Z","iopub.execute_input":"2022-02-22T15:04:38.337536Z","iopub.status.idle":"2022-02-22T15:04:38.357601Z","shell.execute_reply.started":"2022-02-22T15:04:38.337502Z","shell.execute_reply":"2022-02-22T15:04:38.356949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\n\nnum_epochs = 20\nbatch_size = 32\n\n#Compiling the model\nopt = Adam(learning_rate = 1e-4)\nmodel.compile(loss = \"categorical_crossentropy\", optimizer=opt, metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:38.358807Z","iopub.execute_input":"2022-02-22T15:04:38.359053Z","iopub.status.idle":"2022-02-22T15:04:38.375753Z","shell.execute_reply.started":"2022-02-22T15:04:38.359019Z","shell.execute_reply":"2022-02-22T15:04:38.375097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train_s.shape)\nprint(y_train.shape)\nprint(y_valid.shape)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:38.376855Z","iopub.execute_input":"2022-02-22T15:04:38.377093Z","iopub.status.idle":"2022-02-22T15:04:38.382962Z","shell.execute_reply.started":"2022-02-22T15:04:38.377060Z","shell.execute_reply":"2022-02-22T15:04:38.382242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"H = model.fit(X_train_s,y_train, \n              batch_size = batch_size,\n              validation_data = (X_valid_s, y_valid), \n              epochs = num_epochs)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:04:38.384111Z","iopub.execute_input":"2022-02-22T15:04:38.384907Z","iopub.status.idle":"2022-02-22T15:14:24.975261Z","shell.execute_reply.started":"2022-02-22T15:04:38.384870Z","shell.execute_reply":"2022-02-22T15:14:24.974485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_confusion_matrix(cm):\n\n    #Setting the figure size\n    fig, ax = plt.subplots(figsize=(12,5))\n    \n    #Creating the pandas dataframe\n    d_cm = pd.DataFrame(cm)\n    \n    sn.set(font_scale=1)\n    \n    #Setting the fontsize and plotting the heatmap\n    sn.heatmap(d_cm, annot=True, fmt='g',annot_kws={\"size\": 16})\n    \n    plt.title('Confusion matrix')\n    plt.xlabel('Actual label')\n    plt.ylabel('Predicted label');\n\n    plt.show()\n    \ndef acc_versus_epochs(hist):\n    \n    plt.figure(figsize=(10,5))\n    plt.subplot(2,1,2)\n    loss_train = hist.history['accuracy']\n    loss_val = hist.history['val_accuracy']\n    epochs = range(1,21)\n    plt.plot(epochs, loss_train, 'g', label='Training accuracy')\n    plt.plot(epochs, loss_val, 'b', label='Validation accuracy')\n    plt.title('Training and Validation accuracy')\n    plt.xlabel('Epochs')\n    plt.ylabel('Accuracy')\n    plt.legend()\n    plt.show()\n    \n    plt.figure(figsize=(10,5))\n    plt.subplot(2,1,1)\n    loss_train = hist.history['loss']\n    loss_val = hist.history['val_loss']\n    epochs = range(1,21)\n    plt.plot(epochs, loss_train, 'g', label='Training Loss')\n    plt.plot(epochs, loss_val, 'b', label='Validation Loss')\n    plt.title('Training and Validation Loss')\n    plt.xlabel('Epochs')\n    plt.ylabel('Loss')\n    plt.legend()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:14:24.976744Z","iopub.execute_input":"2022-02-22T15:14:24.977100Z","iopub.status.idle":"2022-02-22T15:14:24.989944Z","shell.execute_reply.started":"2022-02-22T15:14:24.977059Z","shell.execute_reply":"2022-02-22T15:14:24.989002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting the accuracy versus epochs\nacc_versus_epochs(H)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:14:24.991002Z","iopub.execute_input":"2022-02-22T15:14:24.991833Z","iopub.status.idle":"2022-02-22T15:14:25.605277Z","shell.execute_reply.started":"2022-02-22T15:14:24.991785Z","shell.execute_reply":"2022-02-22T15:14:25.604553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix\nimport seaborn as sn\n\ny_pred = model.predict(X_valid_s)\nY_pred_classes = np.argmax(y_pred,axis=1) \nY_true = np.argmax(y_valid,axis=1)\n\nconfusion_mtx = [Y_true, Y_pred_classes]\n\nplot_confusion_matrix(confusion_matrix(Y_true,Y_pred_classes))","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:14:25.606376Z","iopub.execute_input":"2022-02-22T15:14:25.606613Z","iopub.status.idle":"2022-02-22T15:14:29.283984Z","shell.execute_reply.started":"2022-02-22T15:14:25.606578Z","shell.execute_reply":"2022-02-22T15:14:29.283193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import recall_score,precision_score,f1_score\n\nprint(\"Accuracy of MobileNetV2 for MNIST : \" , recall_score(Y_true, Y_pred_classes, average='weighted'))\n\nprint(\"Precision score of MobileNetV2 for MNIST \" , precision_score(Y_true, Y_pred_classes, average='weighted'))\n\nprint(\"F1 score of MobileNetV2 for MNIST : \" , recall_score(Y_true, Y_pred_classes, average='weighted'))","metadata":{"execution":{"iopub.status.busy":"2022-02-22T15:14:29.285608Z","iopub.execute_input":"2022-02-22T15:14:29.285868Z","iopub.status.idle":"2022-02-22T15:14:29.308623Z","shell.execute_reply.started":"2022-02-22T15:14:29.285834Z","shell.execute_reply":"2022-02-22T15:14:29.307860Z"},"trusted":true},"execution_count":null,"outputs":[]}]}