{"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":"# Cassava Leaf Disease Classification","metadata":{}},{"cell_type":"markdown","source":"## Prepare Data","metadata":{}},{"cell_type":"markdown","source":"### Import libaries","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np \nimport pandas as pd \nimport json\nimport seaborn as sns\nimport os\nimport tensorflow as tf\n\nfrom PIL import Image\nfrom tensorflow.keras.applications.xception import Xception\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Input, Conv2D, GlobalAveragePooling2D, Flatten, Dense, Dropout, BatchNormalization, LeakyReLU, MaxPooling2D\nfrom tensorflow.keras.optimizers import RMSprop, Adam\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\nfrom tensorflow.keras import models\nfrom tensorflow.keras.applications import InceptionResNetV2\nfrom tensorflow.keras.applications.inception_resnet_v2 import preprocess_input\nfrom tensorflow.keras.layers.experimental.preprocessing import RandomRotation, RandomFlip, RandomZoom, Rescaling","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-15T04:27:41.201215Z","iopub.execute_input":"2022-01-15T04:27:41.201536Z","iopub.status.idle":"2022-01-15T04:27:41.210450Z","shell.execute_reply.started":"2022-01-15T04:27:41.201507Z","shell.execute_reply":"2022-01-15T04:27:41.209100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Set seed to default output","metadata":{}},{"cell_type":"code","source":"seed = 2020\nnp.random.seed(seed)\ntf.random.set_seed(seed)\nos.environ['PYTHONHASHSEED'] = str(seed)\nos.environ['TF_DETERMINISTIC_OPS'] = '1'","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:41.212533Z","iopub.execute_input":"2022-01-15T04:27:41.213234Z","iopub.status.idle":"2022-01-15T04:27:41.397588Z","shell.execute_reply.started":"2022-01-15T04:27:41.213192Z","shell.execute_reply":"2022-01-15T04:27:41.396618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Review dataframe","metadata":{}},{"cell_type":"code","source":"path = '../input/cassava-leaf-disease-classification'\ndf = pd.read_csv(path + \"/train.csv\", sep = ',')\nprint ('Dataframe Shape: ', df.shape)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:41.405766Z","iopub.execute_input":"2022-01-15T04:27:41.406056Z","iopub.status.idle":"2022-01-15T04:27:41.437238Z","shell.execute_reply.started":"2022-01-15T04:27:41.406028Z","shell.execute_reply":"2022-01-15T04:27:41.436260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(8,6))\nsns.set(font_scale = 1.0)\nlabel = sns.countplot(x = \"label\", data = df, order = df.label.value_counts().index)\n\nplt.xlabel(\"Label\", fontsize = 12)\nplt.ylabel(\"Count\", fontsize = 12)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:41.439007Z","iopub.execute_input":"2022-01-15T04:27:41.439349Z","iopub.status.idle":"2022-01-15T04:27:41.590069Z","shell.execute_reply.started":"2022-01-15T04:27:41.439312Z","shell.execute_reply":"2022-01-15T04:27:41.589215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(path + \"/label_num_to_disease_map.json\") as f:\n        label_names = json.loads(f.read())\n        label_names = {int(i): j for i,j in label_names.items()}\n\nlabel_names","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:41.591465Z","iopub.execute_input":"2022-01-15T04:27:41.591811Z","iopub.status.idle":"2022-01-15T04:27:41.601881Z","shell.execute_reply.started":"2022-01-15T04:27:41.591774Z","shell.execute_reply":"2022-01-15T04:27:41.600725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.label = df.label.astype('string')\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:41.603531Z","iopub.execute_input":"2022-01-15T04:27:41.603986Z","iopub.status.idle":"2022-01-15T04:27:41.648833Z","shell.execute_reply.started":"2022-01-15T04:27:41.603926Z","shell.execute_reply":"2022-01-15T04:27:41.647439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Data","metadata":{}},{"cell_type":"markdown","source":"### Pictures of different diseases","metadata":{}},{"cell_type":"code","source":"df_images_path = path + \"/train_images/\"","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:41.650369Z","iopub.execute_input":"2022-01-15T04:27:41.650736Z","iopub.status.idle":"2022-01-15T04:27:41.655666Z","shell.execute_reply.started":"2022-01-15T04:27:41.650696Z","shell.execute_reply":"2022-01-15T04:27:41.654556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize = (8,8))\nnpicture = 9\n\ncount = 1\nimages_list = df[df.label == str(list(label_names.keys())[list(label_names.values()).index(\"Cassava Bacterial Blight (CBB)\")])][\"image_id\"].sample(frac = 1)[:npicture].to_list()\nfor i, img in enumerate(images_list):\n    sample = os.path.join(df_images_path, img)\n    sample_img = Image.open(sample)\n    ax = fig.add_subplot(int(npicture / 3), 3, count, xticks = [], yticks = [])\n    plt.imshow(sample_img)\n    count +=1\n\nfig.suptitle(\"Cassava Bacterial Blight (CBB)\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:41.657549Z","iopub.execute_input":"2022-01-15T04:27:41.658244Z","iopub.status.idle":"2022-01-15T04:27:42.644706Z","shell.execute_reply.started":"2022-01-15T04:27:41.658197Z","shell.execute_reply":"2022-01-15T04:27:42.643874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize = (8,8))\nnpicture = 9\n\ncount = 1\nimages_list = df[df.label == str(list(label_names.keys())[list(label_names.values()).index(\"Cassava Brown Streak Disease (CBSD)\")])][\"image_id\"].sample(frac = 1)[:npicture].to_list()\nfor i, img in enumerate(images_list):\n    sample = os.path.join(df_images_path, img)\n    sample_img = Image.open(sample)\n    ax = fig.add_subplot(int(npicture / 2), 3, count, xticks = [], yticks = [])\n    plt.imshow(sample_img)\n    count +=1\n\nfig.suptitle(\"Cassava Brown Streak Disease (CBSD)\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:42.647658Z","iopub.execute_input":"2022-01-15T04:27:42.648102Z","iopub.status.idle":"2022-01-15T04:27:44.069101Z","shell.execute_reply.started":"2022-01-15T04:27:42.648067Z","shell.execute_reply":"2022-01-15T04:27:44.067417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize = (8,8))\nnpicture = 9\n\ncount = 1\nimages_list = df[df.label == str(list(label_names.keys())[list(label_names.values()).index(\"Cassava Green Mottle (CGM)\")])][\"image_id\"].sample(frac = 1)[:npicture].to_list()\nfor i, img in enumerate(images_list):\n    sample = os.path.join(df_images_path, img)\n    sample_img = Image.open(sample)\n    ax = fig.add_subplot(int(npicture / 2), 3, count, xticks = [], yticks = [])\n    plt.imshow(sample_img)\n    count +=1\n\nfig.suptitle(\"Cassava Green Mottle (CGM)\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:44.072912Z","iopub.execute_input":"2022-01-15T04:27:44.073597Z","iopub.status.idle":"2022-01-15T04:27:45.299862Z","shell.execute_reply.started":"2022-01-15T04:27:44.073556Z","shell.execute_reply":"2022-01-15T04:27:45.298889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize = (8,8))\nnpicture = 9\n\ncount = 1\nimages_list = df[df.label == str(list(label_names.keys())[list(label_names.values()).index(\"Cassava Mosaic Disease (CMD)\")])][\"image_id\"].sample(frac = 1)[:npicture].to_list()\nfor i, img in enumerate(images_list):\n    sample = os.path.join(df_images_path, img)\n    sample_img = Image.open(sample)\n    ax = fig.add_subplot(int(npicture / 2), 3, count, xticks = [], yticks = [])\n    plt.imshow(sample_img)\n    count +=1\n\nfig.suptitle(\"Cassava Mosaic Disease (CMD)\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:45.301322Z","iopub.execute_input":"2022-01-15T04:27:45.301655Z","iopub.status.idle":"2022-01-15T04:27:46.240737Z","shell.execute_reply.started":"2022-01-15T04:27:45.301620Z","shell.execute_reply":"2022-01-15T04:27:46.239958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize = (8,8))\nnpicture = 9\n\ncount = 1\nimages_list = df[df.label == str(list(label_names.keys())[list(label_names.values()).index(\"Healthy\")])][\"image_id\"].sample(frac = 1)[:npicture].to_list()\nfor i, img in enumerate(images_list):\n    sample = os.path.join(df_images_path, img)\n    sample_img = Image.open(sample)\n    ax = fig.add_subplot(int(npicture / 2), 3, count, xticks = [], yticks = [])\n    plt.imshow(sample_img)\n    count +=1\n\nfig.suptitle(\"Healthy\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:46.241882Z","iopub.execute_input":"2022-01-15T04:27:46.242332Z","iopub.status.idle":"2022-01-15T04:27:47.191172Z","shell.execute_reply.started":"2022-01-15T04:27:46.242296Z","shell.execute_reply":"2022-01-15T04:27:47.190310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Check image size if all are same or not","metadata":{}},{"cell_type":"code","source":"im_name_lists = df['image_id'].tolist()\nim_shape_x_lists = []\nim_shape_y_lists = []\nfor i, img in enumerate(im_name_lists):\n    sample = os.path.join(df_images_path, img) \n    sample_img = Image.open(sample)\n    w, h = sample_img.size\n    im_shape_x_lists.append(w)\n    im_shape_y_lists.append(h)\n\nprint ('Check: ', len(im_shape_x_lists), len(im_shape_y_lists))","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:47.192373Z","iopub.execute_input":"2022-01-15T04:27:47.192861Z","iopub.status.idle":"2022-01-15T04:27:56.994710Z","shell.execute_reply.started":"2022-01-15T04:27:47.192801Z","shell.execute_reply":"2022-01-15T04:27:56.993663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(6, 6))\n\nfig.add_subplot(121)\nplt.hist(im_shape_x_lists)\nfig.add_subplot(122)\nplt.hist(im_shape_y_lists)\nplt.tight_layout()\n\nprint(set(im_shape_x_lists), set(im_shape_y_lists))","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:56.996018Z","iopub.execute_input":"2022-01-15T04:27:56.996338Z","iopub.status.idle":"2022-01-15T04:27:57.719709Z","shell.execute_reply.started":"2022-01-15T04:27:56.996302Z","shell.execute_reply":"2022-01-15T04:27:57.718856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Resize image ","metadata":{}},{"cell_type":"code","source":"target_size = (300, 300)\ninput_shape = (300, 300, 3)\nbatch_size = 64\nseed = 10\nepochs = 5","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:57.721133Z","iopub.execute_input":"2022-01-15T04:27:57.721494Z","iopub.status.idle":"2022-01-15T04:27:57.726119Z","shell.execute_reply.started":"2022-01-15T04:27:57.721456Z","shell.execute_reply":"2022-01-15T04:27:57.725009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## InceptionResNetV2","metadata":{}},{"cell_type":"markdown","source":"### Create the Batch Generators","metadata":{}},{"cell_type":"code","source":"data_generator = ImageDataGenerator(validation_split = 0.2)\n\ntrain_data = data_generator.flow_from_dataframe(df, \n                                                directory = df_images_path, \n                                                x_col = \"image_id\",\n                                                y_col = \"label\",\n                                                target_size = target_size,\n                                                batch_size = batch_size,\n                                                shuffle = True, \n                                                seed = seed,\n                                                class_mode = \"sparse\",\n                                                subset = \"training\")\n\nval_data = data_generator.flow_from_dataframe(df, \n                                                directory = df_images_path, \n                                                x_col = \"image_id\",\n                                                y_col = \"label\",\n                                                target_size = target_size,\n                                                batch_size = batch_size,\n                                                shuffle = True, \n                                                seed = seed,\n                                                class_mode = \"sparse\",\n                                                subset = \"validation\")","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:57.727573Z","iopub.execute_input":"2022-01-15T04:27:57.728175Z","iopub.status.idle":"2022-01-15T04:27:58.209569Z","shell.execute_reply.started":"2022-01-15T04:27:57.728135Z","shell.execute_reply":"2022-01-15T04:27:58.208613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(15, 10))\nnpics= 16\ncount = 1\nfor i in range(npics):\n    x,y = val_data.next()\n    image = x[0].astype('uint8')\n\n    label = y[0]  \n    int_label = int(label)  \n    ax = fig.add_subplot(int(npics/4) , 4, count, xticks=[],yticks=[])\n    ax.set_title(label_names[int_label], fontsize = 10)  \n    plt.imshow(image)\n    count = count + 1  \n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:27:58.210910Z","iopub.execute_input":"2022-01-15T04:27:58.211423Z","iopub.status.idle":"2022-01-15T04:28:08.432479Z","shell.execute_reply.started":"2022-01-15T04:27:58.211384Z","shell.execute_reply":"2022-01-15T04:28:08.431416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Build Model With InceptionResNetV2","metadata":{}},{"cell_type":"code","source":"class customCallbacks(tf.keras.callbacks.Callback):\n    def on_epoch_end(self, epoch, logs = None):\n        self.epoch = epoch + 1\n        if self.epoch % 2 == 0:\n            print('epoch num {}, train loss: {}, validation loss: {}'.format(epoch, logs['loss'], logs['val_loss']))\n\nreduce_lr = ReduceLROnPlateau(monitor = 'val_loss', patience = 2, verbose=1)\nmcp_save = ModelCheckpoint(filepath = \"best_model_weights.h5\",\n                           save_best_only = True, save_weights_only = True, monitor = 'val_loss')\nes = EarlyStopping(monitor = \"val_loss\", patience = 10)","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:28:08.433915Z","iopub.execute_input":"2022-01-15T04:28:08.434409Z","iopub.status.idle":"2022-01-15T04:28:08.442429Z","shell.execute_reply.started":"2022-01-15T04:28:08.434365Z","shell.execute_reply":"2022-01-15T04:28:08.441376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception_resnet_v2 = InceptionResNetV2(\n    include_top = False,\n    weights = \"../input/inceptionresnetv2/inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5\",\n    input_shape = input_shape,)\n\ndef build_model():\n    inputs = Input(input_shape)\n    \n    x = preprocess_input(inputs)\n    x = Rescaling(1./255)(x)\n    \n    x = RandomFlip()(x)\n    x = RandomRotation(factor=0.3)(x)\n    \n    x = BatchNormalization()(x)\n    x = inception_resnet_v2(x)\n\n    x = MaxPooling2D((2, 2))(x)\n    x = Conv2D(256, (1, 1), activation=LeakyReLU())(x)\n    x = BatchNormalization()(x)\n    \n    x = Flatten()(x)\n    x = Dropout(0.75)(x)\n\n    x = Dense(256, activation=LeakyReLU())(x)\n    x = Dropout(0.80)(x)\n    x = BatchNormalization()(x)\n    \n    outputs = Dense(5, activation=\"softmax\")(x)\n    \n    model = Model(inputs, outputs)\n    \n    model.compile(optimizer=tf.keras.optimizers.Adam(0.001),\n                  loss=\"sparse_categorical_crossentropy\", \n                  metrics=[\"accuracy\"])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:28:08.444261Z","iopub.execute_input":"2022-01-15T04:28:08.444624Z","iopub.status.idle":"2022-01-15T04:28:17.333322Z","shell.execute_reply.started":"2022-01-15T04:28:08.444586Z","shell.execute_reply":"2022-01-15T04:28:17.332421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:28:17.334569Z","iopub.execute_input":"2022-01-15T04:28:17.334894Z","iopub.status.idle":"2022-01-15T04:28:18.849966Z","shell.execute_reply.started":"2022-01-15T04:28:17.334861Z","shell.execute_reply":"2022-01-15T04:28:18.849143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_data, \n                    validation_data = val_data, \n                    epochs = epochs, \n                    callbacks = [mcp_save, es, reduce_lr])","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:28:18.851274Z","iopub.execute_input":"2022-01-15T04:28:18.851609Z","iopub.status.idle":"2022-01-15T05:01:17.893692Z","shell.execute_reply.started":"2022-01-15T04:28:18.851573Z","shell.execute_reply":"2022-01-15T05:01:17.892669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Check if the model is still fine after loading the trained weights","metadata":{}},{"cell_type":"code","source":"model.load_weights(\"best_model_weights.h5\")\nmodel.evaluate(val_data)","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:01:17.895545Z","iopub.execute_input":"2022-01-15T05:01:17.895927Z","iopub.status.idle":"2022-01-15T05:02:11.269621Z","shell.execute_reply.started":"2022-01-15T05:01:17.895888Z","shell.execute_reply":"2022-01-15T05:02:11.268680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training example image ","metadata":{}},{"cell_type":"code","source":"ex_im = pd.read_csv(\"../input/cassava-leaf-disease-classification/sample_submission.csv\")\nex_im.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:02:11.273483Z","iopub.execute_input":"2022-01-15T05:02:11.275568Z","iopub.status.idle":"2022-01-15T05:02:11.294486Z","shell.execute_reply.started":"2022-01-15T05:02:11.275526Z","shell.execute_reply":"2022-01-15T05:02:11.293714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize = (8,8))\n\nsample = os.path.join('../input/cassava-leaf-disease-classification/test_images/2216849948.jpg')\nsample_img = Image.open(sample)\nax = fig.add_subplot(xticks = [], yticks = [])\nplt.imshow(sample_img)\n\nfig.suptitle(\"Picture to Test\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:02:11.298079Z","iopub.execute_input":"2022-01-15T05:02:11.300043Z","iopub.status.idle":"2022-01-15T05:02:11.638450Z","shell.execute_reply.started":"2022-01-15T05:02:11.300004Z","shell.execute_reply":"2022-01-15T05:02:11.637680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = os.listdir('../input/cassava-leaf-disease-classification/test_images/')\npreds = []\n\nfor i in test_images:\n    image = Image.open(f'../input/cassava-leaf-disease-classification/test_images/{i}')\n    image = image.resize(target_size)\n    image = np.expand_dims(image, axis = 0)\n    preds.append(np.argmax(model.predict(image)))","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:02:11.639555Z","iopub.execute_input":"2022-01-15T05:02:11.640050Z","iopub.status.idle":"2022-01-15T05:02:14.694918Z","shell.execute_reply.started":"2022-01-15T05:02:11.640009Z","shell.execute_reply":"2022-01-15T05:02:14.694034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ans = pd.DataFrame({'image_id': test_images, 'label': preds})\ndf_ans.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:02:14.696427Z","iopub.execute_input":"2022-01-15T05:02:14.696761Z","iopub.status.idle":"2022-01-15T05:02:14.708265Z","shell.execute_reply.started":"2022-01-15T05:02:14.696723Z","shell.execute_reply":"2022-01-15T05:02:14.707336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ans.to_csv(\"submission.csv\", index=None)","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:02:14.712215Z","iopub.execute_input":"2022-01-15T05:02:14.712469Z","iopub.status.idle":"2022-01-15T05:02:14.719296Z","shell.execute_reply.started":"2022-01-15T05:02:14.712446Z","shell.execute_reply":"2022-01-15T05:02:14.718493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training and Validation Curves","metadata":{}},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1, len(acc) + 1)\n\nfig = plt.figure(figsize=(15, 5))\nfig.add_subplot(121)\n\nplt.plot(epochs, acc, linestyle = '--', label = \"Training acc\")\nplt.plot(epochs, val_acc, linestyle = '-.', label = \"Validation acc\")\nplt.title(\"Training and validation acc\")\nplt.legend()\n\nfig.add_subplot(122)\nplt.plot(epochs, loss, linestyle = '--', label = \"Training loss\", alpha = 0.8)\nplt.plot(epochs, val_loss, linestyle = '-.', label = \"Validation loss\", alpha = 0.6)\nplt.title(\"Training and validation loss\")\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:02:14.721274Z","iopub.execute_input":"2022-01-15T05:02:14.721795Z","iopub.status.idle":"2022-01-15T05:02:15.100397Z","shell.execute_reply.started":"2022-01-15T05:02:14.721759Z","shell.execute_reply":"2022-01-15T05:02:15.099539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_types = list(label_names.values())\n\ndef conf_matrix(test_lab, predictions): \n    cm = confusion_matrix(test_lab, np.argmax(np.round(predictions), axis = 1))\n    print(\"Classification Report:\\n\")\n    \n    cr = classification_report(test_lab,\n                                np.argmax(np.round(predictions), axis = 1), \n                                target_names = [class_types[i] for i in range(len(class_types))])\n    print(cr)\n    \n    plt.figure(figsize = (8,8))\n    sns_hmp = sns.heatmap(cm, annot = True, xticklabels = [class_types[i] for i in range(len(class_types))], \n                yticklabels = [class_types[i] for i in range(len(class_types))], fmt = \"d\")\n    fig = sns_hmp.get_figure()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:02:15.101623Z","iopub.execute_input":"2022-01-15T05:02:15.102136Z","iopub.status.idle":"2022-01-15T05:02:15.110047Z","shell.execute_reply.started":"2022-01-15T05:02:15.102098Z","shell.execute_reply":"2022-01-15T05:02:15.109241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Plot the Confusion Matrix (Validation Data)","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, classification_report\nimport seaborn as sns\n\nnpics = 300\nval_pre = []\nval_img = []\nval_label = []\nfor _ in range(npics):\n    x,y = val_data.next()\n    image = x[0].astype('uint8')\n    label = y[0]\n    val_label.append(label)\n    image = np.expand_dims(image, axis = 0)\n    val_img.append(image)","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:02:15.111421Z","iopub.execute_input":"2022-01-15T05:02:15.111883Z","iopub.status.idle":"2022-01-15T05:05:08.060702Z","shell.execute_reply.started":"2022-01-15T05:02:15.111768Z","shell.execute_reply":"2022-01-15T05:05:08.059834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_img_arr = np.array(val_img)\nval_label_arr = np.array(val_label)\nval_img_arr = np.reshape(val_img_arr, (300, 300, 300, 3))","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:05:08.061926Z","iopub.execute_input":"2022-01-15T05:05:08.062293Z","iopub.status.idle":"2022-01-15T05:05:08.092049Z","shell.execute_reply.started":"2022-01-15T05:05:08.062258Z","shell.execute_reply":"2022-01-15T05:05:08.091147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_class_InceptResV2 = model.predict(val_img_arr)","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:05:08.093338Z","iopub.execute_input":"2022-01-15T05:05:08.093861Z","iopub.status.idle":"2022-01-15T05:05:10.831649Z","shell.execute_reply.started":"2022-01-15T05:05:08.093819Z","shell.execute_reply":"2022-01-15T05:05:10.830772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conf_matrix(np.int32(val_label_arr), pred_class_InceptResV2)","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:05:10.834089Z","iopub.execute_input":"2022-01-15T05:05:10.834645Z","iopub.status.idle":"2022-01-15T05:05:11.329801Z","shell.execute_reply.started":"2022-01-15T05:05:10.834601Z","shell.execute_reply":"2022-01-15T05:05:11.328995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Xception","metadata":{}},{"cell_type":"markdown","source":"### Create the Batch Generators","metadata":{}},{"cell_type":"code","source":"image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=0.5,\n                                                                 horizontal_flip=True,\n                                                                 vertical_flip=True,\n                                                                 brightness_range=[0.1,0.3],\n                                                                 zoom_range=[0.1,0.5],\n                                                                 fill_mode='nearest',\n                                                                 preprocessing_function=tf.keras.applications.xception.preprocess_input,\n                                                                 validation_split=0.2,\n                                                                  rescale = 1./255,\n                                                                  dtype='float32')\ntrain_generator = image_generator.flow_from_dataframe(df, \n                                                directory = df_images_path, \n                                                x_col = \"image_id\",\n                                                y_col = \"label\",\n                                                target_size = target_size,\n                                                batch_size = batch_size,\n                                                shuffle = True, \n                                                seed = seed,\n                                                class_mode = \"categorical\",\n                                                subset = \"training\")\nval_generator = image_generator.flow_from_dataframe(df, \n                                                directory = df_images_path, \n                                                x_col = \"image_id\",\n                                                y_col = \"label\",\n                                                target_size = target_size,\n                                                batch_size = batch_size,\n                                                shuffle = True, \n                                                seed = seed,\n                                                class_mode = \"categorical\",\n                                                subset = \"validation\")","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:05:11.333914Z","iopub.execute_input":"2022-01-15T05:05:11.335898Z","iopub.status.idle":"2022-01-15T05:05:16.515273Z","shell.execute_reply.started":"2022-01-15T05:05:11.335858Z","shell.execute_reply":"2022-01-15T05:05:16.514404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Build Model With Xception","metadata":{}},{"cell_type":"code","source":"def cnn_model_pretrain(xception=True,shape=(300,300,3),filters=8,kernel=4,stride=1,pad='same',unit=1024,activation=tf.nn.relu,\n              regularizer=tf.keras.regularizers.L1L2(l1=0.01,l2=0.01)):\n    \n    '''function to define the cnn model architecture with or without transfer learning'''\n    \n    input_img = tf.keras.Input(shape=shape)\n    \n    base_model = tf.keras.applications.xception.Xception(include_top=False,input_shape=shape,weights='imagenet')\n    for layer in base_model.layers:\n        layer.trainable = False\n    \n    if xception:\n        pretrain = base_model(input_img)\n    else:\n        pretrain =  input_img\n        \n    '''One layer of Conv2D, Activation and Pooling layer'''\n    Z1 = tf.keras.layers.Conv2D(filters=filters,kernel_size=(kernel,kernel),strides=stride,padding=pad)(pretrain)\n    A1 = tf.keras.layers.Activation(activation)(Z1)\n    P1 = tf.keras.layers.MaxPool2D(pool_size=(filters,filters),strides=stride*8,padding=pad)(A1)\n    \n    '''Second layer of Conv2D, Activation and Pooling layer'''\n    Z2 = tf.keras.layers.Conv2D(filters=filters*2,kernel_size=(int(kernel/2),int(kernel/2)),strides=stride,\n                               padding=pad) (P1)\n    A2 = tf.keras.layers.Activation(activation)(Z2)\n    P2 = tf.keras.layers.MaxPool2D(pool_size=int(filters/2),strides=stride*4,padding=pad)(A2)\n    \n    '''Flatten the output to feed into the dense layer'''\n    F = tf.keras.layers.Flatten()(P2)\n    \n    '''Dense Layers'''\n    D1 = tf.keras.layers.Dense(units=unit,activation=activation)(F)\n    D2 = tf.keras.layers.Dense(units=unit/2,activation=activation,kernel_regularizer=regularizer)(D1)\n    D3 = tf.keras.layers.Dense(units=unit/4,activation=activation,kernel_regularizer=regularizer)(D2)\n    \n    '''Output layer'''\n    outputs = tf.keras.layers.Dense(units=5,activation=tf.nn.softmax)(D3)\n    \n    '''instantiate the model'''\n    model = tf.keras.Model(inputs=input_img,outputs=outputs)\n    return model  ","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:05:16.516641Z","iopub.execute_input":"2022-01-15T05:05:16.517016Z","iopub.status.idle":"2022-01-15T05:05:16.531346Z","shell.execute_reply.started":"2022-01-15T05:05:16.516979Z","shell.execute_reply":"2022-01-15T05:05:16.530393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"regularizer = tf.keras.regularizers.L2(l2=0.005)\nshape = (300,300,3)\n\nconv_model = cnn_model_pretrain(xception=True,shape=shape,filters=8,kernel=4,stride=1,pad='same',unit=256,activation=tf.nn.relu,\n                      regularizer=regularizer)\n\nconv_model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])\nconv_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:05:16.532687Z","iopub.execute_input":"2022-01-15T05:05:16.533139Z","iopub.status.idle":"2022-01-15T05:05:18.874353Z","shell.execute_reply.started":"2022-01-15T05:05:16.533097Z","shell.execute_reply":"2022-01-15T05:05:18.873288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = conv_model.fit(train_generator,epochs = 5,validation_data = val_generator,batch_size = batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:12:06.332712Z","iopub.execute_input":"2022-01-15T05:12:06.333111Z","iopub.status.idle":"2022-01-15T06:04:58.159558Z","shell.execute_reply.started":"2022-01-15T05:12:06.333077Z","shell.execute_reply":"2022-01-15T06:04:58.158730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training and Validation Curves","metadata":{}},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1, len(acc) + 1)\n\nfig = plt.figure(figsize=(15, 5))\nfig.add_subplot(121)\n\nplt.plot(epochs, acc, linestyle = '--', label = \"Training acc\")\nplt.plot(epochs, val_acc, linestyle = '-.', label = \"Validation acc\")\nplt.title(\"Training and validation acc\")\nplt.legend()\n\nfig.add_subplot(122)\nplt.plot(epochs, loss, linestyle = '--', label = \"Training loss\", alpha = 0.8)\nplt.plot(epochs, val_loss, linestyle = '-.', label = \"Validation loss\", alpha = 0.6)\nplt.title(\"Training and validation loss\")\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T06:04:58.161309Z","iopub.execute_input":"2022-01-15T06:04:58.161661Z","iopub.status.idle":"2022-01-15T06:04:58.555776Z","shell.execute_reply.started":"2022-01-15T06:04:58.161625Z","shell.execute_reply":"2022-01-15T06:04:58.554986Z"},"trusted":true},"execution_count":null,"outputs":[]}]}