{"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":"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","trusted":true},"execution_count":null,"outputs":[]},{"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-17T04:25:12.569388Z","iopub.execute_input":"2024-11-17T04:25:12.569751Z","iopub.status.idle":"2024-11-17T04:25:25.307838Z","shell.execute_reply.started":"2024-11-17T04:25:12.569716Z","shell.execute_reply":"2024-11-17T04:25:25.306902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.efficientnet import preprocess_input","metadata":{},"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-17T04:25:40.986314Z","iopub.execute_input":"2024-11-17T04:25:40.987325Z","iopub.status.idle":"2024-11-17T04:25:40.991875Z","shell.execute_reply.started":"2024-11-17T04:25:40.987278Z","shell.execute_reply":"2024-11-17T04:25:40.990852Z"},"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-17T04:25:59.715653Z","iopub.execute_input":"2024-11-17T04:25:59.716149Z","iopub.status.idle":"2024-11-17T04:25:59.740343Z","shell.execute_reply.started":"2024-11-17T04:25:59.716101Z","shell.execute_reply":"2024-11-17T04:25:59.739411Z"},"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\"))","metadata":{"execution":{"iopub.status.busy":"2024-11-17T04:26:17.322276Z","iopub.execute_input":"2024-11-17T04:26:17.323145Z","iopub.status.idle":"2024-11-17T04:26:17.346055Z","shell.execute_reply.started":"2024-11-17T04:26:17.323104Z","shell.execute_reply":"2024-11-17T04:26:17.344967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['diagnosis'] = train_df['diagnosis'].astype(str)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T04:26:29.448064Z","iopub.execute_input":"2024-11-17T04:26:29.448450Z","iopub.status.idle":"2024-11-17T04:26:29.456182Z","shell.execute_reply.started":"2024-11-17T04:26:29.448413Z","shell.execute_reply":"2024-11-17T04:26:29.455030Z"},"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-17T04:37:22.996510Z","iopub.execute_input":"2024-11-17T04:37:22.997458Z","iopub.status.idle":"2024-11-17T04:37:29.984383Z","shell.execute_reply.started":"2024-11-17T04:37:22.997413Z","shell.execute_reply":"2024-11-17T04:37:29.983342Z"},"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-17T04:38:39.668780Z","iopub.execute_input":"2024-11-17T04:38:39.669170Z","iopub.status.idle":"2024-11-17T04:38:39.674533Z","shell.execute_reply.started":"2024-11-17T04:38:39.669134Z","shell.execute_reply":"2024-11-17T04:38:39.673506Z"},"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-17T04:39:24.747771Z","iopub.execute_input":"2024-11-17T04:39:24.748183Z","iopub.status.idle":"2024-11-17T04:39:26.472889Z","shell.execute_reply.started":"2024-11-17T04:39:24.748146Z","shell.execute_reply":"2024-11-17T04:39:26.471964Z"},"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' # 20% of data used for validation\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T04:40:20.448677Z","iopub.execute_input":"2024-11-17T04:40:20.449576Z","iopub.status.idle":"2024-11-17T04:40:22.366132Z","shell.execute_reply.started":"2024-11-17T04:40:20.449535Z","shell.execute_reply":"2024-11-17T04:40:22.365141Z"},"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-17T04:40:43.347099Z","iopub.execute_input":"2024-11-17T04:40:43.347852Z","iopub.status.idle":"2024-11-17T04:40:43.352179Z","shell.execute_reply.started":"2024-11-17T04:40:43.347812Z","shell.execute_reply":"2024-11-17T04:40:43.351047Z"},"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    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T04:41:20.264939Z","iopub.execute_input":"2024-11-17T04:41:20.265746Z","iopub.status.idle":"2024-11-17T04:41:23.933543Z","shell.execute_reply.started":"2024-11-17T04:41:20.265703Z","shell.execute_reply":"2024-11-17T04:41:23.932677Z"},"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-17T04:41:55.634514Z","iopub.execute_input":"2024-11-17T04:41:55.635475Z","iopub.status.idle":"2024-11-17T04:41:55.640636Z","shell.execute_reply.started":"2024-11-17T04:41:55.635431Z","shell.execute_reply":"2024-11-17T04:41:55.639642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* 3 Autoencoder","metadata":{}},{"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-17T05:12:20.800875Z","iopub.execute_input":"2024-11-17T05:12:20.801389Z","iopub.status.idle":"2024-11-17T05:12:20.813141Z","shell.execute_reply.started":"2024-11-17T05:12:20.801353Z","shell.execute_reply":"2024-11-17T05:12:20.812165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = EfficientNetB0(weights='imagenet', include_top=False,input_shape=(224, 224, 3))\nbase_model.trainable = True","metadata":{"execution":{"iopub.status.busy":"2024-11-17T05:12:39.579150Z","iopub.execute_input":"2024-11-17T05:12:39.579516Z","iopub.status.idle":"2024-11-17T05:12:41.954994Z","shell.execute_reply.started":"2024-11-17T05:12:39.579483Z","shell.execute_reply":"2024-11-17T05:12:41.954167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = base_model.output","metadata":{"execution":{"iopub.status.busy":"2024-11-17T05:21:44.376061Z","iopub.execute_input":"2024-11-17T05:21:44.376822Z","iopub.status.idle":"2024-11-17T05:21:44.381340Z","shell.execute_reply.started":"2024-11-17T05:21:44.376781Z","shell.execute_reply":"2024-11-17T05:21:44.380067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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-17T05:14:43.620118Z","iopub.execute_input":"2024-11-17T05:14:43.621244Z","iopub.status.idle":"2024-11-17T05:14:43.626888Z","shell.execute_reply.started":"2024-11-17T05:14:43.621188Z","shell.execute_reply":"2024-11-17T05:14:43.625911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator_ae = 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='input',\n    subset='training' \n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T05:15:58.550824Z","iopub.execute_input":"2024-11-17T05:15:58.551549Z","iopub.status.idle":"2024-11-17T05:16:02.954098Z","shell.execute_reply.started":"2024-11-17T05:15:58.551508Z","shell.execute_reply":"2024-11-17T05:16:02.953122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_generator_ae = 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='input',\n    subset='validation' \n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T05:17:05.810830Z","iopub.execute_input":"2024-11-17T05:17:05.811762Z","iopub.status.idle":"2024-11-17T05:17:07.536980Z","shell.execute_reply.started":"2024-11-17T05:17:05.811718Z","shell.execute_reply":"2024-11-17T05:17:07.536006Z"},"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' # 80% of data used for training\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T05:17:52.012774Z","iopub.execute_input":"2024-11-17T05:17:52.013179Z","iopub.status.idle":"2024-11-17T05:17:52.065273Z","shell.execute_reply.started":"2024-11-17T05:17:52.013142Z","shell.execute_reply":"2024-11-17T05:17:52.064383Z"},"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-17T05:18:35.869641Z","iopub.execute_input":"2024-11-17T05:18:35.870364Z","iopub.status.idle":"2024-11-17T05:18:37.575299Z","shell.execute_reply.started":"2024-11-17T05:18:35.870322Z","shell.execute_reply":"2024-11-17T05:18:37.574359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* 4. Make Predictions","metadata":{}},{"cell_type":"code","source":"test_df = pd.DataFrame({'file_path': [os.path.join(test_img_dir, filename) for filename in os.listdir(test_img_dir) if filename.endswith('.png')]})","metadata":{"execution":{"iopub.status.busy":"2024-11-17T05:19:46.460365Z","iopub.execute_input":"2024-11-17T05:19:46.460908Z","iopub.status.idle":"2024-11-17T05:19:46.642203Z","shell.execute_reply.started":"2024-11-17T05:19:46.460868Z","shell.execute_reply":"2024-11-17T05:19:46.641376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['id_code'] = test_df['file_path'].apply(lambda x: os.path.basename(x).replace('.png', ''))","metadata":{"execution":{"iopub.status.busy":"2024-11-17T05:20:02.152147Z","iopub.execute_input":"2024-11-17T05:20:02.152820Z","iopub.status.idle":"2024-11-17T05:20:02.161378Z","shell.execute_reply.started":"2024-11-17T05:20:02.152781Z","shell.execute_reply":"2024-11-17T05:20:02.160310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(preprocessing_function=tf.keras.applications.resnet.preprocess_input)\ntest_generator = test_datagen.flow_from_dataframe(\n    dataframe=test_df,\n    x_col='file_path',\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode=None,\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T05:20:55.977391Z","iopub.execute_input":"2024-11-17T05:20:55.977773Z","iopub.status.idle":"2024-11-17T05:20:56.881628Z","shell.execute_reply.started":"2024-11-17T05:20:55.977740Z","shell.execute_reply":"2024-11-17T05:20:56.880659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.efficientnet import preprocess_input\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T07:38:56.804997Z","iopub.execute_input":"2024-11-17T07:38:56.805650Z","iopub.status.idle":"2024-11-17T07:38:56.810019Z","shell.execute_reply.started":"2024-11-17T07:38:56.805609Z","shell.execute_reply":"2024-11-17T07:38:56.809104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, UpSampling2D, Input","metadata":{"execution":{"iopub.status.busy":"2024-11-17T07:34:44.335663Z","iopub.execute_input":"2024-11-17T07:34:44.336022Z","iopub.status.idle":"2024-11-17T07:34:57.261477Z","shell.execute_reply.started":"2024-11-17T07:34:44.335988Z","shell.execute_reply":"2024-11-17T07:34:57.260696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_img = Input(shape=(224, 224, 3))\nx = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)\nx = MaxPooling2D((2, 2), padding='same')(x)\nx = Conv2D(16, (3, 3), activation='relu', padding='same')(x)\nencoded = MaxPooling2D((2, 2), padding='same')(x)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T07:35:20.468255Z","iopub.execute_input":"2024-11-17T07:35:20.469205Z","iopub.status.idle":"2024-11-17T07:35:21.183017Z","shell.execute_reply.started":"2024-11-17T07:35:20.469165Z","shell.execute_reply":"2024-11-17T07:35:21.182164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = Conv2D(16, (3, 3), activation='relu', padding='same')(encoded)\nx = UpSampling2D((2, 2))(x)\nx = Conv2D(32, (3, 3), activation='relu', padding='same')(x)\nx = UpSampling2D((2, 2))(x)\ndecoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T07:36:20.584102Z","iopub.execute_input":"2024-11-17T07:36:20.584835Z","iopub.status.idle":"2024-11-17T07:36:20.617161Z","shell.execute_reply.started":"2024-11-17T07:36:20.584797Z","shell.execute_reply":"2024-11-17T07:36:20.616430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autoencoder = Model(input_img, decoded)\nautoencoder.compile(optimizer='adam', loss='mse')","metadata":{"execution":{"iopub.status.busy":"2024-11-17T07:36:34.112139Z","iopub.execute_input":"2024-11-17T07:36:34.112509Z","iopub.status.idle":"2024-11-17T07:36:34.131128Z","shell.execute_reply.started":"2024-11-17T07:36:34.112476Z","shell.execute_reply":"2024-11-17T07:36:34.130338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autoencoder.fit(\n    train_generator_ae,\n    validation_data=val_generator_ae,\n    epochs=3\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T07:39:42.433647Z","iopub.execute_input":"2024-11-17T07:39:42.434312Z","iopub.status.idle":"2024-11-17T07:39:42.466803Z","shell.execute_reply.started":"2024-11-17T07:39:42.434273Z","shell.execute_reply":"2024-11-17T07:39:42.465508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = Model(input_img, encoded)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T07:40:28.876829Z","iopub.execute_input":"2024-11-17T07:40:28.877497Z","iopub.status.idle":"2024-11-17T07:40:28.884911Z","shell.execute_reply.started":"2024-11-17T07:40:28.877458Z","shell.execute_reply":"2024-11-17T07:40:28.884006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_classes = tf.argmax(model.predict(test_generator), axis=1).numpy()\nsubmission_df = pd.DataFrame({'id_code': test_df['id_code'], 'diagnosis':predicted_classes})\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T07:02:53.010257Z","iopub.execute_input":"2024-11-17T07:02:53.011183Z","iopub.status.idle":"2024-11-17T07:02:53.429330Z","shell.execute_reply.started":"2024-11-17T07:02:53.011140Z","shell.execute_reply":"2024-11-17T07:02:53.428091Z"},"trusted":true},"execution_count":null,"outputs":[]}]}