{"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":"code","source":"import os\n\nimport numpy as np \nimport pandas as pd \nimport json\n\nimport cv2\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom tensorflow.keras.utils import to_categorical # convert to one-hot-encoding\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\n\nfrom sklearn.model_selection import train_test_split\n\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\n\ntrain_image_path = \"/kaggle/input/cassava-leaf-disease-classification/train_images/\"\ntest_image_path = \"/kaggle/input/cassava-leaf-disease-classification/test_images/\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-22T17:24:42.566569Z","iopub.execute_input":"2021-09-22T17:24:42.566881Z","iopub.status.idle":"2021-09-22T17:24:47.798015Z","shell.execute_reply.started":"2021-09-22T17:24:42.566839Z","shell.execute_reply":"2021-09-22T17:24:47.797213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf = pd.read_csv(\"../input/cassava-leaf-disease-classification/train.csv\")\ntraindf.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:24:53.786219Z","iopub.execute_input":"2021-09-22T17:24:53.786552Z","iopub.status.idle":"2021-09-22T17:24:53.834497Z","shell.execute_reply.started":"2021-09-22T17:24:53.786519Z","shell.execute_reply":"2021-09-22T17:24:53.833780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:25:04.102069Z","iopub.execute_input":"2021-09-22T17:25:04.102386Z","iopub.status.idle":"2021-09-22T17:25:04.107987Z","shell.execute_reply.started":"2021-09-22T17:25:04.102357Z","shell.execute_reply":"2021-09-22T17:25:04.106959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.gcf()\nfig.set_size_inches(16, 16)\n\nnext_pix = traindf['image_id'][0:12]\n\nfor i, img_path in enumerate(next_pix):\n    \n    sp = plt.subplot(4, 4, i + 1)\n    sp.axis('Off')\n\n    img = mpimg.imread(train_image_path + img_path)\n    plt.imshow(img)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:25:11.259392Z","iopub.execute_input":"2021-09-22T17:25:11.260252Z","iopub.status.idle":"2021-09-22T17:25:12.683280Z","shell.execute_reply.started":"2021-09-22T17:25:11.260114Z","shell.execute_reply":"2021-09-22T17:25:12.682199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Referecnce : https://www.kaggle.com/bulentsiyah/learn-opencv-by-examples-with-python","metadata":{}},{"cell_type":"markdown","source":"# Sharpening Image","metadata":{}},{"cell_type":"code","source":"fig = plt.gcf()\nfig.set_size_inches(16, 16)\n\nnext_pix = traindf['image_id'][0:12]\n\nfor i, img_path in enumerate(next_pix):\n    \n    sp = plt.subplot(4, 4, i + 1)\n    sp.axis('Off')\n    \n    img = cv2.imread(train_image_path + img_path,1)\n    image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    \n    # Create our shapening kernel, we don't normalize since the \n  \n    kernel_sharpening = np.array([[1,2,3], \n                                  [4,-33,5], \n                                  [6,7,8]])\n\n    # applying different kernels to the input image\n    sharpened = cv2.filter2D(image, -1, kernel_sharpening)\n\n    plt.imshow(sharpened)\n\nplt.show()\n\n\n","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:25:24.141583Z","iopub.execute_input":"2021-09-22T17:25:24.141914Z","iopub.status.idle":"2021-09-22T17:25:25.250423Z","shell.execute_reply.started":"2021-09-22T17:25:24.141865Z","shell.execute_reply":"2021-09-22T17:25:25.242208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Thresholding Image","metadata":{}},{"cell_type":"code","source":"fig = plt.gcf()\nfig.set_size_inches(16, 16)\n\nnext_pix = traindf['image_id'][0:12]\n\nfor i, img_path in enumerate(next_pix):\n    \n    sp = plt.subplot(4, 4, i + 1)\n    sp.axis('Off')\n    \n    img = cv2.imread(train_image_path + img_path,0)\n    \n    # Values below 100 goes to 0 (black), everything above goes to 200 (white)\n    ret,thresh1 = cv2.threshold(img, 100, 200, cv2.THRESH_BINARY)\n\n\n    plt.imshow(thresh1)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:25:34.746825Z","iopub.execute_input":"2021-09-22T17:25:34.747196Z","iopub.status.idle":"2021-09-22T17:25:35.708039Z","shell.execute_reply.started":"2021-09-22T17:25:34.747163Z","shell.execute_reply":"2021-09-22T17:25:35.707224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Binarization ","metadata":{}},{"cell_type":"code","source":"fig = plt.gcf()\nfig.set_size_inches(16, 16)\n\nnext_pix = traindf['image_id'][0:12]\n\nfor i, img_path in enumerate(next_pix):\n    \n    sp = plt.subplot(4, 4, i + 1)\n    sp.axis('Off')\n    \n    img = cv2.imread(train_image_path + img_path,0)\n    \n    # It's good practice to blur images as it removes noise\n    image = cv2.GaussianBlur(img, (3,3), 0)\n\n\n\n    plt.imshow(image)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:25:46.445265Z","iopub.execute_input":"2021-09-22T17:25:46.445623Z","iopub.status.idle":"2021-09-22T17:25:47.394529Z","shell.execute_reply.started":"2021-09-22T17:25:46.445589Z","shell.execute_reply":"2021-09-22T17:25:47.390510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Adaptive Thresholding","metadata":{}},{"cell_type":"code","source":"fig = plt.gcf()\nfig.set_size_inches(16, 16)\n\nnext_pix = traindf['image_id'][0:12]\n\nfor i, img_path in enumerate(next_pix):\n    \n    sp = plt.subplot(4, 4, i + 1)\n    sp.axis('Off')\n    \n    img = cv2.imread(train_image_path + img_path,0)\n    \n    # It's good practice to blur images as it removes noise\n    image = cv2.GaussianBlur(img, (3,3), 0)\n\n    # Using adaptiveThreshold\n    thresh = cv2.adaptiveThreshold(image, 200, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 3)\n\n    plt.imshow(thresh)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:25:55.759590Z","iopub.execute_input":"2021-09-22T17:25:55.759932Z","iopub.status.idle":"2021-09-22T17:25:56.643710Z","shell.execute_reply.started":"2021-09-22T17:25:55.759890Z","shell.execute_reply":"2021-09-22T17:25:56.642933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.gcf()\nfig.set_size_inches(16, 16)\n\nnext_pix = traindf['image_id'][0:12]\n\nfor i, img_path in enumerate(next_pix):\n    \n    sp = plt.subplot(4, 4, i + 1)\n    sp.axis('Off')\n    \n    img = cv2.imread(train_image_path + img_path,0)\n    \n    # It's good practice to blur images as it removes noise\n    image = cv2.GaussianBlur(img, (3,3), 0)\n\n    _, th2 = cv2.threshold(image, 0, 200, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n\n    plt.imshow(th2)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:26:03.085353Z","iopub.execute_input":"2021-09-22T17:26:03.085688Z","iopub.status.idle":"2021-09-22T17:26:03.919799Z","shell.execute_reply.started":"2021-09-22T17:26:03.085656Z","shell.execute_reply":"2021-09-22T17:26:03.918736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.gcf()\nfig.set_size_inches(16, 16)\n\nnext_pix = traindf['image_id'][0:12]\n\nfor i, img_path in enumerate(next_pix):\n    \n    sp = plt.subplot(4, 4, i + 1)\n    sp.axis('Off')\n    \n    img = cv2.imread(train_image_path + img_path,0)\n    \n    # Otsu's thresholding after Gaussian filtering\n    blur = cv2.GaussianBlur(img, (5,5), 0)\n    \n    _, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n    plt.imshow(th3)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:26:11.026542Z","iopub.execute_input":"2021-09-22T17:26:11.026867Z","iopub.status.idle":"2021-09-22T17:26:12.013706Z","shell.execute_reply.started":"2021-09-22T17:26:11.026836Z","shell.execute_reply":"2021-09-22T17:26:12.012757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Background Removal","metadata":{}},{"cell_type":"code","source":"f = open(\"../input/cassava-leaf-disease-classification/label_num_to_disease_map.json\",\"r\")\ndata = json.load(f)\ndata['0']","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:26:26.218553Z","iopub.execute_input":"2021-09-22T17:26:26.218907Z","iopub.status.idle":"2021-09-22T17:26:26.231651Z","shell.execute_reply.started":"2021-09-22T17:26:26.218857Z","shell.execute_reply":"2021-09-22T17:26:26.230963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_data = []\nY_data = []\n\nfor i in range(0,600):\n    img = load_img(train_image_path + traindf['image_id'][i])\n    X_data.append(img_to_array(img))\n    Y_data.append(traindf['label'][i])","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:26:34.515532Z","iopub.execute_input":"2021-09-22T17:26:34.515860Z","iopub.status.idle":"2021-09-22T17:26:44.448018Z","shell.execute_reply.started":"2021-09-22T17:26:34.515829Z","shell.execute_reply":"2021-09-22T17:26:44.447024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_data = np.array(X_data)","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:26:46.074786Z","iopub.execute_input":"2021-09-22T17:26:46.075162Z","iopub.status.idle":"2021-09-22T17:26:47.103063Z","shell.execute_reply.started":"2021-09-22T17:26:46.075129Z","shell.execute_reply":"2021-09-22T17:26:47.102240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Encode labels to one hot vectors (ex : 2 -> [0,0,1,0,0])\nY_data = to_categorical(Y_data, num_classes = 5)\nY_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:26:51.674979Z","iopub.execute_input":"2021-09-22T17:26:51.675308Z","iopub.status.idle":"2021-09-22T17:26:51.681129Z","shell.execute_reply.started":"2021-09-22T17:26:51.675277Z","shell.execute_reply":"2021-09-22T17:26:51.680174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the train and the validation set for the fitting\nX_train, X_val, Y_train, Y_val = train_test_split(X_data, Y_data, test_size = 0.2, random_state=45)","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:26:55.864031Z","iopub.execute_input":"2021-09-22T17:26:55.864368Z","iopub.status.idle":"2021-09-22T17:26:56.825539Z","shell.execute_reply.started":"2021-09-22T17:26:55.864336Z","shell.execute_reply":"2021-09-22T17:26:56.824562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_val.shape)\nprint(Y_train.shape)\nprint(Y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:26:58.551586Z","iopub.execute_input":"2021-09-22T17:26:58.551932Z","iopub.status.idle":"2021-09-22T17:26:58.561218Z","shell.execute_reply.started":"2021-09-22T17:26:58.551898Z","shell.execute_reply":"2021-09-22T17:26:58.560470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (600,800,3)))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(64, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(5, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:27:04.823821Z","iopub.execute_input":"2021-09-22T17:27:04.824165Z","iopub.status.idle":"2021-09-22T17:27:07.049431Z","shell.execute_reply.started":"2021-09-22T17:27:04.824133Z","shell.execute_reply":"2021-09-22T17:27:07.048583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer = 'adam' , loss = \"categorical_crossentropy\", metrics=[\"acc\"])","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:27:10.592794Z","iopub.execute_input":"2021-09-22T17:27:10.593134Z","iopub.status.idle":"2021-09-22T17:27:10.610371Z","shell.execute_reply.started":"2021-09-22T17:27:10.593102Z","shell.execute_reply":"2021-09-22T17:27:10.609376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 50\nbatch_size = 8","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:27:16.831556Z","iopub.execute_input":"2021-09-22T17:27:16.832016Z","iopub.status.idle":"2021-09-22T17:27:16.839022Z","shell.execute_reply.started":"2021-09-22T17:27:16.831967Z","shell.execute_reply":"2021-09-22T17:27:16.838085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#history = model.fit(X_train, Y_train, batch_size = batch_size, epochs = epochs, \n#          validation_data = (X_val, Y_val), verbose = 2)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T02:54:53.786681Z","iopub.execute_input":"2021-09-17T02:54:53.787414Z","iopub.status.idle":"2021-09-17T02:54:53.793797Z","shell.execute_reply.started":"2021-09-17T02:54:53.787367Z","shell.execute_reply":"2021-09-17T02:54:53.792586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n        rescale=1./255,\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # apply ZCA whitening\n        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.1, # Randomly zoom image \n        shear_range=0.1,\n        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False,\n        fill_mode='nearest')  # randomly flip images\n\n\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:27:28.637787Z","iopub.execute_input":"2021-09-22T17:27:28.638166Z","iopub.status.idle":"2021-09-22T17:27:29.968358Z","shell.execute_reply.started":"2021-09-22T17:27:28.638134Z","shell.execute_reply":"2021-09-22T17:27:29.967559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nhistory = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),\n                              epochs = epochs, \n                              validation_data = (X_val,Y_val),\n                              verbose = 1, \n                              steps_per_epoch=4)","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:27:37.440715Z","iopub.execute_input":"2021-09-22T17:27:37.441082Z","iopub.status.idle":"2021-09-22T17:31:48.985955Z","shell.execute_reply.started":"2021-09-22T17:27:37.441049Z","shell.execute_reply":"2021-09-22T17:31:48.985035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\n\nplt.figure()\n\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-22T17:34:52.223143Z","iopub.execute_input":"2021-09-22T17:34:52.223534Z","iopub.status.idle":"2021-09-22T17:34:52.523040Z","shell.execute_reply.started":"2021-09-22T17:34:52.223499Z","shell.execute_reply":"2021-09-22T17:34:52.522068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}