{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport cv2\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport hashlib\n\n%matplotlib inline\nimport matplotlib.pyplot as plt\n\nfrom tqdm.notebook import tqdm\nfrom PIL import Image\n\nDIR_INPUT = '/kaggle/input/cassava-leaf-disease-classification'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(DIR_INPUT + '/train.csv')\ntrain_df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Image metadata"},{"metadata":{"trusted":true},"cell_type":"code","source":"def calculate_hash(im):\n    md5 = hashlib.md5()\n    md5.update(np.array(im).tostring())\n    \n    return md5.hexdigest()\n    \ndef get_image_meta(image_id, image_src, dataset='train'):\n    im = Image.open(image_src)\n    extrema = im.getextrema()\n\n    meta = {\n        'image_id': image_id,\n        'dataset': dataset,\n        'hash': calculate_hash(im),\n        'r_min': extrema[0][0],\n        'r_max': extrema[0][1],\n        'g_min': extrema[1][0],\n        'g_max': extrema[1][1],\n        'b_min': extrema[2][0],\n        'b_max': extrema[2][1],\n        'height': im.size[1],\n        'width': im.size[0],\n        'format': im.format,\n        'mode': im.mode\n    }\n    return meta","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = []\n\nfor i, image_id in enumerate(tqdm(train_df['image_id'].values, total=train_df.shape[0])):\n    data.append(get_image_meta(image_id, f'{DIR_INPUT}/train_images/{image_id}'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"meta_df = pd.DataFrame(data)\nmeta_df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Image sizes\nIt looks like all of the images have the same size: 800x600px"},{"metadata":{"trusted":true},"cell_type":"code","source":"meta_df.groupby(by='dataset')[['width', 'height']].aggregate(['min', 'max'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Duplicated images\nThere is no duplication in the training set."},{"metadata":{"trusted":true},"cell_type":"code","source":"duplicates = meta_df.groupby(by='hash')[['image_id']].count().reset_index()\nduplicates = duplicates[duplicates['image_id'] > 1]\nduplicates.reset_index(drop=True, inplace=True)\n\nduplicates = duplicates.merge(meta_df[['image_id', 'hash']], on='hash')\n\nduplicates.head(20)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Target distribution"},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_images(image_ids):\n    \n    col = 5\n    row = min(len(image_ids) // col, 5)\n    \n    fig, ax = plt.subplots(row, col, figsize=(16, 8))\n    ax = ax.flatten()\n\n    for i, image_id in enumerate(image_ids):\n        image = cv2.imread(f'{DIR_INPUT}/train_images/{image_id}')\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n        ax[i].set_axis_off()\n        ax[i].imshow(image)\n        ax[i].set_title(image_id)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[['CBB', 'CBSD', 'CGM', 'CMD', 'Healthy']] = pd.get_dummies(train_df[\"label\"])\n\nfig = go.Figure(data=[\n    go.Pie(labels=train_df.columns[2:],\n           values=train_df.iloc[:, 2:].sum().values)\n])\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[\"label\"].value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Random images"},{"metadata":{"trusted":true},"cell_type":"code","source":"show_images(train_df.sample(n=15)['image_id'].values)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Healthy leaves"},{"metadata":{"trusted":true},"cell_type":"code","source":"show_images(train_df[train_df['label'] == 4].sample(n=15)['image_id'].values)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Cassava Bacterial Blight (CBB)"},{"metadata":{"trusted":true},"cell_type":"code","source":"show_images(train_df[train_df['label'] == 0].sample(n=15)['image_id'].values)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Cassava Brown Streak Disease (CBSD)"},{"metadata":{"trusted":true},"cell_type":"code","source":"show_images(train_df[train_df['label'] == 1].sample(n=15)['image_id'].values)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Cassava Green Mottle (CGM)"},{"metadata":{"trusted":true},"cell_type":"code","source":"show_images(train_df[train_df['label'] == 2].sample(n=15)['image_id'].values)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Cassava Mosaic Disease (CMD)"},{"metadata":{"trusted":true},"cell_type":"code","source":"show_images(train_df[train_df['label'] == 3].sample(n=15)['image_id'].values)","execution_count":null,"outputs":[]},{"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}