{"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 os\nimport matplotlib.pyplot as plt\nimport json\nimport cv2\nplt.style.use('bmh')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Dataset Stats"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Total number of training images: \", len(train_df))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('../input/cassava-leaf-disease-classification/label_num_to_disease_map.json') as f:\n    data = json.load(f)\nprint('Classes')\nfor i in range(len(data)):\n    print(i,\": \", data[str(i)])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.hist();","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":"Here we can clearly see that there is a large class imbalance in this dataset. Class 3 has more samples than the remaining classes combined."},{"metadata":{},"cell_type":"markdown","source":"## Image Dimensions"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dir = \"../input/cassava-leaf-disease-classification/train_images\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_sizes = []\nfor f in os.listdir(train_dir):\n    image = cv2.imread(os.path.join(train_dir, f))\n    image_sizes.append(image.shape)\n    \nimage_sizes = set(image_sizes)\nimage_sizes","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"All of the images are of size 600x800 which is quite large so the images will have to be downsampled during training."},{"metadata":{"trusted":true},"cell_type":"code","source":"# n must be even\ndef plot_examples(ids, n):\n    np.random.seed(0)\n    rand_ids = np.random.choice(ids, size=n)\n\n    fig = plt.figure(figsize=(15, 10))\n    for i in range(n):\n        I = cv2.imread(os.path.join(train_dir, rand_ids[i]))\n        fig.add_subplot(int(n/2),2,i+1)\n        plt.imshow(I[:,:,::-1])\n        plt.grid(None)\n        plt.xlabel(rand_ids[i])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Class 0 (Cassava Bacterial Blight)"},{"metadata":{"trusted":true},"cell_type":"code","source":"class_0 = train_df.loc[train_df['label'] == 0, ['image_id']].values.flatten()\nplot_examples(class_0, 4)  ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Class 1 (Cassava Brown Streak Disease)"},{"metadata":{"trusted":true},"cell_type":"code","source":"class_1 = train_df.loc[train_df['label'] == 1, ['image_id']].values.flatten()\nplot_examples(class_1, 4)  ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Class 2 (Cassava Green Mottle)"},{"metadata":{"trusted":true},"cell_type":"code","source":"class_2 = train_df.loc[train_df['label'] == 2, ['image_id']].values.flatten()\nplot_examples(class_2, 4)  ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Class 3 (Cassava Mosaic Disease)"},{"metadata":{"trusted":true},"cell_type":"code","source":"class_3 = train_df.loc[train_df['label'] == 3, ['image_id']].values.flatten()\nplot_examples(class_3, 4)  ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Class 4 (Healthy)"},{"metadata":{"trusted":true},"cell_type":"code","source":"class_4 = train_df.loc[train_df['label'] == 4, ['image_id']].values.flatten()\nplot_examples(class_4, 4)  ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Given that this is a real world dataset, the images have been taken using different cameras and under different conditions. The images vary greatly in quality, background/environment, light exposure, perspective, zoom, etc."},{"metadata":{},"cell_type":"markdown","source":"## Image Augmentations"},{"metadata":{"trusted":true},"cell_type":"code","source":"import albumentations as A","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"transform = A.Compose([\n    A.RandomResizedCrop(256,256),\n    A.HorizontalFlip(p=0.5),\n    A.VerticalFlip(p=0.25),\n    A.Transpose(p=0.25),\n    A.RandomBrightnessContrast(\n                brightness_limit=(-0.1,0.1), \n                contrast_limit=(-0.1, 0.1), \n                p=0.5)\n#     A.Normalize(\n#                 mean=[0.485, 0.456, 0.406], \n#                 std=[0.229, 0.224, 0.225], \n#                 max_pixel_value=255.0, \n#                 p=1.0)\n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_augmentations(ids, n, transform):\n#     np.random.seed(0)\n    rand_ids = np.random.choice(ids, size=n)\n    \n    fig = plt.figure(figsize=(15, 25))\n    fig.suptitle('Original Image vs Transformed Image')\n    for i in range(0, 2*n, 2):\n        image = cv2.imread(os.path.join(train_dir, rand_ids[int(i/2)]))\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        fig.add_subplot(n, 2, i+1)\n        plt.imshow(image)\n        plt.grid(None)\n        \n        transformed = transform(image=image)\n        transformed_image = transformed['image']\n        fig.add_subplot(n, 2, i+2)\n        plt.imshow(transformed_image)\n        plt.grid(None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids = train_df['image_id'].values\nplot_augmentations(ids, 5, transform)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')\ntest_df","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}