{"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":"##### 1\nimport gc\nimport re\n\nimport cv2\nimport math\nimport numpy as np\nimport scipy as sp\nimport pandas as pd\nimport pickle\n\nimport tensorflow as tf\nfrom IPython.display import SVG\n#import efficientnet.tfkeras as efn\nfrom tensorflow.keras.utils import plot_model\nimport tensorflow.keras.layers as L\n#from tensorflow.keras.utils import model_to_dot\nimport tensorflow.keras.backend as K\nfrom tensorflow.keras.models import Model\nfrom kaggle_datasets import KaggleDatasets\nfrom tensorflow.keras.applications import DenseNet121\nfrom tensorflow.keras.applications import ResNet50V2\n\nimport seaborn as sns\nfrom tqdm import tqdm\nimport matplotlib.cm as cm\nfrom sklearn import metrics\nimport matplotlib.pyplot as plt\nfrom sklearn.utils import shuffle\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix as cmx\n\n\ntqdm.pandas()\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport plotly.figure_factory as ff\nfrom plotly.subplots import make_subplots\n\nnp.random.seed(0)\ntf.random.set_seed(0)\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport os","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:41:18.156615Z","iopub.execute_input":"2021-10-21T11:41:18.156943Z","iopub.status.idle":"2021-10-21T11:41:27.042020Z","shell.execute_reply.started":"2021-10-21T11:41:18.156860Z","shell.execute_reply":"2021-10-21T11:41:27.040929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### paths declaration\n\nEPOCHS = 20\nSAMPLE_LEN = 100\ntrain_impath = '../input/plant-pathology-2021-fgvc8/train_images'\ntest_impath = '../input/plant-pathology-2021-fgvc8/test_images'\ntrain_csv = '../input/plant-pathology-2021-fgvc8/train.csv'\nIMAGE_PATH = \"../input/plant-pathology-2020-fgvc7/images/\"\nTEST_PATH = \"../input/plant-pathology-2020-fgvc7/test.csv\"\nTRAIN_PATH = \"../input/plant-pathology-2020-fgvc7/train.csv\"\n\ntest_data = pd.read_csv(TEST_PATH)\ntrain_data = pd.read_csv(TRAIN_PATH)","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:41:32.328179Z","iopub.execute_input":"2021-10-21T11:41:32.328761Z","iopub.status.idle":"2021-10-21T11:41:32.369971Z","shell.execute_reply.started":"2021-10-21T11:41:32.328724Z","shell.execute_reply":"2021-10-21T11:41:32.369004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### processing csv\n\ndf_train = pd.read_csv(train_csv)\ndf_train.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:42:20.590332Z","iopub.execute_input":"2021-10-21T11:42:20.590675Z","iopub.status.idle":"2021-10-21T11:42:20.653288Z","shell.execute_reply.started":"2021-10-21T11:42:20.590634Z","shell.execute_reply":"2021-10-21T11:42:20.652701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### counting values of different labels\n\ndf_train['labels'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:42:25.099813Z","iopub.execute_input":"2021-10-21T11:42:25.100254Z","iopub.status.idle":"2021-10-21T11:42:25.115052Z","shell.execute_reply.started":"2021-10-21T11:42:25.100223Z","shell.execute_reply":"2021-10-21T11:42:25.114504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### projecting histogram plot against values\n\nsns.histplot(df_train['labels'].value_counts(sort=True))","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:42:29.045464Z","iopub.execute_input":"2021-10-21T11:42:29.045884Z","iopub.status.idle":"2021-10-21T11:42:29.318988Z","shell.execute_reply.started":"2021-10-21T11:42:29.045854Z","shell.execute_reply":"2021-10-21T11:42:29.318421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### projecting histgram plot for individual labels\n\nplt.figure(figsize =(25, 6))\nlabels = sns.barplot(df_train['labels'].value_counts().index, df_train['labels'].value_counts())\nfor item in labels.get_xticklabels():\n    item.set_rotation(45)","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:42:40.887281Z","iopub.execute_input":"2021-10-21T11:42:40.887621Z","iopub.status.idle":"2021-10-21T11:42:41.244379Z","shell.execute_reply.started":"2021-10-21T11:42:40.887579Z","shell.execute_reply":"2021-10-21T11:42:41.243574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['labels'] = df_train['labels'].apply(lambda string: string.split(' '))\ndf_train","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:42:47.167957Z","iopub.execute_input":"2021-10-21T11:42:47.168235Z","iopub.status.idle":"2021-10-21T11:42:47.195321Z","shell.execute_reply.started":"2021-10-21T11:42:47.168206Z","shell.execute_reply":"2021-10-21T11:42:47.194766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### pie chart for the labels\n\nsource = df_train['labels'].value_counts()\nfig = go.Figure(data=[go.Pie(labels=source.index, values=source.values)])\nfig.update_layout(title='Label Distribution')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:42:51.808913Z","iopub.execute_input":"2021-10-21T11:42:51.809351Z","iopub.status.idle":"2021-10-21T11:42:51.891599Z","shell.execute_reply.started":"2021-10-21T11:42:51.809320Z","shell.execute_reply":"2021-10-21T11:42:51.890827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##### plant pathology\n\ndef visualize_batch(path,image_ids, labels):\n    plt.figure(figsize=(16, 12))\n    \n    for ind, (image_id, label) in enumerate(zip(image_ids, labels)):\n        plt.subplot(3, 3, ind + 1)\n        image = cv2.imread(os.path.join(path, image_id))\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n        plt.imshow(image)\n        plt.title(f\"Class: {label}\", fontsize=12)\n        plt.axis(\"off\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:42:59.169830Z","iopub.execute_input":"2021-10-21T11:42:59.170132Z","iopub.status.idle":"2021-10-21T11:42:59.177273Z","shell.execute_reply.started":"2021-10-21T11:42:59.170098Z","shell.execute_reply":"2021-10-21T11:42:59.176382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp_df = df_train.sample(9)\nimage_ids = tmp_df[\"image\"].values\nlabels = tmp_df[\"labels\"].values\nvisualize_batch(train_impath,image_ids,labels)","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:43:03.226507Z","iopub.execute_input":"2021-10-21T11:43:03.227013Z","iopub.status.idle":"2021-10-21T11:43:14.900038Z","shell.execute_reply.started":"2021-10-21T11:43:03.226978Z","shell.execute_reply":"2021-10-21T11:43:14.897364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### converting BGR value to RGB Format\n\nSAMPLE_LENGTH = 100\n\ndef load_image(image_id):\n    file_path = image_id\n    img = cv2.imread(train_impath+'/'+file_path)\n    return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n\ntrain_images = df_train['image'][:SAMPLE_LENGTH].apply(load_image)","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:43:21.858014Z","iopub.execute_input":"2021-10-21T11:43:21.858301Z","iopub.status.idle":"2021-10-21T11:43:36.667437Z","shell.execute_reply.started":"2021-10-21T11:43:21.858272Z","shell.execute_reply":"2021-10-21T11:43:36.666464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### getting red, green and blue values from the sample images\n\nred_values = [np.mean(train_images[idx][:, :, 0]) for idx in range(len(train_images))]\ngreen_values = [np.mean(train_images[idx][:, :, 1]) for idx in range(len(train_images))]\nblue_values = [np.mean(train_images[idx][:, :, 2]) for idx in range(len(train_images))]\nvalues = [np.mean(train_images[idx]) for idx in range(len(train_images))]","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:43:41.342587Z","iopub.execute_input":"2021-10-21T11:43:41.342886Z","iopub.status.idle":"2021-10-21T11:43:47.684237Z","shell.execute_reply.started":"2021-10-21T11:43:41.342851Z","shell.execute_reply":"2021-10-21T11:43:47.683589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### projection of red chanel values\n\nfig = ff.create_distplot([red_values], group_labels=[\"R\"], colors=[\"red\"])\nfig.update_layout(showlegend=False, template=\"simple_white\")\nfig.update_layout(title_text=\"Distribution of red channel values\")\nfig.data[0].marker.line.color = 'rgb(0, 0, 0)'\nfig.data[0].marker.line.width = 0.5\nfig","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:43:59.240658Z","iopub.execute_input":"2021-10-21T11:43:59.240999Z","iopub.status.idle":"2021-10-21T11:43:59.996641Z","shell.execute_reply.started":"2021-10-21T11:43:59.240962Z","shell.execute_reply":"2021-10-21T11:43:59.995825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### projection of green chanel values\n\nfig = ff.create_distplot([green_values], group_labels=[\"G\"], colors=[\"green\"])\nfig.update_layout(showlegend=False, template=\"simple_white\")\nfig.update_layout(title_text=\"Distribution of green channel values\")\nfig.data[0].marker.line.color = 'rgb(0, 0, 0)'\nfig.data[0].marker.line.width = 0.5\nfig","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:44:09.892276Z","iopub.execute_input":"2021-10-21T11:44:09.892824Z","iopub.status.idle":"2021-10-21T11:44:09.948779Z","shell.execute_reply.started":"2021-10-21T11:44:09.892788Z","shell.execute_reply":"2021-10-21T11:44:09.947984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### projection of blue chanel values\n\nfig = ff.create_distplot([blue_values], group_labels=[\"B\"], colors=[\"blue\"])\nfig.update_layout(showlegend=False, template=\"simple_white\")\nfig.update_layout(title_text=\"Distribution of blue channel values\")\nfig.data[0].marker.line.color = 'rgb(0, 0, 0)'\nfig.data[0].marker.line.width = 0.5\nfig","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:44:16.820082Z","iopub.execute_input":"2021-10-21T11:44:16.820384Z","iopub.status.idle":"2021-10-21T11:44:16.877000Z","shell.execute_reply.started":"2021-10-21T11:44:16.820352Z","shell.execute_reply":"2021-10-21T11:44:16.876159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### projection of RGB boxplot\n\nfig = go.Figure()\n\nfor idx, values in enumerate([red_values, green_values, blue_values]):\n    if idx == 0:\n        color = \"Red\"\n    if idx == 1:\n        color = \"Green\"\n    if idx == 2:\n        color = \"Blue\"\n    fig.add_trace(go.Box(x=[color]*len(values), y=values, name=color, marker=dict(color=color.lower())))\n    \nfig.update_layout(yaxis_title=\"Mean value\", xaxis_title=\"Color channel\",\n                  title=\"Mean value vs. Color channel\", template=\"plotly_white\")","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:44:24.097337Z","iopub.execute_input":"2021-10-21T11:44:24.097656Z","iopub.status.idle":"2021-10-21T11:44:24.167863Z","shell.execute_reply.started":"2021-10-21T11:44:24.097621Z","shell.execute_reply":"2021-10-21T11:44:24.167058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### combined projection\nfig = ff.create_distplot([red_values, green_values, blue_values],\n                         group_labels=[\"R\", \"G\", \"B\"],\n                         colors=[\"red\", \"green\", \"blue\"])\nfig.update_layout(title_text=\"Distribution of red channel values\", template=\"simple_white\")\nfig.data[0].marker.line.color = 'rgb(0, 0, 0)'\nfig.data[0].marker.line.width = 0.5\nfig.data[1].marker.line.color = 'rgb(0, 0, 0)'\nfig.data[1].marker.line.width = 0.5\nfig.data[2].marker.line.color = 'rgb(0, 0, 0)'\nfig.data[2].marker.line.width = 0.5\nfig","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:44:29.817011Z","iopub.execute_input":"2021-10-21T11:44:29.817626Z","iopub.status.idle":"2021-10-21T11:44:29.902618Z","shell.execute_reply.started":"2021-10-21T11:44:29.817585Z","shell.execute_reply":"2021-10-21T11:44:29.902035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def edge_and_cut(img):\n    emb_img = img.copy()\n    edges = cv2.Canny(img, 100, 200)\n    edge_coors = []\n    for i in range(edges.shape[0]):\n        for j in range(edges.shape[1]):\n            if edges[i][j] != 0:\n                edge_coors.append((i, j))\n    \n    row_min = edge_coors[np.argsort([coor[0] for coor in edge_coors])[0]][0]\n    row_max = edge_coors[np.argsort([coor[0] for coor in edge_coors])[-1]][0]\n    col_min = edge_coors[np.argsort([coor[1] for coor in edge_coors])[0]][1]\n    col_max = edge_coors[np.argsort([coor[1] for coor in edge_coors])[-1]][1]\n    new_img = img[row_min:row_max, col_min:col_max]\n    \n    emb_img[row_min-10:row_min+10, col_min:col_max] = [255, 0, 0]\n    emb_img[row_max-10:row_max+10, col_min:col_max] = [255, 0, 0]\n    emb_img[row_min:row_max, col_min-10:col_min+10] = [255, 0, 0]\n    emb_img[row_min:row_max, col_max-10:col_max+10] = [255, 0, 0]\n    \n    fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(30, 20))\n    ax[0].imshow(img, cmap='gray')\n    ax[0].set_title('Original Image', fontsize=24)\n    ax[1].imshow(edges, cmap='gray')\n    ax[1].set_title('Canny Edges', fontsize=24)\n    ax[2].imshow(emb_img, cmap='gray')\n    ax[2].set_title('Bounding Box', fontsize=24)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:44:37.037337Z","iopub.execute_input":"2021-10-21T11:44:37.037918Z","iopub.status.idle":"2021-10-21T11:44:37.052212Z","shell.execute_reply.started":"2021-10-21T11:44:37.037868Z","shell.execute_reply":"2021-10-21T11:44:37.051263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"edge_and_cut(train_images[6])\nedge_and_cut(train_images[7])\nedge_and_cut(train_images[8])","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:44:42.015870Z","iopub.execute_input":"2021-10-21T11:44:42.016183Z","iopub.status.idle":"2021-10-21T11:46:20.120253Z","shell.execute_reply.started":"2021-10-21T11:44:42.016150Z","shell.execute_reply":"2021-10-21T11:46:20.119360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### TRAINING DATASET","metadata":{}},{"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n\ntf.config.experimental_connect_to_cluster(tpu)\ntf.tpu.experimental.initialize_tpu_system(tpu)\nstrategy = tf.distribute.experimental.TPUStrategy(tpu)\n\nBATCH_SIZE = 16 * strategy.num_replicas_in_sync\nGCS_DS_PATH = KaggleDatasets().get_gcs_path('plant-pathology-2020-fgvc7')","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:46:35.207784Z","iopub.execute_input":"2021-10-21T11:46:35.208057Z","iopub.status.idle":"2021-10-21T11:46:41.375249Z","shell.execute_reply.started":"2021-10-21T11:46:35.208029Z","shell.execute_reply":"2021-10-21T11:46:41.374362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def format_path(st):\n    return GCS_DS_PATH + '/images/' + st + '.jpg'\n\ntest_paths = test_data.image_id.apply(format_path).values\ntrain_paths = train_data.image_id.apply(format_path).values\n\ntrain_labels = np.float32(train_data.loc[:, 'healthy':'scab'].values)\ntrain_paths, valid_paths, train_labels, valid_labels =\\\ntrain_test_split(train_paths, train_labels, test_size=0.15, random_state=2020)","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:46:50.190137Z","iopub.execute_input":"2021-10-21T11:46:50.190568Z","iopub.status.idle":"2021-10-21T11:46:50.205803Z","shell.execute_reply.started":"2021-10-21T11:46:50.190525Z","shell.execute_reply":"2021-10-21T11:46:50.204721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_image(filename, label=None, image_size=(512, 512)):\n    bits = tf.io.read_file(filename)\n    image = tf.image.decode_jpeg(bits, channels=3)\n    image = tf.cast(image, tf.float32) / 255.0\n    image = tf.image.resize(image, image_size)\n    \n    if label is None:\n        return image\n    else:\n        return image, label\n\ndef data_augment(image, label=None):\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_flip_up_down(image)\n    \n    if label is None:\n        return image\n    else:\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:46:55.221035Z","iopub.execute_input":"2021-10-21T11:46:55.221340Z","iopub.status.idle":"2021-10-21T11:46:55.228928Z","shell.execute_reply.started":"2021-10-21T11:46:55.221304Z","shell.execute_reply":"2021-10-21T11:46:55.227978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = (\n    tf.data.Dataset\n    .from_tensor_slices((train_paths, train_labels))\n    .map(decode_image, num_parallel_calls=AUTO)\n    .map(data_augment, num_parallel_calls=AUTO)\n    .repeat()\n    .shuffle(512)\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n\nvalid_dataset = (\n    tf.data.Dataset\n    .from_tensor_slices((valid_paths, valid_labels))\n    .map(decode_image, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n    .cache()\n    .prefetch(AUTO)\n)\n\ntest_dataset = (\n    tf.data.Dataset\n    .from_tensor_slices(test_paths)\n    .map(decode_image, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n)","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:47:07.853640Z","iopub.execute_input":"2021-10-21T11:47:07.853963Z","iopub.status.idle":"2021-10-21T11:47:08.109992Z","shell.execute_reply.started":"2021-10-21T11:47:07.853930Z","shell.execute_reply":"2021-10-21T11:47:08.109257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_lrfn(lr_start=0.00001, lr_max=0.00005, \n               lr_min=0.00001, lr_rampup_epochs=5, \n               lr_sustain_epochs=0, lr_exp_decay=.8):\n    lr_max = lr_max * strategy.num_replicas_in_sync\n\n    def lrfn(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) *\\\n                 lr_exp_decay**(epoch - lr_rampup_epochs\\\n                                - lr_sustain_epochs) + lr_min\n        return lr\n    return lrfn","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:47:25.615242Z","iopub.execute_input":"2021-10-21T11:47:25.615542Z","iopub.status.idle":"2021-10-21T11:47:25.624202Z","shell.execute_reply.started":"2021-10-21T11:47:25.615505Z","shell.execute_reply":"2021-10-21T11:47:25.623514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lrfn = build_lrfn()\nSTEPS_PER_EPOCH = train_labels.shape[0] // BATCH_SIZE\nlr_schedule = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:47:40.917500Z","iopub.execute_input":"2021-10-21T11:47:40.918259Z","iopub.status.idle":"2021-10-21T11:47:40.922861Z","shell.execute_reply.started":"2021-10-21T11:47:40.918200Z","shell.execute_reply":"2021-10-21T11:47:40.922017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### desneNet121\nwith strategy.scope():\n    model = tf.keras.Sequential([DenseNet121(input_shape=(512, 512, 3),\n                                             weights='imagenet',\n                                             include_top=False),\n                                 L.GlobalAveragePooling2D(),\n                                 L.Dense(train_labels.shape[1],\n                                         activation='softmax')])\n        \n    model.compile(optimizer='adam',\n                  loss = 'categorical_crossentropy',\n                  metrics=['categorical_accuracy'])\n    model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:47:45.901129Z","iopub.execute_input":"2021-10-21T11:47:45.901878Z","iopub.status.idle":"2021-10-21T11:48:08.166349Z","shell.execute_reply.started":"2021-10-21T11:47:45.901839Z","shell.execute_reply":"2021-10-21T11:48:08.164753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_dataset,\n                    epochs=EPOCHS,\n                    callbacks=[lr_schedule],\n                    steps_per_epoch=STEPS_PER_EPOCH,\n                    validation_data=valid_dataset)","metadata":{"execution":{"iopub.status.busy":"2021-10-21T11:48:20.088436Z","iopub.execute_input":"2021-10-21T11:48:20.088737Z","iopub.status.idle":"2021-10-21T12:03:06.311167Z","shell.execute_reply.started":"2021-10-21T11:48:20.088704Z","shell.execute_reply":"2021-10-21T12:03:06.310278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"densenet_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-10-21T12:10:29.928979Z","iopub.execute_input":"2021-10-21T12:10:29.929838Z","iopub.status.idle":"2021-10-21T12:10:34.668672Z","shell.execute_reply.started":"2021-10-21T12:10:29.929782Z","shell.execute_reply":"2021-10-21T12:10:34.667672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### ResNetv50\nwith strategy.scope():\n    model_res = tf.keras.Sequential([ResNet50V2(input_shape=(512, 512, 3),\n                                             weights='imagenet',\n                                             include_top=False),\n                                 L.GlobalAveragePooling2D(),\n                                 L.Dense(train_labels.shape[1],\n                                         activation='softmax')])\n        \n    model_res.compile(optimizer='adam',\n                  loss = 'categorical_crossentropy',\n                  metrics=['categorical_accuracy'])\n    model_res.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-21T12:11:12.116543Z","iopub.execute_input":"2021-10-21T12:11:12.117011Z","iopub.status.idle":"2021-10-21T12:11:25.534494Z","shell.execute_reply.started":"2021-10-21T12:11:12.116978Z","shell.execute_reply":"2021-10-21T12:11:25.533376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_res = model_res.fit(train_dataset,\n                    epochs=EPOCHS,\n                    callbacks=[lr_schedule],\n                    steps_per_epoch=STEPS_PER_EPOCH,\n                    validation_data=valid_dataset)","metadata":{"execution":{"iopub.status.busy":"2021-10-21T12:12:28.595036Z","iopub.execute_input":"2021-10-21T12:12:28.595324Z","iopub.status.idle":"2021-10-21T12:26:06.239105Z","shell.execute_reply.started":"2021-10-21T12:12:28.595294Z","shell.execute_reply":"2021-10-21T12:26:06.238004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_res.save(\"resnet_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-10-21T12:27:20.871818Z","iopub.execute_input":"2021-10-21T12:27:20.872599Z","iopub.status.idle":"2021-10-21T12:27:23.651573Z","shell.execute_reply.started":"2021-10-21T12:27:20.872551Z","shell.execute_reply":"2021-10-21T12:27:23.650926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_training_curves(training, validation, yaxis):\n    if yaxis == \"loss\":\n        ylabel = \"Loss\"\n        title = \"Loss vs. Epochs\"\n    else:\n        ylabel = \"Accuracy\"\n        title = \"Accuracy vs. Epochs\"\n        \n    fig = go.Figure()\n        \n    fig.add_trace(\n        go.Scatter(x=np.arange(1, EPOCHS+1), mode='lines+markers', y=training, marker=dict(color=\"dodgerblue\"),\n               name=\"Train\"))\n    \n    fig.add_trace(\n        go.Scatter(x=np.arange(1, EPOCHS+1), mode='lines+markers', y=validation, marker=dict(color=\"darkorange\"),\n               name=\"Val\"))\n    \n    fig.update_layout(title_text=title, yaxis_title=ylabel, xaxis_title=\"Epochs\", template=\"plotly_white\")\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-21T12:27:47.851039Z","iopub.execute_input":"2021-10-21T12:27:47.851811Z","iopub.status.idle":"2021-10-21T12:27:47.859217Z","shell.execute_reply.started":"2021-10-21T12:27:47.851772Z","shell.execute_reply":"2021-10-21T12:27:47.858334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### DenseNet\ndisplay_training_curves(\n    history.history['categorical_accuracy'], \n    history.history['val_categorical_accuracy'], \n    'accuracy')","metadata":{"execution":{"iopub.status.busy":"2021-10-21T12:28:03.502295Z","iopub.execute_input":"2021-10-21T12:28:03.502605Z","iopub.status.idle":"2021-10-21T12:28:03.540921Z","shell.execute_reply.started":"2021-10-21T12:28:03.502576Z","shell.execute_reply":"2021-10-21T12:28:03.539831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### ResNet\ndisplay_training_curves(\n    history_res.history['categorical_accuracy'], \n    history_res.history['val_categorical_accuracy'], \n    'accuracy')","metadata":{"execution":{"iopub.status.busy":"2021-10-21T12:28:19.255573Z","iopub.execute_input":"2021-10-21T12:28:19.255846Z","iopub.status.idle":"2021-10-21T12:28:19.293076Z","shell.execute_reply.started":"2021-10-21T12:28:19.255818Z","shell.execute_reply":"2021-10-21T12:28:19.292205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process(img):\n    return cv2.resize(img/255.0, (512, 512)).reshape(-1, 512, 512, 3)\ndef predict(img):\n    return model.layers[2](model.layers[1](model.layers[0](process(img)))).numpy()[0]\n    \n\nfig = make_subplots(rows=4, cols=2)\npreds = predict(train_images[2])\n\ncolors = {\"Healthy\":px.colors.qualitative.Plotly[0], \"Scab\":px.colors.qualitative.Plotly[0], \"Rust\":px.colors.qualitative.Plotly[0], \"Multiple diseases\":px.colors.qualitative.Plotly[0]}\nif list.index(preds.tolist(), max(preds)) == 0:\n    pred = \"Healthy\"\nif list.index(preds.tolist(), max(preds)) == 1:\n    pred = \"Scab\"\nif list.index(preds.tolist(), max(preds)) == 2:\n    pred = \"Rust\"\nif list.index(preds.tolist(), max(preds)) == 3:\n    pred = \"Multiple diseases\"\n\ncolors[pred] = px.colors.qualitative.Plotly[1]\ncolors[\"Healthy\"] = \"seagreen\"\ncolors = [colors[val] for val in colors.keys()]\nfig.add_trace(go.Image(z=cv2.resize(train_images[2], (205, 136))), row=1, col=1)\nfig.add_trace(go.Bar(x=[\"Healthy\", \"Multiple diseases\", \"Rust\", \"Scab\"], y=preds, marker=dict(color=colors)), row=1, col=2)\nfig.update_layout(height=1200, width=800, title_text=\"DenseNet Predictions\", showlegend=False)\n\npreds = predict(train_images[0])\ncolors = {\"Healthy\":px.colors.qualitative.Plotly[0], \"Scab\":px.colors.qualitative.Plotly[0], \"Rust\":px.colors.qualitative.Plotly[0], \"Multiple diseases\":px.colors.qualitative.Plotly[0]}\nif list.index(preds.tolist(), max(preds)) == 0:\n    pred = \"Healthy\"\nif list.index(preds.tolist(), max(preds)) == 1:\n    pred = \"Multiple diseases\"\nif list.index(preds.tolist(), max(preds)) == 2:\n    pred = \"Rust\"\nif list.index(preds.tolist(), max(preds)) == 3:\n    pred = \"Scab\"\n    \ncolors[pred] = px.colors.qualitative.Plotly[1]\ncolors[\"Multiple diseases\"] = \"seagreen\"\ncolors = [colors[val] for val in colors.keys()]\nfig.add_trace(go.Image(z=cv2.resize(train_images[0], (205, 136))), row=2, col=1)\nfig.add_trace(go.Bar(x=[\"Healthy\", \"Multiple diseases\", \"Rust\", \"Scab\"], y=preds, marker=dict(color=colors)), row=2, col=2)\n\npreds = predict(train_images[3])\ncolors = {\"Healthy\":px.colors.qualitative.Plotly[0], \"Scab\":px.colors.qualitative.Plotly[0], \"Rust\":px.colors.qualitative.Plotly[0], \"Multiple diseases\":px.colors.qualitative.Plotly[0]}\nif list.index(preds.tolist(), max(preds)) == 0:\n    pred = \"Healthy\"\nif list.index(preds.tolist(), max(preds)) == 1:\n    pred = \"Multiple diseases\"\nif list.index(preds.tolist(), max(preds)) == 2:\n    pred = \"Rust\"\nif list.index(preds.tolist(), max(preds)) == 3:\n    pred = \"Scab\"\n    \ncolors[pred] = px.colors.qualitative.Plotly[1]\ncolors[\"Rust\"] = \"seagreen\"\ncolors = [colors[val] for val in colors.keys()]\nfig.add_trace(go.Image(z=cv2.resize(train_images[3], (205, 136))), row=3, col=1)\nfig.add_trace(go.Bar(x=[\"Healthy\", \"Multiple diseases\", \"Rust\", \"Scab\"], y=preds, marker=dict(color=colors)), row=3, col=2)\n\npreds = predict(train_images[1])\ncolors = {\"Healthy\":px.colors.qualitative.Plotly[0], \"Scab\":px.colors.qualitative.Plotly[0], \"Rust\":px.colors.qualitative.Plotly[0], \"Multiple diseases\":px.colors.qualitative.Plotly[0]}\nif list.index(preds.tolist(), max(preds)) == 0:\n    pred = \"Healthy\"\nif list.index(preds.tolist(), max(preds)) == 1:\n    pred = \"Multiple diseases\"\nif list.index(preds.tolist(), max(preds)) == 2:\n    pred = \"Rust\"\nif list.index(preds.tolist(), max(preds)) == 3:\n    pred = \"Scab\"\n    \ncolors[pred] = px.colors.qualitative.Plotly[1]\ncolors[\"Scab\"] = \"seagreen\"\ncolors = [colors[val] for val in colors.keys()]\nfig.add_trace(go.Image(z=cv2.resize(train_images[1], (205, 136))), row=4, col=1)\nfig.add_trace(go.Bar(x=[\"Healthy\", \"Multiple diseases\", \"Rust\", \"Scab\"], y=preds, marker=dict(color=colors)), row=4, col=2)\n\nfig.update_layout(template=\"plotly_white\")","metadata":{"execution":{"iopub.status.busy":"2021-10-21T12:29:01.545935Z","iopub.execute_input":"2021-10-21T12:29:01.546707Z","iopub.status.idle":"2021-10-21T12:29:10.629356Z","shell.execute_reply.started":"2021-10-21T12:29:01.546667Z","shell.execute_reply":"2021-10-21T12:29:10.628454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process(img):\n    return cv2.resize(img/255.0, (512, 512)).reshape(-1, 512, 512, 3)\ndef predict(img):\n    return model_res.layers[2](model_res.layers[1](model_res.layers[0](process(img)))).numpy()[0]\n\nfig = make_subplots(rows=4, cols=2)\npreds = predict(train_images[2])\n\ncolors = {\"Healthy\":px.colors.qualitative.Plotly[0], \"Scab\":px.colors.qualitative.Plotly[0], \"Rust\":px.colors.qualitative.Plotly[0], \"Multiple diseases\":px.colors.qualitative.Plotly[0]}\nif list.index(preds.tolist(), max(preds)) == 0:\n    pred = \"Healthy\"\nif list.index(preds.tolist(), max(preds)) == 1:\n    pred = \"Scab\"\nif list.index(preds.tolist(), max(preds)) == 2:\n    pred = \"Rust\"\nif list.index(preds.tolist(), max(preds)) == 3:\n    pred = \"Multiple diseases\"\n\ncolors[pred] = px.colors.qualitative.Plotly[1]\ncolors[\"Healthy\"] = \"seagreen\"\ncolors = [colors[val] for val in colors.keys()]\nfig.add_trace(go.Image(z=cv2.resize(train_images[2], (205, 136))), row=1, col=1)\nfig.add_trace(go.Bar(x=[\"Healthy\", \"Multiple diseases\", \"Rust\", \"Scab\"], y=preds, marker=dict(color=colors)), row=1, col=2)\nfig.update_layout(height=1200, width=800, title_text=\"ResNet Predictions\", showlegend=False)\n\npreds = predict(train_images[0])\ncolors = {\"Healthy\":px.colors.qualitative.Plotly[0], \"Scab\":px.colors.qualitative.Plotly[0], \"Rust\":px.colors.qualitative.Plotly[0], \"Multiple diseases\":px.colors.qualitative.Plotly[0]}\nif list.index(preds.tolist(), max(preds)) == 0:\n    pred = \"Healthy\"\nif list.index(preds.tolist(), max(preds)) == 1:\n    pred = \"Multiple diseases\"\nif list.index(preds.tolist(), max(preds)) == 2:\n    pred = \"Rust\"\nif list.index(preds.tolist(), max(preds)) == 3:\n    pred = \"Scab\"\n    \ncolors[pred] = px.colors.qualitative.Plotly[1]\ncolors[\"Multiple diseases\"] = \"seagreen\"\ncolors = [colors[val] for val in colors.keys()]\nfig.add_trace(go.Image(z=cv2.resize(train_images[0], (205, 136))), row=2, col=1)\nfig.add_trace(go.Bar(x=[\"Healthy\", \"Multiple diseases\", \"Rust\", \"Scab\"], y=preds, marker=dict(color=colors)), row=2, col=2)\n\npreds = predict(train_images[3])\ncolors = {\"Healthy\":px.colors.qualitative.Plotly[0], \"Scab\":px.colors.qualitative.Plotly[0], \"Rust\":px.colors.qualitative.Plotly[0], \"Multiple diseases\":px.colors.qualitative.Plotly[0]}\nif list.index(preds.tolist(), max(preds)) == 0:\n    pred = \"Healthy\"\nif list.index(preds.tolist(), max(preds)) == 1:\n    pred = \"Multiple diseases\"\nif list.index(preds.tolist(), max(preds)) == 2:\n    pred = \"Rust\"\nif list.index(preds.tolist(), max(preds)) == 3:\n    pred = \"Scab\"\n    \ncolors[pred] = px.colors.qualitative.Plotly[1]\ncolors[\"Rust\"] = \"seagreen\"\ncolors = [colors[val] for val in colors.keys()]\nfig.add_trace(go.Image(z=cv2.resize(train_images[3], (205, 136))), row=3, col=1)\nfig.add_trace(go.Bar(x=[\"Healthy\", \"Multiple diseases\", \"Rust\", \"Scab\"], y=preds, marker=dict(color=colors)), row=3, col=2)\n\npreds = predict(train_images[1])\ncolors = {\"Healthy\":px.colors.qualitative.Plotly[0], \"Scab\":px.colors.qualitative.Plotly[0], \"Rust\":px.colors.qualitative.Plotly[0], \"Multiple diseases\":px.colors.qualitative.Plotly[0]}\nif list.index(preds.tolist(), max(preds)) == 0:\n    pred = \"Healthy\"\nif list.index(preds.tolist(), max(preds)) == 1:\n    pred = \"Multiple diseases\"\nif list.index(preds.tolist(), max(preds)) == 2:\n    pred = \"Rust\"\nif list.index(preds.tolist(), max(preds)) == 3:\n    pred = \"Scab\"\n    \ncolors[pred] = px.colors.qualitative.Plotly[1]\ncolors[\"Scab\"] = \"seagreen\"\ncolors = [colors[val] for val in colors.keys()]\nfig.add_trace(go.Image(z=cv2.resize(train_images[1], (205, 136))), row=4, col=1)\nfig.add_trace(go.Bar(x=[\"Healthy\", \"Multiple diseases\", \"Rust\", \"Scab\"], y=preds, marker=dict(color=colors)), row=4, col=2)\n\nfig.update_layout(template=\"plotly_white\")","metadata":{"execution":{"iopub.status.busy":"2021-10-21T12:42:42.120636Z","iopub.execute_input":"2021-10-21T12:42:42.120926Z","iopub.status.idle":"2021-10-21T12:42:46.371369Z","shell.execute_reply.started":"2021-10-21T12:42:42.120898Z","shell.execute_reply":"2021-10-21T12:42:46.370506Z"},"trusted":true},"execution_count":null,"outputs":[]}]}