{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"},{"sourceId":418031,"sourceType":"datasetVersion","datasetId":131128},{"sourceId":952401,"sourceType":"datasetVersion","datasetId":517172},{"sourceId":7987155,"sourceType":"datasetVersion","datasetId":4701648}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from tensorflow import lite\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport numpy as np\nimport pandas as pd\nimport random, os\nimport shutil\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\n#from keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.metrics import categorical_accuracy\nfrom sklearn.model_selection import train_test_split\nfrom pathlib import Path\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-04T17:20:34.672690Z","iopub.execute_input":"2024-04-04T17:20:34.673598Z","iopub.status.idle":"2024-04-04T17:20:34.679717Z","shell.execute_reply.started":"2024-04-04T17:20:34.673561Z","shell.execute_reply":"2024-04-04T17:20:34.678809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -Uqq fastbook","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:20:40.042933Z","iopub.execute_input":"2024-04-04T17:20:40.043306Z","iopub.status.idle":"2024-04-04T17:20:55.465037Z","shell.execute_reply.started":"2024-04-04T17:20:40.043276Z","shell.execute_reply":"2024-04-04T17:20:55.463737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastbook import *\nfrom fastai.vision.widgets import *","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:21:00.738258Z","iopub.execute_input":"2024-04-04T17:21:00.738650Z","iopub.status.idle":"2024-04-04T17:21:00.745640Z","shell.execute_reply.started":"2024-04-04T17:21:00.738614Z","shell.execute_reply":"2024-04-04T17:21:00.744536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/diabetic-retinopathy-224x224-gaussian-filtered/train.csv')\ndf_train.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:21:07.842506Z","iopub.execute_input":"2024-04-04T17:21:07.843036Z","iopub.status.idle":"2024-04-04T17:21:07.863433Z","shell.execute_reply.started":"2024-04-04T17:21:07.842997Z","shell.execute_reply":"2024-04-04T17:21:07.862484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_train.drop(columns=['Unnamed: 0.1','Unnamed: 0'], inplace=True)\n#df_train.rename(columns={'image': 'id_code'}, inplace=True)\n#df_train.rename(columns={'level': 'diagnosis'}, inplace=True)\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:21:09.407850Z","iopub.execute_input":"2024-04-04T17:21:09.408714Z","iopub.status.idle":"2024-04-04T17:21:09.417977Z","shell.execute_reply.started":"2024-04-04T17:21:09.408674Z","shell.execute_reply":"2024-04-04T17:21:09.416840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['diagnosis'].hist()\ndf_train['diagnosis'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:21:11.032653Z","iopub.execute_input":"2024-04-04T17:21:11.033059Z","iopub.status.idle":"2024-04-04T17:21:11.342571Z","shell.execute_reply.started":"2024-04-04T17:21:11.033027Z","shell.execute_reply":"2024-04-04T17:21:11.341557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diagnosis_dict = {\n    0: 'No_DR',\n    1: 'Mild',\n    2: 'Moderate',\n    3: 'Severe',\n    4: 'Proliferate_DR',\n}\n\ndf_train['type'] = df_train['diagnosis'].map(diagnosis_dict.get)\ndf_train.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:21:13.367877Z","iopub.execute_input":"2024-04-04T17:21:13.368723Z","iopub.status.idle":"2024-04-04T17:21:13.383993Z","shell.execute_reply.started":"2024-04-04T17:21:13.368684Z","shell.execute_reply":"2024-04-04T17:21:13.382787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['type'].value_counts().plot(kind='barh')","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:21:14.907836Z","iopub.execute_input":"2024-04-04T17:21:14.908223Z","iopub.status.idle":"2024-04-04T17:21:15.124105Z","shell.execute_reply.started":"2024-04-04T17:21:14.908194Z","shell.execute_reply":"2024-04-04T17:21:15.123168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train, test = train_test_split(df_train, test_size = 0.1, stratify = df_train['type'])\ntrain = df_train\nprint(\"For Training Dataset :\")\nprint(train['type'].value_counts(), '\\n')\n# print(\"For Testing Dataset :\")\n# print(test['type'].value_counts(), '\\n')\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:21:17.537936Z","iopub.execute_input":"2024-04-04T17:21:17.538675Z","iopub.status.idle":"2024-04-04T17:21:17.546480Z","shell.execute_reply.started":"2024-04-04T17:21:17.538640Z","shell.execute_reply":"2024-04-04T17:21:17.545314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#histogram eq\ndef preprocessing1(img):\n    red, green, blue = cv2.split(img)\n    hist_red = cv2.equalizeHist(red)\n    hist_green = cv2.equalizeHist(green)\n    hist_blue = cv2.equalizeHist(blue)\n    img_eq = cv2.merge((hist_red, hist_green, hist_blue))\n    return img_eq\n\n#gaussian blur\ndef preprocessing2(img):\n    img_ben = cv2.addWeighted(img, 4, cv2.GaussianBlur(img, (0, 0), 10), -4, 128)\n    return img_ben\n\ndef preprocessing3(image, kernel_size=30, sigma=19.9, theta = 0, lambda_=9.8, gamma=6.08):\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    gabor_kernel = cv2.getGaborKernel(\n        (kernel_size, kernel_size), \n        sigma, \n        theta, \n        lambda_, \n        gamma, \n        ktype=cv2.CV_32F\n    )\n\n    filtered_image = cv2.filter2D(image, cv2.CV_32F, gabor_kernel)\n    filtered_rgb_image = cv2.cvtColor(filtered_image, cv2.COLOR_GRAY2RGB)\n#     print(filtered_rgb_image.shape)\n    return filtered_rgb_image\n\ndef preprocessing4(img):\n    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n    \n    kernel = np.ones((1000,1000), np.float64)\n    background = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)\n    \n    shading_correction = cv2.subtract(gray, background)\n    shading_correction = cv2.cvtColor(shading_correction, cv2.COLOR_GRAY2RGB)\n    shading_correction = cv2.resize(shading_correction,(224,224))\n    return shading_correction","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:21:32.273361Z","iopub.execute_input":"2024-04-04T17:21:32.273813Z","iopub.status.idle":"2024-04-04T17:21:32.285914Z","shell.execute_reply.started":"2024-04-04T17:21:32.273776Z","shell.execute_reply":"2024-04-04T17:21:32.284825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def apply_gaussian_filter(image, sigmaX=10):\n    # Applying Gaussian blur\n    gaussian = cv2.addWeighted(image, 4, cv2.GaussianBlur(image, (0,0), sigmaX), -4, 128)\n    return gaussian","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:21:34.522984Z","iopub.execute_input":"2024-04-04T17:21:34.523342Z","iopub.status.idle":"2024-04-04T17:21:34.528858Z","shell.execute_reply.started":"2024-04-04T17:21:34.523314Z","shell.execute_reply":"2024-04-04T17:21:34.527614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = ''\n\ntrain_dir = os.path.join(base_dir, 'train')\n#test_dir = os.path.join(base_dir, 'test')\n\nif os.path.exists(train_dir):\n    shutil.rmtree(train_dir)\nos.makedirs(train_dir)\n\n# if os.path.exists(test_dir):\n#     shutil.rmtree(test_dir)\n# os.makedirs(test_dir)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:21:43.732715Z","iopub.execute_input":"2024-04-04T17:21:43.733512Z","iopub.status.idle":"2024-04-04T17:21:43.739053Z","shell.execute_reply.started":"2024-04-04T17:21:43.733478Z","shell.execute_reply":"2024-04-04T17:21:43.738189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src_dir = \"/kaggle/input/diabetic-retinopathy-224x224-gaussian-filtered/gaussian_filtered_images/gaussian_filtered_images\"\n\n\nfor index,row in train.iterrows():\n    diagnosis = row['type']\n    #diagnosis_bia = row['diagnosis']\n    if diagnosis == 'No_DR':\n        continue\n    id_code = row['id_code'] + \".png\"\n    srcfile = os.path.join(src_dir,diagnosis,id_code)\n    dstfile = os.path.join(train_dir, diagnosis)\n    os.makedirs(dstfile, exist_ok = True)\n    image = cv2.imread(srcfile)\n    processed_image = apply_gaussian_filter(image)\n    dstfile_processed = os.path.join(dstfile, id_code)\n    cv2.imwrite(dstfile_processed, processed_image)\n#\n# for index, row in test.iterrows():\n#     diagnosis = row['type']\n#     id_code = row['image'] + \".jpeg\"\n#     srcfile = os.path.join(src_dir,id_code)\n#     dstfile = os.path.join(test_dir, diagnosis)\n#     os.makedirs(dstfile, exist_ok = True)\n#     shutil.copy(srcfile, dstfile)\n    \n#     image = cv2.imread(srcfile)\n    \n#     processed_image = preprocessing2(image)\n#     dstfile_processed = os.path.join(dstfile, id_code)\n#     cv2.imwrite(dstfile_processed, processed_image)\"\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:22:42.327871Z","iopub.execute_input":"2024-04-04T17:22:42.328914Z","iopub.status.idle":"2024-04-04T17:23:11.856011Z","shell.execute_reply.started":"2024-04-04T17:22:42.328876Z","shell.execute_reply":"2024-04-04T17:23:11.855002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = 'train'\n#test_path = 'test'\n\ntrain_batches = ImageDataGenerator(rescale = 1./255).flow_from_directory(train_path, target_size=(224,224), shuffle = True)\n#test_batches = ImageDataGenerator(rescale = 1./255).flow_from_directory(test_path, target_size=(224,224), shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:24:25.128405Z","iopub.execute_input":"2024-04-04T17:24:25.128922Z","iopub.status.idle":"2024-04-04T17:24:25.236021Z","shell.execute_reply.started":"2024-04-04T17:24:25.128880Z","shell.execute_reply":"2024-04-04T17:24:25.235025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path=Path(\"/kaggle/working/train\")\n#test_path=Path(\"/kaggle/working/test\")","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:24:27.062744Z","iopub.execute_input":"2024-04-04T17:24:27.063517Z","iopub.status.idle":"2024-04-04T17:24:27.067929Z","shell.execute_reply.started":"2024-04-04T17:24:27.063481Z","shell.execute_reply":"2024-04-04T17:24:27.066966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dc = DataBlock(\n    blocks=(ImageBlock,CategoryBlock),\n    get_items=get_image_files,\n    splitter=RandomSplitter(valid_pct=0.1,seed=42),\n    get_y=parent_label,\n    item_tfms=Resize(128)\n)\ndls = dc.dataloaders(train_path)\ndls.valid.show_batch(max_n=4,nrows=1)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:24:30.252663Z","iopub.execute_input":"2024-04-04T17:24:30.253077Z","iopub.status.idle":"2024-04-04T17:24:31.237925Z","shell.execute_reply.started":"2024-04-04T17:24:30.253042Z","shell.execute_reply":"2024-04-04T17:24:31.236827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls,resnet18,metrics=error_rate)\nlearn.fine_tune(5)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:24:41.792928Z","iopub.execute_input":"2024-04-04T17:24:41.793351Z","iopub.status.idle":"2024-04-04T17:25:07.440334Z","shell.execute_reply.started":"2024-04-04T17:24:41.793316Z","shell.execute_reply":"2024-04-04T17:25:07.439110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:25:36.498102Z","iopub.execute_input":"2024-04-04T17:25:36.498861Z","iopub.status.idle":"2024-04-04T17:25:38.093316Z","shell.execute_reply.started":"2024-04-04T17:25:36.498819Z","shell.execute_reply":"2024-04-04T17:25:38.091848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(5,nrows=1)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:25:52.573389Z","iopub.execute_input":"2024-04-04T17:25:52.573837Z","iopub.status.idle":"2024-04-04T17:25:53.443336Z","shell.execute_reply.started":"2024-04-04T17:25:52.573734Z","shell.execute_reply":"2024-04-04T17:25:53.442248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# test_db = DataBlock(\n#     blocks=(ImageBlock, CategoryBlock), \n#     get_items=get_image_files,\n#     #splitter=GrandparentSplitter(valid_name='test'),\n    \n#     get_y=parent_label,\n#     item_tfms=Resize(224)\n# )\n\n\n# test_dl = test_db.dataloaders(test_path)\n# learn.test_dl = test_dl\n# results = learn.validate()\n# #print(f\"loss:{results[0]}\")\n# print(f\"accuracy:{1-results[1]}\")","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:26:13.473631Z","iopub.execute_input":"2024-04-04T17:26:13.474060Z","iopub.status.idle":"2024-04-04T17:26:13.478998Z","shell.execute_reply.started":"2024-04-04T17:26:13.474024Z","shell.execute_reply":"2024-04-04T17:26:13.478090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred,idx,probs = learn.predict(PILImage.create('/kaggle/working/train/Moderate/b98f77098b9d.png'))\nprint(pred)\nprint(f\"The probability of {pred} '{probs[idx]:.4f}'\")","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:27:00.359409Z","iopub.execute_input":"2024-04-04T17:27:00.359801Z","iopub.status.idle":"2024-04-04T17:27:00.516316Z","shell.execute_reply.started":"2024-04-04T17:27:00.359769Z","shell.execute_reply":"2024-04-04T17:27:00.515384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = cv2.imread('/kaggle/input/diabetic-retinopathy-resized/resized_train_cropped/resized_train_cropped/54_left.jpeg') \nabc = preprocessing2(image)\npred,idx,probs = learn.predict(PILImage.create(abc))\nprint(pred)\nprint(f\"The probability of {pred} '{probs[idx]:.4f}'\")","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:35:21.303101Z","iopub.execute_input":"2024-04-04T17:35:21.303516Z","iopub.status.idle":"2024-04-04T17:35:21.389187Z","shell.execute_reply.started":"2024-04-04T17:35:21.303483Z","shell.execute_reply":"2024-04-04T17:35:21.388269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export('/kaggle/working/stage-2_resnet.pkl')","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:36:40.942865Z","iopub.execute_input":"2024-04-04T17:36:40.943618Z","iopub.status.idle":"2024-04-04T17:36:41.060475Z","shell.execute_reply.started":"2024-04-04T17:36:40.943577Z","shell.execute_reply":"2024-04-04T17:36:41.059415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:38:47.415101Z","iopub.execute_input":"2024-04-04T17:38:47.415518Z","iopub.status.idle":"2024-04-04T17:38:48.405633Z","shell.execute_reply.started":"2024-04-04T17:38:47.415484Z","shell.execute_reply":"2024-04-04T17:38:48.404425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink \nFileLink(r'stage-2_resnet.pkl')","metadata":{"execution":{"iopub.status.busy":"2024-04-04T17:38:57.603256Z","iopub.execute_input":"2024-04-04T17:38:57.603680Z","iopub.status.idle":"2024-04-04T17:38:57.610990Z","shell.execute_reply.started":"2024-04-04T17:38:57.603641Z","shell.execute_reply":"2024-04-04T17:38:57.609992Z"},"trusted":true},"execution_count":null,"outputs":[]}]}