{"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-17T02:54:29.095512Z","iopub.execute_input":"2021-09-17T02:54:29.095911Z","iopub.status.idle":"2021-09-17T02:54:34.243402Z","shell.execute_reply.started":"2021-09-17T02:54:29.095879Z","shell.execute_reply":"2021-09-17T02:54:34.242570Z"},"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-17T02:54:34.247342Z","iopub.execute_input":"2021-09-17T02:54:34.247653Z","iopub.status.idle":"2021-09-17T02:54:34.292192Z","shell.execute_reply.started":"2021-09-17T02:54:34.247624Z","shell.execute_reply":"2021-09-17T02:54:34.291346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-17T02:54:34.293402Z","iopub.execute_input":"2021-09-17T02:54:34.293797Z","iopub.status.idle":"2021-09-17T02:54:34.302723Z","shell.execute_reply.started":"2021-09-17T02:54:34.293758Z","shell.execute_reply":"2021-09-17T02:54:34.301913Z"},"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-17T02:54:34.305320Z","iopub.execute_input":"2021-09-17T02:54:34.307723Z","iopub.status.idle":"2021-09-17T02:54:35.568628Z","shell.execute_reply.started":"2021-09-17T02:54:34.307693Z","shell.execute_reply":"2021-09-17T02:54:35.567648Z"},"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-17T02:54:35.572400Z","iopub.execute_input":"2021-09-17T02:54:35.572843Z","iopub.status.idle":"2021-09-17T02:54:36.834130Z","shell.execute_reply.started":"2021-09-17T02:54:35.572796Z","shell.execute_reply":"2021-09-17T02:54:36.828619Z"},"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-17T02:54:36.836901Z","iopub.execute_input":"2021-09-17T02:54:36.837298Z","iopub.status.idle":"2021-09-17T02:54:37.831297Z","shell.execute_reply.started":"2021-09-17T02:54:36.837259Z","shell.execute_reply":"2021-09-17T02:54:37.830418Z"},"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-17T02:54:37.832937Z","iopub.execute_input":"2021-09-17T02:54:37.833285Z","iopub.status.idle":"2021-09-17T02:54:38.785645Z","shell.execute_reply.started":"2021-09-17T02:54:37.833250Z","shell.execute_reply":"2021-09-17T02:54:38.784557Z"},"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-17T02:54:38.787655Z","iopub.execute_input":"2021-09-17T02:54:38.788036Z","iopub.status.idle":"2021-09-17T02:54:39.678034Z","shell.execute_reply.started":"2021-09-17T02:54:38.787999Z","shell.execute_reply":"2021-09-17T02:54:39.671003Z"},"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-17T02:54:39.679857Z","iopub.execute_input":"2021-09-17T02:54:39.680226Z","iopub.status.idle":"2021-09-17T02:54:40.517302Z","shell.execute_reply.started":"2021-09-17T02:54:39.680190Z","shell.execute_reply":"2021-09-17T02:54:40.516505Z"},"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-17T02:54:40.518493Z","iopub.execute_input":"2021-09-17T02:54:40.519039Z","iopub.status.idle":"2021-09-17T02:54:41.530534Z","shell.execute_reply.started":"2021-09-17T02:54:40.518982Z","shell.execute_reply":"2021-09-17T02:54:41.529714Z"},"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-17T02:54:41.531708Z","iopub.execute_input":"2021-09-17T02:54:41.532196Z","iopub.status.idle":"2021-09-17T02:54:41.545038Z","shell.execute_reply.started":"2021-09-17T02:54:41.532155Z","shell.execute_reply":"2021-09-17T02:54:41.544045Z"},"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-17T02:54:41.546206Z","iopub.execute_input":"2021-09-17T02:54:41.546713Z","iopub.status.idle":"2021-09-17T02:54:49.612016Z","shell.execute_reply.started":"2021-09-17T02:54:41.546671Z","shell.execute_reply":"2021-09-17T02:54:49.611190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_data = np.array(X_data)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T02:54:49.613262Z","iopub.execute_input":"2021-09-17T02:54:49.613652Z","iopub.status.idle":"2021-09-17T02:54:50.645822Z","shell.execute_reply.started":"2021-09-17T02:54:49.613612Z","shell.execute_reply":"2021-09-17T02:54:50.644610Z"},"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-17T02:54:50.647335Z","iopub.execute_input":"2021-09-17T02:54:50.647865Z","iopub.status.idle":"2021-09-17T02:54:50.654638Z","shell.execute_reply.started":"2021-09-17T02:54:50.647816Z","shell.execute_reply":"2021-09-17T02:54:50.653626Z"},"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-17T02:54:50.656492Z","iopub.execute_input":"2021-09-17T02:54:50.656895Z","iopub.status.idle":"2021-09-17T02:54:51.644342Z","shell.execute_reply.started":"2021-09-17T02:54:50.656856Z","shell.execute_reply":"2021-09-17T02:54:51.643526Z"},"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-17T02:54:51.645987Z","iopub.execute_input":"2021-09-17T02:54:51.646378Z","iopub.status.idle":"2021-09-17T02:54:51.652925Z","shell.execute_reply.started":"2021-09-17T02:54:51.646335Z","shell.execute_reply":"2021-09-17T02:54:51.651587Z"},"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-17T02:54:51.654697Z","iopub.execute_input":"2021-09-17T02:54:51.655306Z","iopub.status.idle":"2021-09-17T02:54:53.760202Z","shell.execute_reply.started":"2021-09-17T02:54:51.655268Z","shell.execute_reply":"2021-09-17T02:54:53.759423Z"},"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-17T02:54:53.761707Z","iopub.execute_input":"2021-09-17T02:54:53.762078Z","iopub.status.idle":"2021-09-17T02:54:53.777921Z","shell.execute_reply.started":"2021-09-17T02:54:53.762040Z","shell.execute_reply":"2021-09-17T02:54:53.776906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 50\nbatch_size = 8","metadata":{"execution":{"iopub.status.busy":"2021-09-17T02:54:53.779545Z","iopub.execute_input":"2021-09-17T02:54:53.779951Z","iopub.status.idle":"2021-09-17T02:54:53.784546Z","shell.execute_reply.started":"2021-09-17T02:54:53.779902Z","shell.execute_reply":"2021-09-17T02:54:53.783586Z"},"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-17T02:54:53.796467Z","iopub.execute_input":"2021-09-17T02:54:53.796966Z","iopub.status.idle":"2021-09-17T02:54:55.025351Z","shell.execute_reply.started":"2021-09-17T02:54:53.796925Z","shell.execute_reply":"2021-09-17T02:54:55.024377Z"},"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-17T02:54:55.026762Z","iopub.execute_input":"2021-09-17T02:54:55.027233Z","iopub.status.idle":"2021-09-17T02:59:03.427034Z","shell.execute_reply.started":"2021-09-17T02:54:55.027192Z","shell.execute_reply":"2021-09-17T02:59:03.426184Z"},"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-17T02:59:43.131412Z","iopub.execute_input":"2021-09-17T02:59:43.131843Z","iopub.status.idle":"2021-09-17T02:59:43.438626Z","shell.execute_reply.started":"2021-09-17T02:59:43.131808Z","shell.execute_reply":"2021-09-17T02:59:43.437759Z"},"trusted":true},"execution_count":null,"outputs":[]}]}