{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import seaborn as sns\nfrom skimage.io.collection import ImageCollection\nimport matplotlib.pyplot as plt\n%matplotlib inline\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# I: Inspect the Given Files\n\nBegin by importing all the files into the notebook environment by clicking the '+ Add data' button. These can be used to easily copy the file paths to all the training data.\n\n### Load Files\nNext, use Pandas to load the available .json and .csv files into dataframes for easy visualization."},{"metadata":{"trusted":true},"cell_type":"code","source":"# Read in data\ndisease_numbers = pd.read_json('../input/cassava-leaf-disease-classification/label_num_to_disease_map.json', \n                              orient = 'index')\n\nsample_submission = pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')\n\ntrain = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Disease Numbers:\\n {} \\n\\nSample_Submission:\\n {} \\n\\ntrain.csv:\\n {}'.format(disease_numbers,\n                                                                                 sample_submission,\n                                                                                 train))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train.info())\n\nprint('\\nDescription:\\n', train.describe())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('The image IDs are stored as {}.'.format(type(train['image_id'][1])))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Use a small image collection to look at the train_images files\nic = ImageCollection('../input/cassava-leaf-disease-classification/train_images/1000*.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Inspect the image collection data\ni = 0\nfor pic in ic:\n    print('{} \\nPic type: {} \\nPic shape: {} \\n\\n'.format(i, type(pic), np.shape(pic)))\n    i += 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('The image collection is type {} with shape {}.'.format(type(ic), \n                                                              np.shape(ic)))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Summary of Given Files\nThe .jpg file names and corresponding classes are stored in the file train.csv as strings and int64s, respectively. The disease names and their number codes are stored in the file label_num_to_disease_map.json.\n\nThe .jpg files themselves are stored in the train_images folder; the test file summarized by the sample submission is in the test_images folder. The pictures are three channel numpy arrays, corresponding to R, G, B intensities. There are $21397$ individual pictures.\n\n# II: Disease Distribution\nThe train.csv file is visualized to see any disparity between the number of pictures for each disease."},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train.groupby('label').count())\n\nplt.figure(figsize = (12, 6))\nax = sns.distplot(train['label'], bins = 5, kde = False, norm_hist = True,\n                  hist_kws = {'edgecolor':'k', 'align':'mid'})","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Disease Distribution Summary\nThe vast majority of pictures belong to Class 3. Classes 1, 2, and 4 contain roughly the same amount of pictures, while class 0 has the least. This suggests the use of stratify when splitting the data into validation and test sets."},{"metadata":{},"cell_type":"markdown","source":"# III: Picture Files\n\nDetermine whether the picture shapes are all uniform."},{"metadata":{"trusted":true},"cell_type":"code","source":"test_collection = ImageCollection('../input/cassava-leaf-disease-classification/train_images/1*.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Stored array:\\n', test_collection[0][0])\n\nprint('\\nThe values stored are of type {}, so they must be normalized and \\\nconverted to float64 before during processing.'.format(type(test_collection[0][0][0][0])))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(np.shape(test_collection))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The files all appear to be of the same shape. Thus, the image collection is a tensor with \n$5626$ color images, each of size $600x800$. These could be reshaped upon loading with\ncreation of a function.\n\n### Sample Pictures & Pixel Distributions\nAn arbitrary range of pictures is sampled to illustrate differences in picture and pixel distribution type. Each channel is plotted in its respective color and shown above the original image. "},{"metadata":{"trusted":true},"cell_type":"code","source":"for j in range(0, 2000, 200):\n    plt.figure(j, figsize = (12, 6))\n    pic = test_collection[j]\n    plt.subplot(2, 1, 1)\n    for i, col in enumerate(['r', 'g', 'b']):\n        channel = pic[:, :, i]\n        sns.distplot(channel, color = col, \n                     hist_kws = {'edgecolor':'k'})\n    plt.subplot(2,1, 2)\n    plt.imshow(pic)\n    plt.axis('off')\n    plt.show()\n        ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### A Filtered Image\nAn arbitrary picture is chosen and each individual color channel isolated below the original."},{"metadata":{"trusted":true},"cell_type":"code","source":"# Show the original picture\nplt.imshow(test_collection[5])\nplt.axis('off')\nplt.show()\n\n# Show the R, G, B channels individualy\nplt.figure(figsize = (14, 9))\nplt.subplot(1, 3, 1)\nplt.imshow(test_collection[5][:, :, 0])\nplt.axis('off')\nplt.title('R')\nplt.subplot(1, 3, 2)\nplt.imshow(test_collection[5][:, :, 1])\nplt.axis('off')\nplt.title('G')\nplt.subplot(1, 3, 3)\nplt.imshow(test_collection[5][:, :, 2])\nplt.axis('off')\nplt.title('B')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Sample Filtered Pic\npict = test_collection[5][:, :, 1] + test_collection[5][:, :, 2]\n\nplt.imshow(pict)\nplt.axis('off')\nplt.title('G & B Only')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Initial Conclusions & Next Steps\nThe pictures are not evenly distributed across each disease class, however they do appear to be uniform in shape. The small sample of pictures observed shows a wide range of sample types from single leaves to larger groups of whole plants. The red, green, and blue pixel distributions also appear to show a plethora of configurations. While the matching picture sizes is fortunate, their size could also lead to a problem and necessitate smaller batches.\n\nMost of the backgrounds appear to be red, so red pixels could possibly be filtered out during image processing. Isolating the green pixels may help as well.\n\nThe next notebook will focus on loading batches of pictures and creating a loading function to resize the pictures as required. The image collection itself is a tensor which can possibly be used for original ingestion into a convolutional model without reshaping."}],"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}