{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"* Preprocessing","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:27.192414Z","iopub.execute_input":"2024-11-17T23:12:27.192791Z","iopub.status.idle":"2024-11-17T23:12:27.198200Z","shell.execute_reply.started":"2024-11-17T23:12:27.192754Z","shell.execute_reply":"2024-11-17T23:12:27.197257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = '/kaggle/input/aptos2019-blindness-detection'\ntrain_img_dir = os.path.join(root_dir, 'train_images')\ntest_img_dir = os.path.join(root_dir, 'test_images')","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:30.430119Z","iopub.execute_input":"2024-11-17T23:12:30.430508Z","iopub.status.idle":"2024-11-17T23:12:30.435179Z","shell.execute_reply.started":"2024-11-17T23:12:30.430471Z","shell.execute_reply":"2024-11-17T23:12:30.434280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:32.934217Z","iopub.execute_input":"2024-11-17T23:12:32.935013Z","iopub.status.idle":"2024-11-17T23:12:32.938981Z","shell.execute_reply.started":"2024-11-17T23:12:32.934972Z","shell.execute_reply":"2024-11-17T23:12:32.937993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(os.path.join(root_dir, 'train.csv'))\ntest_df = pd.read_csv(os.path.join(root_dir, 'test.csv'))\ntrain_df['file_path'] = train_df['id_code'].apply(lambda x: os.path.join(train_img_dir, f\"{x}.png\"))\ntest_df['file_path'] = test_df['id_code'].apply(lambda x: os.path.join(test_img_dir, f\"{x}.png\"))","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:34.210431Z","iopub.execute_input":"2024-11-17T23:12:34.210806Z","iopub.status.idle":"2024-11-17T23:12:34.242963Z","shell.execute_reply.started":"2024-11-17T23:12:34.210769Z","shell.execute_reply":"2024-11-17T23:12:34.242134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['diagnosis'] = train_df['diagnosis'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:36.118407Z","iopub.execute_input":"2024-11-17T23:12:36.119122Z","iopub.status.idle":"2024-11-17T23:12:36.125447Z","shell.execute_reply.started":"2024-11-17T23:12:36.119084Z","shell.execute_reply":"2024-11-17T23:12:36.124368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_train_files = train_df[~train_df['file_path'].apply(os.path.exists)]\nif len(missing_train_files) > 0:\n    print(f\"Missing training files: {len(missing_train_files)}\")","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:37.812862Z","iopub.execute_input":"2024-11-17T23:12:37.813574Z","iopub.status.idle":"2024-11-17T23:12:39.158819Z","shell.execute_reply.started":"2024-11-17T23:12:37.813533Z","shell.execute_reply":"2024-11-17T23:12:39.157792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n     preprocessing_function=tf.keras.applications.resnet.preprocess_input,\n     rotation_range=20,\n     width_shift_range=0.2,\n     height_shift_range=0.2,\n     shear_range=0.2,\n     zoom_range=0.2,\n     horizontal_flip=True,\n     fill_mode='nearest',\n     validation_split=0.2\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:40.799666Z","iopub.execute_input":"2024-11-17T23:12:40.800388Z","iopub.status.idle":"2024-11-17T23:12:40.805391Z","shell.execute_reply.started":"2024-11-17T23:12:40.800347Z","shell.execute_reply":"2024-11-17T23:12:40.804404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    x_col='file_path',\n    y_col='diagnosis',\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='categorical',\n    subset='training' \n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:43.449969Z","iopub.execute_input":"2024-11-17T23:12:43.450346Z","iopub.status.idle":"2024-11-17T23:12:43.502917Z","shell.execute_reply.started":"2024-11-17T23:12:43.450309Z","shell.execute_reply":"2024-11-17T23:12:43.502050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_generator = train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    x_col='file_path',\n    y_col='diagnosis',\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode='categorical',\n    subset='validation' \n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:46.227495Z","iopub.execute_input":"2024-11-17T23:12:46.228357Z","iopub.status.idle":"2024-11-17T23:12:46.275837Z","shell.execute_reply.started":"2024-11-17T23:12:46.228318Z","shell.execute_reply":"2024-11-17T23:12:46.275140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(\n    preprocessing_function=tf.keras.applications.resnet.preprocess_input\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:49.092733Z","iopub.execute_input":"2024-11-17T23:12:49.093352Z","iopub.status.idle":"2024-11-17T23:12:49.097670Z","shell.execute_reply.started":"2024-11-17T23:12:49.093312Z","shell.execute_reply":"2024-11-17T23:12:49.096762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = test_datagen.flow_from_dataframe(\n    dataframe=test_df,\n    x_col='file_path',\n    y_col=None,\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode=None\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:50.992733Z","iopub.execute_input":"2024-11-17T23:12:50.993488Z","iopub.status.idle":"2024-11-17T23:12:51.809422Z","shell.execute_reply.started":"2024-11-17T23:12:50.993450Z","shell.execute_reply":"2024-11-17T23:12:51.808481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Training samples: {train_generator.samples}\")\nprint(f\"Validation samples: {val_generator.samples}\")\nprint(f\"Test samples: {test_generator.samples}\")","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:53.915734Z","iopub.execute_input":"2024-11-17T23:12:53.916449Z","iopub.status.idle":"2024-11-17T23:12:53.921277Z","shell.execute_reply.started":"2024-11-17T23:12:53.916407Z","shell.execute_reply":"2024-11-17T23:12:53.920347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:56.524288Z","iopub.execute_input":"2024-11-17T23:12:56.525156Z","iopub.status.idle":"2024-11-17T23:12:56.529876Z","shell.execute_reply.started":"2024-11-17T23:12:56.525116Z","shell.execute_reply":"2024-11-17T23:12:56.528954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Question 1","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:12:58.774777Z","iopub.execute_input":"2024-11-17T23:12:58.775164Z","iopub.status.idle":"2024-11-17T23:12:58.779633Z","shell.execute_reply.started":"2024-11-17T23:12:58.775129Z","shell.execute_reply":"2024-11-17T23:12:58.778716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.columns)  \nprint(test_df.columns)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:13:00.509792Z","iopub.execute_input":"2024-11-17T23:13:00.510155Z","iopub.status.idle":"2024-11-17T23:13:00.515222Z","shell.execute_reply.started":"2024-11-17T23:13:00.510124Z","shell.execute_reply":"2024-11-17T23:13:00.514306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(os.path.join(root_dir, 'train.csv'))\ntest_df = pd.read_csv(os.path.join(root_dir, 'test.csv'))","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:18:13.443476Z","iopub.execute_input":"2024-11-17T23:18:13.444253Z","iopub.status.idle":"2024-11-17T23:18:13.458206Z","shell.execute_reply.started":"2024-11-17T23:18:13.444214Z","shell.execute_reply":"2024-11-17T23:18:13.457119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['file_path'] = train_df['id_code'].apply(lambda x: os.path.join(train_img_dir, f\"{x}.png\"))\ntest_df['file_path'] = test_df['id_code'].apply(lambda x: os.path.join(test_img_dir, f\"{x}.png\"))\n\nprint(train_df[['id_code', 'file_path']].head())  \nprint(test_df[['id_code', 'file_path']].head())","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:13:02.476761Z","iopub.execute_input":"2024-11-17T23:13:02.477416Z","iopub.status.idle":"2024-11-17T23:13:02.501961Z","shell.execute_reply.started":"2024-11-17T23:13:02.477367Z","shell.execute_reply":"2024-11-17T23:13:02.501003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:18:41.162079Z","iopub.execute_input":"2024-11-17T23:18:41.162952Z","iopub.status.idle":"2024-11-17T23:18:41.167062Z","shell.execute_reply.started":"2024-11-17T23:18:41.162903Z","shell.execute_reply":"2024-11-17T23:18:41.166148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 3, figsize=(10, 5))\nfor i, idx in enumerate(train_df.sample(3).index):\n    img_path = os.path.join(train_img_dir, f\"{train_df.loc[idx, 'id_code']}.png\")\n    img = Image.open(img_path)\n    axes[i].imshow(img)\n    axes[i].axis('off')\n    axes[i].set_title(f\"Diagnosis: {train_df.loc[idx, 'diagnosis']}\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:18:54.458710Z","iopub.execute_input":"2024-11-17T23:18:54.459373Z","iopub.status.idle":"2024-11-17T23:18:55.800749Z","shell.execute_reply.started":"2024-11-17T23:18:54.459336Z","shell.execute_reply":"2024-11-17T23:18:55.799859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.head())  \nprint(test_df.head()) ","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:30:31.042232Z","iopub.execute_input":"2024-11-17T22:30:31.042616Z","iopub.status.idle":"2024-11-17T22:30:31.051223Z","shell.execute_reply.started":"2024-11-17T22:30:31.042579Z","shell.execute_reply":"2024-11-17T22:30:31.050158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.applications import EfficientNetB0, ResNet50\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:30:47.137496Z","iopub.execute_input":"2024-11-17T22:30:47.138218Z","iopub.status.idle":"2024-11-17T22:30:47.146091Z","shell.execute_reply.started":"2024-11-17T22:30:47.138167Z","shell.execute_reply":"2024-11-17T22:30:47.145157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:30:57.961489Z","iopub.execute_input":"2024-11-17T22:30:57.961833Z","iopub.status.idle":"2024-11-17T22:31:00.260179Z","shell.execute_reply.started":"2024-11-17T22:30:57.961802Z","shell.execute_reply":"2024-11-17T22:31:00.259417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(1024, activation='relu')(x)\nx = Dense(512, activation='relu')(x)\npredictions = Dense(len(train_generator.class_indices), activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:31:12.245992Z","iopub.execute_input":"2024-11-17T22:31:12.246868Z","iopub.status.idle":"2024-11-17T22:31:12.282769Z","shell.execute_reply.started":"2024-11-17T22:31:12.246828Z","shell.execute_reply":"2024-11-17T22:31:12.282057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Model(inputs=base_model.input, outputs=predictions)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:31:27.717479Z","iopub.execute_input":"2024-11-17T22:31:27.718184Z","iopub.status.idle":"2024-11-17T22:31:27.757946Z","shell.execute_reply.started":"2024-11-17T22:31:27.718145Z","shell.execute_reply":"2024-11-17T22:31:27.757014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(learning_rate=1e-4), \n              loss='categorical_crossentropy', \n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:31:36.461987Z","iopub.execute_input":"2024-11-17T22:31:36.462803Z","iopub.status.idle":"2024-11-17T22:31:36.476710Z","shell.execute_reply.started":"2024-11-17T22:31:36.462764Z","shell.execute_reply":"2024-11-17T22:31:36.475776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:31:47.446766Z","iopub.execute_input":"2024-11-17T22:31:47.447404Z","iopub.status.idle":"2024-11-17T22:31:47.769318Z","shell.execute_reply.started":"2024-11-17T22:31:47.447362Z","shell.execute_reply":"2024-11-17T22:31:47.768513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:32:11.177502Z","iopub.execute_input":"2024-11-17T22:32:11.177845Z","iopub.status.idle":"2024-11-17T22:32:11.182899Z","shell.execute_reply.started":"2024-11-17T22:32:11.177813Z","shell.execute_reply":"2024-11-17T22:32:11.181752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs=5,  \n    validation_data=val_generator,\n    callbacks=[early_stopping]\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T22:32:26.059284Z","iopub.execute_input":"2024-11-17T22:32:26.060307Z","iopub.status.idle":"2024-11-17T23:08:41.124108Z","shell.execute_reply.started":"2024-11-17T22:32:26.060265Z","shell.execute_reply":"2024-11-17T23:08:41.123077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_learning_curves(history):\n    plt.figure(figsize=(10, 5))","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:11:11.813088Z","iopub.execute_input":"2024-11-17T23:11:11.813543Z","iopub.status.idle":"2024-11-17T23:11:11.817867Z","shell.execute_reply.started":"2024-11-17T23:11:11.813494Z","shell.execute_reply":"2024-11-17T23:11:11.816977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Train Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Model Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:11:22.387006Z","iopub.execute_input":"2024-11-17T23:11:22.387657Z","iopub.status.idle":"2024-11-17T23:11:22.684236Z","shell.execute_reply.started":"2024-11-17T23:11:22.387617Z","shell.execute_reply":"2024-11-17T23:11:22.683296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplot(1, 2, 2)\nplt.plot(history.history['loss'], label='Train Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Model Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T23:11:50.121861Z","iopub.execute_input":"2024-11-17T23:11:50.122593Z","iopub.status.idle":"2024-11-17T23:11:50.419573Z","shell.execute_reply.started":"2024-11-17T23:11:50.122554Z","shell.execute_reply":"2024-11-17T23:11:50.418677Z"},"trusted":true},"execution_count":null,"outputs":[]}]}