{"cells":[{"metadata":{},"cell_type":"markdown","source":"# EDA using computer vision tools and albumentations"},{"metadata":{},"cell_type":"markdown","source":"## About Casava Plants\n\n![io](https://www.researchgate.net/profile/Vasavi_Rama_Karri/publication/307511709/figure/fig2/AS:401188076965891@1472662099994/Two-months-old-cassava-plant.png)\n\nManihot esculenta, commonly called cassava, yuca, macaxeira, mandioca, aipim, and agbeli, is a woody shrub native to South America of the spurge family, Euphorbiaceae. Although a perennial plant, cassava is extensively cultivated as an annual crop in tropical and subtropical regions for its edible starchy tuberous root, a major source of carbohydrates. Though it is often called yuca in parts of Spanish America and in the United States, it is not related to yucca, a shrub in the family Asparagaceae. Cassava is predominantly consumed in boiled form, but substantial quantities are used to extract cassava starch, called tapioca, which is used for food, animal feed, and industrial purposes. The Brazilian farinha, and the related garri of West Africa, is an edible coarse flour obtained by grating cassava roots, pressing moisture off the obtained grated pulp, and finally drying it (and roasting in the case of farinha).\n\nCassava is the third-largest source of food carbohydrates in the tropics, after rice and maize. Cassava is a major staple food in the developing world, providing a basic diet for over half a billion people.It is one of the most drought-tolerant crops, capable of growing on marginal soils. Nigeria is the world's largest producer of cassava, while Thailand is the largest exporter of cassava starch."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"## IMPORT THE LIBRARIES\n\nimport numpy as np\nimport pandas as pd \nimport PIL\nimport cv2\nimport os\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport json","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"main_dir = '../input/cassava-leaf-disease-classification/'\nprint(os.listdir(main_dir) )\ntrain_img_path = '../input/cassava-leaf-disease-classification/train_images'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataframe = pd.read_csv(main_dir+'train.csv')\n\nimgs_id = dataframe['image_id'].values\nimgs_label = dataframe['label'].values\nprint('csv file \\n\\n',dataframe.head())\nprint('\\n No. of the image label : ',len(dataframe['label']))\nprint('\\n No. of the image id :', len(dataframe['image_id']))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"js = open(main_dir + 'label_num_to_disease_map.json')\nreal_classes = json.load(js)\nreal_classes = {int(k):v for k,v in real_classes.items()}\ndataframe['class_name'] = dataframe.label.map(real_classes)\n\nprint(dataframe['class_name'])                \n                ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Visualiztions"},{"metadata":{"trusted":true},"cell_type":"code","source":"def display_image(img_ids, img_classes):\n    plt.figure(figsize=(16, 12))\n    \n    for i, (img_id, img_class) in enumerate(zip(img_ids, img_classes)):\n        plt.subplot(3, 3, i + 1)\n        image = cv2.imread(os.path.join(main_dir, \"train_images\", img_id))\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n        plt.imshow(image)\n        plt.title(f\"Class: {img_class}\", fontsize=12)\n        plt.axis(\"off\")\n    \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_df = dataframe.sample(9)\nimg_ids = new_df[\"image_id\"].values\nlabels = new_df[\"class_name\"].values\n\ndisplay_image(img_ids, labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(dataframe['class_name'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os \nfrom skimage import util\nfrom skimage import io, img_as_ubyte\nimport matplotlib.pyplot as plt\n##grey scale image function defined    \ndef gray_scale(img_ids, img_classes):\n    plt.figure(figsize=(16, 12))\n    \n    for i, (img_id, img_class) in enumerate(zip(img_ids, img_classes)):\n        plt.subplot(2, 3, i + 1)\n        src = train_img_path+'/'+img_id\n        img= io.imread(src)\n        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n        plt.imshow(gray)\n        plt.title(f\"Class: {img_class}\", fontsize=12)\n        plt.axis(\"off\")\n    \n    plt.show()\n\n\n## thresholded image function defined\ndef thresh_image(img_ids, img_classes):\n    plt.figure(figsize=(20, 16))\n    \n    for i, (img_id, img_class) in enumerate(zip(img_ids, img_classes)):\n        plt.subplot(2, 3, i + 1)\n        src = train_img_path+'/'+img_id\n        img= io.imread(src)\n        kernel = np.ones((5,5),np.uint8)\n        morpo = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)\n        plt.imshow(morpo)\n        plt.title(f\"Class: {img_class}\", fontsize=12)\n        plt.axis(\"off\")\n    \n    plt.show()\n\n\n## colored image function defined\ndef colored(img_ids, img_classes):\n    plt.figure(figsize=(16, 12))\n    \n    for i, (img_id, img_class) in enumerate(zip(img_ids, img_classes)):\n        plt.subplot(2,3, i + 1)\n        src = train_img_path+'/'+img_id\n        img= io.imread(src)\n        inverted_img = util.invert(img)\n        plt.imshow(inverted_img)\n        plt.title(f\"Class: {img_class}\", fontsize=12)\n        plt.axis(\"off\")\n    \n    plt.show()\n\n\n## another threshold function defined\ndef thresold(img_ids, img_classes):\n    plt.figure(figsize=(16, 12))\n    \n    for i, (img_id, img_class) in enumerate(zip(img_ids, img_classes)):\n        plt.subplot(2,3, i + 1)\n        src = train_img_path+'/'+img_id\n        img= io.imread(src)\n        thresh = cv2.threshold(img,170,255,cv2.THRESH_BINARY)[1]\n        plt.imshow(thresh)\n        plt.title(f\"Class: {img_class}\", fontsize=12)\n        plt.axis(\"off\")\n    \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Data Visualizations"},{"metadata":{"trusted":true},"cell_type":"code","source":"df = dataframe.sample(6)\nimg_ids = df[\"image_id\"].values\nlabels = df[\"class_name\"].values\n\ngray_scale(img_ids, labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"thresh_image(img_ids, labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"colored(img_ids, labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = dataframe[10:16]\nimg_ids = df[\"image_id\"].values\nlabels = df[\"class_name\"].values\n\nthresold(img_ids, labels)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Using Albumnetations"},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install albumentations","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import albumentations as albu","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image=cv2.imread(os.path.join(main_dir, \"train_images\", img_ids[4]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## horizontal flip of an image\ntransform = albu.HorizontalFlip(p=0.8)\naugmented_image = transform(image=image)['image']\nplt.imshow(augmented_image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## vertical flip of an image\ntransform = albu.VerticalFlip(p=0.8)\naugmented_image = transform(image=image)['image']\nplt.imshow(augmented_image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## blur the image using albumentations\ntransform = albu.Blur(blur_limit=4)\naugmented_image = transform(image=image)['image']\nplt.imshow(augmented_image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## gaussian blurring the image\ntransform = albu.GaussianBlur(sigma_limit=5)\naugmented_image = transform(image=image)['image']\nplt.imshow(augmented_image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## motion blurring the image\ntransform = albu.MotionBlur(blur_limit=(5,8))\naugmented_image = transform(image=image)['image']\nplt.imshow(augmented_image)","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}