{"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":"gpu","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"}],"dockerImageVersionId":30748,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport tensorflow as tf\nfrom tensorflow.keras.applications import MobileNetV2, ResNet50\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout, Input, concatenate, Average\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nfrom tensorflow.keras.regularizers import l2\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-14T01:42:28.679141Z","iopub.execute_input":"2024-08-14T01:42:28.679528Z","iopub.status.idle":"2024-08-14T01:42:28.686364Z","shell.execute_reply.started":"2024-08-14T01:42:28.679474Z","shell.execute_reply":"2024-08-14T01:42:28.685414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set parameters\nIMAGE_SIZE = 224\nBATCH_SIZE = 32\nEPOCHS = 20\nLEARNING_RATE = 1e-4\n\n# Load the dataset\ntrain_df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')\ntest_df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/test.csv')\n\ntrain_df['id_code'] = train_df['id_code'].apply(lambda x: x + '.png')\ntest_df['id_code'] = test_df['id_code'].apply(lambda x: x + '.png')\n\ntrain_df['diagnosis'] = train_df['diagnosis'].astype(str)\n\n\n# Split the data\ntrain_df, val_df = train_test_split(train_df, test_size=0.2, random_state=42)\n\n# Data generators\ndatagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=20,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    vertical_flip=True\n)\n\ntrain_generator = datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory='/kaggle/input/aptos2019-blindness-detection/train_images',\n    x_col='id_code',\n    y_col='diagnosis',\n    target_size=(IMAGE_SIZE, IMAGE_SIZE),\n    batch_size=BATCH_SIZE,\n    class_mode='categorical'\n)\n\nval_generator = datagen.flow_from_dataframe(\n    dataframe=val_df,\n    directory='/kaggle/input/aptos2019-blindness-detection/train_images',\n    x_col='id_code',\n    y_col='diagnosis',\n    target_size=(IMAGE_SIZE, IMAGE_SIZE),\n    batch_size=BATCH_SIZE,\n    class_mode='categorical'\n)\n\n\ntest_generator = ImageDataGenerator(rescale=1./255).flow_from_dataframe(\n    dataframe=test_df,\n    directory='/kaggle/input/aptos2019-blindness-detection/test_images',\n    x_col='id_code',\n    y_col=None,\n    target_size=(IMAGE_SIZE, IMAGE_SIZE),\n    batch_size=BATCH_SIZE,\n    class_mode=None,\n    shuffle=False\n)\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-14T01:41:16.791624Z","iopub.execute_input":"2024-08-14T01:41:16.792277Z","iopub.status.idle":"2024-08-14T01:41:28.917962Z","shell.execute_reply.started":"2024-08-14T01:41:16.792247Z","shell.execute_reply":"2024-08-14T01:41:28.916936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"desired_input_shape = (224, 224, 3)\ninput_layer = Input(shape=desired_input_shape)\n\n# Load MobileNetV2 pre-trained on ImageNet data but without the top classification layer\nbase_model_mobilenet = MobileNetV2(weights='imagenet', include_top=False, input_tensor=input_layer)\n\n# Freeze the layers of the base_model\nfor layer in base_model_mobilenet.layers:\n    layer.trainable = False\n\n# Add custom layers on top of MobileNetV2 with L2 regularization\nx = base_model_mobilenet.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(512, activation='relu', kernel_regularizer=l2(0.001))(x)\nx = Dropout(0.5)(x)\nx = Dense(256, activation='relu', kernel_regularizer=l2(0.001))(x)\nx = Dropout(0.3)(x)\npredictions_mobilenet = Dense(5, activation='softmax', kernel_regularizer=l2(0.001))(x)\n\n# This is the MobileNetV2 model we will train\nmobilenet_model = Model(inputs=base_model_mobilenet.input, outputs=predictions_mobilenet)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-14T01:41:36.275770Z","iopub.execute_input":"2024-08-14T01:41:36.276152Z","iopub.status.idle":"2024-08-14T01:41:38.282970Z","shell.execute_reply.started":"2024-08-14T01:41:36.276124Z","shell.execute_reply":"2024-08-14T01:41:38.281996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model_resnet = ResNet50(weights='imagenet', include_top=False, input_tensor=input_layer)\n\n# Freeze the layers of the base_model\nfor layer in base_model_resnet.layers:\n    layer.trainable = False\n\n# Add custom layers on top of ResNet50 with L2 regularization\nx = base_model_resnet.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(512, activation='relu', kernel_regularizer=l2(0.001))(x)\nx = Dropout(0.5)(x)\nx = Dense(256, activation='relu', kernel_regularizer=l2(0.001))(x)\nx = Dropout(0.3)(x)\nx = Dense(128, activation='relu', kernel_regularizer=l2(0.001))(x)\npredictions_resnet = Dense(5, activation='softmax', kernel_regularizer=l2(0.001))(x)\n\n# This is the ResNet50 model we will train\nresnet_model = Model(inputs=base_model_resnet.input, outputs=predictions_resnet)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-14T01:41:38.284973Z","iopub.execute_input":"2024-08-14T01:41:38.285823Z","iopub.status.idle":"2024-08-14T01:41:40.361115Z","shell.execute_reply.started":"2024-08-14T01:41:38.285780Z","shell.execute_reply":"2024-08-14T01:41:40.360335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the function to create the ensemble model\ndef ensemble(models, model_input):\n    outputs = [model(model_input) for model in models]\n    y = Average()(outputs)\n    model = Model(model_input, y, name='ensemble')\n    return model\n\n# Create the ensemble model\nensemble_model = ensemble([resnet_model, mobilenet_model], input_layer)\n\n# Compile the ensemble model with L2 regularization\nensemble_model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.0001), metrics=['accuracy'])\n\n# View the structure of the ensemble model\nensemble_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-08-14T01:42:37.286670Z","iopub.execute_input":"2024-08-14T01:42:37.287046Z","iopub.status.idle":"2024-08-14T01:42:37.351544Z","shell.execute_reply.started":"2024-08-14T01:42:37.287016Z","shell.execute_reply":"2024-08-14T01:42:37.350514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ensemble_model.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Train the model\nhistory = ensemble_model.fit(\n    train_generator,\n    epochs=EPOCHS,\n    validation_data=val_generator\n)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T01:42:41.258922Z","iopub.execute_input":"2024-08-14T01:42:41.259762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}