{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip uninstall scipy\n!pip install scipy\n#pip install --upgrade scipy\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:03:55.160785Z","iopub.execute_input":"2023-05-22T14:03:55.161144Z","iopub.status.idle":"2023-05-22T14:04:01.253128Z","shell.execute_reply.started":"2023-05-22T14:03:55.161116Z","shell.execute_reply":"2023-05-22T14:04:01.246575Z"},"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":"import json\nimport math\nimport os\n\nimport cv2\nfrom PIL import Image\n# from keras_efficientnets import *\nfrom keras import layers\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom keras.applications import DenseNet121\nfrom keras.callbacks import Callback, ModelCheckpoint,EarlyStopping\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.optimizers import Adam\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import cohen_kappa_score, accuracy_score\nimport scipy\nfrom tqdm import tqdm\nprint(os.listdir('/kaggle/input/aptos2019-blindness-detection/train_images'))\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-05-24T09:09:33.115705Z","iopub.execute_input":"2023-05-24T09:09:33.116068Z","iopub.status.idle":"2023-05-24T09:09:41.051810Z","shell.execute_reply.started":"2023-05-24T09:09:33.116037Z","shell.execute_reply":"2023-05-24T09:09:41.050918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')\ntest_df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.268503Z","iopub.status.idle":"2023-05-22T14:04:01.270928Z","shell.execute_reply.started":"2023-05-22T14:04:01.270670Z","shell.execute_reply":"2023-05-22T14:04:01.270694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.272322Z","iopub.status.idle":"2023-05-22T14:04:01.274546Z","shell.execute_reply.started":"2023-05-22T14:04:01.274288Z","shell.execute_reply":"2023-05-22T14:04:01.274313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['diagnosis'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.275925Z","iopub.status.idle":"2023-05-22T14:04:01.283317Z","shell.execute_reply.started":"2023-05-22T14:04:01.283030Z","shell.execute_reply":"2023-05-22T14:04:01.283055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['diagnosis'].hist()\ntrain_df['diagnosis'].value_counts","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.284772Z","iopub.status.idle":"2023-05-22T14:04:01.285574Z","shell.execute_reply.started":"2023-05-22T14:04:01.285305Z","shell.execute_reply":"2023-05-22T14:04:01.285331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_samples(df, columns=4, rows=3):\n    fig=plt.figure(figsize=(5*columns, 4*rows))\n\n    for i in range(columns*rows):\n        image_path = df.loc[i,'id_code']\n        image_id = df.loc[i,'diagnosis']\n        img = cv2.imread(f'../input/aptos2019-blindness-detection/train_images/{image_path}.png')\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        \n        fig.add_subplot(rows, columns, i+1)\n        plt.title(image_id)\n        plt.axis('off')\n        plt.imshow(img)\n    \n    plt.tight_layout()\n\ndisplay_samples(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.286980Z","iopub.status.idle":"2023-05-22T14:04:01.287764Z","shell.execute_reply.started":"2023-05-22T14:04:01.287525Z","shell.execute_reply":"2023-05-22T14:04:01.287548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def crop_image_from_gray(img,tol=7):\n    if img.ndim ==2:\n        mask = img>tol\n        return img[np.ix_(mask.any(1),mask.any(0))]\n    elif img.ndim==3:\n        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n        mask = gray_img>tol\n        \n        check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]\n        if (check_shape == 0): # image is too dark so that we crop out everything,\n            return img # return original image\n        else:\n            img1=img[:,:,0][np.ix_(mask.any(1),mask.any(0))]\n            img2=img[:,:,1][np.ix_(mask.any(1),mask.any(0))]\n            img3=img[:,:,2][np.ix_(mask.any(1),mask.any(0))]\n    #         print(img1.shape,img2.shape,img3.shape)\n            img = np.stack([img1,img2,img3],axis=-1)\n    #         print(img.shape)\n        return img\ndef preprocess_image(path, sigmaX=10):\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n#     image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , sigmaX) ,-4 ,128)\n    image=CLAHEgreen(image)\n        \n    return image\ndef CLAHEgreen(image):\n    green=image[:, :, 1]\n    clipLimit = 2.0\n    tileGridSize = (8,8)\n    clahe=cv2.createCLAHE(clipLimit = clipLimit, tileGridSize = tileGridSize)\n    cla=clahe.apply(green)\n#     cla=clahe.apply(cla)\n    img=cv2.merge((cla,cla,cla))\n    \n    return img\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.289149Z","iopub.status.idle":"2023-05-22T14:04:01.289961Z","shell.execute_reply.started":"2023-05-22T14:04:01.289709Z","shell.execute_reply":"2023-05-22T14:04:01.289733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****image_path = train_df.loc[i,'id_code']\nimage_id = train_df.loc[i,'diagnosis']\nimg = cv2.imread(f'../kaggle/input/aptos2019-blindness-detection/train_images/{image_path}.png')\nif img is not None:\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    print(f\"Processing image {image_path}\")\n    print(f\"Image shape: {img.shape}\")\n    plt.hist(img.flatten(), 256, [0, 256], color='r')\nelse:\n    print(f\"Failed to load image {image_path}\")\n    *** i dont know if the files are properly downloaded or not","metadata":{"execution":{"iopub.status.busy":"2023-05-17T16:07:24.572142Z","iopub.execute_input":"2023-05-17T16:07:24.572584Z","iopub.status.idle":"2023-05-17T16:07:24.611012Z","shell.execute_reply.started":"2023-05-17T16:07:24.572550Z","shell.execute_reply":"2023-05-17T16:07:24.609758Z"}}},{"cell_type":"code","source":"from PIL import Image\ndef get_histograms(df, columns=3, rows=2):\n    fig = plt.figure(figsize=(3 * columns, 4 * rows))\n    for i in range(columns * rows):\n        img_path = df.loc[i,'id_code']\n        img_id = df.loc[i,'diagnosis']\n        img = cv2.imread(f'../kaggle/input/aptos2019-blindness-detection/train_images/{img_path}.png')\n        ax = fig.add_subplot(rows, columns, i + 1)\n        ax.set_title(f'ID: {img_id}')\n        plt.hist(img.flatten(), 256, [0, 256], color='r')\n        ax.remove()\n\n            \n        \n#         fig.add_subplot(rows, columns, i+1)\n#plt.title(image_id)\n#         plt.axis('off')\n#         plt.imshow(img)\n    \n    #plt.tight_layout()    ","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.291375Z","iopub.status.idle":"2023-05-22T14:04:01.292122Z","shell.execute_reply.started":"2023-05-22T14:04:01.291884Z","shell.execute_reply":"2023-05-22T14:04:01.291907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_histograms(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.293482Z","iopub.status.idle":"2023-05-22T14:04:01.300531Z","shell.execute_reply.started":"2023-05-22T14:04:01.300278Z","shell.execute_reply":"2023-05-22T14:04:01.300302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#def display_samples_gaussian(df, columns=4, rows=3):\n    #fig=plt.figure(figsize=(5*columns, 4*rows))\n\n    #for i in range(columns*rows):\n        #image_path = df.loc[i,'id_code']\n        #image_id = df.loc[i,'diagnosis']\n        #img = preprocess_image(f'/kaggle/input/aptos2019-blindness-detection/train_images/{image_path}.png')\n        \n        #fig.add_subplot(rows, columns, i+1)\n        #plt.title(image_id)\n        #plt.axis('off')\n        #plt.imshow(img)\n    \n    #plt.tight_layout()\n\n#display_samples_gaussian(train_df)\n\nfrom PIL import Image\n\ndef preprocess_image(path, sigmaX=10):\n    # Load image using PIL.Image.open()\n    image = np.array(Image.open(path))\n    \n    # Convert color space using cv2.cvtColor()\n    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n    \n    # Check if the image is empty\n    if image.size == 0:\n        raise ValueError(\"Image is empty or could not be loaded\")\n    \n    # Apply Gaussian blur using cv2.GaussianBlur()\n    image = cv2.GaussianBlur(image, (5, 5), sigmaX)\n    image = cv2.addWeighted(image, 4, cv2.GaussianBlur(image, (0, 0), sigmaX), -4, 128)\n    \n    # Convert color space back to RGB using cv2.cvtColor()\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    \n    return image\n\ndef display_samples_gaussian(df, columns=3, rows=2):\n    fig = plt.figure(figsize=(3 * columns, 4 * rows))\n    for i in range(columns * rows):\n        image_path = df.loc[i,'id_code']\n        image_id = df.loc[i,'diagnosis']\n        try:\n            img = preprocess_image(f'../kaggle/input/aptos2019-blindness-detection/train_images/{image_path}.png')\n        except ValueError:\n            continue\n        fig.add_subplot(rows, columns, i+1)\n        plt.title(image_id)\n        plt.imshow(img)\n    plt.tight_layout()\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.302125Z","iopub.status.idle":"2023-05-22T14:04:01.302897Z","shell.execute_reply.started":"2023-05-22T14:04:01.302651Z","shell.execute_reply":"2023-05-22T14:04:01.302676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 256   # Define the constant\n\ndef preprocess_image(path, sigmaX=10):\n    # Load image using cv2.imread()\n    image = cv2.imread(path)\n    ...\n    \nN = train_df.shape[0]\nx_train = np.empty((N, IMG_SIZE, IMG_SIZE, 3), dtype=np.uint8)   # Use the constant","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.304206Z","iopub.status.idle":"2023-05-22T14:04:01.304940Z","shell.execute_reply.started":"2023-05-22T14:04:01.304707Z","shell.execute_reply":"2023-05-22T14:04:01.304729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = pd.get_dummies(train_df['diagnosis']).values\n\n\nprint(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.306211Z","iopub.status.idle":"2023-05-22T14:04:01.306938Z","shell.execute_reply.started":"2023-05-22T14:04:01.306696Z","shell.execute_reply":"2023-05-22T14:04:01.306718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"***\nfrom PIL import Image\n#from my_module import get_histograms_preprocess\n\n\nget_histograms(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T16:03:19.298975Z","iopub.status.idle":"2023-05-17T16:03:19.299498Z","shell.execute_reply.started":"2023-05-17T16:03:19.299233Z","shell.execute_reply":"2023-05-17T16:03:19.299265Z"}}},{"cell_type":"code","source":"y_train_multi = np.empty(y_train.shape, dtype=y_train.dtype)\ny_train_multi[:, 4] = y_train[:, 4]\n\nfor i in range(3, -1, -1):\n    y_train_multi[:, i] = np.logical_or(y_train[:, i], y_train_multi[:, i+1])\n\nprint(\"Original y_train:\", y_train.sum(axis=0))\nprint(\"Multilabel version:\", y_train_multi.sum(axis=0))\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.308214Z","iopub.status.idle":"2023-05-22T14:04:01.308991Z","shell.execute_reply.started":"2023-05-22T14:04:01.308748Z","shell.execute_reply":"2023-05-22T14:04:01.308771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(\n    x_train, y_train_multi, \n    test_size=0.15, \n    random_state=2019\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.310347Z","iopub.status.idle":"2023-05-22T14:04:01.311102Z","shell.execute_reply.started":"2023-05-22T14:04:01.310868Z","shell.execute_reply":"2023-05-22T14:04:01.310890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val=x_val/255","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.312458Z","iopub.status.idle":"2023-05-22T14:04:01.313242Z","shell.execute_reply.started":"2023-05-22T14:04:01.312973Z","shell.execute_reply":"2023-05-22T14:04:01.312996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['diagnosis']=train_df['diagnosis'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.314540Z","iopub.status.idle":"2023-05-22T14:04:01.315291Z","shell.execute_reply.started":"2023-05-22T14:04:01.315038Z","shell.execute_reply":"2023-05-22T14:04:01.315061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen =  ImageDataGenerator(\n        zoom_range=0.6,  # set range for random zoom, changed from 0.15 to 0.3, now changed from 0.3 to 0.45, from 0.45 to 0.6\n        # set mode for filling points outside the input boundaries\n        fill_mode='constant',\n        cval=0.,  # value used for fill_mode = \"constant\"\n        horizontal_flip=True,  # randomly flip images\n        vertical_flip=True,# randomly flip images\n        rotation_range=360,\n        width_shift_range=0.1,\n        height_shift_range=0.1,\n        rescale=1./255\n    )","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.316656Z","iopub.status.idle":"2023-05-22T14:04:01.317480Z","shell.execute_reply.started":"2023-05-22T14:04:01.317216Z","shell.execute_reply":"2023-05-22T14:04:01.317239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE=32\n\ndata_generator = datagen.flow(x_train, y_train, batch_size=BATCH_SIZE, seed=2019)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.318880Z","iopub.status.idle":"2023-05-22T14:04:01.319649Z","shell.execute_reply.started":"2023-05-22T14:04:01.319411Z","shell.execute_reply":"2023-05-22T14:04:01.319433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_labels = np.array([1, 0, 1, 1, 0, 1])\npred_labels = np.array([1, 0, 0, 0, 0, 1])","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.320976Z","iopub.status.idle":"2023-05-22T14:04:01.321746Z","shell.execute_reply.started":"2023-05-22T14:04:01.321508Z","shell.execute_reply":"2023-05-22T14:04:01.321531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(true_labels, pred_labels)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.323172Z","iopub.status.idle":"2023-05-22T14:04:01.323921Z","shell.execute_reply.started":"2023-05-22T14:04:01.323685Z","shell.execute_reply":"2023-05-22T14:04:01.323708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cohen_kappa_score(true_labels, pred_labels)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.325282Z","iopub.status.idle":"2023-05-22T14:04:01.326064Z","shell.execute_reply.started":"2023-05-22T14:04:01.325795Z","shell.execute_reply":"2023-05-22T14:04:01.325817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import Callback\n\nclass Metrics(Callback):\n    def on_train_begin(self, logs={}):\n        self.val_kappas = []\n\n    def on_epoch_end(self, epoch, logs={}):\n        X_val, y_val = self.validation_data[:2]\n        y_val = y_val.sum(axis=1) - 1\n\n        y_pred = self.model.predict(X_val) > 0.5\n        y_pred = y_pred.astype(int).sum(axis=1) - 1\n\n        _val_kappa = cohen_kappa_score(\n            y_val,\n            y_pred, \n            weights='quadratic'\n        )\n\n        self.val_kappas.append(_val_kappa)\n\n        print(f\"val_kappa: {_val_kappa:.4f}\")\n\n        if _val_kappa == max(self.val_kappas):\n            print(\"Validation Kappa has improved. Saving model.\")\n            model.save_weights('model.h5')\n            model_json = model.to_json()\n            with open('model.json', \"w\") as json_file:\n                json_file.write(model_json)\n            json_file.close()\n\n        return","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.327707Z","iopub.status.idle":"2023-05-22T14:04:01.328630Z","shell.execute_reply.started":"2023-05-22T14:04:01.328304Z","shell.execute_reply":"2023-05-22T14:04:01.328374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install efficientnet","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.329991Z","iopub.status.idle":"2023-05-22T14:04:01.330748Z","shell.execute_reply.started":"2023-05-22T14:04:01.330508Z","shell.execute_reply":"2023-05-22T14:04:01.330531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from efficientnet.keras import EfficientNetB5\n\nefficient = EfficientNetB5(\n    weights=None,\n    include_top=False,\n    input_shape=(IMG_SIZE,IMG_SIZE,3)\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.332060Z","iopub.status.idle":"2023-05-22T14:04:01.332821Z","shell.execute_reply.started":"2023-05-22T14:04:01.332590Z","shell.execute_reply":"2023-05-22T14:04:01.332613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wget https://github.com/qubvel/efficientnet/releases/download/v1.0/efficientnet-b5_imagenet_1000_notop.h5 -O efficientnet-b5_imagenet_1000_notop.h5","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.334175Z","iopub.status.idle":"2023-05-22T14:04:01.334981Z","shell.execute_reply.started":"2023-05-22T14:04:01.334723Z","shell.execute_reply":"2023-05-22T14:04:01.334746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications import EfficientNetB5\n\n# create an instance of the EfficientNetB5 model\nmodel = EfficientNetB5(weights=None, include_top=False, input_shape=(224, 224, 3))\n\n# load the weights from the file\nweights_path = '../input/efficientnet-keras-weights-b0b5//kaggle/input/aptos2019-blindness-detection/train_images'\nmodel.load_weights(weights_path)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-05-22T14:04:01.336351Z","iopub.status.idle":"2023-05-22T14:04:01.337130Z","shell.execute_reply.started":"2023-05-22T14:04:01.336882Z","shell.execute_reply":"2023-05-22T14:04:01.336906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"efficient.load_weights('../input/efficientnet-keras-weights-b0b5/efficientnet-b5_imagenet_1000_notop.h5')","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.338541Z","iopub.status.idle":"2023-05-22T14:04:01.339320Z","shell.execute_reply.started":"2023-05-22T14:04:01.339054Z","shell.execute_reply":"2023-05-22T14:04:01.339077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from efficientnet.keras import EfficientNetB5\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Define the input shape and number of classes\ninput_shape = (224, 224, 3)\nnum_classes = 2\n\n# Define the EfficientNetB5 model\nmodel = EfficientNetB5(\n    weights=None,\n    include_top=True,\n    input_shape=input_shape,\n    classes=num_classes\n)\n\n# Compile the model with a loss function, optimizer, and metrics\nmodel.compile(\n    loss='categorical_crossentropy',\n    optimizer='adam',\n    metrics=['accuracy']\n)\n\n# Define the data generators for training and validation data\ntrain_datagen = ImageDataGenerator(rescale=1./255)\nval_datagen = ImageDataGenerator(rescale=1./255)\n\ndata_generator = data_datagen.flow_from_directory(\n    '/kaggle/input/aptos2019-blindness-detection/train_images',\n    target_size=input_shape[:2],\n    batch_size=32,\n    class_mode='categorical'\n)\n\nval_generator = val_datagen.flow_from_directory(\n    '/kaggle/input/aptos2019-blindness-detection/test_images',\n    target_size=input_shape[:2],\n    batch_size=32,\n    class_mode='categorical'\n)\n\n# Train the model on the training data\nmodel.fit(\n    data_generator,\n    epochs=10,\n    validation_data=val_generator\n)\n\n# Evaluate the model on the validation data\nval_loss, val_acc = model.evaluate(val_generator)\n\n# Save the trained weights to a file\nmodel.save_weights('path/to/efficientnet_weights.h5')","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.340678Z","iopub.status.idle":"2023-05-22T14:04:01.341463Z","shell.execute_reply.started":"2023-05-22T14:04:01.341214Z","shell.execute_reply":"2023-05-22T14:04:01.341239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom efficientnet.keras import EfficientNetB5\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nimport os\n\n# Define the input shape and number of classes\ninput_shape = (224, 224, 3)\nnum_classes = 5\n\n# Define the EfficientNetB5 model\nmodel = EfficientNetB5(\n    weights=None,\n    include_top=True,\n    input_shape=input_shape,\n    classes=num_classes\n)\n\n# Compile the model with a loss function, optimizer, and metrics\nmodel.compile(\n    loss='categorical_crossentropy',\n    optimizer='adam',\n    metrics=['accuracy']\n)\n\n# Set up the data generators for training and validation data\ndatagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)\n\ntrain_generator = datagen.flow_from_directory(\n    '/kaggle/input/aptos2019-blindness-detection/train_images',\n    target_size=input_shape[:2],\n    batch_size=32,\n    class_mode='categorical',\n    subset='training'\n)\n\nval_generator = datagen.flow_from_directory(\n    '/kaggle/input/aptos2019-blindness-detection/train_images',\n    target_size=input_shape[:2],\n    batch_size=32,\n    class_mode='categorical',\n    subset='validation'\n)\n\n# Train the model on the training data\nmodel.fit(\n    train_generator,\n    epochs=10,\n    validation_data=val_generator\n)\n\n# Evaluate the model on the test data\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_generator = test_datagen.flow_from_directory(\n    '/kaggle/input/aptos2019-blindness-detection/test_images',\n    target_size=input_shape[:2],\n    batch_size=32,\n    class_mode='categorical',\n    shuffle=False\n)\n\ntest_loss, test_acc = model.evaluate(test_generator)\n\n# Save the trained weights to a file\nmodel.save_weights('path/to/efficientnet_weights.h5')","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.342799Z","iopub.status.idle":"2023-05-22T14:04:01.343564Z","shell.execute_reply.started":"2023-05-22T14:04:01.343315Z","shell.execute_reply":"2023-05-22T14:04:01.343337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_dir = '/kaggle/input/aptos2019-blindness-detection/train_images'\ntrain_filenames = os.listdir(train_data_dir)\n\ntrain_filenames, val_filenames = train_test_split(train_filenames, test_size=0.2)\n\ntrain_generator = datagen.flow_from_directory(\n    train_data_dir,\n    target_size=input_shape[:2],\n    batch_size=32,\n    class_mode='categorical',\n    subset='training'\n)\n\nval_generator = datagen.flow_from_directory(\n    train_data_dir,\n    target_size=input_shape[:2],\n    batch_size=32,\n    class_mode='categorical',\n    subset='validation'\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.344904Z","iopub.status.idle":"2023-05-22T14:04:01.345707Z","shell.execute_reply.started":"2023-05-22T14:04:01.345457Z","shell.execute_reply":"2023-05-22T14:04:01.345482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model():\n    model = Sequential()\n    model.add(efficient)\n    model.add(layers.GlobalAveragePooling2D())\n    model.add(layers.Dropout(0.5))\n    model.add(layers.Dense(5, activation='sigmoid'))\n    \n    \n    model.compile(\n        loss='binary_crossentropy',\n        optimizer=Adam(lr=0.00005),\n        metrics=['accuracy']\n    )\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.347082Z","iopub.status.idle":"2023-05-22T14:04:01.347874Z","shell.execute_reply.started":"2023-05-22T14:04:01.347636Z","shell.execute_reply":"2023-05-22T14:04:01.347659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.349256Z","iopub.status.idle":"2023-05-22T14:04:01.350067Z","shell.execute_reply.started":"2023-05-22T14:04:01.349818Z","shell.execute_reply":"2023-05-22T14:04:01.349843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nclass Metrics(tf.keras.callbacks.Callback):\n    def __init__(self):\n        super().__init__()\n        self.kappa_scores = []\n\n    def on_epoch_end(self, epoch, logs=None):\n        val_pred = np.argmax(self.model.predict(val_images), axis=1)\n        val_kappa = cohen_kappa_score(val_labels, val_pred, weights='quadratic')\n        self.kappa_scores.append(val_kappa)\n        print(f'val_kappa: {val_kappa:.4f}')","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.351389Z","iopub.status.idle":"2023-05-22T14:04:01.352142Z","shell.execute_reply.started":"2023-05-22T14:04:01.351910Z","shell.execute_reply":"2023-05-22T14:04:01.351932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kappa_metrics = Metrics()\nest=EarlyStopping(monitor='val_loss',patience=5, min_delta=0.005)\ncall_backs=[est,kappa_metrics]\nhistory = model.fit(\n    data_generator,\n    steps_per_epoch=x_train.shape[0] / BATCH_SIZE,\n    validation_data=(x_val,y_val),\n    epochs=20,\n    callbacks=call_backs)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-22T14:04:01.353537Z","iopub.status.idle":"2023-05-22T14:04:01.354315Z","shell.execute_reply.started":"2023-05-22T14:04:01.354057Z","shell.execute_reply":"2023-05-22T14:04:01.354081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### kappa_metrics = Metrics()\nest=EarlyStopping(monitor='val_loss',patience=5, min_delta=0.005)\ncall_backs=[est,kappa_metrics]\n\nhistory = model.fit(\n    train_generator,\n    steps_per_epoch=len(train_generator),\n    epochs=num_epochs,\n    validation_data=val_generator,\n    validation_steps=len(val_generator),\n    callbacks=[checkpoint_callback, early_stopping_callback]\n)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-21T20:04:43.117697Z","iopub.status.idle":"2023-05-21T20:04:43.118117Z","shell.execute_reply.started":"2023-05-21T20:04:43.117958Z","shell.execute_reply":"2023-05-21T20:04:43.117974Z"}}}]}