{"cells":[{"metadata":{},"cell_type":"markdown","source":"<center><h1 style= 'color: green'> Cassava Leaf Disease classifier (tensorflow)</h1></center>"},{"metadata":{},"cell_type":"markdown","source":"## Import  Library"},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport glob\nimport shutil\nimport json\nimport keras\nimport itertools\nfrom collections import Counter\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom keras.applications import VGG19\nfrom keras.models import Model\nimport tensorflow as tf\nfrom sklearn.metrics import confusion_matrix","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Working directory"},{"metadata":{"trusted":true},"cell_type":"code","source":"main_dir = '../input/cassava-leaf-disease-classification/'\nos.listdir(main_dir) \ntrain_img_path = '../input/cassava-leaf-disease-classification/train_images'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### CSV and JSON files"},{"metadata":{"trusted":true},"cell_type":"code","source":"data = pd.read_csv(main_dir+'train.csv')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\njs = open(main_dir + 'label_num_to_disease_map.json')\nreal_classes = json.load(js)\nreal_classes = {int(k):v for k,v in real_classes.items()}\n\ndata['class_name'] = data.label.map(real_classes)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Train and validation split data"},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\ntrain,val = train_test_split(data, test_size = 0.1, random_state = 42, stratify = data['class_name'])\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data agumentation\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"\nimg_row= 400\nimg_col=400\n\ntrain_datagen = ImageDataGenerator(rescale = 1/255.0,\n                            rotation_range = 40,\n                            width_shift_range = 0.4,\n                            height_shift_range = 0.4,\n                            shear_range = 0.2,\n                            zoom_range = 0.2,\n                            horizontal_flip = True,\n                            vertical_flip = True,\n                            fill_mode = 'nearest')\n\nvalidation_datagen = ImageDataGenerator(rescale=1.0/255)\n\ntrain_generator = train_datagen.flow_from_dataframe(train,\n                                                directory = train_img_path,\n                                                x_col = 'image_id',\n                                                y_col = 'class_name',\n                                                target_size = (img_row,img_col),\n                                                color_mode = 'rgb',\n                                                class_mode = 'categorical',\n                                                interpolation = 'nearest',\n                                                shuffle = True,\n                                                batch_size = 64, \n                                                )\n\n\nvalidation_generator = validation_datagen.flow_from_dataframe(val,\n                                                directory = train_img_path,\n                                                x_col = 'image_id',\n                                                y_col = 'class_name',\n                                                target_size = (img_row,img_col),\n                                                color_mode = 'rgb',\n                                                class_mode = 'categorical',\n                                                interpolation = 'nearest',\n                                                shuffle = True,\n                                                batch_size = 64, \n                                                )\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model"},{"metadata":{},"cell_type":"markdown","source":"## VGG19 model"},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.applications import VGG19\nvgg19 = VGG19(weights = 'imagenet',\n             include_top = False,\n             input_shape = (img_row, img_col,3))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Freeze layer"},{"metadata":{"trusted":true},"cell_type":"code","source":"for layer in vgg19.layers:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Create top model"},{"metadata":{"trusted":true},"cell_type":"code","source":"def topmodel(bottom_model, num_classes):\n    top_model = bottom_model.output\n    top_model = tf.keras.layers.Flatten(name='flatten')(top_model)\n    top_model = tf.keras.layers.Dense(512, activation='relu')(top_model)\n    top_model = tf.keras.layers.Dense(1024, activation = 'relu')(top_model)\n    top_model = tf.keras.layers.Dense(512, activation = 'relu')(top_model)\n    top_model= tf.keras.layers.Dense(num_classes, activation='softmax')(top_model)\n    return top_model","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Last part of model creation"},{"metadata":{"trusted":true},"cell_type":"code","source":"FC_head = topmodel(vgg19, 5),\nmodel = Model(inputs=vgg19.input, outputs=FC_head)\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### compile model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer = 'adam',\n             loss = 'categorical_crossentropy',\n             metrics = ['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"callbacks function"},{"metadata":{"trusted":true},"cell_type":"code","source":"    earlystop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', \n                                                 mode='min',\n                                                 patience=2,\n                                                 restore_best_weights=True,\n                                                 verbose=1)\n    \n    checkpoint = tf.keras.callbacks.ModelCheckpoint(\"Cassava_model.h5\",\n                                    save_best_only=True,\n                                    monitor = 'val_loss',\n                                    mode='min')\n    \n    re_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_loss',\n                                  factor = 0.3,\n                                  patience = 2,\n                                  min_lr = 1e-6,\n                                  mode = 'min',\n                                  verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Model fitting"},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(train_generator,\n                   epochs = 3,\n                   steps_per_epoch= 301,\n                   verbose = 1,\n                   \n                   validation_data = validation_generator,\n                   validation_steps =34 ,\n                   callbacks = [earlystop,checkpoint,re_lr],\n                   )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history.history","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Accuracy graph of the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"history_dict = history.history\n\nacc = history_dict['accuracy']\nval_acc = history_dict['val_accuracy']\nepochs = range(1, len(acc) + 1)\nline1 = plt.plot(epochs, acc, label='train_Accuracy', color='red')\nline2 = plt.plot(epochs, val_acc, label='Val_acuuracy',color='green')\n\nplt.title('Accuracy ~ Epochs graph', fontsize=20)\nplt.xlabel('Epochs') \nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Loss graph of the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"history_dict = history.history\n\nloss = history_dict['loss']\nval_loss = history_dict['val_loss']\nepochs = range(1, len(acc) + 1)\nline1 = plt.plot(epochs, loss, label='train_Accuracy', color='red')\nline2 = plt.plot(epochs, val_loss, label='Val_acuuracy',color='green')\n\nplt.title('Loss ~ Epochs graph', fontsize=20)\nplt.xlabel('Epochs') \nplt.ylabel('Loss')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Test Part"},{"metadata":{},"cell_type":"markdown","source":"### Load model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = keras.models.load_model('Cassava_model.h5')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Load test image predict"},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image\nTEST_DIR = '../input/cassava-leaf-disease-classification/test_images/'\ntest_imgs = os.listdir(TEST_DIR)\npredictions = []\n\nfor image in test_imgs:\n    img = Image.open(TEST_DIR + image)\n    img = img.resize((400,400))\n    img = np.expand_dims(img, axis=0)\n    pred = model.predict(img).argmax(axis = 1)[0]\n    predictions.append(pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(predictions)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Make submission.csv file"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({'image_id': test_imgs, 'label': predictions})\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('submission.csv', index = False)","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}