{"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":"markdown","source":"<div style=\"background-color:#5D73F2; color:#19180F; font-size:40px; font-family:Arial; padding:10px; border: 5px solid #19180F; border-radius:10px\"> MobileNet V2 </div>\n<div style=\"background-color:#A8B4F6; color:#19180F; font-size:30px; font-family:Arial; padding:10px; border: 5px solid #19180F; border-radius:10px\"> Architecture Overview</div>\n<div style=\"background-color:#D5D9F2; color:#19180F; font-size:15px; font-family:Arial; padding:10px; border: 5px solid #19180F; border-radius:10px\"> \nMobileNetV2 is a popular deep neural network architecture designed for mobile devices and other resource-constrained environments. Google launched it in 2018 as an improved version of its predecessor MobileNetV1. The main goal of MobileNetV2 is to achieve high accuracy while reducing computational cost and network memory consumption.<br><br>\nMobileNetV2 uses depth-separable convolutions, which consist of depth and point convolutions. This approach reduces the number of parameters in the network and makes it more efficient. In addition, MobileNetV2 implements a linear convolution consisting of a pointwise convolution and a linear activation function. This improves the non-linearity of the network while keeping the computational cost low.<br><br>\nThe MobileNetV2 architecture consists of several building blocks called \"inverted residual blocks\". Each block takes as input a tensor and performs a sequence of operations on it. The output of one block is then fed into the next block, forming a deep neural network.<br><br>\nThe first layer of the network is a standard convolutional layer that performs a 3x3 convolution on the input tensor. This is followed by a batch normalization layer and a ReLU activation function. The output of this layer is then passed through a series of inverted residual blocks.<br><br>\nEach inverted residual block consists of three main operations: a depthwise convolution, a pointwise convolution, and a skip connection. The depthwise convolution performs a spatial convolution on each channel of the input tensor separately, followed by a batch normalization and a ReLU activation function. The pointwise convolution then applies a 1x1 convolution to the output of the depthwise convolution, followed by another batch normalization and ReLU activation. The skip connection then adds the input tensor to the output of the pointwise convolution.<br><br>\nIn addition to the standard inverted residual block, MobileNetV2 also includes an \"expansion layer\" that increases the number of channels in the input tensor before the depthwise convolution. This is followed by a \"projection layer\" that reduces the number of channels back to the original size before the skip connection.<br><br>\nThe architecture also includes a number of global pooling and fully connected layers at the end of the network, which are used to classify the input image.</div>\n","metadata":{}},{"cell_type":"code","source":"from IPython.display import SVG, display\nsvg_file = '/kaggle/input/machine-learning-architecture-diagrams/MobileNetV2.svg'\ndisplay(SVG(filename=svg_file))","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:08.721040Z","iopub.execute_input":"2023-07-11T12:10:08.722012Z","iopub.status.idle":"2023-07-11T12:10:08.774522Z","shell.execute_reply.started":"2023-07-11T12:10:08.721924Z","shell.execute_reply":"2023-07-11T12:10:08.773568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 Importing modules </div>","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n\nimport zipfile\nimport glob\nimport os\nimport random\nimport pandas as pd\nimport numpy as np\nfrom PIL import Image\n\nimport matplotlib\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nfrom sklearn.utils import shuffle\nfrom keras.preprocessing.image import ImageDataGenerator, img_to_array, array_to_img, load_img\nfrom sklearn.model_selection import train_test_split\n\n#importing various metrics\nfrom sklearn.metrics import confusion_matrix, classification_report, accuracy_score, f1_score, roc_auc_score\n\n# importing model relevant dependencies\nimport tensorflow as tf\nfrom keras.models import Model, Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten\nfrom keras import optimizers, regularizers\nfrom tensorflow.keras.applications.mobilenet_v2 import MobileNetV2\nfrom tensorflow.keras.applications.mobilenet_v2 import preprocess_input\nfrom keras.callbacks import EarlyStopping, LearningRateScheduler\n\nfrom keras import backend as K\nK.clear_session()\n\n#","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:08.776182Z","iopub.execute_input":"2023-07-11T12:10:08.776821Z","iopub.status.idle":"2023-07-11T12:10:16.086922Z","shell.execute_reply.started":"2023-07-11T12:10:08.776783Z","shell.execute_reply":"2023-07-11T12:10:16.085851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌\n    This code snippet is used to extract the training and testing data from zip files in the input directory of the Kaggle Dogs-vs-Cats challenge. </div>\n\n","metadata":{}},{"cell_type":"code","source":"\n# extracting train and test data from zipfiles to data folder\nzip_files = glob.glob('/kaggle/input/dogs-vs-cats/*.zip')\nprint(\"{} files found in the input directory\".format(len(zip_files)))","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:16.090215Z","iopub.execute_input":"2023-07-11T12:10:16.091135Z","iopub.status.idle":"2023-07-11T12:10:16.100598Z","shell.execute_reply.started":"2023-07-11T12:10:16.091091Z","shell.execute_reply":"2023-07-11T12:10:16.099404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌\nThis code snippet is used for extracting the contents of a zipped file in the \"data\" directory. </div>\n\n","metadata":{}},{"cell_type":"code","source":"for file in zip_files:\n    with zipfile.ZipFile(file,'r') as Z:\n        Z.extractall('data')\n    print('{} extraction complete'.format(file.split('/')[-1]))","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:16.102180Z","iopub.execute_input":"2023-07-11T12:10:16.102826Z","iopub.status.idle":"2023-07-11T12:10:33.031185Z","shell.execute_reply.started":"2023-07-11T12:10:16.102786Z","shell.execute_reply":"2023-07-11T12:10:33.030029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌\nThis code snippet performs exploratory data analysis (EDA) on the dataset of the Kaggle Dogs-vs-Cats challenge. The objective of EDA is to gain insights into the dataset and identify any patterns or trends that can help in building a better model. </div>\n\n","metadata":{}},{"cell_type":"code","source":"# EDA with images in the directory\ntrain_dir = '/kaggle/working/data/train'\ntest_dir = '/kaggle/working/data/test1'\n\nprint(\"{} is the count of images in train_dir\".format(len(os.listdir(train_dir))))\nprint(\"{} is the count of images in test_dir\".format(len(os.listdir(test_dir))))","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:33.034072Z","iopub.execute_input":"2023-07-11T12:10:33.034755Z","iopub.status.idle":"2023-07-11T12:10:33.064620Z","shell.execute_reply.started":"2023-07-11T12:10:33.034700Z","shell.execute_reply":"2023-07-11T12:10:33.063677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# let's explore a sample file and how to extract label and file name from it.\n\nfor _ in range(5):\n    print(random.choice(os.listdir(train_dir)))","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:33.066024Z","iopub.execute_input":"2023-07-11T12:10:33.066412Z","iopub.status.idle":"2023-07-11T12:10:33.139874Z","shell.execute_reply.started":"2023-07-11T12:10:33.066372Z","shell.execute_reply":"2023-07-11T12:10:33.138889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 In this code snippet, we are saving the extracted data into a dataframe for further processing. </div>\n\n\n","metadata":{}},{"cell_type":"code","source":"# saving extracted data into dataframe for further processing\ndef label(path):\n    return [file.split('.')[0] for file in os.listdir(path)]\n\ndef filename(path):\n    return [file for file in os.listdir(path)]\n\nx_train_images_name = filename(train_dir)\nx_test_images_name = filename(test_dir)\ny_train_label_name = label(train_dir)\n\n#creating df with name and label\n\ndf_train_image = pd.DataFrame({'filename':x_train_images_name, 'label':y_train_label_name})\ndf_submission = pd.DataFrame({'filename':x_test_images_name})","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:33.141327Z","iopub.execute_input":"2023-07-11T12:10:33.141928Z","iopub.status.idle":"2023-07-11T12:10:33.201684Z","shell.execute_reply.started":"2023-07-11T12:10:33.141890Z","shell.execute_reply":"2023-07-11T12:10:33.200748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 In this code snippet, We are printing out the head of the dataframe </div>","metadata":{}},{"cell_type":"code","source":"df_train_image.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:33.203045Z","iopub.execute_input":"2023-07-11T12:10:33.204015Z","iopub.status.idle":"2023-07-11T12:10:33.221575Z","shell.execute_reply.started":"2023-07-11T12:10:33.203975Z","shell.execute_reply":"2023-07-11T12:10:33.219751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:33.222904Z","iopub.execute_input":"2023-07-11T12:10:33.223336Z","iopub.status.idle":"2023-07-11T12:10:33.232716Z","shell.execute_reply.started":"2023-07-11T12:10:33.223299Z","shell.execute_reply":"2023-07-11T12:10:33.231762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 The code iterates over the loop five times, each time selecting a random image from the training directory. </div>","metadata":{}},{"cell_type":"code","source":"# training directory\nfor _ in range(5):\n    file_name = train_dir + \"/\"+ random.choice(os.listdir(train_dir))\n    #read image\n    image = load_img(file_name, target_size=(224,224))\n    plt.imshow(image)\n    plt.title(\"{}: displayed\".format(file_name))\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:33.234108Z","iopub.execute_input":"2023-07-11T12:10:33.235058Z","iopub.status.idle":"2023-07-11T12:10:34.830534Z","shell.execute_reply.started":"2023-07-11T12:10:33.235018Z","shell.execute_reply":"2023-07-11T12:10:34.829576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 The code iterates over the loop five times, each time selecting a random image from the test directory. </div>","metadata":{}},{"cell_type":"code","source":"# test directory\nfor _ in range(5):\n    file_name = test_dir + \"/\"+ random.choice(os.listdir(test_dir))\n    #read image\n    image = load_img(file_name, target_size=(224,224))\n    plt.imshow(image)\n    plt.title(\"{}: displayed\".format(file_name))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:34.835654Z","iopub.execute_input":"2023-07-11T12:10:34.836632Z","iopub.status.idle":"2023-07-11T12:10:36.024436Z","shell.execute_reply.started":"2023-07-11T12:10:34.836586Z","shell.execute_reply":"2023-07-11T12:10:36.023451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 This code snippet is used to split the dataset into train, validation, and test sets. </div>\n\n","metadata":{}},{"cell_type":"code","source":"# splitting the data into train test\ntrain_val_df, test_df = train_test_split(df_train_image, test_size=0.02)\n# splitting the train val further into train, val\ntrain_df, val_df = train_test_split(train_val_df, test_size=0.2)\n\nprint(\"Total number of images in the train is {}\".format(train_df.shape[0]))\nprint(\"Total number of images in the internal test is {}\".format(test_df.shape[0]))\nprint(\"Total number of images in the val is {}\".format(val_df.shape[0]))\nprint(\"Total number of images in the submission is {}\".format(df_submission.shape[0]))","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:36.025947Z","iopub.execute_input":"2023-07-11T12:10:36.027038Z","iopub.status.idle":"2023-07-11T12:10:36.044282Z","shell.execute_reply.started":"2023-07-11T12:10:36.026997Z","shell.execute_reply":"2023-07-11T12:10:36.043194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 This code snippet uses the seaborn library to create a countplot graph that shows the distribution of data within each of the three sets - train, validation, and internal testing data - in the Kaggle Dogs-vs-Cats challenge. </div>\n","metadata":{}},{"cell_type":"code","source":"\n# checking the data distribution within each train,val and internal testing data.\nsns.countplot(x ='label', data = train_df)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:36.045939Z","iopub.execute_input":"2023-07-11T12:10:36.046725Z","iopub.status.idle":"2023-07-11T12:10:36.262595Z","shell.execute_reply.started":"2023-07-11T12:10:36.046678Z","shell.execute_reply":"2023-07-11T12:10:36.261657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x ='label', data = train_df)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:36.263921Z","iopub.execute_input":"2023-07-11T12:10:36.264295Z","iopub.status.idle":"2023-07-11T12:10:36.647550Z","shell.execute_reply.started":"2023-07-11T12:10:36.264256Z","shell.execute_reply":"2023-07-11T12:10:36.646450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x ='label', data = test_df)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:36.649109Z","iopub.execute_input":"2023-07-11T12:10:36.649480Z","iopub.status.idle":"2023-07-11T12:10:36.831538Z","shell.execute_reply.started":"2023-07-11T12:10:36.649443Z","shell.execute_reply":"2023-07-11T12:10:36.830478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n    📌 <b> Classification with MobileNetV2 </b></div>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 This code snippet is used to prepare the data for the Kaggle Dogs-vs-Cats challenge using image augmentation with the help of Image Data Generator from Keras. </div>\n\n","metadata":{}},{"cell_type":"code","source":"# Image augmentation data preparation with Image Data Generator\nimg_size = 224\nbatch_size = 1024\ntrain_aug_map = ImageDataGenerator(\n                rotation_range=10,\n                horizontal_flip = True,\n                fill_mode = 'nearest',\n                preprocessing_function = preprocess_input)\n\ntrain_data = train_aug_map.flow_from_dataframe(\n                            train_df, train_dir,\n                            x_col='filename',\n                            y_col = 'label',\n                            target_size = (img_size, img_size),\n                            batch_size= batch_size,\n                            class_mode='categorical')\n\n\nval_aug_mag = ImageDataGenerator(\n                preprocessing_function = preprocess_input)\n\nval_data = val_aug_mag.flow_from_dataframe(\n                            val_df, train_dir,\n                            x_col='filename',\n                            y_col = 'label',\n                            target_size = (img_size, img_size),\n                            batch_size= batch_size,\n                            class_mode='categorical')\n\ntest_aug_map = ImageDataGenerator(\n                preprocessing_function = preprocess_input)\n\ntest_data = val_aug_mag.flow_from_dataframe(\n                            test_df, train_dir,\n                            x_col='filename',\n                            y_col = None,\n                            class_mode = None,\n                            target_size = (img_size, img_size),\n                            shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:36.833011Z","iopub.execute_input":"2023-07-11T12:10:36.833708Z","iopub.status.idle":"2023-07-11T12:10:37.178718Z","shell.execute_reply.started":"2023-07-11T12:10:36.833668Z","shell.execute_reply":"2023-07-11T12:10:37.177659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 The code snippet is building Mobilenetv2 </div>","metadata":{}},{"cell_type":"code","source":"# building mobile net model\nmobilenet = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224,224,3))\nmobilenet.trainable = False\nmobilenet_model = Sequential([mobilenet,Flatten(),Dense(1024, activation='relu'),\n                             Dropout(0.4), Dense(2,activation='softmax')])\n\noptimizer = tf.keras.optimizers.Adam(1e-6)\n\nmobilenet_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:37.180409Z","iopub.execute_input":"2023-07-11T12:10:37.180811Z","iopub.status.idle":"2023-07-11T12:10:41.977054Z","shell.execute_reply.started":"2023-07-11T12:10:37.180771Z","shell.execute_reply":"2023-07-11T12:10:41.975911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 Defining early stopping criterion </div>","metadata":{}},{"cell_type":"code","source":"\nepoch = 50\nlearning_rate = 3e-5 \nlr_start = 0.00000001\nlr_min = 0.000001\nlr_max = 3e-5 \nlr_rampup_epochs = 1\nlr_sustain_epochs = 1\nlr_exp_decay = .8\n\ndef adjustable_lr(epoch):\n    if epoch < lr_rampup_epochs:\n        lr = (lr_max - lr_start) / lr_rampup_epochs * epoch + lr_start\n    elif epoch < lr_rampup_epochs + lr_sustain_epochs:\n        lr = lr_max\n    else:\n        lr = (lr_max - lr_min) * lr_exp_decay**(epoch - lr_rampup_epochs - lr_sustain_epochs) + lr_min\n    return lr\n    \n    \nearly_stop = EarlyStopping(patience=5)\nlr_callback = LearningRateScheduler(adjustable_lr, verbose=True)\ncallbacks = [early_stop, lr_callback]","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:10:41.978473Z","iopub.execute_input":"2023-07-11T12:10:41.979354Z","iopub.status.idle":"2023-07-11T12:10:41.988466Z","shell.execute_reply.started":"2023-07-11T12:10:41.979313Z","shell.execute_reply":"2023-07-11T12:10:41.987349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 Training the model </div>","metadata":{}},{"cell_type":"code","source":"mobilenet_model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\ntrain_images = train_df.shape[0]\nval_images = val_df.shape[0]\nhistory = mobilenet_model.fit_generator(train_data, epochs=20, validation_data=val_data, validation_steps=val_images//batch_size, steps_per_epoch=train_images//batch_size, callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T12:12:16.830309Z","iopub.execute_input":"2023-07-11T12:12:16.830742Z","iopub.status.idle":"2023-07-11T13:33:00.177431Z","shell.execute_reply.started":"2023-07-11T12:12:16.830702Z","shell.execute_reply":"2023-07-11T13:33:00.176280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 Plotting accuracy and loss plots for train and val splits </div>","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-11T13:33:00.180105Z","iopub.execute_input":"2023-07-11T13:33:00.180422Z","iopub.status.idle":"2023-07-11T13:33:45.659518Z","shell.execute_reply.started":"2023-07-11T13:33:00.180392Z","shell.execute_reply":"2023-07-11T13:33:45.658463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-11T13:33:45.661032Z","iopub.execute_input":"2023-07-11T13:33:45.662114Z","iopub.status.idle":"2023-07-11T13:33:45.887725Z","shell.execute_reply.started":"2023-07-11T13:33:45.662070Z","shell.execute_reply":"2023-07-11T13:33:45.886554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobilenet_v2_pred = mobilenet_model.predict_generator(test_data)\ntest_df['prediction'] = np.argmax(mobilenet_v2_pred, axis=-1)\nlabels = dict((v,k) for k,v in train_data.class_indices.items())\ntest_df['prediction'] = test_df['prediction'].map(labels)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T13:33:45.892555Z","iopub.execute_input":"2023-07-11T13:33:45.892905Z","iopub.status.idle":"2023-07-11T13:33:48.709440Z","shell.execute_reply.started":"2023-07-11T13:33:45.892872Z","shell.execute_reply":"2023-07-11T13:33:48.708151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobilenet_confusion_matrix = confusion_matrix(test_df['label'], test_df['prediction'])\nprint(mobilenet_confusion_matrix)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T13:33:48.710983Z","iopub.execute_input":"2023-07-11T13:33:48.711388Z","iopub.status.idle":"2023-07-11T13:33:48.725628Z","shell.execute_reply.started":"2023-07-11T13:33:48.711340Z","shell.execute_reply":"2023-07-11T13:33:48.724214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 Generating predictions on submission dataframe </div>","metadata":{}},{"cell_type":"code","source":"mobilenet_submission_augmap = ImageDataGenerator(preprocessing_function = preprocess_input)\nmobilenet_submission_data = mobilenet_submission_augmap.flow_from_dataframe(\n             df_submission, test_dir,\n             x_col = 'filename',\n             y_col = None,\n             class_mode = None,\n             target_size = (img_size, img_size),\n             shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T13:33:48.727733Z","iopub.execute_input":"2023-07-11T13:33:48.728216Z","iopub.status.idle":"2023-07-11T13:33:48.866162Z","shell.execute_reply.started":"2023-07-11T13:33:48.728175Z","shell.execute_reply":"2023-07-11T13:33:48.864932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"background-color:#F0E3D2; color:#19180F; font-size:15px; font-family:Verdana; padding:10px; border: 2px solid #19180F; border-radius:10px\"> \n📌 Writing the predictions </div>","metadata":{}},{"cell_type":"code","source":"mobilenet_submission_pred = mobilenet_model.predict_generator(mobilenet_submission_data)\ndf_submission['prediction'] = np.argmax(mobilenet_submission_pred, axis = -1)\nlabels = dict((v,k) for k,v in train_data.class_indices.items())\n#df_submission['prediction'] = df_submission['prediction'].map(labels)","metadata":{"execution":{"iopub.status.busy":"2023-07-11T13:33:48.868001Z","iopub.execute_input":"2023-07-11T13:33:48.868400Z","iopub.status.idle":"2023-07-11T13:34:32.180758Z","shell.execute_reply.started":"2023-07-11T13:33:48.868360Z","shell.execute_reply":"2023-07-11T13:34:32.179485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# finally printing\n\ndf_submission","metadata":{"execution":{"iopub.status.busy":"2023-07-11T13:34:32.182464Z","iopub.execute_input":"2023-07-11T13:34:32.182928Z","iopub.status.idle":"2023-07-11T13:34:32.200479Z","shell.execute_reply.started":"2023-07-11T13:34:32.182885Z","shell.execute_reply":"2023-07-11T13:34:32.199251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}