{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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"}],"dockerImageVersionId":30919,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport os\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as  plt\nimport tensorflow as tf\nfrom tensorflow.keras import layers,models,optimizers\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout,Flatten\nfrom sklearn.metrics import accuracy_score\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img,img_to_array\nfrom tensorflow.keras.applications import VGG16,VGG19\nfrom tensorflow.keras.applications.efficientnet import EfficientNetB3\nimport warnings\nwarnings.simplefilter('ignore')\nfrom PIL import Image\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\n\nbase_dir = \"/kaggle/input/aptos2019-blindness-detection\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:17:45.619029Z","iopub.execute_input":"2025-03-28T07:17:45.619324Z","iopub.status.idle":"2025-03-28T07:17:59.367088Z","shell.execute_reply.started":"2025-03-28T07:17:45.619296Z","shell.execute_reply":"2025-03-28T07:17:59.366087Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Loading Data + EDA","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/aptos2019-blindness-detection/train.csv\")\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:00.168891Z","iopub.execute_input":"2025-03-28T07:18:00.169426Z","iopub.status.idle":"2025-03-28T07:18:00.205207Z","shell.execute_reply.started":"2025-03-28T07:18:00.169398Z","shell.execute_reply":"2025-03-28T07:18:00.204456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_csv = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')\ncounts = train_csv['diagnosis'].value_counts()\nclass_list = ['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferate']\nfor i,x in enumerate(class_list):\n    counts[x] = counts.pop(i)\n\nplt.figure(figsize=(10,5))\nsns.barplot(x=counts.index, y=counts.values, alpha=0.8, palette='bright')\nplt.title('Distribution of Output Classes')\nplt.ylabel('Number of Occurrences', fontsize=12)\nplt.xlabel('Target Classes', fontsize=12)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:00.519533Z","iopub.execute_input":"2025-03-28T07:18:00.519814Z","iopub.status.idle":"2025-03-28T07:18:00.783126Z","shell.execute_reply.started":"2025-03-28T07:18:00.519790Z","shell.execute_reply":"2025-03-28T07:18:00.782180Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig = plt.figure(figsize=(30, 6))\n# display 20 images\ntrain_imgs = os.listdir(base_dir+\"/train_images\")\nfor idx, img in enumerate(np.random.choice(train_imgs, 16)):\n    ax = fig.add_subplot(2, 16//2, idx+1, xticks=[], yticks=[])\n    im = Image.open(base_dir+\"/train_images/\" + img)\n    plt.imshow(im)\n    lab = train_csv.loc[train_csv['id_code'] == img.split('.')[0], 'diagnosis'].values[0]\n    ax.set_title('Severity: %s'%lab)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:00.784394Z","iopub.execute_input":"2025-03-28T07:18:00.784611Z","iopub.status.idle":"2025-03-28T07:18:12.628098Z","shell.execute_reply.started":"2025-03-28T07:18:00.784592Z","shell.execute_reply":"2025-03-28T07:18:12.627221Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Visualizing Test Set","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(30, 6))\n# display 20 images\ntest_imgs = os.listdir(base_dir+\"/test_images\")\nfor idx, img in enumerate(np.random.choice(test_imgs, 16)):\n    ax = fig.add_subplot(2, 16//2, idx+1, xticks=[], yticks=[])\n    im = Image.open(base_dir+\"/test_images/\" + img)\n    plt.imshow(im)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:12.629187Z","iopub.execute_input":"2025-03-28T07:18:12.629422Z","iopub.status.idle":"2025-03-28T07:18:17.437586Z","shell.execute_reply.started":"2025-03-28T07:18:12.629402Z","shell.execute_reply":"2025-03-28T07:18:17.436711Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Processing","metadata":{}},{"cell_type":"code","source":"df[\"id_code\"] = df[\"id_code\"].apply(lambda x: x + \".png\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:19.456889Z","iopub.execute_input":"2025-03-28T07:18:19.457206Z","iopub.status.idle":"2025-03-28T07:18:19.462975Z","shell.execute_reply.started":"2025-03-28T07:18:19.457180Z","shell.execute_reply":"2025-03-28T07:18:19.462038Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = df.iloc[:3000,:]\ntest_df = df.iloc[3000:,:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:19.849779Z","iopub.execute_input":"2025-03-28T07:18:19.850059Z","iopub.status.idle":"2025-03-28T07:18:19.854094Z","shell.execute_reply.started":"2025-03-28T07:18:19.850036Z","shell.execute_reply":"2025-03-28T07:18:19.853123Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['diagnosis'] = train_df['diagnosis'].astype('str')\ngen = ImageDataGenerator(\n    horizontal_flip = True,\n    vertical_flip = True,\n    shear_range = 0.2,\n    zoom_range = 0.2,\n    rescale = 1/255.,\n)\ntrain_datagen = gen.flow_from_dataframe(\n    train_df,\n    directory = \"/kaggle/input/aptos2019-blindness-detection/train_images\",\n    batch_size = 32,\n    target_size = (224,224),\n    seed = 42,\n    x_col = 'id_code',\n    y_col = 'diagnosis',\n    class_mode = 'categorical'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:20.165647Z","iopub.execute_input":"2025-03-28T07:18:20.165901Z","iopub.status.idle":"2025-03-28T07:18:23.186545Z","shell.execute_reply.started":"2025-03-28T07:18:20.165880Z","shell.execute_reply":"2025-03-28T07:18:23.185642Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['diagnosis'] = test_df['diagnosis'].astype('str')\ngen = ImageDataGenerator(\n    rescale = 1/255.,\n)\ntest_datagen = gen.flow_from_dataframe(\n    test_df,\n    directory=\"/kaggle/input/aptos2019-blindness-detection/train_images\",\n    batch_size = 32,\n    target_size = (224,224),\n    seed = 42,\n    x_col = 'id_code',\n    y_col = 'diagnosis',\n    class_mode = 'categorical'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:23.380118Z","iopub.execute_input":"2025-03-28T07:18:23.380357Z","iopub.status.idle":"2025-03-28T07:18:24.064004Z","shell.execute_reply.started":"2025-03-28T07:18:23.380338Z","shell.execute_reply":"2025-03-28T07:18:24.063285Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Modelling","metadata":{}},{"cell_type":"code","source":"model = models.Sequential()\nvgg = VGG19(include_top = False,weights = 'imagenet',input_shape=(224,224,3))\nvgg.trainable = False\nmodel.add(vgg)\nmodel.add(GlobalAveragePooling2D())\n\nmodel.add(Flatten())\n\nmodel.add(Dense(256,activation='elu'))\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(5,activation='softmax'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:27.682755Z","iopub.execute_input":"2025-03-28T07:18:27.683072Z","iopub.status.idle":"2025-03-28T07:18:30.826415Z","shell.execute_reply.started":"2025-03-28T07:18:27.683047Z","shell.execute_reply":"2025-03-28T07:18:30.825767Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss = tf.keras.losses.CategoricalCrossentropy(\n    label_smoothing = 0.001,\n    name = 'categorical_crossentropy'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:30.827336Z","iopub.execute_input":"2025-03-28T07:18:30.827650Z","iopub.status.idle":"2025-03-28T07:18:30.831265Z","shell.execute_reply.started":"2025-03-28T07:18:30.827614Z","shell.execute_reply":"2025-03-28T07:18:30.830453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = Adam(learning_rate = 1e-4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:30.832546Z","iopub.execute_input":"2025-03-28T07:18:30.832840Z","iopub.status.idle":"2025-03-28T07:18:30.851127Z","shell.execute_reply.started":"2025-03-28T07:18:30.832820Z","shell.execute_reply":"2025-03-28T07:18:30.850292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer = optimizer,loss=loss,metrics= ['categorical_accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:38.264102Z","iopub.execute_input":"2025-03-28T07:18:38.264386Z","iopub.status.idle":"2025-03-28T07:18:38.272641Z","shell.execute_reply.started":"2025-03-28T07:18:38.264365Z","shell.execute_reply":"2025-03-28T07:18:38.271755Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:40.786509Z","iopub.execute_input":"2025-03-28T07:18:40.786816Z","iopub.status.idle":"2025-03-28T07:18:40.804179Z","shell.execute_reply.started":"2025-03-28T07:18:40.786794Z","shell.execute_reply":"2025-03-28T07:18:40.803322Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Training ","metadata":{}},{"cell_type":"code","source":"rlrong = ReduceLROnPlateau(\n    monitor='val_loss',\n    factor=0.2,\n    mode='min',\n    min_lr = 1e-5,\n    patience = 2,\n    verbose=1\n)\nestop = EarlyStopping(\n    monitor = 'val_loss',\n    mode= 'min',\n    patience = 3,\n    verbose = 1,\n    restore_best_weights = True\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:54.996893Z","iopub.execute_input":"2025-03-28T07:18:54.997252Z","iopub.status.idle":"2025-03-28T07:18:55.001403Z","shell.execute_reply.started":"2025-03-28T07:18:54.997210Z","shell.execute_reply":"2025-03-28T07:18:55.000467Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(train_datagen,epochs = 20,verbose=1,validation_data = test_datagen,callbacks = [rlrong,estop])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T07:18:59.544107Z","iopub.execute_input":"2025-03-28T07:18:59.544427Z","iopub.status.idle":"2025-03-28T09:12:30.668191Z","shell.execute_reply.started":"2025-03-28T07:18:59.544394Z","shell.execute_reply":"2025-03-28T09:12:30.667069Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['loss'],label='loss',color='red')\nplt.plot(history.history['val_loss'],label='val loss',color='blue')\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T09:12:30.669394Z","iopub.execute_input":"2025-03-28T09:12:30.669616Z","iopub.status.idle":"2025-03-28T09:12:30.829438Z","shell.execute_reply.started":"2025-03-28T09:12:30.669598Z","shell.execute_reply":"2025-03-28T09:12:30.828727Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['categorical_accuracy'],label='categorical accuracy',color='red')\nplt.plot(history.history['val_categorical_accuracy'],label='val categorical accuracy',color='blue')\nplt.legend()\nplt.grid()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T09:12:30.830675Z","iopub.execute_input":"2025-03-28T09:12:30.830883Z","iopub.status.idle":"2025-03-28T09:12:30.985853Z","shell.execute_reply.started":"2025-03-28T09:12:30.830863Z","shell.execute_reply":"2025-03-28T09:12:30.985166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save('model_VGG16.h5')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T09:12:30.987135Z","iopub.execute_input":"2025-03-28T09:12:30.987386Z","iopub.status.idle":"2025-03-28T09:12:31.140326Z","shell.execute_reply.started":"2025-03-28T09:12:30.987366Z","shell.execute_reply":"2025-03-28T09:12:31.139681Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}