{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport json\nimport cv2\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom PIL import Image\nfrom keras.optimizers import Adam, RMSprop\n\n%matplotlib inline\nimport matplotlib.image  as mpimg\nimport matplotlib.pyplot as plt\n\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom tensorflow.keras.applications import ResNet50, EfficientNetB3, EfficientNetB0, EfficientNetB4, InceptionResNetV2\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report\n\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten,GlobalAveragePooling2D,BatchNormalization, Activation\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom tensorflow.keras import utils\n\nfrom tensorflow.keras.utils import plot_model\nfrom IPython.display import Image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pd.set_option('display.max_columns', None)\npd.set_option('display.expand_frame_repr', False)\npd.set_option('max_colwidth', 1000)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"INPUT_DIR = '/kaggle/input/cassava-leaf-disease-classification'\nos.listdir(INPUT_DIR)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(os.path.join(INPUT_DIR, 'train.csv'))\nprint(train_df.shape)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['path'] = os.path.join(INPUT_DIR, 'train_images/') + train_df['image_id'] \ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":" round(train_df.label.value_counts(normalize=True) * 100,2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_sub_df = pd.read_csv(os.path.join(INPUT_DIR, 'sample_submission.csv'))\nprint(sample_sub_df.shape)\nsample_sub_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open(os.path.join(INPUT_DIR, 'label_num_to_disease_map.json')) as file:\n    train_classes = json.load(file)\n    \ntrain_classes = {int(k): v for k, v in train_classes.items()}\ntrain_classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['label'] = train_df['label'].astype('str')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Augmentation"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Add our data-augmentation parameters to ImageDataGenerator\ntrain_datagen = ImageDataGenerator(rescale = 1./255.,\n                                   rotation_range = 40,\n                                   width_shift_range = 0.2,\n                                   height_shift_range = 0.2,\n                                   shear_range = 0.2,\n                                   zoom_range = 0.2,\n                                   horizontal_flip = True,\n                                   vertical_flip = True,\n                                   samplewise_center=True,\n                                   samplewise_std_normalization=True)\n\n# Note that the validation data should not be augmented!\ntest_datagen = ImageDataGenerator( rescale = 1.0/255. )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_valid = train_test_split(train_df, \n                                    test_size = 0.2, \n                                    random_state=42, \n                                    shuffle=True, \n                                    stratify=train_df['label'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"round(X_train.label.value_counts(normalize=True) * 100,2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_valid.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"round(X_valid.label.value_counts(normalize=True) * 100,2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_data_gen(image_size, batch_size):\n    train_generator = train_datagen.flow_from_dataframe(dataframe=X_train,\n                                                    x_col='path',\n                                                    y_col='label',\n                                                    batch_size = batch_size,\n                                                    class_mode = 'categorical', \n                                                    shuffle = True,\n                                                    target_size = (image_size, image_size)) \n\n    validation_generator = train_datagen.flow_from_dataframe(dataframe=X_valid,\n                                                    x_col='path',\n                                                    y_col='label',\n                                                    batch_size = batch_size,\n                                                    class_mode = 'categorical',\n                                                    shuffle = False,\n                                                    target_size = (image_size, image_size)) \n    return train_generator, validation_generator","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_history(history):\n    acc = history.history['acc']\n    val_acc = history.history['val_acc']\n    loss = history.history['loss']\n    val_loss = history.history['val_loss']\n\n    epochs = range(len(acc))\n\n    plt.plot(epochs, acc, 'r', label='Training accuracy')\n    plt.plot(epochs, val_acc, 'b', label='Validation accuracy')\n    plt.title('Training and validation accuracy')\n    plt.legend(loc=0)\n    plt.figure()\n\n    plt.plot(epochs, loss, 'r', label='Training loss')\n    plt.plot(epochs, val_loss, 'b', label='Validation loss')\n    plt.title('Training and validation loss')\n    plt.legend(loc=0)\n    plt.figure()\n\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"    ##### Reduce LR on Plateau:\n    -------------------------------\n    * monitor\t  quantity to be monitored.\n    * factor\t   factor by which the learning rate will be reduced. new_lr = lr * factor.\n    * patience\t number of epochs with no improvement after which learning rate will be reduced.\n    * verbose\t  int. 0: quiet, 1: update messages.\n    * mode\t     one of {'auto', 'min', 'max'}. In 'min' mode, the learning rate will be reduced when the quantity monitored has stopped decreasing; in 'max' mode it will be reduced when the quantity monitored has stopped increasing; in 'auto' mode, the direction is automatically inferred from the name of the monitored quantity.\n    * min_delta\tthreshold for measuring the new optimum, to only focus on significant changes.\n    * cooldown\t number of epochs to wait before resuming normal operation after lr has been reduced.\n    * min_lr\t   lower bound on the learning rate."},{"metadata":{},"cell_type":"markdown","source":"    ##### ModelCheckpoint:\n    -----------------------\n\n    This callback saves the model after every epoch. Here are some relevant metrics:\n\n    * filepath: the file path you want to save your model in\n    * monitor: the value being monitored\n    * save_best_only: set this to True if you do not want to overwrite the latest best model\n    * mode: auto, min, or max. For example, you set mode=’min’ if the monitored value is val_loss and you want to minimize it."},{"metadata":{},"cell_type":"markdown","source":"    ##### EarlyStopping:\n    -----------------------\n    Overfitting is a nightmare for Machine Learning practitioners. One way to avoid overfitting is to terminate the process early. The EarlyStoppingfunction has various metrics/arguments that you can modify to set up when the training process should stop. Here are some relevant metrics:\n\n    * monitor: value being monitored, i.e: val_loss\n    * min_delta: minimum change in the monitored value. For example, min_delta=1 means that the training process will be stopped if the absolute change of the monitored value is less than 1\n    * patience: number of epochs with no improvement after which training will be stopped\n    * restore_best_weights: set this metric to True if you want to keep the best weights once stopped"},{"metadata":{"trusted":true},"cell_type":"code","source":"def callbacks_func(weight_path_save):\n\n    checkpoint = ModelCheckpoint(weight_path_save, \n                             monitor= 'val_loss', \n                             verbose=1, \n                             save_best_only=True, \n                             mode= 'min', \n                             save_weights_only = False)\n\n\n    early = EarlyStopping(monitor= 'val_loss', mode= 'min', patience=3)\n\n    reduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', \n                                   factor=0.8, \n                                   patience=2, \n                                   verbose=1, \n                                   mode='auto', \n                                   epsilon=0.0001, \n                                   cooldown=5, \n                                   min_lr=0.00001)\n\n    print(checkpoint)\n    print(early)\n    print(reduceLROnPlat)\n    return checkpoint, early, reduceLROnPlat","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_image_size(model_name):\n    if model_name==\"InceptionV3\":\n        image_size=300\n    elif model_name==\"EfficientNetB3\":\n        image_size=300\n    elif model_name==\"EfficientNetB4\":\n        image_size=380\n    elif model_name==\"InceptionResNetV2\":\n        image_size=320\n    print(\"Image_size: \", image_size)\n    return image_size","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## InceptionResNetV2: 85%"},{"metadata":{"trusted":true},"cell_type":"code","source":"MODEL_NAME = 'InceptionResNetV2'\nIMAGE_SIZE = get_image_size(MODEL_NAME)\nBATCH_SIZE = 32\nWEIGHT_PATH_SAVE = 'best_model_InceptionResNetV2.hdf5'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator, validation_generator = create_data_gen(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"callbacks_list = list(callbacks_func(WEIGHT_PATH_SAVE))","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"base = InceptionResNetV2(include_top=False, weights='imagenet',input_shape=[IMAGE_SIZE,IMAGE_SIZE,3])\n# base.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"layers = base.layers\nprint(f\"The model has {len(layers)} layers\")\n\nprint(f\"The input shape {base.input}\")\nprint(f\"The output shape {base.output}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"    ##### bias_regularizer\n    -----------------------\n    Regularizers allow you to apply penalties on layer parameters or layer activity during optimization. These penalties are summed into the loss function that the network optimizes.\n\n    Regularization penalties are applied on a per-layer basis. The exact API will depend on the layer, but many layers (e.g. Dense, Conv1D, Conv2D and Conv3D) have a unified API.\n\n    These layers expose 3 keyword arguments:\n\n    * kernel_regularizer: Regularizer to apply a penalty on the layer's kernel\n    * bias_regularizer: Regularizer to apply a penalty on the layer's bias\n    * activity_regularizer: Regularizer to apply a penalty on the layer's output\n"},{"metadata":{},"cell_type":"markdown","source":"    ##### dropout\n    --------------\n    Dropout is a regularization technique for neural network models proposed by Srivastava, et al. in their 2014 paper Dropout: A Simple Way to Prevent Neural Networks from Overfitting (download the PDF).\n\n    Dropout is a technique where randomly selected neurons are ignored during training. They are “dropped-out” randomly. This means that their contribution to the activation of downstream neurons is temporally removed on the forward pass and any weight updates are not applied to the neuron on the backward pass."},{"metadata":{},"cell_type":"markdown","source":"    ##### activation function\n    -------------------------\n    In a neural network, numeric data points, called inputs, are fed into the neurons in the input layer. Each neuron has a weight, and multiplying the input number with the weight gives the output of the neuron, which is transferred to the next layer."},{"metadata":{},"cell_type":"markdown","source":"    ##### relu\n    ------------\n    Advantages\n    * Computationally efficient—allows the network to converge very quickly\n    * Non-linear—although it looks like a linear function, ReLU has a derivative function and allows for backpropagation\n    Disadvantages\n    * The Dying ReLU problem—when inputs approach zero, or are negative, the gradient of the function becomes zero, the network cannot perform backpropagation and cannot learn."},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.Sequential()\nmodel.add(base)\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Flatten())\nmodel.add(Dense(1024, activation = 'relu', bias_regularizer=tf.keras.regularizers.L1L2(l1=0.01, l2=0.001)))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(512, activation = 'relu'))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(5, activation='softmax'))\n\nopt=tf.keras.optimizers.Adam(learning_rate=0.0001)\n# opt = tf.keras.optimizers.SGD(learning_rate=1e-6, momentum=0.9)\n\nmodel.compile(loss=tf.keras.losses.CategoricalCrossentropy(),\n              optimizer=opt, \n              metrics=['accuracy'])\n# model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"utils.plot_model(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(train_generator, \n                    epochs=5, \n                    validation_data = validation_generator, \n                    verbose = 1,\n                    callbacks=callbacks_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_history(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Done\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## InceptionV3: 75%"},{"metadata":{"trusted":true},"cell_type":"code","source":"# MODEL_NAME = 'InceptionV3'\n# IMAGE_SIZE = get_image_size(MODEL_NAME)\n# BATCH_SIZE = 16\n# WEIGHT_PATH_SAVE = 'best_model_InceptionV3.hdf5'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train_generator, validation_generator = create_data_gen(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"# callbacks_list = list(callbacks_func(WEIGHT_PATH_SAVE))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# pre_trained_model = InceptionV3(input_shape = (IMAGE_SIZE, IMAGE_SIZE, 3), \n#                                 include_top = False, \n#                                 weights = \"imagenet\")\n\n# # pre_trained_model.load_weights(local_weights_file)\n\n# # Make all the layers in the pre-trained model non-trainable\n# for layer in pre_trained_model.layers:\n#     layer.trainable = False\n\n# pre_trained_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot_model(pre_trained_model, to_file='pre_trained_model.png', show_shapes=True)\n# Image(filename='pre_trained_model.png') ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# layers = pre_trained_model.layers\n# print(f\"The model has {len(layers)} layers\")\n\n# print(f\"The input shape {pre_trained_model.input}\")\n# print(f\"The output shape {pre_trained_model.output}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# last_layer = pre_trained_model.get_layer('mixed7')\n# print('last layer output shape: ', last_layer.output_shape)\n# last_output = last_layer.output","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# x = layers.Flatten()(last_output)              # Flatten the output layer to 1 dimension\n# x = layers.Dense(1024, activation='relu')(x)   # Add a fully connected layer with 1,024 hidden units and ReLU activation\n# x = layers.Dropout(0.2)(x)                     # Add a dropout rate of 0.2              \n# x = layers.Dense(5, activation='softmax')(x)   # Add a final sigmoid layer for classification            \n\n# model = Model( pre_trained_model.input, x) \n\n# model.compile(optimizer = RMSprop(lr=0.0001), \n#               loss = 'categorical_crossentropy', \n#               metrics = ['accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# utils.plot_model(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# history = model.fit(train_generator, epochs=15, \n#                     validation_data = validation_generator, verbose = 1,\n#                     callbacks=callbacks_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot_history(history)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## EffecientNetB4"},{"metadata":{"trusted":true},"cell_type":"code","source":"MODEL_NAME = 'EfficientNetB4'\nIMAGE_SIZE = get_image_size(MODEL_NAME)\nBATCH_SIZE = 16\nWEIGHT_PATH_SAVE = 'best_model_EfficientNetB4.hdf5'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator, validation_generator = create_data_gen(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"callbacks_list = list(callbacks_func(WEIGHT_PATH_SAVE))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Approach 1: Pretrained model and train from speicific layers"},{"metadata":{"trusted":true},"cell_type":"code","source":"# EfficientNetB4_base_model = EfficientNetB4(input_shape = (380, 380, 3), \n#                                 include_top = False, \n#                                 weights = \"imagenet\")\n\n# # pre_trained_model.load_weights(local_weights_file)\n\n# EfficientNetB4_base_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# last_layer = EfficientNetB4_base_model.get_layer(\"block7a_expand_conv\")\n# # last_layer = pre_trained_model\n# print('last layer output shape: ', last_layer.output_shape)\n# last_output = last_layer.output","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# x = layers.GlobalAveragePooling2D()(last_output)              \n# x = layers.Dense(1024, activation='relu')(x) \n# x = layers.Dropout(0.2)(x)                     \n# x = layers.Dense(5, activation='softmax')(x)   \n\n# model = Model( pre_trained_model.input, x) \n# print(model.summary)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot_model(model, to_file='model.png', show_shapes=True)\n# Image(filename='model.png') ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model.compile(optimizer = Adam(lr=1e-4), \n#               loss = 'categorical_crossentropy', \n#               metrics = ['accuracy'])\n\n# history = model.fit(train_generator, epochs=15, \n#                     validation_data = validation_generator, verbose = 1,\n#                     callbacks=callbacks_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot_history(model)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Approach 2: Pretrained model and no changes to any layers"},{"metadata":{"trusted":true},"cell_type":"code","source":"EfficientNetB4_base_model = EfficientNetB4(input_shape = (IMAGE_SIZE, IMAGE_SIZE, 3), \n                                include_top = False, \n                                weights = \"imagenet\")\n\n# pre_trained_model.load_weights(local_weights_file)\n\n# EfficientNetB4_base_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"layers = EfficientNetB4_base_model.layers\nprint(f\"The model has {len(layers)} layers\")\n\nprint(f\"The input shape {EfficientNetB4_base_model.input}\")\nprint(f\"The output shape {EfficientNetB4_base_model.output}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model = Sequential()\n# model.add(pre_trained_model)\n# model.add(GlobalAveragePooling2D())\n# model.add(Dense(512))\n# model.add(BatchNormalization())\n# model.add(Activation(\"relu\"))\n# model.add(Dense(5, activation='softmax'))\n# print(model.summary())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model = tf.keras.Sequential([\n#     EfficientNetB4_base_model,\n#     GlobalAveragePooling2D(),\n#     Dense(512, activation=\"relu\"),  # First Dense layer\n#     BatchNormalization(),\n#     Dropout(0.6),\n#     Dense(128, activation=\"relu\"),  # Second Dense layer\n#     BatchNormalization(),\n#     Dropout(0.4),\n#     Dense(64,activation=\"relu\"),    # Third Dense layer\n#     BatchNormalization(),  \n#     Dropout(0.3),\n#     Dense(5,activation=\"softmax\")\n# ])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.Sequential()\nmodel.add(EfficientNetB4_base_model)\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = 'relu', bias_regularizer=tf.keras.regularizers.L1L2(l1=0.01, l2=0.001)))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(5, activation='softmax'))\n\nopt=tf.keras.optimizers.Adam(learning_rate=0.0001)\n# opt = tf.keras.optimizers.SGD(learning_rate=1e-6, momentum=0.9)\n\nmodel.compile(loss=tf.keras.losses.CategoricalCrossentropy(),\n              optimizer=opt, \n              metrics=['acc'])\n# model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"utils.plot_model(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot_model(EfficientNetB4_model, to_file='model.png', show_shapes=True)\n# Image(filename='model.png') ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model.compile(optimizer = opt, loss = 'categorical_crossentropy', metrics = ['acc'])\n\nhistory = model.fit(train_generator, \n                    epochs=5,\n                    validation_data = validation_generator,\n                    verbose = 1,\n                    callbacks=callbacks_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_history(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Done\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## CNN model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(300, 300, 3)),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Dropout(0.5),\n    \n    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),       # The second convolution\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.5),\n    \n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),      # The third convolution\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.5),\n    \n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),      # The fourth convolution\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.5),\n    \n    tf.keras.layers.Flatten(),                                  # Flatten the results to feed into a DNN\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(512, activation='relu'),              # 512 neuron hidden layer\n    tf.keras.layers.Dense(5, activation='softmax')\n])\n\n\n# model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot_model(model, to_file='model.png', show_shapes=True)\n# Image(filename='model.png') ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"utils.plot_model(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"MODEL_NAME = 'CNN_Model'\nIMAGE_SIZE = 300\nBATCH_SIZE = 16\n# WEIGHT_PATH_SAVE = './best_model_cnn_model.hdf5'\nWEIGHT_PATH_SAVE = 'best_model_cnn_model.hdf5'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator, validation_generator = create_data_gen(image_size=IMAGE_SIZE, batch_size=BATCH_SIZE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# weight_path_save = 'best_model_CNN.hdf5'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss = 'categorical_crossentropy', optimizer='Adam', metrics=['acc'])\n\nhistory = model.fit(train_generator, \n                    epochs=5, \n                    validation_data = validation_generator, \n                    verbose = 1,\n                    callbacks=callbacks_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_model(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Model Evaluation"},{"metadata":{"trusted":true},"cell_type":"code","source":"# weight_path_save = 'best_model_cnn_model.hdf5'\n# model.load_weights(weight_path_save)\n\n# pred_valid_y = model.predict(validation_generator, verbose = 2)\n# preds_labels = np.argmax(pred_valid_y, axis=-1)\n# valid_labels=validation_generator.labels\n\n# print(classification_report(valid_labels, preds_labels ))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# print(confusion_matrix(valid_labels, preds_labels ))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}