{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt # draw plots\nfrom PIL import Image   # image viewing and access\n\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-13T18:59:00.613869Z","iopub.execute_input":"2023-09-13T18:59:00.614374Z","iopub.status.idle":"2023-09-13T18:59:00.648039Z","shell.execute_reply.started":"2023-09-13T18:59:00.614325Z","shell.execute_reply":"2023-09-13T18:59:00.64663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip ../input/diabetic-retinopathy-detection/sample.zip","metadata":{"execution":{"iopub.status.busy":"2023-09-13T18:59:59.344188Z","iopub.execute_input":"2023-09-13T18:59:59.344685Z","iopub.status.idle":"2023-09-13T19:00:00.786302Z","shell.execute_reply.started":"2023-09-13T18:59:59.344645Z","shell.execute_reply":"2023-09-13T19:00:00.784742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip ../input/diabetic-retinopathy-detection/trainLabels.csv.zip","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:00:27.785465Z","iopub.execute_input":"2023-09-13T19:00:27.785935Z","iopub.status.idle":"2023-09-13T19:00:28.828097Z","shell.execute_reply.started":"2023-09-13T19:00:27.785893Z","shell.execute_reply":"2023-09-13T19:00:28.826306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip ../input/diabetic-retinopathy-detection/sampleSubmission.csv.zip","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:00:48.162227Z","iopub.execute_input":"2023-09-13T19:00:48.162728Z","iopub.status.idle":"2023-09-13T19:00:49.212543Z","shell.execute_reply.started":"2023-09-13T19:00:48.16268Z","shell.execute_reply":"2023-09-13T19:00:49.211018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Check number of sample images**","metadata":{}},{"cell_type":"code","source":"data_dir = '/kaggle/working/sample'\nprint('Number of images:', len(os.listdir(data_dir))) # length of the list\n","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:01:09.849586Z","iopub.execute_input":"2023-09-13T19:01:09.850111Z","iopub.status.idle":"2023-09-13T19:01:09.857381Z","shell.execute_reply.started":"2023-09-13T19:01:09.850061Z","shell.execute_reply":"2023-09-13T19:01:09.856095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Looking into images","metadata":{}},{"cell_type":"code","source":"f, axarr = plt.subplots(2,2,figsize=(10, 10))\naxarr[0,0].imshow(Image.open(\"./sample/10_right.jpeg\"))     # used PIL\naxarr[0,1].imshow(Image.open(\"./sample/13_right.jpeg\"))\naxarr[1,0].imshow(Image.open(\"./sample/15_right.jpeg\"))\naxarr[1,1].imshow(Image.open(\"./sample/17_right.jpeg\"))","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:04:21.309349Z","iopub.execute_input":"2023-09-13T19:04:21.309781Z","iopub.status.idle":"2023-09-13T19:04:32.170652Z","shell.execute_reply.started":"2023-09-13T19:04:21.309745Z","shell.execute_reply":"2023-09-13T19:04:32.169393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %matplotlib inline\n# import matplotlib.pyplot as plt\n# import matplotlib.image as mpimg\n# img = mpimg.imread('./sample/10_right.jpeg')\n# imgplot = plt.imshow(img)\n# plt.show()\n\n# from IPython.display import Image\n# Image('./sample/10_right.jpeg')\n\n# from PIL import Image\n# Image.open(os.path.join(data_dir, os.listdir(data_dir)[0]))","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:07:12.604616Z","iopub.execute_input":"2023-09-13T19:07:12.606272Z","iopub.status.idle":"2023-09-13T19:07:12.610911Z","shell.execute_reply.started":"2023-09-13T19:07:12.606208Z","shell.execute_reply":"2023-09-13T19:07:12.609859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking data distribution","metadata":{}},{"cell_type":"code","source":"# Load the dataset\ndf = pd.read_csv('/kaggle/working/trainLabels.csv')\n\n# Step 1: Check the shape of the data\nprint('Number of rows:', df.shape[0])\nprint('Number of columns:', df.shape[1])\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:10:25.434763Z","iopub.execute_input":"2023-09-13T19:10:25.435359Z","iopub.status.idle":"2023-09-13T19:10:25.481935Z","shell.execute_reply.started":"2023-09-13T19:10:25.435306Z","shell.execute_reply":"2023-09-13T19:10:25.480464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:10:35.934051Z","iopub.execute_input":"2023-09-13T19:10:35.934485Z","iopub.status.idle":"2023-09-13T19:10:35.967197Z","shell.execute_reply.started":"2023-09-13T19:10:35.934439Z","shell.execute_reply":"2023-09-13T19:10:35.965938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Check sample image size**","metadata":{}},{"cell_type":"code","source":"data_dir","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:10:59.190318Z","iopub.execute_input":"2023-09-13T19:10:59.191678Z","iopub.status.idle":"2023-09-13T19:10:59.200055Z","shell.execute_reply.started":"2023-09-13T19:10:59.191618Z","shell.execute_reply":"2023-09-13T19:10:59.198712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(data_dir)[0]","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:11:44.988476Z","iopub.execute_input":"2023-09-13T19:11:44.988959Z","iopub.status.idle":"2023-09-13T19:11:44.99756Z","shell.execute_reply.started":"2023-09-13T19:11:44.988904Z","shell.execute_reply":"2023-09-13T19:11:44.996152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.path.join(data_dir, os.listdir(data_dir)[0])","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:12:00.690041Z","iopub.execute_input":"2023-09-13T19:12:00.691452Z","iopub.status.idle":"2023-09-13T19:12:00.699312Z","shell.execute_reply.started":"2023-09-13T19:12:00.691394Z","shell.execute_reply":"2023-09-13T19:12:00.698168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(os.path.join(data_dir, os.listdir(data_dir)[0]))","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:15:33.093505Z","iopub.execute_input":"2023-09-13T19:15:33.094819Z","iopub.status.idle":"2023-09-13T19:15:36.999424Z","shell.execute_reply.started":"2023-09-13T19:15:33.094751Z","shell.execute_reply":"2023-09-13T19:15:36.996023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Image size:', sample_img.size)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:15:18.385897Z","iopub.execute_input":"2023-09-13T19:15:18.386463Z","iopub.status.idle":"2023-09-13T19:15:18.395723Z","shell.execute_reply.started":"2023-09-13T19:15:18.386415Z","shell.execute_reply":"2023-09-13T19:15:18.394163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Check the image mode**","metadata":{}},{"cell_type":"code","source":"print('Image mode:', sample_img.mode)","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:16:17.438154Z","iopub.execute_input":"2023-09-13T19:16:17.438592Z","iopub.status.idle":"2023-09-13T19:16:17.445084Z","shell.execute_reply.started":"2023-09-13T19:16:17.438552Z","shell.execute_reply":"2023-09-13T19:16:17.443991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img.mode","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:18:33.567089Z","iopub.execute_input":"2023-09-13T19:18:33.56831Z","iopub.status.idle":"2023-09-13T19:18:33.575506Z","shell.execute_reply.started":"2023-09-13T19:18:33.568266Z","shell.execute_reply":"2023-09-13T19:18:33.574211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Checking Data Distribution**","metadata":{}},{"cell_type":"code","source":"widths = []\nheights = []\n\nfor img_file in os.listdir(data_dir):\n    img = Image.open(os.path.join(data_dir, img_file))\n    width, height = img.size\n    widths.append(width)\n    heights.append(height)\n\nprint('Average image size:', np.mean(widths), 'x', np.mean(heights))\n\n# Check the distribution of image sizes\nfig, axs = plt.subplots(1, 2, figsize=(15, 6))\naxs[0].hist(widths, bins=50)\naxs[0].set_xlabel('Image width')\naxs[0].set_ylabel('Frequency')\naxs[1].hist(heights, bins=50)\naxs[1].set_xlabel('Image height')\naxs[1].set_ylabel('Frequency')\nplt.show()\n\n# Check the distribution of image modes\nmodes = []\n\nfor img_file in os.listdir(data_dir):\n    img = Image.open(os.path.join(data_dir, img_file))\n    modes.append(img.mode)\n\nprint('Image modes:', set(modes))","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:16:59.932484Z","iopub.execute_input":"2023-09-13T19:16:59.933015Z","iopub.status.idle":"2023-09-13T19:17:00.483451Z","shell.execute_reply.started":"2023-09-13T19:16:59.932962Z","shell.execute_reply":"2023-09-13T19:17:00.481791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modes","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:24:57.246194Z","iopub.execute_input":"2023-09-13T19:24:57.246645Z","iopub.status.idle":"2023-09-13T19:24:57.255478Z","shell.execute_reply.started":"2023-09-13T19:24:57.246607Z","shell.execute_reply":"2023-09-13T19:24:57.254327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Check for class distribution**","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df = pd.read_csv('/kaggle/working/trainLabels.csv')\nlabels_df['level'].hist(bins=5)     # bins means no. of class used (histogram specific)\nplt.xlabel('Class')\nplt.ylabel('Frequency')\nplt.title('Class Distribution')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:25:13.835916Z","iopub.execute_input":"2023-09-13T19:25:13.836429Z","iopub.status.idle":"2023-09-13T19:25:14.143651Z","shell.execute_reply.started":"2023-09-13T19:25:13.836382Z","shell.execute_reply":"2023-09-13T19:25:14.142178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_df['level']","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:25:18.70763Z","iopub.execute_input":"2023-09-13T19:25:18.708179Z","iopub.status.idle":"2023-09-13T19:25:18.718482Z","shell.execute_reply.started":"2023-09-13T19:25:18.708133Z","shell.execute_reply":"2023-09-13T19:25:18.717488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Checking for class Imbalance**","metadata":{}},{"cell_type":"code","source":"class_counts = labels_df['level'].value_counts()\nclass_counts","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:27:31.058597Z","iopub.execute_input":"2023-09-13T19:27:31.059273Z","iopub.status.idle":"2023-09-13T19:27:31.072148Z","shell.execute_reply.started":"2023-09-13T19:27:31.059223Z","shell.execute_reply":"2023-09-13T19:27:31.070811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_counts = labels_df['level'].value_counts()\ntotal_samples = class_counts.sum()\n\nfor i in range(5):\n    count = class_counts[i]\n    percent = (count / total_samples) * 100\n    print('Level', i, ':', count, 'samples (', percent, '% of total )')","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:27:56.151132Z","iopub.execute_input":"2023-09-13T19:27:56.152437Z","iopub.status.idle":"2023-09-13T19:27:56.161936Z","shell.execute_reply.started":"2023-09-13T19:27:56.152372Z","shell.execute_reply":"2023-09-13T19:27:56.160384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Analysing the distribution of target variable**","metadata":{}},{"cell_type":"code","source":"labels_df['level'].value_counts().plot(kind='pie', autopct='%1.1f%%', startangle=90, colors=['#7FB3D5', '#F7CAC9', '#EEDD82', '#FFA07A', '#90EE90'])\nplt.axis('equal')\nplt.legend(labels=['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferative DR'], loc='upper left', bbox_to_anchor=(-0.1, 1.))\nplt.title('Distribution of Diabetic Retinopathy Levels')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-13T19:28:15.599326Z","iopub.execute_input":"2023-09-13T19:28:15.600134Z","iopub.status.idle":"2023-09-13T19:28:16.035307Z","shell.execute_reply.started":"2023-09-13T19:28:15.600091Z","shell.execute_reply":"2023-09-13T19:28:16.033674Z"},"trusted":true},"execution_count":null,"outputs":[]}],"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"}}