{"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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":6243,"databundleVersionId":868544,"sourceType":"competition"},{"sourceId":1173340,"sourceType":"datasetVersion","datasetId":665647}],"dockerImageVersionId":30170,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Importing Required Packages","metadata":{"papermill":{"duration":0.037505,"end_time":"2022-03-15T11:50:33.372117","exception":false,"start_time":"2022-03-15T11:50:33.334612","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport glob\nimport tensorflow as tf\nimport time\nfrom sklearn.model_selection import train_test_split\nfrom collections import Counter\nfrom sklearn.model_selection import train_test_split\nfrom collections import Counter\nimport cv2\nfrom concurrent import futures\nimport threading\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.utils import to_categorical\nimport datetime\nfrom prettytable import PrettyTable","metadata":{"papermill":{"duration":5.57698,"end_time":"2022-03-15T11:50:38.98425","exception":false,"start_time":"2022-03-15T11:50:33.40727","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:43:55.017568Z","iopub.execute_input":"2022-03-31T17:43:55.018043Z","iopub.status.idle":"2022-03-31T17:44:01.633041Z","shell.execute_reply.started":"2022-03-31T17:43:55.017942Z","shell.execute_reply":"2022-03-31T17:44:01.632253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading the Dataset","metadata":{"papermill":{"duration":0.035429,"end_time":"2022-03-15T11:50:39.055278","exception":false,"start_time":"2022-03-15T11:50:39.019849","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#getting the total number of images in the training set\nprint(os.listdir(\"../input/intel-mobileodt-cervical-cancer-screening\"))\n\n\n\nbase_dir = os.path.join('../input/intel-mobileodt-cervical-cancer-screening/train/train')\ntype1_dir = os.path.join(base_dir,'Type_1')\ntype2_dir = os.path.join(base_dir,'Type_2')\ntype3_dir = os.path.join(base_dir,'Type_3')\n\ntype1_files = glob.glob(type1_dir+'/*.jpg')\ntype2_files = glob.glob(type2_dir+'/*.jpg')\ntype3_files = glob.glob(type3_dir+'/*.jpg')\n\nlen(type1_files),len(type2_files),len(type3_files)\n","metadata":{"papermill":{"duration":0.416269,"end_time":"2022-03-15T11:50:39.50687","exception":false,"start_time":"2022-03-15T11:50:39.090601","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:44:04.514403Z","iopub.execute_input":"2022-03-31T17:44:04.514879Z","iopub.status.idle":"2022-03-31T17:44:04.888862Z","shell.execute_reply.started":"2022-03-31T17:44:04.514841Z","shell.execute_reply":"2022-03-31T17:44:04.887958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Building a dataframe mapping images and Cancer type\nnp.random.seed(42)\n\nfiles_df = pd.DataFrame({\n    'filename': type1_files + type2_files + type3_files,\n    'label': ['Type_1'] * len(type1_files) + ['Type_2'] * len(type2_files) + ['Type_3'] * len(type3_files)\n}).sample(frac=1, random_state=42).reset_index(drop=True)\n\nfiles_df.head()","metadata":{"papermill":{"duration":0.058034,"end_time":"2022-03-15T11:50:39.600419","exception":false,"start_time":"2022-03-15T11:50:39.542385","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:44:09.457941Z","iopub.execute_input":"2022-03-31T17:44:09.458571Z","iopub.status.idle":"2022-03-31T17:44:09.483015Z","shell.execute_reply.started":"2022-03-31T17:44:09.458531Z","shell.execute_reply":"2022-03-31T17:44:09.482259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split Dataset","metadata":{"papermill":{"duration":0.035513,"end_time":"2022-03-15T11:50:39.672929","exception":false,"start_time":"2022-03-15T11:50:39.637416","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#split training,dev and test set : 60:10:30\n\ntrain_files, test_files, train_labels, test_labels = train_test_split(files_df['filename'].values,\n                                                                      files_df['label'].values, \n                                                                      test_size=0.3, random_state=42)\ntrain_files, val_files, train_labels, val_labels = train_test_split(train_files,\n                                                                    train_labels, \n                                                                    test_size=0.1, random_state=42)\n\nprint(train_files.shape, val_files.shape, test_files.shape)\nprint('Train:', Counter(train_labels), '\\nVal:', Counter(val_labels), '\\nTest:', Counter(test_labels))","metadata":{"papermill":{"duration":0.051352,"end_time":"2022-03-15T11:50:39.760236","exception":false,"start_time":"2022-03-15T11:50:39.708884","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:44:14.025464Z","iopub.execute_input":"2022-03-31T17:44:14.025782Z","iopub.status.idle":"2022-03-31T17:44:14.043777Z","shell.execute_reply.started":"2022-03-31T17:44:14.025748Z","shell.execute_reply":"2022-03-31T17:44:14.043106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Summary of the dataset dimensions\n","metadata":{"papermill":{"duration":0.036328,"end_time":"2022-03-15T11:50:39.833125","exception":false,"start_time":"2022-03-15T11:50:39.796797","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def get_img_shape_parallel(idx, img, total_imgs):\n    if idx % 5000 == 0 or idx == (total_imgs - 1):\n        print('{}: working on img num: {}'.format(threading.current_thread().name,\n                                                  idx))\n    return cv2.imread(img).shape\n  \nex = futures.ThreadPoolExecutor(max_workers=None)\ndata_inp = [(idx, img, len(train_files)) for idx, img in enumerate(train_files)]\nprint('Starting Img shape computation:')\ntrain_img_dims_map = ex.map(get_img_shape_parallel, \n                            [record[0] for record in data_inp],\n                            [record[1] for record in data_inp],\n                            [record[2] for record in data_inp])\ntrain_img_dims = list(train_img_dims_map)\nprint('Min Dimensions:', np.min(train_img_dims, axis=0)) \nprint('Avg Dimensions:', np.mean(train_img_dims, axis=0))\nprint('Median Dimensions:', np.median(train_img_dims, axis=0))\nprint('Max Dimensions:', np.max(train_img_dims, axis=0))","metadata":{"papermill":{"duration":141.357546,"end_time":"2022-03-15T11:53:01.226635","exception":false,"start_time":"2022-03-15T11:50:39.869089","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:44:21.390278Z","iopub.execute_input":"2022-03-31T17:44:21.391019Z","iopub.status.idle":"2022-03-31T17:46:43.986172Z","shell.execute_reply.started":"2022-03-31T17:44:21.390962Z","shell.execute_reply":"2022-03-31T17:46:43.985460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_DIMS = (224, 224)\n\ndef get_img_data_parallel(idx, img, total_imgs):\n    if idx % 5000 == 0 or idx == (total_imgs - 1):\n        print('{}: working on img num: {}'.format(threading.current_thread().name,\n                                                  idx))\n    img = cv2.imread(img)\n    img = cv2.resize(img, dsize=IMG_DIMS, \n                     interpolation=cv2.INTER_CUBIC)\n    img = np.array(img, dtype=np.float32)\n    return img\n\nex = futures.ThreadPoolExecutor(max_workers=None)\ntrain_data_inp = [(idx, img, len(train_files)) for idx, img in enumerate(train_files)]\nval_data_inp = [(idx, img, len(val_files)) for idx, img in enumerate(val_files)]\ntest_data_inp = [(idx, img, len(test_files)) for idx, img in enumerate(test_files)]\n\nprint('Loading Train Images:')\ntrain_data_map = ex.map(get_img_data_parallel, \n                        [record[0] for record in train_data_inp],\n                        [record[1] for record in train_data_inp],\n                        [record[2] for record in train_data_inp])\ntrain_data = np.array(list(train_data_map))\n\nprint('\\nLoading Validation Images:')\nval_data_map = ex.map(get_img_data_parallel, \n                        [record[0] for record in val_data_inp],\n                        [record[1] for record in val_data_inp],\n                        [record[2] for record in val_data_inp])\nval_data = np.array(list(val_data_map))\n\nprint('\\nLoading Test Images:')\ntest_data_map = ex.map(get_img_data_parallel, \n                        [record[0] for record in test_data_inp],\n                        [record[1] for record in test_data_inp],\n                        [record[2] for record in test_data_inp])\ntest_data = np.array(list(test_data_map))\n\ntrain_data.shape, val_data.shape, test_data.shape  ","metadata":{"papermill":{"duration":223.672182,"end_time":"2022-03-15T11:56:44.936788","exception":false,"start_time":"2022-03-15T11:53:01.264606","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:47:42.407233Z","iopub.execute_input":"2022-03-31T17:47:42.407531Z","iopub.status.idle":"2022-03-31T17:51:28.915331Z","shell.execute_reply.started":"2022-03-31T17:47:42.407495Z","shell.execute_reply":"2022-03-31T17:51:28.914600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Viewing some sample images\n","metadata":{"papermill":{"duration":0.039082,"end_time":"2022-03-15T11:56:45.01562","exception":false,"start_time":"2022-03-15T11:56:44.976538","status":"completed"},"tags":[]}},{"cell_type":"code","source":"plt.figure(1 , figsize = (8 , 8))\nn = 0 \nfor i in range(16):\n    n += 1 \n    r = np.random.randint(0 , train_data.shape[0] , 1)\n    plt.subplot(4 , 4 , n)\n    plt.subplots_adjust(hspace = 0.5 , wspace = 0.5)\n    plt.imshow(train_data[r[0]]/255.)\n    plt.title('{}'.format(train_labels[r[0]]))\n    plt.xticks([]) , plt.yticks([])","metadata":{"papermill":{"duration":1.026501,"end_time":"2022-03-15T11:56:46.082054","exception":false,"start_time":"2022-03-15T11:56:45.055553","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:51:34.366170Z","iopub.execute_input":"2022-03-31T17:51:34.366902Z","iopub.status.idle":"2022-03-31T17:51:35.152968Z","shell.execute_reply.started":"2022-03-31T17:51:34.366863Z","shell.execute_reply":"2022-03-31T17:51:35.152308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#encode text categories with labels\nBATCH_SIZE = 64\nNUM_CLASSES = 2\nEPOCHS = 20\nINPUT_SHAPE = (224, 224, 3)\n\ntrain_imgs_scaled = train_data / 255.\nval_imgs_scaled = val_data / 255.\n\nle = LabelEncoder()\nle.fit(train_labels)\ntrain_labels_enc = le.transform(train_labels)\nval_labels_enc = le.transform(val_labels)\n\ntrain_labels_1hotenc = to_categorical(train_labels_enc, num_classes=3)\nval_labels_1hotenc = to_categorical(val_labels_enc, num_classes=3)\n\nprint(train_labels[:6], train_labels_enc[:6])\nprint(train_labels[:6], train_labels_1hotenc[:6])\n","metadata":{"papermill":{"duration":0.240047,"end_time":"2022-03-15T11:56:46.367661","exception":false,"start_time":"2022-03-15T11:56:46.127614","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:51:40.846632Z","iopub.execute_input":"2022-03-31T17:51:40.847367Z","iopub.status.idle":"2022-03-31T17:51:41.042928Z","shell.execute_reply.started":"2022-03-31T17:51:40.847326Z","shell.execute_reply":"2022-03-31T17:51:41.042156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#scaling the test set and one-hot encoding the test labels\ntest_imgs_scaled = test_data / 255.\ntest_imgs_scaled.shape, test_labels.shape\n\nle = LabelEncoder()\nle.fit(test_labels)\ntest_labels_enc = le.transform(test_labels)\n\ntest_labels_1hotenc = to_categorical(test_labels_enc, num_classes=3)\n\n\nprint(test_labels[:6], test_labels_enc[:6])\nprint(test_labels[:6], test_labels_1hotenc[:6])","metadata":{"papermill":{"duration":0.136063,"end_time":"2022-03-15T11:56:46.550277","exception":false,"start_time":"2022-03-15T11:56:46.414214","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:51:51.400117Z","iopub.execute_input":"2022-03-31T17:51:51.400379Z","iopub.status.idle":"2022-03-31T17:51:51.489292Z","shell.execute_reply.started":"2022-03-31T17:51:51.400348Z","shell.execute_reply":"2022-03-31T17:51:51.488445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VGG16 Model","metadata":{"papermill":{"duration":0.045968,"end_time":"2022-03-15T11:56:46.644521","exception":false,"start_time":"2022-03-15T11:56:46.598553","status":"completed"},"tags":[]}},{"cell_type":"code","source":"vgg16Net = tf.keras.applications.vgg16.VGG16(include_top=False, weights='imagenet', \n                                        input_shape=INPUT_SHAPE)\nvgg16Net.trainable = False\n# Freeze the layers\nfor layer in vgg16Net.layers:\n    layer.trainable = False\n    \nbase_vgg16 = vgg16Net\nbase_out_vgg16 = base_vgg16.output\npool_out_vgg16 = tf.keras.layers.Flatten()(base_out_vgg16)\nhidden1_vgg16 = tf.keras.layers.Dense(512, activation='relu')(pool_out_vgg16)\ndrop1_vgg16 = tf.keras.layers.Dropout(rate=0.3)(hidden1_vgg16)\nhidden2_vgg16 = tf.keras.layers.Dense(512, activation='relu')(drop1_vgg16)\ndrop2_vgg16 = tf.keras.layers.Dropout(rate=0.3)(hidden2_vgg16)\nout_vgg16 = tf.keras.layers.Dense(3, activation='softmax')(drop2_vgg16)\n\nvgg16_model = tf.keras.Model(inputs=base_vgg16.input, outputs=out_vgg16)\nvgg16_model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=1e-4),\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])\nvgg16_model.summary()\n","metadata":{"papermill":{"duration":3.304752,"end_time":"2022-03-15T11:56:49.995025","exception":false,"start_time":"2022-03-15T11:56:46.690273","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:51:56.144486Z","iopub.execute_input":"2022-03-31T17:51:56.144756Z","iopub.status.idle":"2022-03-31T17:51:59.121421Z","shell.execute_reply.started":"2022-03-31T17:51:56.144726Z","shell.execute_reply":"2022-03-31T17:51:59.120724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg16_start = time.time()\nvgg16_history = vgg16_model.fit(x=train_imgs_scaled, y=train_labels_1hotenc, \n                    batch_size=BATCH_SIZE,\n                    epochs=EPOCHS, \n                    validation_data=(val_imgs_scaled, val_labels_1hotenc),\n                    verbose=1)\nvgg16_stop = time.time()\n","metadata":{"papermill":{"duration":59.903155,"end_time":"2022-03-15T11:57:49.948705","exception":false,"start_time":"2022-03-15T11:56:50.04555","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:52:04.802368Z","iopub.execute_input":"2022-03-31T17:52:04.803076Z","iopub.status.idle":"2022-03-31T17:53:28.676247Z","shell.execute_reply.started":"2022-03-31T17:52:04.803037Z","shell.execute_reply":"2022-03-31T17:53:28.675349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## VGG16 Summary","metadata":{"papermill":{"duration":0.147868,"end_time":"2022-03-15T11:57:50.244726","exception":false,"start_time":"2022-03-15T11:57:50.096858","status":"completed"},"tags":[]}},{"cell_type":"code","source":"vgg16_trainTime=vgg16_stop-vgg16_start\nvgg16_model_accuracy = vgg16_history.history['accuracy'][np.argmin(vgg16_history.history['loss'])]\nvgg16_model_score=vgg16_model.evaluate(test_imgs_scaled,test_labels_1hotenc)\nvgg16_Summary = PrettyTable([\"VGG16\",\" \"])\nvgg16_Summary.add_row([\"Model Accuracy in %\", \"{:.2f}\".format(vgg16_model_accuracy*100)])\nvgg16_Summary.add_row([\"Test Accuracy in %\", \"{:.2f}\".format(vgg16_model_score[1]*100)])\nvgg16_Summary.add_row([\"Test Loss in %\", \"{:.2f}\".format(vgg16_model_score[0]*100)])\nvgg16_Summary.add_row([\"Time Taken To Train in Seconds\", \"{:.2f}\".format(vgg16_trainTime)])\nprint(vgg16_Summary)\n\nf, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))\nt = f.suptitle('VGG16 Perfomance', fontsize=12)\nf.subplots_adjust(top=0.85, wspace=0.3)\n\nmax_epoch = len(vgg16_history.history['accuracy'])+1\nepoch_list = list(range(1,max_epoch))\nax1.plot(epoch_list, vgg16_history.history['accuracy'], label='Train Accuracy')\nax1.plot(epoch_list, vgg16_history.history['val_accuracy'], label='Validation Accuracy')\nax1.set_xticks(np.arange(1, max_epoch, 5))\nax1.set_ylabel('Accuracy Value')\nax1.set_xlabel('Epoch')\nax1.set_title('Accuracy')\nl1 = ax1.legend(loc=\"best\")\n\nax2.plot(epoch_list, vgg16_history.history['loss'], label='Train Loss')\nax2.plot(epoch_list, vgg16_history.history['val_loss'], label='Validation Loss')\nax2.set_xticks(np.arange(1, max_epoch, 5))\nax2.set_ylabel('Loss Value')\nax2.set_xlabel('Epoch')\nax2.set_title('Loss')\nl2 = ax2.legend(loc=\"best\")","metadata":{"papermill":{"duration":7.474749,"end_time":"2022-03-15T11:57:57.866271","exception":false,"start_time":"2022-03-15T11:57:50.391522","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:53:33.775890Z","iopub.execute_input":"2022-03-31T17:53:33.776157Z","iopub.status.idle":"2022-03-31T17:53:41.135650Z","shell.execute_reply.started":"2022-03-31T17:53:33.776128Z","shell.execute_reply":"2022-03-31T17:53:41.134978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **VGG19**","metadata":{"papermill":{"duration":0.153741,"end_time":"2022-03-15T11:57:58.174583","exception":false,"start_time":"2022-03-15T11:57:58.020842","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#using VGG19 pre-trained model\nvgg19Net = tf.keras.applications.vgg19.VGG19(include_top=False, weights='imagenet', \n                                        input_shape=INPUT_SHAPE)\nvgg19Net.trainable = False\n# Freeze the layers\nfor layer in vgg16Net.layers:\n    layer.trainable = False\n    \nbase_vgg19 = vgg16Net\nbase_out_vgg19= base_vgg19.output\npool_out_vgg19 = tf.keras.layers.Flatten()(base_out_vgg19)\nhidden1_vgg19 = tf.keras.layers.Dense(512, activation='relu')(pool_out_vgg19)\ndrop1_vgg19 = tf.keras.layers.Dropout(rate=0.3)(hidden1_vgg19)\nhidden2_vgg19 = tf.keras.layers.Dense(512, activation='relu')(drop1_vgg19)\ndrop2_vgg19 = tf.keras.layers.Dropout(rate=0.3)(hidden2_vgg19)\nout_vgg19 = tf.keras.layers.Dense(3, activation='softmax')(drop2_vgg19)\n\nvgg19_model = tf.keras.Model(inputs=base_vgg19.input, outputs=out_vgg19)\nvgg19_model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=1e-4),\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])\nvgg19_model.summary()\n","metadata":{"papermill":{"duration":1.340985,"end_time":"2022-03-15T11:57:59.668275","exception":false,"start_time":"2022-03-15T11:57:58.32729","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:53:58.512886Z","iopub.execute_input":"2022-03-31T17:53:58.513166Z","iopub.status.idle":"2022-03-31T17:53:59.303063Z","shell.execute_reply.started":"2022-03-31T17:53:58.513135Z","shell.execute_reply":"2022-03-31T17:53:59.302329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg19_start = time.time()\nvgg19_history = vgg19_model.fit(x=train_imgs_scaled, y=train_labels_1hotenc, \n                    batch_size=BATCH_SIZE,\n                    epochs=EPOCHS, \n                    validation_data=(val_imgs_scaled, val_labels_1hotenc),\n                    verbose=1)\nvgg19_stop = time.time()","metadata":{"papermill":{"duration":43.472684,"end_time":"2022-03-15T11:58:43.302101","exception":false,"start_time":"2022-03-15T11:57:59.829417","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:54:54.177297Z","iopub.execute_input":"2022-03-31T17:54:54.177575Z","iopub.status.idle":"2022-03-31T17:56:17.099938Z","shell.execute_reply.started":"2022-03-31T17:54:54.177537Z","shell.execute_reply":"2022-03-31T17:56:17.099023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## VGG19 Summary","metadata":{"papermill":{"duration":0.257609,"end_time":"2022-03-15T11:58:43.817419","exception":false,"start_time":"2022-03-15T11:58:43.55981","status":"completed"},"tags":[]}},{"cell_type":"code","source":"vgg19_trainTime=vgg19_stop-vgg19_start\nvgg19_model_accuracy = vgg19_history.history['accuracy'][np.argmin(vgg19_history.history['loss'])]\nvgg19_model_score=vgg19_model.evaluate(test_imgs_scaled,test_labels_1hotenc)\nvgg19_Summary = PrettyTable([\"Vgg19\",\" \"])\nvgg19_Summary.add_row([\"Model Accuracy in %\", \"{:.2f}\".format(vgg19_model_accuracy*100)])\nvgg19_Summary.add_row([\"Test Accuracy in %\", \"{:.2f}\".format(vgg19_model_score[1]*100)])\nvgg19_Summary.add_row([\"Test Loss in %\", \"{:.2f}\".format(vgg19_model_score[0]*100)])\nvgg19_Summary.add_row([\"Time Taken To Train in Seconds\", \"{:.2f}\".format(vgg19_trainTime)])\nprint(vgg19_Summary)\n\n\nf, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))\nt = f.suptitle('VGG19 Perfomance', fontsize=12)\nf.subplots_adjust(top=0.85, wspace=0.3)\n\nmax_epoch = len(vgg19_history.history['accuracy'])+1\nepoch_list = list(range(1,max_epoch))\nax1.plot(epoch_list, vgg19_history.history['accuracy'], label='Train Accuracy')\nax1.plot(epoch_list, vgg19_history.history['val_accuracy'], label='Validation Accuracy')\nax1.set_xticks(np.arange(1, max_epoch, 5))\nax1.set_ylabel('Accuracy Value')\nax1.set_xlabel('Epoch')\nax1.set_title('Accuracy')\nl1 = ax1.legend(loc=\"best\")\n\nax2.plot(epoch_list, vgg19_history.history['loss'], label='Train Loss')\nax2.plot(epoch_list, vgg19_history.history['val_loss'], label='Validation Loss')\nax2.set_xticks(np.arange(1, max_epoch, 5))\nax2.set_ylabel('Loss Value')\nax2.set_xlabel('Epoch')\nax2.set_title('Loss')\nl2 = ax2.legend(loc=\"best\")","metadata":{"papermill":{"duration":2.008864,"end_time":"2022-03-15T11:58:46.092156","exception":false,"start_time":"2022-03-15T11:58:44.083292","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:56:28.206082Z","iopub.execute_input":"2022-03-31T17:56:28.206520Z","iopub.status.idle":"2022-03-31T17:56:29.984404Z","shell.execute_reply.started":"2022-03-31T17:56:28.206463Z","shell.execute_reply":"2022-03-31T17:56:29.983720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Xception","metadata":{"papermill":{"duration":0.263947,"end_time":"2022-03-15T11:58:46.618293","exception":false,"start_time":"2022-03-15T11:58:46.354346","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#using Xception trained model\nXception = tf.keras.applications.Xception(include_top=False, weights='imagenet', \n                                        input_shape=INPUT_SHAPE)\nXception.trainable = False\n# Freeze the layers\nfor layer in Xception.layers:\n    layer.trainable = False\n    \nbase_Xception = Xception\nbase_out_Xception = base_Xception.output\npool_out_Xception = tf.keras.layers.Flatten()(base_out_Xception)\nhidden1_Xception = tf.keras.layers.Dense(512, activation='relu')(pool_out_Xception)\ndrop1_Xception = tf.keras.layers.Dropout(rate=0.3)(hidden1_Xception)\nhidden2_Xception = tf.keras.layers.Dense(512, activation='relu')(drop1_Xception)\ndrop2_Xception = tf.keras.layers.Dropout(rate=0.3)(hidden2_Xception)\nout_Xception = tf.keras.layers.Dense(3, activation='softmax')(drop2_Xception)\n\nXception_model = tf.keras.Model(inputs=base_Xception.input, outputs=out_Xception)\nXception_model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=1e-4),\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])\nXception.summary()","metadata":{"papermill":{"duration":1.842791,"end_time":"2022-03-15T11:58:48.724419","exception":false,"start_time":"2022-03-15T11:58:46.881628","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:56:47.326194Z","iopub.execute_input":"2022-03-31T17:56:47.326451Z","iopub.status.idle":"2022-03-31T17:56:48.946862Z","shell.execute_reply.started":"2022-03-31T17:56:47.326422Z","shell.execute_reply":"2022-03-31T17:56:48.946140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xception_start = time.time()\nXception_history = Xception_model.fit(x=train_imgs_scaled, y=train_labels_1hotenc, \n                    batch_size=BATCH_SIZE,\n                    epochs=EPOCHS, \n                    validation_data=(val_imgs_scaled, val_labels_1hotenc),\n                    verbose=1)\nXception_stop = time.time()\n","metadata":{"papermill":{"duration":58.806934,"end_time":"2022-03-15T11:59:47.844128","exception":false,"start_time":"2022-03-15T11:58:49.037194","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:57:06.134840Z","iopub.execute_input":"2022-03-31T17:57:06.135099Z","iopub.status.idle":"2022-03-31T17:58:05.113113Z","shell.execute_reply.started":"2022-03-31T17:57:06.135070Z","shell.execute_reply":"2022-03-31T17:58:05.112398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Xception Summary","metadata":{"papermill":{"duration":0.36104,"end_time":"2022-03-15T11:59:48.57103","exception":false,"start_time":"2022-03-15T11:59:48.20999","status":"completed"},"tags":[]}},{"cell_type":"code","source":"Xception_trainTime=Xception_stop-Xception_start\nXception_model_accuracy = Xception_history.history['accuracy'][np.argmin(Xception_history.history['loss'])]\nXception_model_score=Xception_model.evaluate(test_imgs_scaled,test_labels_1hotenc)\nXception_Summary = PrettyTable([\"Xception\",\" \"])\nXception_Summary.add_row([\"Model Accuracy in %\", \"{:.2f}\".format(Xception_model_accuracy*100)])\nXception_Summary.add_row([\"Test Accuracy in %\", \"{:.2f}\".format(Xception_model_score[1]*100)])\nXception_Summary.add_row([\"Test Loss in %\", \"{:.2f}\".format(Xception_model_score[0]*100)])\nXception_Summary.add_row([\"Time Taken To Train in Seconds\", \"{:.2f}\".format(Xception_trainTime)])\nprint(Xception_Summary)\n\nf, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))\nt = f.suptitle('Xception  Perfomance', fontsize=12)\nf.subplots_adjust(top=0.85, wspace=0.3)\n\nmax_epoch = len(Xception_history.history['accuracy'])+1\nepoch_list = list(range(1,max_epoch))\nax1.plot(epoch_list, Xception_history.history['accuracy'], label='Train Accuracy')\nax1.plot(epoch_list, Xception_history.history['val_accuracy'], label='Validation Accuracy')\nax1.set_xticks(np.arange(1, max_epoch, 5))\nax1.set_ylabel('Accuracy Value')\nax1.set_xlabel('Epoch')\nax1.set_title('Accuracy')\nl1 = ax1.legend(loc=\"best\")\n\nax2.plot(epoch_list, Xception_history.history['loss'], label='Train Loss')\nax2.plot(epoch_list, Xception_history.history['val_loss'], label='Validation Loss')\nax2.set_xticks(np.arange(1, max_epoch, 5))\nax2.set_ylabel('Loss Value')\nax2.set_xlabel('Epoch')\nax2.set_title('Loss')\nl2 = ax2.legend(loc=\"best\")","metadata":{"papermill":{"duration":2.831622,"end_time":"2022-03-15T11:59:51.767438","exception":false,"start_time":"2022-03-15T11:59:48.935816","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:58:13.141158Z","iopub.execute_input":"2022-03-31T17:58:13.141415Z","iopub.status.idle":"2022-03-31T17:58:16.510321Z","shell.execute_reply.started":"2022-03-31T17:58:13.141385Z","shell.execute_reply":"2022-03-31T17:58:16.509635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNet ","metadata":{"papermill":{"duration":0.375382,"end_time":"2022-03-15T11:59:52.520271","exception":false,"start_time":"2022-03-15T11:59:52.144889","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#using ResNet trained model\nResNet = tf.keras.applications.ResNet50(include_top=False, weights='imagenet', \n                                        input_shape=INPUT_SHAPE)\nResNet.trainable = False\n# Freeze the layers\nfor layer in ResNet.layers:\n    layer.trainable = False\n    \nbase_ResNet = ResNet\nbase_out_ResNet = base_ResNet.output\npool_out_ResNet = tf.keras.layers.Flatten()(base_out_ResNet)\nhidden1_ResNet = tf.keras.layers.Dense(512, activation='relu')(pool_out_ResNet)\ndrop1_ResNet = tf.keras.layers.Dropout(rate=0.3)(hidden1_ResNet)\nhidden2_ResNet = tf.keras.layers.Dense(512, activation='relu')(drop1_ResNet)\ndrop2_ResNet = tf.keras.layers.Dropout(rate=0.3)(hidden2_ResNet)\nout_ResNet = tf.keras.layers.Dense(3, activation='softmax')(drop2_ResNet)\n\nResNet_model = tf.keras.Model(inputs=base_ResNet.input, outputs=out_ResNet)\nResNet_model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=1e-4),\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])\nResNet.summary()","metadata":{"papermill":{"duration":2.649153,"end_time":"2022-03-15T11:59:55.558121","exception":false,"start_time":"2022-03-15T11:59:52.908968","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:58:39.127393Z","iopub.execute_input":"2022-03-31T17:58:39.127673Z","iopub.status.idle":"2022-03-31T17:58:41.300625Z","shell.execute_reply.started":"2022-03-31T17:58:39.127642Z","shell.execute_reply":"2022-03-31T17:58:41.299914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ResNet_start = time.time()\nResNet_history = ResNet_model.fit(x=train_imgs_scaled, y=train_labels_1hotenc, \n                    batch_size=BATCH_SIZE,\n                    epochs=EPOCHS, \n                    validation_data=(val_imgs_scaled, val_labels_1hotenc),\n                    verbose=1)\nResNet_stop = time.time()","metadata":{"papermill":{"duration":44.909437,"end_time":"2022-03-15T12:00:41.080389","exception":false,"start_time":"2022-03-15T11:59:56.170952","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T17:59:39.038790Z","iopub.execute_input":"2022-03-31T17:59:39.039005Z","iopub.status.idle":"2022-03-31T18:00:20.981986Z","shell.execute_reply.started":"2022-03-31T17:59:39.038978Z","shell.execute_reply":"2022-03-31T18:00:20.981168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ResNet Summary","metadata":{"papermill":{"duration":0.527083,"end_time":"2022-03-15T12:00:42.081546","exception":false,"start_time":"2022-03-15T12:00:41.554463","status":"completed"},"tags":[]}},{"cell_type":"code","source":"ResNet_trainTime=ResNet_stop-ResNet_start\nResNet_model_accuracy = ResNet_history.history['accuracy'][np.argmin(ResNet_history.history['loss'])]\nResNet_model_score=ResNet_model.evaluate(test_imgs_scaled,test_labels_1hotenc)\nResNet_Summary = PrettyTable([\"ResNet\",\" \"])\nResNet_Summary.add_row([\"Model Accuracy in %\", \"{:.2f}\".format(ResNet_model_accuracy*100)])\nResNet_Summary.add_row([\"Test Accuracy in %\", \"{:.2f}\".format(ResNet_model_score[1]*100)])\nResNet_Summary.add_row([\"Test Loss in %\", \"{:.2f}\".format(ResNet_model_score[0]*100)])\nResNet_Summary.add_row([\"Time Taken To Train in Seconds\", \"{:.2f}\".format(ResNet_trainTime)])\nprint(ResNet_Summary)\n\nf, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))\nt = f.suptitle('ResNet  Transfer Learning Perfomance', fontsize=12)\nf.subplots_adjust(top=0.85, wspace=0.3)\n\nmax_epoch = len(ResNet_history.history['accuracy'])+1\nepoch_list = list(range(1,max_epoch))\nax1.plot(epoch_list, ResNet_history.history['accuracy'], label='Train Accuracy')\nax1.plot(epoch_list, ResNet_history.history['val_accuracy'], label='Validation Accuracy')\nax1.set_xticks(np.arange(1, max_epoch, 5))\nax1.set_ylabel('Accuracy Value')\nax1.set_xlabel('Epoch')\nax1.set_title('Accuracy')\nl1 = ax1.legend(loc=\"best\")\n\nax2.plot(epoch_list, ResNet_history.history['loss'], label='Train Loss')\nax2.plot(epoch_list, ResNet_history.history['val_loss'], label='Validation Loss')\nax2.set_xticks(np.arange(1, max_epoch, 5))\nax2.set_ylabel('Loss Value')\nax2.set_xlabel('Epoch')\nax2.set_title('Loss')\nl2 = ax2.legend(loc=\"best\")","metadata":{"papermill":{"duration":2.811899,"end_time":"2022-03-15T12:00:45.368136","exception":false,"start_time":"2022-03-15T12:00:42.556237","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T18:01:25.266255Z","iopub.execute_input":"2022-03-31T18:01:25.266538Z","iopub.status.idle":"2022-03-31T18:01:27.399089Z","shell.execute_reply.started":"2022-03-31T18:01:25.266505Z","shell.execute_reply":"2022-03-31T18:01:27.398308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# InceptionV3","metadata":{"papermill":{"duration":0.47956,"end_time":"2022-03-15T12:00:46.325337","exception":false,"start_time":"2022-03-15T12:00:45.845777","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#using InceptionV3 trained model\nInceptionV3 = tf.keras.applications.InceptionV3(include_top=False, weights='imagenet', \n                                        input_shape=INPUT_SHAPE)\nInceptionV3.trainable = False\n# Freeze the layers\nfor layer in InceptionV3.layers:\n    layer.trainable = False\n    \nbase_InceptionV3 = InceptionV3\nbase_out_InceptionV3 = base_InceptionV3.output\npool_out_InceptionV3 = tf.keras.layers.Flatten()(base_out_InceptionV3)\nhidden1_InceptionV3 = tf.keras.layers.Dense(512, activation='relu')(pool_out_InceptionV3)\ndrop1_InceptionV3 = tf.keras.layers.Dropout(rate=0.3)(hidden1_InceptionV3)\nhidden2_InceptionV3 = tf.keras.layers.Dense(512, activation='relu')(drop1_InceptionV3)\ndrop2_InceptionV3 = tf.keras.layers.Dropout(rate=0.3)(hidden2_InceptionV3)\nout_InceptionV3 = tf.keras.layers.Dense(3, activation='softmax')(drop2_InceptionV3)\n\nInceptionV3_model = tf.keras.Model(inputs=base_InceptionV3.input, outputs=out_InceptionV3)\nInceptionV3_model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=1e-4),\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])\nInceptionV3.summary()","metadata":{"papermill":{"duration":2.893698,"end_time":"2022-03-15T12:00:49.697878","exception":false,"start_time":"2022-03-15T12:00:46.80418","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T18:01:42.795907Z","iopub.execute_input":"2022-03-31T18:01:42.796498Z","iopub.status.idle":"2022-03-31T18:01:45.490513Z","shell.execute_reply.started":"2022-03-31T18:01:42.796443Z","shell.execute_reply":"2022-03-31T18:01:45.489816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"InceptionV3_start = time.time()\nInceptionV3_history = InceptionV3_model.fit(x=train_imgs_scaled, y=train_labels_1hotenc, \n                    batch_size=BATCH_SIZE,\n                    epochs=EPOCHS, \n                    validation_data=(val_imgs_scaled, val_labels_1hotenc),\n                    verbose=1)\nInceptionV3_stop = time.time()","metadata":{"papermill":{"duration":46.43628,"end_time":"2022-03-15T12:01:36.622476","exception":false,"start_time":"2022-03-15T12:00:50.186196","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T18:02:07.520864Z","iopub.execute_input":"2022-03-31T18:02:07.521128Z","iopub.status.idle":"2022-03-31T18:02:53.006483Z","shell.execute_reply.started":"2022-03-31T18:02:07.521099Z","shell.execute_reply":"2022-03-31T18:02:53.005424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## InceptionV3 Summary","metadata":{"papermill":{"duration":0.586877,"end_time":"2022-03-15T12:01:37.79771","exception":false,"start_time":"2022-03-15T12:01:37.210833","status":"completed"},"tags":[]}},{"cell_type":"code","source":"InceptionV3_trainTime=InceptionV3_stop-InceptionV3_start\nInceptionV3_model_accuracy = InceptionV3_history.history['accuracy'][np.argmin(InceptionV3_history.history['loss'])]\nInceptionV3_model_score=InceptionV3_model.evaluate(test_imgs_scaled,test_labels_1hotenc)\nInceptionV3_Summary = PrettyTable([\"InceptionV3\",\" \"])\nInceptionV3_Summary.add_row([\"Model Accuracy in %\", \"{:.2f}\".format(InceptionV3_model_accuracy*100)])\nInceptionV3_Summary.add_row([\"Test Accuracy in %\", \"{:.2f}\".format(InceptionV3_model_score[1]*100)])\nInceptionV3_Summary.add_row([\"Test Loss in %\", \"{:.2f}\".format(InceptionV3_model_score[0]*100)])\nInceptionV3_Summary.add_row([\"Time Taken To Train in Seconds\", \"{:.2f}\".format(InceptionV3_trainTime)])\nprint(InceptionV3_Summary)\n\nf, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))\nt = f.suptitle('InceptionV3  Perfomance', fontsize=12)\nf.subplots_adjust(top=0.85, wspace=0.3)\n\nmax_epoch = len(InceptionV3_history.history['accuracy'])+1\nepoch_list = list(range(1,max_epoch))\n\nax1.plot(epoch_list, InceptionV3_history.history['accuracy'], label='Train Accuracy')\nax1.plot(epoch_list, InceptionV3_history.history['val_accuracy'], label='Validation Accuracy')\nax1.set_xticks(np.arange(1, max_epoch, 5))\nax1.set_ylabel('Accuracy Value')\nax1.set_xlabel('Epoch')\nax1.set_title('Accuracy')\nl1 = ax1.legend(loc=\"best\")\n\nax2.plot(epoch_list, InceptionV3_history.history['loss'], label='Train Loss')\nax2.plot(epoch_list, InceptionV3_history.history['val_loss'], label='Validation Loss')\nax2.set_xticks(np.arange(1, max_epoch, 5))\nax2.set_ylabel('Loss Value')\nax2.set_xlabel('Epoch')\nax2.set_title('Loss')\nl2 = ax2.legend(loc=\"best\")","metadata":{"papermill":{"duration":3.955136,"end_time":"2022-03-15T12:01:42.343376","exception":false,"start_time":"2022-03-15T12:01:38.38824","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T18:03:01.618878Z","iopub.execute_input":"2022-03-31T18:03:01.619604Z","iopub.status.idle":"2022-03-31T18:03:03.979987Z","shell.execute_reply.started":"2022-03-31T18:03:01.619563Z","shell.execute_reply":"2022-03-31T18:03:03.979286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MobileNet","metadata":{"papermill":{"duration":0.594247,"end_time":"2022-03-15T12:01:43.536907","exception":false,"start_time":"2022-03-15T12:01:42.94266","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#using InceptionV3 trained model\nMobileNet = tf.keras.applications.MobileNet(include_top=False, weights='imagenet', \n                                        input_shape=INPUT_SHAPE)\nMobileNet.trainable = False\n# Freeze the layers\nfor layer in MobileNet.layers:\n    layer.trainable = False\n    \nbase_MobileNet = MobileNet\nbase_out_MobileNet = base_MobileNet.output\npool_out_MobileNet = tf.keras.layers.Flatten()(base_out_MobileNet)\nhidden1_MobileNet = tf.keras.layers.Dense(512, activation='relu')(pool_out_MobileNet)\ndrop1_MobileNet = tf.keras.layers.Dropout(rate=0.3)(hidden1_MobileNet)\nhidden2_MobileNet = tf.keras.layers.Dense(512, activation='relu')(drop1_MobileNet)\ndrop2_MobileNet = tf.keras.layers.Dropout(rate=0.3)(hidden2_MobileNet)\nout_MobileNet = tf.keras.layers.Dense(3, activation='softmax')(drop2_MobileNet)\n\nMobileNet_model = tf.keras.Model(inputs=base_MobileNet.input, outputs=out_MobileNet)\nMobileNet_model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=1e-4),\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])\nMobileNet.summary()","metadata":{"papermill":{"duration":1.562668,"end_time":"2022-03-15T12:01:45.694942","exception":false,"start_time":"2022-03-15T12:01:44.132274","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T18:03:12.488998Z","iopub.execute_input":"2022-03-31T18:03:12.489300Z","iopub.status.idle":"2022-03-31T18:03:13.769456Z","shell.execute_reply.started":"2022-03-31T18:03:12.489267Z","shell.execute_reply":"2022-03-31T18:03:13.768759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MobileNet_start = time.time()\nMobileNet_history = MobileNet_model.fit(x=train_imgs_scaled, y=train_labels_1hotenc, \n                    batch_size=BATCH_SIZE,\n                    epochs=EPOCHS, \n                    validation_data=(val_imgs_scaled, val_labels_1hotenc),\n                    verbose=1)\nMobileNet_stop = time.time()\n","metadata":{"papermill":{"duration":44.282194,"end_time":"2022-03-15T12:02:30.897252","exception":false,"start_time":"2022-03-15T12:01:46.615058","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T18:03:35.884143Z","iopub.execute_input":"2022-03-31T18:03:35.884413Z","iopub.status.idle":"2022-03-31T18:03:59.311757Z","shell.execute_reply.started":"2022-03-31T18:03:35.884381Z","shell.execute_reply":"2022-03-31T18:03:59.311004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## MobileNet Summary","metadata":{"papermill":{"duration":0.700356,"end_time":"2022-03-15T12:02:32.304275","exception":false,"start_time":"2022-03-15T12:02:31.603919","status":"completed"},"tags":[]}},{"cell_type":"code","source":"MobileNet_trainTime=MobileNet_stop-MobileNet_start\nMobileNet_model_accuracy = MobileNet_history.history['accuracy'][np.argmin(MobileNet_history.history['loss'])]\nMobileNet_model_score=MobileNet_model.evaluate(test_imgs_scaled,test_labels_1hotenc)\nMobileNet_Summary = PrettyTable([\"MobileNet\",\" \"])\nMobileNet_Summary.add_row([\"Model Accuracy in %\", \"{:.2f}\".format(MobileNet_model_accuracy*100)])\nMobileNet_Summary.add_row([\"Test Accuracy in %\", \"{:.2f}\".format(MobileNet_model_score[1]*100)])\nMobileNet_Summary.add_row([\"Test Loss in %\", \"{:.2f}\".format(MobileNet_model_score[0]*100)])\nMobileNet_Summary.add_row([\"Time Taken To Train in Seconds\", \"{:.2f}\".format(MobileNet_trainTime)])\nprint(MobileNet_Summary)\n\n####graph\n\nf, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))\nt = f.suptitle('MobileNet Perfomance', fontsize=12)\nf.subplots_adjust(top=0.85, wspace=0.3)\n\nmax_epoch = len(MobileNet_history.history['accuracy'])+1\nepoch_list = list(range(1,max_epoch))\n\nax1.plot(epoch_list, MobileNet_history.history['accuracy'], label='Train Accuracy')\nax1.plot(epoch_list, MobileNet_history.history['val_accuracy'], label='Validation Accuracy')\nax1.set_xticks(np.arange(1, max_epoch, 5))\nax1.set_ylabel('Accuracy Value')\nax1.set_xlabel('Epoch')\nax1.set_title('Accuracy')\nl1 = ax1.legend(loc=\"best\")\n\nax2.plot(epoch_list, MobileNet_history.history['loss'], label='Train Loss')\nax2.plot(epoch_list, MobileNet_history.history['val_loss'], label='Validation Loss')\nax2.set_xticks(np.arange(1, max_epoch, 5))\nax2.set_ylabel('Loss Value')\nax2.set_xlabel('Epoch')\nax2.set_title('Loss')\nl2 = ax2.legend(loc=\"best\")","metadata":{"papermill":{"duration":2.192684,"end_time":"2022-03-15T12:02:35.209418","exception":false,"start_time":"2022-03-15T12:02:33.016734","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T18:04:06.055368Z","iopub.execute_input":"2022-03-31T18:04:06.056082Z","iopub.status.idle":"2022-03-31T18:04:07.493902Z","shell.execute_reply.started":"2022-03-31T18:04:06.056041Z","shell.execute_reply":"2022-03-31T18:04:07.493205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Summary","metadata":{"papermill":{"duration":0.704328,"end_time":"2022-03-15T12:02:36.618672","exception":false,"start_time":"2022-03-15T12:02:35.914344","status":"completed"},"tags":[]}},{"cell_type":"code","source":"Summary = PrettyTable([\"Model Name\", \"Model Train Accuracy in %\", \"Model Test Accuracy in %\",\"Time Taken To Train in Seconds\"])\nSummary.add_row([\"Vgg16\", \"{:.2f}\".format(vgg16_model_accuracy*100),\"{:.2f}\".format(vgg16_model_score[1]*100),\"{:.2f}\".format(vgg16_trainTime)])\nSummary.add_row([\"Vgg19\", \"{:.2f}\".format(vgg19_model_accuracy*100),\"{:.2f}\".format(vgg19_model_score[1]*100),\"{:.2f}\".format(vgg19_trainTime)])\nSummary.add_row([\"Xception\", \"{:.2f}\".format(Xception_model_accuracy*100),\"{:.2f}\".format(Xception_model_score[1]*100),\"{:.2f}\".format(Xception_trainTime)])\nSummary.add_row([\"ResNet\", \"{:.2f}\".format(ResNet_model_accuracy*100),\"{:.2f}\".format(ResNet_model_score[1]*100),\"{:.2f}\".format(ResNet_trainTime)])\nSummary.add_row([\"InceptionV3\", \"{:.2f}\".format(InceptionV3_model_accuracy*100),\"{:.2f}\".format(InceptionV3_model_score[1]*100),\"{:.2f}\".format(InceptionV3_trainTime)])\nSummary.add_row([\"MobileNet\", \"{:.2f}\".format(MobileNet_model_accuracy*100),\"{:.2f}\".format(MobileNet_model_score[1]*100),\"{:.2f}\".format(MobileNet_trainTime)])\n\nprint(Summary)\nf, ax = plt.subplots(2, 2, figsize=(15, 12))\nt = f.suptitle('Summary', fontsize=12)\nf.subplots_adjust(top=0.85, wspace=0.3)\nepoch_list = list(range(1,21))\n\n\nax[0,0].plot(epoch_list, vgg16_history.history['accuracy'], label='vgg16')\nax[0,0].plot(epoch_list, vgg19_history.history['accuracy'], label='vgg19')\nax[0,0].plot(epoch_list, Xception_history.history['accuracy'], label='Xception')\nax[0,0].plot(epoch_list, ResNet_history.history['accuracy'], label='ResNet')\nax[0,0].plot(epoch_list, InceptionV3_history.history['accuracy'], label='InceptionV3')\nax[0,0].plot(epoch_list, MobileNet_history.history['accuracy'], label='MobileNet')\nax[0,0].set_xticks(np.arange(1, max_epoch, 5))\nax[0,0].set_ylabel('Accuracy Value')\nax[0,0].set_xlabel('Epoch')\nax[0,0].set_title('Train Accuracy')\nl1 = ax[0,0].legend(loc=\"best\")\n\n\nax[0,1].plot(epoch_list, vgg16_history.history['loss'], label='vgg16')\nax[0,1].plot(epoch_list, vgg19_history.history['loss'], label='vgg19')\nax[0,1].plot(epoch_list, Xception_history.history['loss'], label='Xception')\nax[0,1].plot(epoch_list, ResNet_history.history['loss'], label='ResNet')\nax[0,1].plot(epoch_list, InceptionV3_history.history['loss'], label='InceptionV3')\nax[0,1].plot(epoch_list, MobileNet_history.history['loss'], label='MobileNet')\nax[0,1].set_xticks(np.arange(1, max_epoch, 5))\nax[0,1].set_ylabel('Loss Value')\nax[0,1].set_xlabel('Epoch')\nax[0,1].set_title('Train Loss')\nl2 = ax[0,1].legend(loc=\"best\")\n\nax[1,0].plot(epoch_list, vgg16_history.history['val_accuracy'], label='vgg16')\nax[1,0].plot(epoch_list, vgg19_history.history['val_accuracy'], label='vgg19')\nax[1,0].plot(epoch_list, Xception_history.history['val_accuracy'], label='Xception')\nax[1,0].plot(epoch_list, ResNet_history.history['val_accuracy'], label='ResNet')\nax[1,0].plot(epoch_list, InceptionV3_history.history['val_accuracy'], label='InceptionV3')\nax[1,0].plot(epoch_list, MobileNet_history.history['val_accuracy'], label='MobileNet')\nax[1,0].set_xticks(np.arange(1, max_epoch, 5))\nax[1,0].set_ylabel('Accuracy Value')\nax[1,0].set_xlabel('Epoch')\nax[1,0].set_title(' Validation Accuracy')\nl3 = ax[1,0].legend(loc=\"best\")\n\n\nax[1,1].plot(epoch_list, vgg16_history.history['val_loss'], label='vgg16')\nax[1,1].plot(epoch_list, vgg19_history.history['val_loss'], label='vgg19')\nax[1,1].plot(epoch_list, Xception_history.history['val_loss'], label='Xception')\nax[1,1].plot(epoch_list, ResNet_history.history['val_loss'], label='ResNet')\nax[1,1].plot(epoch_list, InceptionV3_history.history['val_loss'], label='InceptionV3')\nax[1,1].plot(epoch_list, MobileNet_history.history['val_loss'], label='MobileNet')\nax[1,1].set_xticks(np.arange(1, max_epoch, 5))\nax[1,1].set_ylabel('Loss Value')\nax[1,1].set_xlabel('Epoch')\nax[1,1].set_title('Validation Loss')\nl4 = ax[1,1].legend(loc=\"best\")","metadata":{"papermill":{"duration":1.411925,"end_time":"2022-03-15T12:02:38.788725","exception":false,"start_time":"2022-03-15T12:02:37.3768","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-31T18:04:13.718273Z","iopub.execute_input":"2022-03-31T18:04:13.718559Z","iopub.status.idle":"2022-03-31T18:04:14.476098Z","shell.execute_reply.started":"2022-03-31T18:04:13.718525Z","shell.execute_reply":"2022-03-31T18:04:14.475361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction ","metadata":{}},{"cell_type":"code","source":" from matplotlib import pyplot\nimg='../input/intel-mobileodt-cervical-cancer-screening/test/test/1.jpg'\ndata = pyplot.imread(img)\npyplot.imshow(data)\nax = pyplot.gca()\npyplot.show()\n\n\ntest1_data_inp = [(idx, img, len(test_files)) for idx, img in enumerate([img])]\ntest1_data_map = ex.map(get_img_data_parallel, \n                        [record[0] for record in test1_data_inp],\n                        [record[1] for record in test1_data_inp],\n                        [record[2] for record in test1_data_inp])\ntest1_data = np.array(list(test1_data_map))\ntest1_imgs_scaled = test1_data / 255.\n\nprint(\"######Generate a vgg16 prediction########\")\nvgg16_prediction = vgg16_model.predict(test1_imgs_scaled)\n\nif (vgg16_prediction[0][0] >= vgg16_prediction[0][1]) and (vgg16_prediction[0][0] >= vgg16_prediction[0][2]):\n    print(\"Test image is Classified as Type 1 with {:.2f}% in vgg16\".format(vgg16_prediction[0][0]*100))\nelif (vgg16_prediction[0][1] >= vgg16_prediction[0][0]) and (vgg16_prediction[0][1] >= vgg16_prediction[0][2]):\n    print(\"Test image is Classified as Type 2 with {:.2f}% in vgg16\".format(vgg16_prediction[0][1]*100))\nelse:\n    print(\"Test image is Classified as Type 3 with {:.2f}% in vgg16\".format(vgg16_prediction[0][2]*100))\n    \nprint(\"                                                                 \")    \nprint(\"######Generate a vgg19 prediction########\")\nvgg19_prediction = vgg19_model.predict(test1_imgs_scaled)\n\nif (vgg19_prediction[0][0] >= vgg19_prediction[0][1]) and (vgg19_prediction[0][0] >= vgg19_prediction[0][2]):\n    print(\"Test image is Classified as Type 1 with {:.2f}% in vgg19\".format(vgg19_prediction[0][0]*100))\nelif (vgg19_prediction[0][1] >= vgg19_prediction[0][0]) and (vgg19_prediction[0][1] >= vgg19_prediction[0][2]):\n    print(\"Test image is Classified as Type 2 with {:.2f}% in vgg19\".format(vgg19_prediction[0][1]*100))\nelse:\n    print(\"Test image is Classified as Type 3 with {:.2f}% in vgg19\".format(vgg19_prediction[0][2]*100))\n    \n    \nprint(\"                                                                 \")    \nprint(\"######Generate a Xception  prediction########\")\nXception_prediction = Xception_model.predict(test1_imgs_scaled)\n\nif (Xception_prediction[0][0] >= Xception_prediction[0][1]) and (Xception_prediction[0][0] >= Xception_prediction[0][2]):\n    print(\"Test image is Classified as Type 1 with {:.2f}% in Xception\".format(Xception_prediction[0][0]*100))\nelif (Xception_prediction[0][1] >= Xception_prediction[0][0]) and (Xception_prediction[0][1] >= Xception_prediction[0][2]):\n    print(\"Test image is Classified as Type 2 with {:.2f}% in Xception\".format(Xception_prediction[0][1]*100))\nelse:\n    print(\"Test image is Classified as Type 3 with {:.2f}% in Xception\".format(Xception_prediction[0][2]*100))\n    \nprint(\"                                                                 \")    \nprint(\"######Generate a ResNet prediction########\")\nResNet_prediction = ResNet_model.predict(test1_imgs_scaled)\n\nif (ResNet_prediction[0][0] >= ResNet_prediction[0][1]) and (ResNet_prediction[0][0] >= ResNet_prediction[0][2]):\n    print(\"Test image is Classified as Type 1 with {:.2f}% in ResNet\".format(ResNet_prediction[0][0]*100))\nelif (ResNet_prediction[0][1] >= ResNet_prediction[0][0]) and (ResNet_prediction[0][1] >= ResNet_prediction[0][2]):\n    print(\"Test image is Classified as Type 2 with {:.2f}% in ResNet\".format(ResNet_prediction[0][1]*100))\nelse:\n    print(\"Test image is Classified as Type 3 with {:.2f}% in ResNet\".format(ResNet_prediction[0][2]*100))\n    \nprint(\"                                                                 \")    \nprint(\"######Generate a InceptionV3  prediction########\")\nInceptionV3_prediction = InceptionV3_model.predict(test1_imgs_scaled)\n\nif (InceptionV3_prediction[0][0] >= InceptionV3_prediction[0][1]) and (InceptionV3_prediction[0][0] >= InceptionV3_prediction[0][2]):\n    print(\"Test image is Classified as Type 1 with {:.2f}% in InceptionV3 \".format(InceptionV3_prediction[0][0]*100))\nelif (InceptionV3_prediction[0][1] >= vgg19_prediction[0][0]) and (InceptionV3_prediction[0][1] >= InceptionV3_prediction[0][2]):\n    print(\"Test image is Classified as Type 2 with {:.2f}% in InceptionV3\".format(InceptionV3_prediction[0][1]*100))\nelse:\n    print(\"Test image is Classified as Type 3 with {:.2f}% in InceptionV3\".format(InceptionV3_prediction[0][2]*100))\n    \n    \nprint(\"                                                                 \")    \nprint(\"######Generate a MobileNet prediction########\")\nMobileNet_prediction = MobileNet_model.predict(test1_imgs_scaled)\n\nif (MobileNet_prediction[0][0] >= MobileNet_prediction[0][1]) and (MobileNet_prediction[0][0] >= MobileNet_prediction[0][2]):\n    print(\"Test image is Classified as Type 1 with {:.2f}% in MobileNet\".format(MobileNet_prediction[0][0]*100))\nelif (MobileNet_prediction[0][1] >= MobileNet_prediction[0][0]) and (MobileNet_prediction[0][1] >= MobileNet_prediction[0][2]):\n    print(\"Test image is Classified as Type 2 with {:.2f}% in MobileNet\".format(MobileNet_prediction[0][1]*100))\nelse:\n    print(\"Test image is Classified as Type 3 with {:.2f}% in MobileNet\".format(MobileNet_prediction[0][2]*100))","metadata":{"execution":{"iopub.status.busy":"2022-03-31T18:38:27.900024Z","iopub.execute_input":"2022-03-31T18:38:27.900445Z","iopub.status.idle":"2022-03-31T18:38:29.429424Z","shell.execute_reply.started":"2022-03-31T18:38:27.900325Z","shell.execute_reply":"2022-03-31T18:38:29.428185Z"},"trusted":true},"execution_count":null,"outputs":[]}]}