{"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":4104,"databundleVersionId":46661,"sourceType":"competition"},{"sourceId":7866129,"sourceType":"datasetVersion","datasetId":4614938}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","scrolled":true,"execution":{"iopub.status.busy":"2024-07-01T11:48:18.277577Z","iopub.execute_input":"2024-07-01T11:48:18.277889Z","iopub.status.idle":"2024-07-01T11:49:00.121990Z","shell.execute_reply.started":"2024-07-01T11:48:18.277863Z","shell.execute_reply":"2024-07-01T11:49:00.121083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nimport os\n","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:07:37.119128Z","iopub.execute_input":"2024-07-01T11:07:37.119636Z","iopub.status.idle":"2024-07-01T11:07:37.124398Z","shell.execute_reply.started":"2024-07-01T11:07:37.119603Z","shell.execute_reply":"2024-07-01T11:07:37.123357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arritems = os.listdir('/kaggle/input/diabetic-retinopathy-detection')\narritems","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:07:37.853077Z","iopub.execute_input":"2024-07-01T11:07:37.853844Z","iopub.status.idle":"2024-07-01T11:07:37.861392Z","shell.execute_reply.started":"2024-07-01T11:07:37.853809Z","shell.execute_reply":"2024-07-01T11:07:37.860276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"/kaggle/input/diabetic-retinopathy-detection/sampleSubmission.csv.zip\", \"r\") as zip_ref:\n#     zip_ref.extractall()\n    zip_ref.extractall(\"/kaggle/working/sample\") \n","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:07:40.077111Z","iopub.execute_input":"2024-07-01T11:07:40.077464Z","iopub.status.idle":"2024-07-01T11:07:40.094769Z","shell.execute_reply.started":"2024-07-01T11:07:40.077436Z","shell.execute_reply":"2024-07-01T11:07:40.093868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"/kaggle/input/diabetic-retinopathy-detection/sample.zip\", \"r\") as zip_ref:\n#     zip_ref.extractall()\n    zip_ref.extractall(\"/kaggle/working/sample\") ","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:07:41.347257Z","iopub.execute_input":"2024-07-01T11:07:41.347912Z","iopub.status.idle":"2024-07-01T11:07:41.583982Z","shell.execute_reply.started":"2024-07-01T11:07:41.347878Z","shell.execute_reply":"2024-07-01T11:07:41.583006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten, Input, Activation\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:07:42.368391Z","iopub.execute_input":"2024-07-01T11:07:42.369302Z","iopub.status.idle":"2024-07-01T11:07:55.876433Z","shell.execute_reply.started":"2024-07-01T11:07:42.369266Z","shell.execute_reply":"2024-07-01T11:07:55.875472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputPath= \"/kaggle/input/diabetic-retinopathy-detection\"\noutputPath = \"/kaggle/working/sample\"\n","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:07:55.878197Z","iopub.execute_input":"2024-07-01T11:07:55.878800Z","iopub.status.idle":"2024-07-01T11:07:55.883428Z","shell.execute_reply.started":"2024-07-01T11:07:55.878771Z","shell.execute_reply":"2024-07-01T11:07:55.882333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_classes = [0,1,2,3,4]","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:07:55.884927Z","iopub.execute_input":"2024-07-01T11:07:55.885298Z","iopub.status.idle":"2024-07-01T11:07:56.004718Z","shell.execute_reply.started":"2024-07-01T11:07:55.885265Z","shell.execute_reply":"2024-07-01T11:07:56.003676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = outputPath+\"/sample\"\nos.listdir(train_path)\nimg_size = 224","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:07:56.006538Z","iopub.execute_input":"2024-07-01T11:07:56.006863Z","iopub.status.idle":"2024-07-01T11:07:56.013647Z","shell.execute_reply.started":"2024-07-01T11:07:56.006835Z","shell.execute_reply":"2024-07-01T11:07:56.012709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(train_path)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:07:56.052241Z","iopub.execute_input":"2024-07-01T11:07:56.052625Z","iopub.status.idle":"2024-07-01T11:07:56.059925Z","shell.execute_reply.started":"2024-07-01T11:07:56.052595Z","shell.execute_reply":"2024-07-01T11:07:56.058889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_tr = []\nfor img in os.listdir(train_path):\n    try:\n        img_arr =cv2.imread(os.path.join(train_path, img))[..., ::-1]\n#         print(img_arr)\n        resized_arr =cv2.resize(img_arr, (img_size, img_size))\n        x_tr.append(resized_arr)\n    except Exception as e:\n        print(e)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:08:00.798763Z","iopub.execute_input":"2024-07-01T11:08:00.799453Z","iopub.status.idle":"2024-07-01T11:08:02.870322Z","shell.execute_reply.started":"2024-07-01T11:08:00.799421Z","shell.execute_reply":"2024-07-01T11:08:02.869339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_tr[0])","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-07-01T11:08:03.181839Z","iopub.execute_input":"2024-07-01T11:08:03.182501Z","iopub.status.idle":"2024-07-01T11:08:03.552596Z","shell.execute_reply.started":"2024-07-01T11:08:03.182457Z","shell.execute_reply":"2024-07-01T11:08:03.551525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_tr = [0,0,1,0,2,0,0,4,1,4]","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:08:04.349647Z","iopub.execute_input":"2024-07-01T11:08:04.350036Z","iopub.status.idle":"2024-07-01T11:08:04.354883Z","shell.execute_reply.started":"2024-07-01T11:08:04.350005Z","shell.execute_reply":"2024-07-01T11:08:04.353766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_tr = np.asarray(x_tr)\nx_tr = x_tr/255\ny_tr = np.asarray(y_tr)\ny_tr = to_categorical(y_tr)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:08:06.005692Z","iopub.execute_input":"2024-07-01T11:08:06.006579Z","iopub.status.idle":"2024-07-01T11:08:06.014089Z","shell.execute_reply.started":"2024-07-01T11:08:06.006546Z","shell.execute_reply":"2024-07-01T11:08:06.013117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_tr.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:08:07.569205Z","iopub.execute_input":"2024-07-01T11:08:07.569585Z","iopub.status.idle":"2024-07-01T11:08:07.576276Z","shell.execute_reply.started":"2024-07-01T11:08:07.569556Z","shell.execute_reply":"2024-07-01T11:08:07.575201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n        featurewise_center = False, # set input mean to 0 over the dataset\n        samplewise_center = False, # set each sample mean to 0\n        featurewise_std_normalization = False, # divide inputs by std of the dataset\n        samplewise_std_normalization = False, # divide each input by its std\n        zca_whitening = False, # apply ZCA whitening\n        rotation_range = 0, # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.25, # Randomly zoom image\n        width_shift_range = 0.125, # randomly shift images horizontally (fraction of total width)\n        height_shift_range = 0.125, # randomly shift images vertically (fraction of total height)\n        horizontal_flip = True, # randomly flip images\n        vertical_flip = False) # randomly flip images\ndatagen.fit(x_tr)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:08:11.990249Z","iopub.execute_input":"2024-07-01T11:08:11.990950Z","iopub.status.idle":"2024-07-01T11:08:12.000283Z","shell.execute_reply.started":"2024-07-01T11:08:11.990917Z","shell.execute_reply":"2024-07-01T11:08:11.999311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.vgg19 import VGG19\nmodel = VGG19(weights='imagenet')\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:08:13.934885Z","iopub.execute_input":"2024-07-01T11:08:13.935271Z","iopub.status.idle":"2024-07-01T11:08:33.011544Z","shell.execute_reply.started":"2024-07-01T11:08:13.935242Z","shell.execute_reply":"2024-07-01T11:08:33.010529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_eye = VGG19(weights='imagenet', input_shape = (224, 224, 3), include_top = False)\nprint(model_eye.summary())","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:08:33.013653Z","iopub.execute_input":"2024-07-01T11:08:33.014139Z","iopub.status.idle":"2024-07-01T11:08:36.118212Z","shell.execute_reply.started":"2024-07-01T11:08:33.014104Z","shell.execute_reply":"2024-07-01T11:08:36.117249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layers in model_eye.layers:\n    layers.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:08:36.119635Z","iopub.execute_input":"2024-07-01T11:08:36.120432Z","iopub.status.idle":"2024-07-01T11:08:36.126429Z","shell.execute_reply.started":"2024-07-01T11:08:36.120395Z","shell.execute_reply":"2024-07-01T11:08:36.125530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_eye = Flatten()(model_eye.output)\nprediction_eye = Dense(5, activation = 'leaky_relu')(x_eye)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:13:10.631264Z","iopub.execute_input":"2024-07-01T11:13:10.631640Z","iopub.status.idle":"2024-07-01T11:13:10.647933Z","shell.execute_reply.started":"2024-07-01T11:13:10.631610Z","shell.execute_reply":"2024-07-01T11:13:10.646873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nmodel_eye = Model(inputs = model_eye.input, outputs = prediction_eye)\nprint(model_eye.summary())","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:13:11.782926Z","iopub.execute_input":"2024-07-01T11:13:11.783305Z","iopub.status.idle":"2024-07-01T11:13:11.831288Z","shell.execute_reply.started":"2024-07-01T11:13:11.783275Z","shell.execute_reply":"2024-07-01T11:13:11.830236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_eye.compile(loss = 'categorical_crossentropy', \n                 optimizer = 'adam',\n                 metrics = [\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:14:21.565863Z","iopub.execute_input":"2024-07-01T11:14:21.566245Z","iopub.status.idle":"2024-07-01T11:14:21.577114Z","shell.execute_reply.started":"2024-07-01T11:14:21.566214Z","shell.execute_reply":"2024-07-01T11:14:21.575946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_tr.shape, y_tr.shape","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2024-07-01T11:13:14.501426Z","iopub.execute_input":"2024-07-01T11:13:14.501834Z","iopub.status.idle":"2024-07-01T11:13:14.510242Z","shell.execute_reply.started":"2024-07-01T11:13:14.501802Z","shell.execute_reply":"2024-07-01T11:13:14.509006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_tr","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:13:15.897162Z","iopub.execute_input":"2024-07-01T11:13:15.897547Z","iopub.status.idle":"2024-07-01T11:13:15.904808Z","shell.execute_reply.started":"2024-07-01T11:13:15.897517Z","shell.execute_reply":"2024-07-01T11:13:15.903696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 5\nhist = model_eye.fit(x_tr,\n                 y_tr,\n                 epochs = epochs)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:14:26.181119Z","iopub.execute_input":"2024-07-01T11:14:26.181476Z","iopub.status.idle":"2024-07-01T11:14:44.617858Z","shell.execute_reply.started":"2024-07-01T11:14:26.181450Z","shell.execute_reply":"2024-07-01T11:14:44.616768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_X = model_eye.predict(x_tr)\ny_pred = np.argmax(pred_X, axis = 1)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2024-07-01T11:14:52.436765Z","iopub.execute_input":"2024-07-01T11:14:52.437157Z","iopub.status.idle":"2024-07-01T11:14:55.638533Z","shell.execute_reply.started":"2024-07-01T11:14:52.437128Z","shell.execute_reply":"2024-07-01T11:14:55.637436Z"},"trusted":true},"execution_count":null,"outputs":[]}]}