{"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":30918,"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\nfrom tensorflow.keras.applications import MobileNet\nfrom tensorflow.keras.models import Sequential\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-28T09:17:49.095545Z","iopub.execute_input":"2025-03-28T09:17:49.095996Z","iopub.status.idle":"2025-03-28T09:18:01.528725Z","shell.execute_reply.started":"2025-03-28T09:17:49.095954Z","shell.execute_reply":"2025-03-28T09:18:01.527826Z"}},"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-28T09:18:01.530059Z","iopub.execute_input":"2025-03-28T09:18:01.530689Z","iopub.status.idle":"2025-03-28T09:18:01.564507Z","shell.execute_reply.started":"2025-03-28T09:18:01.530663Z","shell.execute_reply":"2025-03-28T09:18:01.563875Z"}},"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-28T09:18:01.565863Z","iopub.execute_input":"2025-03-28T09:18:01.566079Z","iopub.status.idle":"2025-03-28T09:18:01.822000Z","shell.execute_reply.started":"2025-03-28T09:18:01.566060Z","shell.execute_reply":"2025-03-28T09:18:01.821264Z"}},"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-28T09:18:01.822933Z","iopub.execute_input":"2025-03-28T09:18:01.823125Z","iopub.status.idle":"2025-03-28T09:18:12.042712Z","shell.execute_reply.started":"2025-03-28T09:18:01.823108Z","shell.execute_reply":"2025-03-28T09:18:12.041806Z"}},"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-28T09:18:12.043896Z","iopub.execute_input":"2025-03-28T09:18:12.044264Z","iopub.status.idle":"2025-03-28T09:18:16.668750Z","shell.execute_reply.started":"2025-03-28T09:18:12.044233Z","shell.execute_reply":"2025-03-28T09:18:16.667479Z"}},"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-28T09:18:16.669617Z","iopub.execute_input":"2025-03-28T09:18:16.669852Z","iopub.status.idle":"2025-03-28T09:18:16.675289Z","shell.execute_reply.started":"2025-03-28T09:18:16.669833Z","shell.execute_reply":"2025-03-28T09:18:16.674422Z"}},"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-28T09:18:16.676396Z","iopub.execute_input":"2025-03-28T09:18:16.676941Z","iopub.status.idle":"2025-03-28T09:18:16.730456Z","shell.execute_reply.started":"2025-03-28T09:18:16.676914Z","shell.execute_reply":"2025-03-28T09:18:16.729490Z"}},"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-28T09:18:16.732713Z","iopub.execute_input":"2025-03-28T09:18:16.732939Z","iopub.status.idle":"2025-03-28T09:18:20.057183Z","shell.execute_reply.started":"2025-03-28T09:18:16.732920Z","shell.execute_reply":"2025-03-28T09:18:20.056461Z"}},"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-28T09:18:20.058474Z","iopub.execute_input":"2025-03-28T09:18:20.058721Z","iopub.status.idle":"2025-03-28T09:18:20.691307Z","shell.execute_reply.started":"2025-03-28T09:18:20.058700Z","shell.execute_reply":"2025-03-28T09:18:20.690637Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Modelling","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmobilenet = MobileNet(include_top=False, weights='imagenet', input_shape=(224,224,3))\nmobilenet.trainable = False  # Freeze pre-trained layers\n\nmodel.add(mobilenet)\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'))  # Output layer for 5 classes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T09:18:20.692038Z","iopub.execute_input":"2025-03-28T09:18:20.692367Z","iopub.status.idle":"2025-03-28T09:18:23.492263Z","shell.execute_reply.started":"2025-03-28T09:18:20.692340Z","shell.execute_reply":"2025-03-28T09:18:23.491616Z"}},"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-28T09:18:23.493095Z","iopub.execute_input":"2025-03-28T09:18:23.493385Z","iopub.status.idle":"2025-03-28T09:18:23.497165Z","shell.execute_reply.started":"2025-03-28T09:18:23.493356Z","shell.execute_reply":"2025-03-28T09:18:23.496445Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = Adam(learning_rate = 1e-4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T09:18:23.497986Z","iopub.execute_input":"2025-03-28T09:18:23.498264Z","iopub.status.idle":"2025-03-28T09:18:23.512688Z","shell.execute_reply.started":"2025-03-28T09:18:23.498237Z","shell.execute_reply":"2025-03-28T09:18:23.511879Z"}},"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-28T09:18:23.513456Z","iopub.execute_input":"2025-03-28T09:18:23.513702Z","iopub.status.idle":"2025-03-28T09:18:23.526093Z","shell.execute_reply.started":"2025-03-28T09:18:23.513683Z","shell.execute_reply":"2025-03-28T09:18:23.525421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T09:18:23.526983Z","iopub.execute_input":"2025-03-28T09:18:23.527259Z","iopub.status.idle":"2025-03-28T09:18:23.545714Z","shell.execute_reply.started":"2025-03-28T09:18:23.527237Z","shell.execute_reply":"2025-03-28T09:18:23.545120Z"}},"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-28T09:18:23.546396Z","iopub.execute_input":"2025-03-28T09:18:23.546700Z","iopub.status.idle":"2025-03-28T09:18:23.550241Z","shell.execute_reply.started":"2025-03-28T09:18:23.546672Z","shell.execute_reply":"2025-03-28T09:18:23.549590Z"}},"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-28T09:18:23.551030Z","iopub.execute_input":"2025-03-28T09:18:23.551251Z","iopub.status.idle":"2025-03-28T10:27:06.141676Z","shell.execute_reply.started":"2025-03-28T09:18:23.551232Z","shell.execute_reply":"2025-03-28T10:27:06.140974Z"}},"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-28T10:27:06.142482Z","iopub.execute_input":"2025-03-28T10:27:06.142758Z","iopub.status.idle":"2025-03-28T10:27:06.303689Z","shell.execute_reply.started":"2025-03-28T10:27:06.142729Z","shell.execute_reply":"2025-03-28T10:27:06.303005Z"}},"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-28T10:27:06.304411Z","iopub.execute_input":"2025-03-28T10:27:06.304667Z","iopub.status.idle":"2025-03-28T10:27:06.459231Z","shell.execute_reply.started":"2025-03-28T10:27:06.304635Z","shell.execute_reply":"2025-03-28T10:27:06.458448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save('model_MobileNet.h5')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-28T10:27:06.459942Z","iopub.execute_input":"2025-03-28T10:27:06.460147Z","iopub.status.idle":"2025-03-28T10:27:06.599862Z","shell.execute_reply.started":"2025-03-28T10:27:06.460127Z","shell.execute_reply":"2025-03-28T10:27:06.599010Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}