{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# EDA Sorghum -100 Image Classification","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:08.772299Z","iopub.execute_input":"2022-05-23T02:46:08.773114Z","iopub.status.idle":"2022-05-23T02:46:08.799304Z","shell.execute_reply.started":"2022-05-23T02:46:08.773004Z","shell.execute_reply":"2022-05-23T02:46:08.798671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir='../input/sorghum-id-fgvc-9/train_images/'","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:08.800443Z","iopub.execute_input":"2022-05-23T02:46:08.801049Z","iopub.status.idle":"2022-05-23T02:46:08.804181Z","shell.execute_reply.started":"2022-05-23T02:46:08.801018Z","shell.execute_reply":"2022-05-23T02:46:08.803625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir='../input/sorghum-id-fgvc-9/test/'","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:08.805526Z","iopub.execute_input":"2022-05-23T02:46:08.805809Z","iopub.status.idle":"2022-05-23T02:46:08.815515Z","shell.execute_reply.started":"2022-05-23T02:46:08.805781Z","shell.execute_reply":"2022-05-23T02:46:08.814810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/sorghum-id-fgvc-9/train_cultivar_mapping.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:08.817532Z","iopub.execute_input":"2022-05-23T02:46:08.818413Z","iopub.status.idle":"2022-05-23T02:46:08.866961Z","shell.execute_reply.started":"2022-05-23T02:46:08.818369Z","shell.execute_reply":"2022-05-23T02:46:08.866160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:08.867903Z","iopub.execute_input":"2022-05-23T02:46:08.868712Z","iopub.status.idle":"2022-05-23T02:46:08.887141Z","shell.execute_reply.started":"2022-05-23T02:46:08.868670Z","shell.execute_reply":"2022-05-23T02:46:08.886612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_length=len(os.listdir(train_dir))\ntest_length=len(os.listdir(test_dir))\nprint('There are a total of {} images in Train set and a total of {} images in Test set'.format(train_length, test_length ))","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:08.888414Z","iopub.execute_input":"2022-05-23T02:46:08.888869Z","iopub.status.idle":"2022-05-23T02:46:09.981560Z","shell.execute_reply.started":"2022-05-23T02:46:08.888829Z","shell.execute_reply":"2022-05-23T02:46:09.980823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### MISSING VALUES","metadata":{}},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:09.982474Z","iopub.execute_input":"2022-05-23T02:46:09.983112Z","iopub.status.idle":"2022-05-23T02:46:09.995518Z","shell.execute_reply.started":"2022-05-23T02:46:09.983077Z","shell.execute_reply":"2022-05-23T02:46:09.994756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Deleting rows with missing values\ntrain=train.dropna(axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:09.996939Z","iopub.execute_input":"2022-05-23T02:46:09.997151Z","iopub.status.idle":"2022-05-23T02:46:10.013062Z","shell.execute_reply.started":"2022-05-23T02:46:09.997127Z","shell.execute_reply":"2022-05-23T02:46:10.012370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### COUNTS IN EACH CATEGORY","metadata":{}},{"cell_type":"code","source":"train['cultivar'].value_counts().mean()","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:10.015190Z","iopub.execute_input":"2022-05-23T02:46:10.016060Z","iopub.status.idle":"2022-05-23T02:46:10.031885Z","shell.execute_reply.started":"2022-05-23T02:46:10.016011Z","shell.execute_reply":"2022-05-23T02:46:10.031039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['cultivar'].value_counts().plot(kind='bar', figsize=(14,6))\nplt.xticks(visible = False)\nplt.title('Number of images in each Category')\nplt.show()\n\nprint('There are on an average {} images in each Category'.format(train['cultivar'].value_counts().mean()))","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:10.033529Z","iopub.execute_input":"2022-05-23T02:46:10.034037Z","iopub.status.idle":"2022-05-23T02:46:10.703160Z","shell.execute_reply.started":"2022-05-23T02:46:10.034001Z","shell.execute_reply":"2022-05-23T02:46:10.702363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images=os.listdir(train_dir)","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:10.704587Z","iopub.execute_input":"2022-05-23T02:46:10.705539Z","iopub.status.idle":"2022-05-23T02:46:10.720870Z","shell.execute_reply.started":"2022-05-23T02:46:10.705493Z","shell.execute_reply":"2022-05-23T02:46:10.720234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images=os.listdir(test_dir)","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:10.722032Z","iopub.execute_input":"2022-05-23T02:46:10.722455Z","iopub.status.idle":"2022-05-23T02:46:10.735953Z","shell.execute_reply.started":"2022-05-23T02:46:10.722423Z","shell.execute_reply":"2022-05-23T02:46:10.735261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### IMAGE PIXEL DETAILS","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import load_img,img_to_array\n\n\nsample_image  = load_img(os.path.join(train_dir, train_images[0]))\nsample_grayscale=load_img(os.path.join(train_dir, train_images[0]),color_mode = \"grayscale\")\nsample_gray_array=  img_to_array(sample_grayscale)\nsample_array = img_to_array(sample_image)\n\nprint(f\"Each image has shape: {sample_array.shape}\")\n\nprint(f\"The maximum pixel value used is: {np.max(sample_array)}\")\nprint(f\"The minimum pixel value used is: {np.min(sample_array)}\")","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:10.737393Z","iopub.execute_input":"2022-05-23T02:46:10.737811Z","iopub.status.idle":"2022-05-23T02:46:17.312332Z","shell.execute_reply.started":"2022-05-23T02:46:10.737780Z","shell.execute_reply":"2022-05-23T02:46:17.311385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### VISUALIZE TRAIN IMAGES","metadata":{}},{"cell_type":"code","source":"# Visualize a sample of 16 images\nimport matplotlib.image as mpimg\nnrows=4\nncols=4\n\nplt.figure(figsize=(16, 16))\nfor i in range (16):\n    plt.subplot(nrows, ncols, i+1)\n    img_path=os.path.join(train_dir, train_images[i])\n    img = mpimg.imread(img_path)\n    \n    plt.imshow(img)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:17.313685Z","iopub.execute_input":"2022-05-23T02:46:17.314004Z","iopub.status.idle":"2022-05-23T02:46:23.707896Z","shell.execute_reply.started":"2022-05-23T02:46:17.313957Z","shell.execute_reply":"2022-05-23T02:46:23.707268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### VISUALIZE TEST IMAGES","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(16, 16))\nfor i in range (16):\n    plt.subplot(nrows, ncols, i+1)\n    img_path=os.path.join(test_dir, test_images[i])\n    img = mpimg.imread(img_path)\n    \n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:23.708845Z","iopub.execute_input":"2022-05-23T02:46:23.709070Z","iopub.status.idle":"2022-05-23T02:46:29.999501Z","shell.execute_reply.started":"2022-05-23T02:46:23.709041Z","shell.execute_reply":"2022-05-23T02:46:29.998477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### VISUALIZE IMAGE FROM EACH CATEGORY","metadata":{}},{"cell_type":"code","source":"train_unique_images=train.groupby('cultivar')['image'].min().to_list()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# VISUALIZE IMAGE FROM 16 DIFFERENT CATEGORIES\nnrows=4\nncols=4\nplt.figure(figsize=(16, 16))\nfor i in range (16):\n    plt.subplot(nrows, ncols, i+1)\n    img_path=os.path.join(train_dir, train_unique_images[i])\n    img = mpimg.imread(img_path)\n    \n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:53:57.038672Z","iopub.execute_input":"2022-05-23T02:53:57.038945Z","iopub.status.idle":"2022-05-23T02:54:02.435950Z","shell.execute_reply.started":"2022-05-23T02:53:57.038915Z","shell.execute_reply":"2022-05-23T02:54:02.435031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### HISTOGRAM OF TRAIN IMAGES","metadata":{}},{"cell_type":"code","source":"for fname in train_unique_images[0:8]:\n    image  = load_img(os.path.join(train_dir, fname))\n    grayscale=load_img(os.path.join(train_dir, fname),color_mode = \"grayscale\")\n    gray_array=  img_to_array(grayscale)\n    image_array = img_to_array(image)\n\n    plt.imshow(image)\n    plt.title('Image and its color Histogram in Red, Green, Blue and Grayscale' )\n    fig, (ax1, ax2, ax3, ax4) = plt.subplots(nrows=1,ncols=4, sharey=True, figsize=(24,5))\n\n    ax1.hist(image_array[:,:,0].ravel(),256,[0,256],color='red')\n    plt.ylim(0,120000)\n    ax2.hist(image_array[:,:,1].ravel(),256,[0,256], color='green')\n    ax3.hist(image_array[:,:,1].ravel(),256,[0,256], color='blue')\n    ax4.hist(gray_array.ravel(),256,[0,256])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:36.235534Z","iopub.execute_input":"2022-05-23T02:46:36.235757Z","iopub.status.idle":"2022-05-23T02:46:57.717515Z","shell.execute_reply.started":"2022-05-23T02:46:36.235729Z","shell.execute_reply":"2022-05-23T02:46:57.716722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### HISTOGRAM OF TEST IMAGES","metadata":{}},{"cell_type":"code","source":"for fname in test_images[0:8]:\n    image  = load_img(os.path.join(test_dir, fname))\n    grayscale=load_img(os.path.join(test_dir, fname),color_mode = \"grayscale\")\n    gray_array=  img_to_array(grayscale)\n    image_array = img_to_array(image)\n\n    plt.imshow(image)\n    plt.title('Image and its color Histogram in Red, Green, Blue and Grayscale' )\n    fig, (ax1, ax2, ax3, ax4) = plt.subplots(nrows=1,ncols=4, sharey=True, figsize=(24,5))\n\n    ax1.hist(image_array[:,:,0].ravel(),256,[0,256],color='red')\n    plt.ylim(0,120000)\n    ax2.hist(image_array[:,:,1].ravel(),256,[0,256], color='green')\n    ax3.hist(image_array[:,:,1].ravel(),256,[0,256], color='blue')\n    ax4.hist(gray_array.ravel(),256,[0,256])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-23T02:46:57.718816Z","iopub.execute_input":"2022-05-23T02:46:57.719245Z","iopub.status.idle":"2022-05-23T02:47:19.441641Z","shell.execute_reply.started":"2022-05-23T02:46:57.719194Z","shell.execute_reply":"2022-05-23T02:47:19.440647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Thanks! for viewing my notebook. If you liked it, please do upvote.","metadata":{}}]}