{"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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt \n\nfrom glob import glob \n\nfrom keras.utils import np_utils\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Dropout,Flatten\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import BatchNormalization\n#from keras.callbacks import (ModelCheckpoint,ReduceLROnPlateau,CSVLogger)\nfrom sklearn import preprocessing\nfrom tensorflow.keras.initializers import glorot_uniform\nfrom sklearn.model_selection import train_test_split\nimport cv2\nfrom tensorflow.python.keras import *\nimport copy\nimport numpy as np \nimport pandas as pd\n\nimport tensorflow as tf\nfrom keras import layers\nfrom keras import models\nfrom keras import optimizers\n\nfrom keras.preprocessing.image import img_to_array, load_img\nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D,GlobalMaxPool2D,ZeroPadding2D,Activation,MaxPool2D\nfrom keras.layers. normalization import BatchNormalization\nfrom keras.optimizers import Adam\nfrom tensorflow.keras.utils import plot_model\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint, LearningRateScheduler\nfrom IPython.display import display\nfrom tensorflow.keras import Input\nfrom tensorflow.python.keras import *\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import layers, optimizers\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras import backend as K\nfrom keras import optimizers","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_DATA_DIR = '/kaggle/input/cassava-leaf-disease-classification/train_images/*.jpg'\nTEST_DATA_DIR = '/kaggle/input/cassava-leaf-disease-classification/train_images/*.jpg'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\n\nCassava_dir = \"../input/cassava-leaf-disease-classification/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(os.path.join(Cassava_dir, \"train.csv\"))\ntrain_df['label'] = train_df['label'].astype('str')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TARGET_SIZE = (320,320)\nBATCH_SIZE = 64\nSTEPS_PER_EPOCH = len(train_df)*0.8 // BATCH_SIZE\nVALIDATION_STEPS = len(train_df)*0.2 // BATCH_SIZE\nEPOCHS = 20","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(validation_split = 0.2,\n                                    rotation_range = 45,\n                                    zoom_range = 0.2,\n                                    horizontal_flip = True,\n                                    fill_mode = 'nearest',\n                                    height_shift_range = 0.2,\n                                    width_shift_range = 0.2,\n                                    rescale=1./255\n                              )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntrain_generator = train_datagen.flow_from_dataframe(train_df,\n                         directory = os.path.join(Cassava_dir, \"train_images\"),\n                         subset = \"training\",\n                         x_col = \"image_id\",\n                         y_col = \"label\",\n                         target_size = TARGET_SIZE,\n                         batch_size = BATCH_SIZE,\n                         class_mode = \"sparse\",\n                         seed = 2020,\n                         shuffle= True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"validation_datagen=ImageDataGenerator(validation_split = 0.2,rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(train_df,\n                         directory = os.path.join(Cassava_dir, \"train_images\"),\n                         subset = \"validation\",\n                         x_col = \"image_id\",\n                         y_col = \"label\",\n                         target_size = TARGET_SIZE,\n                         batch_size = BATCH_SIZE,\n                         class_mode = \"sparse\",\n                         seed = 2020,\n                         shuffle= True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_names=train_df['label'].nunique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_names","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\ntf.keras.backend.clear_session()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tf.keras.backend.clear_session()\n#initialising sequential model\nmodel = Sequential()\n#adding 1st convolution layer with 32 filter and imput shape 320 x320x3 with relu function\nmodel.add(Conv2D(filters=128, kernel_size=(5, 5), input_shape=(320, 320, 3), activation='relu'))\n#adding 2nd convolution layer with 32 filters\nmodel.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))\nmodel.add(Conv2D(filters=32, kernel_size=(5, 5), activation='relu'))\n#maxpooling\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.2))\n#adding 3rd convolution layer with 64 filters\nmodel.add(Conv2D(filters=128, kernel_size=(3, 3), activation='relu'))\n#normalising batch\n#adding 4th convolution layer with 32 filters\nmodel.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))\nmodel.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu'))\n#maxpooling\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.2))\n#flattening layer\nmodel.add(Flatten())\n#Dense layer\n#adding 1 st dense layer\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dropout(0.1))\nmodel.add(Dense(256, activation='relu'))\n#adding dense layer with same output as no of cateogries, in our case 5 category with softmax function\nmodel.add(Dense(5, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(model.layers) #number of layers","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.output","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Compile the modelac\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"keras.utils.plot_model(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Saving the best model using model checkpoint callback\nmodel_checkpoint=tf.keras.callbacks.ModelCheckpoint('leaf_resnet.h5', \n                                                    save_best_only=True, \n                                                    monitor='val_accuracy', \n                                                    mode='max', \n                                                    verbose=1)\nes = tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', patience=3,\n                       restore_best_weights=True, verbose=1)\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_loss',\n                                  factor = 0.2,\n                                  patience = 2,\n                                  min_lr = 1e-6,\n                                  mode = 'min',\n                                  verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"history=model.fit(train_generator,\n                epochs=EPOCHS,\n                steps_per_epoch= 17118//BATCH_SIZE,\n                validation_data=validation_generator,\n                validation_steps = 4279//BATCH_SIZE,\n                callbacks=[es,model_checkpoint,reduce_lr])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history=model.fit(train_generator,\n                epochs=40,\n                initial_epoch=20,\n                steps_per_epoch= 17118//BATCH_SIZE,\n                validation_data=validation_generator,\n                validation_steps = 4279//BATCH_SIZE,\n                callbacks=[es,model_checkpoint,reduce_lr])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image\nTEST_DIR = '../input/cassava-leaf-disease-classification/test_images/'\ntest_images = os.listdir(TEST_DIR)\npredictions = []\ntest_images\nfor image in test_images:\n    img = Image.open(TEST_DIR + image)\n    img = img.resize(TARGET_SIZE)   \n    img = np.expand_dims(img, axis=0)\n    img = img/255\n    predictions.extend(model.predict(img).argmax(axis = 1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.DataFrame({'image_id': test_images, 'label': predictions})\ndisplay(sub)\nsub.to_csv('submission.csv', index = False)","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}