{"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":"markdown","source":"# Importing libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\n\nimport random\nfrom sklearn.preprocessing import MultiLabelBinarizer\n\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-20T13:24:14.888314Z","iopub.execute_input":"2022-10-20T13:24:14.889194Z","iopub.status.idle":"2022-10-20T13:24:17.644383Z","shell.execute_reply.started":"2022-10-20T13:24:14.889087Z","shell.execute_reply":"2022-10-20T13:24:17.643160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Show single image","metadata":{}},{"cell_type":"code","source":"img = cv2.imread('../input/plant-pathology-2021-fgvc8/train_images/80273091d9e9bddb.jpg')\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\nplt.figure()\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:17.647158Z","iopub.execute_input":"2022-10-20T13:24:17.648133Z","iopub.status.idle":"2022-10-20T13:24:19.988057Z","shell.execute_reply.started":"2022-10-20T13:24:17.648081Z","shell.execute_reply":"2022-10-20T13:24:19.986774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Let Us Perform Some Image Operations that We Can Perform on This Image","metadata":{}},{"cell_type":"code","source":"def show_image(image, title='Image', cmap_type='gray'): \n  plt.imshow(image, cmap=cmap_type)\n  plt.title(title)\n  plt.axis('off')\n  plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:19.989638Z","iopub.execute_input":"2022-10-20T13:24:19.990563Z","iopub.status.idle":"2022-10-20T13:24:19.997661Z","shell.execute_reply.started":"2022-10-20T13:24:19.990513Z","shell.execute_reply":"2022-10-20T13:24:19.996051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"leaf_image = cv2.imread('../input/plant-pathology-2021-fgvc8/train_images/80273091d9e9bddb.jpg')\nleaf_image = cv2.cvtColor(leaf_image, cv2.COLOR_BGR2RGB)\n\nshow_image(leaf_image, 'Original Image')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:20.001378Z","iopub.execute_input":"2022-10-20T13:24:20.001901Z","iopub.status.idle":"2022-10-20T13:24:21.632075Z","shell.execute_reply.started":"2022-10-20T13:24:20.001843Z","shell.execute_reply":"2022-10-20T13:24:21.630338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let Us See The Histogram Of An Image\n","metadata":{}},{"cell_type":"code","source":"red = leaf_image[:, :, 0] # using the red channel of the rocket image.\n\nplt.hist(red.ravel(), bins=256) # plot its histogram with 256 bins, the number of possible values of a pixel.\nplt.title('Red Histogram')\nplt.show","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:21.634095Z","iopub.execute_input":"2022-10-20T13:24:21.634697Z","iopub.status.idle":"2022-10-20T13:24:22.527045Z","shell.execute_reply.started":"2022-10-20T13:24:21.634658Z","shell.execute_reply":"2022-10-20T13:24:22.525270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Grayscale Image","metadata":{}},{"cell_type":"code","source":"grayimg = cv2.cvtColor(leaf_image,cv2.COLOR_BGR2GRAY)\n\nplt.imshow(grayimg,cmap='gray') #cmap has been used as matplotlib uses some default colormap to plot grayscale images\nplt.xticks([]) #To get rid of the x-ticks and y-ticks on the image axis\nplt.yticks([])\nprint('New Image Shape',grayimg.shape)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:22.528821Z","iopub.execute_input":"2022-10-20T13:24:22.529227Z","iopub.status.idle":"2022-10-20T13:24:24.263966Z","shell.execute_reply.started":"2022-10-20T13:24:22.529189Z","shell.execute_reply":"2022-10-20T13:24:24.262694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Finding optimal threshold\nfrom skimage.filters import threshold_otsu\nthresh_val = threshold_otsu(grayimg)\nprint('The optimal seperation value is',thresh_val)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:24.265830Z","iopub.execute_input":"2022-10-20T13:24:24.266239Z","iopub.status.idle":"2022-10-20T13:24:24.430949Z","shell.execute_reply.started":"2022-10-20T13:24:24.266193Z","shell.execute_reply":"2022-10-20T13:24:24.429507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"binary_high = grayimg > thresh_val\nbinary_low = grayimg <= thresh_val\n\nshow_image(binary_high, 'Tresholded high values')\nshow_image(binary_low, 'Tresholded low values')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:24.432607Z","iopub.execute_input":"2022-10-20T13:24:24.432955Z","iopub.status.idle":"2022-10-20T13:24:26.730543Z","shell.execute_reply.started":"2022-10-20T13:24:24.432924Z","shell.execute_reply":"2022-10-20T13:24:26.729356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Applying Edge Detection Techniques on The Image","metadata":{}},{"cell_type":"code","source":"def plot_comparison(original, filtered, title_filtered):\n  fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 6), sharex=True, sharey=True)\n  ax1.imshow(original, cmap=plt.cm.gray) \n  ax1.set_title('original') \n  ax1.axis('off')\n  ax2.imshow(filtered, cmap=plt.cm.gray) \n  ax2.set_title(title_filtered) \n  ax2.axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:26.732387Z","iopub.execute_input":"2022-10-20T13:24:26.733258Z","iopub.status.idle":"2022-10-20T13:24:26.742427Z","shell.execute_reply.started":"2022-10-20T13:24:26.733205Z","shell.execute_reply":"2022-10-20T13:24:26.741042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sobel Operator\nfrom skimage.filters import sobel\n\nedge_image = sobel(grayimg) # apply the filter\n\nplot_comparison(grayimg, edge_image, 'Edge image')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:26.747588Z","iopub.execute_input":"2022-10-20T13:24:26.748314Z","iopub.status.idle":"2022-10-20T13:24:29.774975Z","shell.execute_reply.started":"2022-10-20T13:24:26.748273Z","shell.execute_reply":"2022-10-20T13:24:29.773649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prewitt Operator\nfrom skimage.filters import prewitt\n\nedge_image = prewitt(grayimg) # apply the filter\n\nplot_comparison(grayimg, edge_image, 'Edge image')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:29.776506Z","iopub.execute_input":"2022-10-20T13:24:29.776958Z","iopub.status.idle":"2022-10-20T13:24:32.466605Z","shell.execute_reply.started":"2022-10-20T13:24:29.776911Z","shell.execute_reply":"2022-10-20T13:24:32.465359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Contrast Enhancement by Histogram equalization","metadata":{}},{"cell_type":"code","source":"from skimage import exposure\nequalized_leaf_image = exposure.equalize_hist(leaf_image)\n\nplot_comparison(leaf_image, equalized_leaf_image, 'Histogram equalization')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:32.468273Z","iopub.execute_input":"2022-10-20T13:24:32.469545Z","iopub.status.idle":"2022-10-20T13:24:39.596669Z","shell.execute_reply.started":"2022-10-20T13:24:32.469493Z","shell.execute_reply":"2022-10-20T13:24:39.595498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ADAPTIVE HISTOGRAM EQUALISATION","metadata":{}},{"cell_type":"code","source":"adapthits_leag_image = exposure.equalize_adapthist(leaf_image)\n\nplot_comparison(leaf_image, adapthits_leag_image, 'Adaptive Histogram equalization')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:39.597993Z","iopub.execute_input":"2022-10-20T13:24:39.598366Z","iopub.status.idle":"2022-10-20T13:24:53.378445Z","shell.execute_reply.started":"2022-10-20T13:24:39.598332Z","shell.execute_reply":"2022-10-20T13:24:53.377169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let's Now Have a look at the Dataset and Study it better","metadata":{}},{"cell_type":"code","source":"train_dir= '../input/plant-pathology-2021-fgvc8/train_images'\ntest_dir =  '../input/plant-pathology-2021-fgvc8/test_images'\ntrain = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:53.379774Z","iopub.execute_input":"2022-10-20T13:24:53.380173Z","iopub.status.idle":"2022-10-20T13:24:53.402224Z","shell.execute_reply.started":"2022-10-20T13:24:53.380138Z","shell.execute_reply":"2022-10-20T13:24:53.400936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Let's Study the dataset in a better way and try to find some interesting stuff!","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:53.403631Z","iopub.execute_input":"2022-10-20T13:24:53.403982Z","iopub.status.idle":"2022-10-20T13:24:53.417337Z","shell.execute_reply.started":"2022-10-20T13:24:53.403948Z","shell.execute_reply":"2022-10-20T13:24:53.416012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['labels'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:53.418821Z","iopub.execute_input":"2022-10-20T13:24:53.419161Z","iopub.status.idle":"2022-10-20T13:24:53.429352Z","shell.execute_reply.started":"2022-10-20T13:24:53.419130Z","shell.execute_reply":"2022-10-20T13:24:53.428230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Let's look at the number of images for various of 12 categories present**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,12))\nlabels = sns.barplot(train.labels.value_counts().index,train.labels.value_counts())\nfor item in labels.get_xticklabels():\n    item.set_rotation(45)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:53.430908Z","iopub.execute_input":"2022-10-20T13:24:53.431394Z","iopub.status.idle":"2022-10-20T13:24:53.794065Z","shell.execute_reply.started":"2022-10-20T13:24:53.431347Z","shell.execute_reply":"2022-10-20T13:24:53.792679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Note**\n\nNotice that there is a huge imbalance in dataset with \"scab\" having the highest number of frequency and \"powdery_mildew complex\" , the least","metadata":{}},{"cell_type":"markdown","source":"## Important Observation\n**Look at the labels, doesn't it strike you ??**\n\n**Some of the labels are mixture of one or more types !!! And thus the problem becomes Multilabel Problem**\n\nSo there are not 12 labels, its actually just 6 labels. 5 diseases:\n\nrust\nscab\ncomplex\nfrog eye leaf spot\npowdery mildew\nand another label is\n\nhealthy (healthy leaves)\nNow the most important thing is, as one image can have multiple diseases, that means this problem is Multi label classification problem. Many get confused betweeen multilabel and multiclass classification. if you are new to multilabel classification I would suggest going over this An introduction to MultiLabel classification .\n\nSo now we gotta process the labels. And then lets find out the actual frequencies of the labels.\n\nWe divide it based on \" \" or space character , in order to get the labels for each of the image","metadata":{}},{"cell_type":"code","source":"train['labels'] = train['labels'].apply(lambda string: string.split(' '))\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:53.796084Z","iopub.execute_input":"2022-10-20T13:24:53.796978Z","iopub.status.idle":"2022-10-20T13:24:54.055959Z","shell.execute_reply.started":"2022-10-20T13:24:53.796937Z","shell.execute_reply":"2022-10-20T13:24:54.054771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Converting the labels representation into one hot encoded format using MultilabelBinarizer from Scikit learn. Now we can see and plot the frequencies of each label.**","metadata":{}},{"cell_type":"code","source":"s = list(train['labels'])\nmlb = MultiLabelBinarizer()\ntrainx = pd.DataFrame(mlb.fit_transform(s), columns=mlb.classes_, index=train.index)\nprint(trainx.columns)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:54.057382Z","iopub.execute_input":"2022-10-20T13:24:54.058013Z","iopub.status.idle":"2022-10-20T13:24:54.087056Z","shell.execute_reply.started":"2022-10-20T13:24:54.057978Z","shell.execute_reply":"2022-10-20T13:24:54.085639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"These are the 6 different labels","metadata":{}},{"cell_type":"code","source":"print(trainx.sum())","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:54.088647Z","iopub.execute_input":"2022-10-20T13:24:54.089012Z","iopub.status.idle":"2022-10-20T13:24:54.097522Z","shell.execute_reply.started":"2022-10-20T13:24:54.088979Z","shell.execute_reply":"2022-10-20T13:24:54.096110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = list(trainx.sum().keys())\n#print(labels)\nlabel_counts = trainx.sum().values.tolist()\n\nfig, ax = plt.subplots(1,1, figsize=(20,6))\n\nsns.barplot(x= labels, y= label_counts, ax=ax)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:54.099321Z","iopub.execute_input":"2022-10-20T13:24:54.099882Z","iopub.status.idle":"2022-10-20T13:24:54.354483Z","shell.execute_reply.started":"2022-10-20T13:24:54.099836Z","shell.execute_reply":"2022-10-20T13:24:54.353006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**NOW WE CAN SEE THE DATASET BECOMES MORE OR LESS BALANCED , AT LEAST BETTER THAN WHAT IT WAS PREVIOUSLY!**","metadata":{}},{"cell_type":"code","source":"labels = pd.concat([train['image'], trainx], axis=1)\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:54.355901Z","iopub.execute_input":"2022-10-20T13:24:54.356244Z","iopub.status.idle":"2022-10-20T13:24:54.372171Z","shell.execute_reply.started":"2022-10-20T13:24:54.356213Z","shell.execute_reply":"2022-10-20T13:24:54.370901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show 12 random image from the dataset","metadata":{}},{"cell_type":"code","source":"img_list=[]\nfor i in range(1,13):\n    rand =  random.randrange(1, 18000)\n    sample = os.path.join('../input/plant-pathology-2021-fgvc8/train_images/', train['image'][rand])\n    img_list.append(sample)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:54.373698Z","iopub.execute_input":"2022-10-20T13:24:54.374079Z","iopub.status.idle":"2022-10-20T13:24:54.383455Z","shell.execute_reply.started":"2022-10-20T13:24:54.374045Z","shell.execute_reply":"2022-10-20T13:24:54.382446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig1 = plt.figure(figsize=(20,10))\n\nfor j, im in enumerate(img_list,1):\n    \n    img = cv2.imread(im)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    \n    ax = fig1.add_subplot(4,3,j)\n    ax.imshow(img)\n    title = f\"{train['labels'][j]}{img.shape[:-1]}\"\n    plt.title(title)\n    \n    fig1.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:24:54.384786Z","iopub.execute_input":"2022-10-20T13:24:54.385320Z","iopub.status.idle":"2022-10-20T13:25:15.846812Z","shell.execute_reply.started":"2022-10-20T13:24:54.385287Z","shell.execute_reply":"2022-10-20T13:25:15.845627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convert the images to gray scale","metadata":{}},{"cell_type":"code","source":"fig1 = plt.figure(figsize=(20,10))\n\nfor j, im in enumerate(img_list,1):\n    \n    img = cv2.imread(im)\n    grayimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n    \n    ax = fig1.add_subplot(4,3,j)\n    ax.imshow(grayimg,cmap='gray')\n    title = f\"{train['labels'][j]}{grayimg.shape}\"\n    plt.title(title)\n    \n    fig1.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:25:15.848455Z","iopub.execute_input":"2022-10-20T13:25:15.848938Z","iopub.status.idle":"2022-10-20T13:25:30.022336Z","shell.execute_reply.started":"2022-10-20T13:25:15.848894Z","shell.execute_reply":"2022-10-20T13:25:30.021125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show the image edges","metadata":{}},{"cell_type":"code","source":"fig1 = plt.figure(figsize=(20,10))\n\nfor j, im in enumerate(img_list,1):\n    \n    img = cv2.imread(im)\n    grayimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n    edge_image = sobel(grayimg) # apply the filter\n    ax = fig1.add_subplot(4,3,j)\n    ax.imshow(edge_image, cmap=plt.cm.gray)\n    title = f\"{train['labels'][j]}{edge_image.shape}\"\n    plt.title(title)\n    \n    fig1.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:25:30.024150Z","iopub.execute_input":"2022-10-20T13:25:30.024700Z","iopub.status.idle":"2022-10-20T13:25:51.210012Z","shell.execute_reply.started":"2022-10-20T13:25:30.024664Z","shell.execute_reply":"2022-10-20T13:25:51.208688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Histogram Equalization","metadata":{}},{"cell_type":"code","source":"fig1 = plt.figure(figsize=(20,10))\n\nfor j, im in enumerate(img_list,1):\n    \n    img = cv2.imread(im)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    equalized_leaf_image = exposure.equalize_hist(img)\n    ax = fig1.add_subplot(4,3,j)\n    ax.imshow(equalized_leaf_image)\n    title = f\"{train['labels'][j]}{equalized_leaf_image.shape[:-1]}\"\n    plt.title(title)\n    \n    fig1.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-10-20T13:25:51.211632Z","iopub.execute_input":"2022-10-20T13:25:51.212015Z","iopub.status.idle":"2022-10-20T13:26:55.216477Z","shell.execute_reply.started":"2022-10-20T13:25:51.211980Z","shell.execute_reply":"2022-10-20T13:26:55.215213Z"},"trusted":true},"execution_count":null,"outputs":[]}]}