{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"# Cassava Leaf Disease EDA\n\n\n\nCassava anthracnose disease (CAD) is widespread in most of the cassava growing regions of Africa. The disease is caused by a fungus (Collectothricum gloeosporioides) that is also capable of causing diseases on other food crops. It is estimated that CAD causes yield losses in the neighbourhood of 30% or more in susceptible cultivars. The disease affects both leaf and stem production. Severe anthracnose attacks can cause death of stems which can affect the availability of planting materials especially in large scale production systems."},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install seaborn==0.11.1","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import sys               \nimport time              \nimport pickle     \nimport numpy as np\nimport pandas as pd\nimport json\nfrom scipy import ndimage, stats, signal\n\nimport matplotlib.pyplot as plt\nimport seaborn as sb\nfrom PIL import Image, ImageStat\nfrom skimage import io, color\n\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sb.__version__","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp=Image.open('../input/cassava-leaf-disease-classification/test_images/2216849948.jpg')\nplt.imshow(temp);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('../input/cassava-leaf-disease-classification/label_num_to_disease_map.json','rb') as file:\n    map_disease = json.load(file)\n    print(json.dumps(map_disease, indent=4, sort_keys=True))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_eda = train.copy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"brightness=[]\nmedian=[]\ncontrast=[]\nsize=[]\nkurtosis=[]\nfor i in range(df_eda.shape[0]):\n    path = '../input/cassava-leaf-disease-classification/train_images/' + df_eda.iloc[i,0]\n    \n    im_sk = io.imread(path)\n    temp_im_sk = color.rgb2gray(im_sk)\n    im_kurtosis = stats.kurtosis(temp_im_sk.flatten(), fisher=False)\n    kurtosis.append(im_kurtosis)\n    \n    im = Image.open(path)\n    im_temp = im.convert('L')\n    stat = ImageStat.Stat(im_temp)\n    brightness.append(stat.rms[0])\n    median.append(stat.median[0])\n    contrast.append(stat.stddev[0])\n    size.append(np.array(im).shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_eda['brightness']=brightness\ndf_eda['median']=median\ndf_eda['contrast']=contrast\ndf_eda['kurtosis']=kurtosis\ndf_eda['height']= [item[0] for item in size]\ndf_eda['width']= [item[1] for item in size]\ndf_eda['channels']= [item[2] for item in size]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_eda","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(6,4))\nsb.countplot(data=df_eda, y='label')\nplt.yticks(ticks=range(0,5),labels=list(map_disease.values()))\nplt.title('Representation of classes on the training data')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_sample_imgs(label):\n    k=1\n    plt.figure(figsize=(12,7))\n    plt.suptitle(map_disease[str(label)] + ' sample images', y=0.9)\n    for i in range(6):\n        temp_df = df_eda.query(\"label == @label\")\n        im = temp_df.iloc[np.random.randint(temp_df.shape[0]),0]\n        path = '../input/cassava-leaf-disease-classification/train_images/' + im\n        img = Image.open(path)\n        plt.subplot(2,3,k)\n        plt.imshow(img)\n        k+=1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(5):\n    show_sample_imgs(i)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(15,8))\nplt.subplot(2,2,1)\nsb.histplot(data=df_eda.iloc[:,1:], x='brightness', hue='label')\nplt.subplot(2,2,2)\nsb.histplot(data=df_eda.iloc[:,1:], x='median', hue='label')\nplt.subplot(2,2,3)\nsb.histplot(data=df_eda.iloc[:,1:], x='contrast', hue='label')\nplt.subplot(2,2,4)\nsb.histplot(data=df_eda.iloc[:,1:], x='kurtosis', hue='label');","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"To be continued..."},{"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}