{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_size = 380\nbatch_size = 32","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"basePath = '/kaggle/input/cassava-leaf-disease-classification/'\ntrainImagesPath = basePath + 'train_images/'\ntestImagesPath = basePath + 'test_images/'\ntrain_info = pd.read_csv(basePath + 'train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainingSize = train_info.shape\nprint('The number of training examples are: ',trainingSize[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\nsns.countplot(train_info['label'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"It is obvious that the data is not distributed uniformly in the training set with more than half of the training images in class label 3. But the metric for the model is set to be categorization accuracy. Will it make sense?"},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB4\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.layers.experimental import preprocessing\nfrom tensorflow.keras.models import Sequential\nfrom keras.preprocessing.image import ImageDataGenerator,array_to_img,img_to_array,load_img\nimport tensorflow as tf\nfrom keras.callbacks import EarlyStopping,ModelCheckpoint,ReduceLROnPlateau\nfrom tensorflow.keras.mixed_precision import experimental as mixed_precision\npolicy = tf.keras.mixed_precision.experimental.Policy('mixed_float16')\nmixed_precision.set_policy(policy)\n\ndef buildModel(IMG_SIZE,NUM_CLASSES):\n    inputs = layers.Input(shape=(IMG_SIZE, IMG_SIZE, 3))\n    model = EfficientNetB4(include_top=False, input_tensor=inputs, weights=\"imagenet\")\n    #model.trainable = False\n\n    # Rebuild top\n    x = layers.GlobalAveragePooling2D(name=\"avg_pool\")(model.output)\n    x = layers.BatchNormalization()(x)\n\n    top_dropout_rate = 0.2\n    x = layers.Dropout(top_dropout_rate, name=\"top_dropout\")(x)\n    outputs = layers.Dense(NUM_CLASSES, activation=\"softmax\",dtype=\"float32\",name=\"pred\")(x)\n    \n    model = tf.keras.Model(inputs, outputs, name=\"EfficientNet\")\n    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4)\n    model.compile(optimizer=optimizer, loss=\"sparse_categorical_crossentropy\", metrics=[\"accuracy\"])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef plot_hist(hist):\n    plt.figure()\n    plt.plot(hist.history[\"accuracy\"])\n    plt.plot(hist.history[\"val_accuracy\"])\n    plt.title(\"model accuracy\")\n    plt.ylabel(\"accuracy\")\n    plt.xlabel(\"epoch\")\n    plt.legend([\"train\", \"validation\"], loc=\"upper left\")\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\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        fill_mode='nearest')\n\nval_datagen = ImageDataGenerator(\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        fill_mode='nearest')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_info['label'] = train_info['label'].astype('str')\ntrain_df, val_df = train_test_split(train_info, test_size=0.1, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(\n    train_df,directory=trainImagesPath,\n    x_col=\"image_id\",y_col=\"label\",\n    target_size=(img_size, img_size), \n    batch_size=batch_size,class_mode = \"sparse\"\n    ) \nval_generator = val_datagen.flow_from_dataframe(\n    val_df,directory=trainImagesPath,\n    x_col=\"image_id\",y_col=\"label\",\n    target_size=(img_size, img_size),\n    batch_size=batch_size,class_mode = \"sparse\"\n    )     \nmodel = buildModel(img_size,5)\nearlyStopping = EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='min')\nreduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.8, patience=2, verbose=1, mode='auto', epsilon=0.0001, cooldown=5, min_lr=0.00001)\nmcp_save = ModelCheckpoint('best_mdl.h5', save_best_only=True, monitor='val_loss', mode='min')\nhist = model.fit_generator(\n    train_generator,\n    steps_per_epoch=train_df.shape[0]  // batch_size,\n    epochs=20,\n    validation_data=val_generator,\n    validation_steps=val_df.shape[0]  // batch_size,\n    callbacks=[earlyStopping, mcp_save,reduceLROnPlat])\nplot_hist(hist)","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}