{"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 install keras","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:11:24.598320Z","iopub.execute_input":"2023-01-30T20:11:24.598608Z","iopub.status.idle":"2023-01-30T20:11:32.200606Z","shell.execute_reply.started":"2023-01-30T20:11:24.598576Z","shell.execute_reply":"2023-01-30T20:11:32.199632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport random\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\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))","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:11:32.203232Z","iopub.execute_input":"2023-01-30T20:11:32.203520Z","iopub.status.idle":"2023-01-30T20:11:36.802209Z","shell.execute_reply.started":"2023-01-30T20:11:32.203483Z","shell.execute_reply":"2023-01-30T20:11:36.781808Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install huggingface_hub","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:11:36.805885Z","iopub.execute_input":"2023-01-30T20:11:36.806103Z","iopub.status.idle":"2023-01-30T20:11:44.445456Z","shell.execute_reply.started":"2023-01-30T20:11:36.806076Z","shell.execute_reply":"2023-01-30T20:11:44.444587Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # array manipulation\nimport pandas as pd \nfrom tqdm import tqdm\nfrom huggingface_hub import from_pretrained_keras # download the model\nimport keras # deep learning\nfrom PIL import Image # Image processing","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:11:44.448830Z","iopub.execute_input":"2023-01-30T20:11:44.449104Z","iopub.status.idle":"2023-01-30T20:11:44.454332Z","shell.execute_reply.started":"2023-01-30T20:11:44.449073Z","shell.execute_reply":"2023-01-30T20:11:44.453043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = from_pretrained_keras(\"keras-io/lowlight-enhance-mirnet\", compile=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:11:44.455469Z","iopub.execute_input":"2023-01-30T20:11:44.456110Z","iopub.status.idle":"2023-01-30T20:12:21.092615Z","shell.execute_reply.started":"2023-01-30T20:11:44.456074Z","shell.execute_reply":"2023-01-30T20:12:21.091779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/ocular-disease-recognition-odir5k/full_df.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:12:21.097652Z","iopub.execute_input":"2023-01-30T20:12:21.099681Z","iopub.status.idle":"2023-01-30T20:12:21.147062Z","shell.execute_reply.started":"2023-01-30T20:12:21.099638Z","shell.execute_reply":"2023-01-30T20:12:21.146432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def has_cataract(text):\n    if \"cataract\" in text:\n        return 1\n    else:\n        return 0","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:12:21.150977Z","iopub.execute_input":"2023-01-30T20:12:21.152776Z","iopub.status.idle":"2023-01-30T20:12:21.158217Z","shell.execute_reply.started":"2023-01-30T20:12:21.152738Z","shell.execute_reply":"2023-01-30T20:12:21.157587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"left_cataract\"] = df[\"Left-Diagnostic Keywords\"].apply(lambda x: has_cataract(x))\ndf[\"right_cataract\"] = df[\"Right-Diagnostic Keywords\"].apply(lambda x: has_cataract(x))","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:12:21.162143Z","iopub.execute_input":"2023-01-30T20:12:21.164346Z","iopub.status.idle":"2023-01-30T20:12:21.184581Z","shell.execute_reply.started":"2023-01-30T20:12:21.164309Z","shell.execute_reply":"2023-01-30T20:12:21.184017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"left_cataract = df.loc[(df.C ==1) & (df.left_cataract == 1)][\"Left-Fundus\"].values\nright_cataract = df.loc[(df.C ==1) & (df.right_cataract == 1)][\"Right-Fundus\"].values","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:12:21.187976Z","iopub.execute_input":"2023-01-30T20:12:21.188595Z","iopub.status.idle":"2023-01-30T20:12:21.199160Z","shell.execute_reply.started":"2023-01-30T20:12:21.188553Z","shell.execute_reply":"2023-01-30T20:12:21.198572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"left_normal = df.loc[(df.C ==0) & (df[\"Left-Diagnostic Keywords\"] == \"normal fundus\")][\"Left-Fundus\"].sample(250,random_state=42).values\nright_normal = df.loc[(df.C ==0) & (df[\"Right-Diagnostic Keywords\"] == \"normal fundus\")][\"Right-Fundus\"].sample(250,random_state=42).values","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:12:21.204412Z","iopub.execute_input":"2023-01-30T20:12:21.206279Z","iopub.status.idle":"2023-01-30T20:12:21.223104Z","shell.execute_reply.started":"2023-01-30T20:12:21.206244Z","shell.execute_reply":"2023-01-30T20:12:21.222368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cataract = np.concatenate((left_cataract,right_cataract),axis=0)\nnormal = np.concatenate((left_normal,right_normal),axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:12:21.226824Z","iopub.execute_input":"2023-01-30T20:12:21.228641Z","iopub.status.idle":"2023-01-30T20:12:21.234246Z","shell.execute_reply.started":"2023-01-30T20:12:21.228606Z","shell.execute_reply":"2023-01-30T20:12:21.233652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import load_img,img_to_array\ndataset_dir = \"/kaggle/input/ocular-disease-recognition-odir5k/preprocessed_images/\"\nimage_size=256\nlabels = []\ndataset = []\ndef create_dataset(image_category,label):\n    for img in tqdm(image_category):\n        image_path = os.path.join(dataset_dir,img)\n        try:\n            image = cv2.imread(image_path,cv2.IMREAD_COLOR)\n            image = cv2.resize(image,(image_size,image_size))\n            image = keras.preprocessing.image.img_to_array(image)\n            image = image.astype('float32') / 255.0\n            image = np.expand_dims(image, axis = 0)\n            output = model.predict(image)\n            image = output[0] * 255.0\n#             image = image.reshape((np.shape(image)[0],np.shape(image)[1],3))\n            image = np.uint32(image)\n            image = Image.fromarray(image.astype('uint8'),'RGB')\n            \n        except:\n            continue\n        \n        dataset.append([np.array(image),np.array(label)])\n    random.shuffle(dataset)\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:12:21.238314Z","iopub.execute_input":"2023-01-30T20:12:21.240568Z","iopub.status.idle":"2023-01-30T20:12:21.251358Z","shell.execute_reply.started":"2023-01-30T20:12:21.240520Z","shell.execute_reply":"2023-01-30T20:12:21.250799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = create_dataset(cataract,1)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:12:21.255510Z","iopub.execute_input":"2023-01-30T20:12:21.257662Z","iopub.status.idle":"2023-01-30T20:15:39.994286Z","shell.execute_reply.started":"2023-01-30T20:12:21.257626Z","shell.execute_reply":"2023-01-30T20:15:39.993572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dataset)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:15:39.995617Z","iopub.execute_input":"2023-01-30T20:15:39.996387Z","iopub.status.idle":"2023-01-30T20:15:40.002521Z","shell.execute_reply.started":"2023-01-30T20:15:39.996349Z","shell.execute_reply":"2023-01-30T20:15:40.001549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = create_dataset(normal,0)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:15:40.003863Z","iopub.execute_input":"2023-01-30T20:15:40.004168Z","iopub.status.idle":"2023-01-30T20:18:25.539971Z","shell.execute_reply.started":"2023-01-30T20:15:40.004134Z","shell.execute_reply":"2023-01-30T20:18:25.539187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dataset)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:18:25.541304Z","iopub.execute_input":"2023-01-30T20:18:25.541593Z","iopub.status.idle":"2023-01-30T20:18:25.547492Z","shell.execute_reply.started":"2023-01-30T20:18:25.541556Z","shell.execute_reply":"2023-01-30T20:18:25.546604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,7))\nfor i in range(10):\n    sample = random.choice(range(len(dataset)))\n    image = dataset[sample][0]\n    category = dataset[sample][1]\n    if category== 0:\n        label = \"Normal\"\n    else:\n        label = \"Cataract\"\n    plt.subplot(2,5,i+1)\n    plt.imshow(image)\n    plt.xlabel(label)\nplt.tight_layout() ","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:18:25.549472Z","iopub.execute_input":"2023-01-30T20:18:25.549826Z","iopub.status.idle":"2023-01-30T20:18:27.034418Z","shell.execute_reply.started":"2023-01-30T20:18:25.549788Z","shell.execute_reply":"2023-01-30T20:18:27.033453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array([i[0] for i in dataset]).reshape(-1,image_size,image_size,3)\ny = np.array([i[1] for i in dataset])","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:20:47.729399Z","iopub.execute_input":"2023-01-30T20:20:47.730185Z","iopub.status.idle":"2023-01-30T20:20:48.089683Z","shell.execute_reply.started":"2023-01-30T20:20:47.730144Z","shell.execute_reply":"2023-01-30T20:20:48.088887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:20:49.370302Z","iopub.execute_input":"2023-01-30T20:20:49.370601Z","iopub.status.idle":"2023-01-30T20:20:49.442295Z","shell.execute_reply.started":"2023-01-30T20:20:49.370567Z","shell.execute_reply":"2023-01-30T20:20:49.441523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Flatten,Dense, Conv2D, BatchNormalization, Dropout\nfrom tensorflow.keras.applications import ResNet50\n\nresnet = ResNet50(weights=\"imagenet\", input_shape=(image_size,image_size,3), include_top=False)\n\nfor layer in resnet.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:20:50.525403Z","iopub.execute_input":"2023-01-30T20:20:50.526232Z","iopub.status.idle":"2023-01-30T20:20:51.934567Z","shell.execute_reply.started":"2023-01-30T20:20:50.526192Z","shell.execute_reply":"2023-01-30T20:20:51.933633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Dropout, Conv2D, BatchNormalization\nx = Flatten() (resnet.output)\nx = Dropout(0.5)(x) # Add a dropout layer with a rate of 0.5\nx = BatchNormalization()(x) # Add a batch normalization layer\nprediction = Dense(1, activation = 'relu')(x)\nmodel = Model(inputs = resnet.input, outputs = prediction)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:20:55.125867Z","iopub.execute_input":"2023-01-30T20:20:55.126137Z","iopub.status.idle":"2023-01-30T20:20:55.175716Z","shell.execute_reply.started":"2023-01-30T20:20:55.126106Z","shell.execute_reply":"2023-01-30T20:20:55.175012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:20:56.899011Z","iopub.execute_input":"2023-01-30T20:20:56.899284Z","iopub.status.idle":"2023-01-30T20:20:56.995327Z","shell.execute_reply.started":"2023-01-30T20:20:56.899253Z","shell.execute_reply":"2023-01-30T20:20:56.994577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=\"adam\",loss=\"binary_crossentropy\",metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:21:16.562014Z","iopub.execute_input":"2023-01-30T20:21:16.562577Z","iopub.status.idle":"2023-01-30T20:21:16.576030Z","shell.execute_reply.started":"2023-01-30T20:21:16.562514Z","shell.execute_reply":"2023-01-30T20:21:16.575153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping\ncheckpoint = ModelCheckpoint(\"resnet50.h5\",monitor=\"val_acc\",verbose=1,save_best_only=True,\n                             save_weights_only=False,period=1)\nearlystop = EarlyStopping(monitor=\"val_acc\",patience=5,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:21:20.867828Z","iopub.execute_input":"2023-01-30T20:21:20.868432Z","iopub.status.idle":"2023-01-30T20:21:20.873641Z","shell.execute_reply.started":"2023-01-30T20:21:20.868395Z","shell.execute_reply":"2023-01-30T20:21:20.872805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train,y_train,batch_size=64,epochs=50,validation_data=(x_test,y_test),\n                    verbose=1,callbacks=[checkpoint,earlystop])","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:23:27.784156Z","iopub.execute_input":"2023-01-30T20:23:27.784426Z","iopub.status.idle":"2023-01-30T20:26:51.682848Z","shell.execute_reply.started":"2023-01-30T20:23:27.784394Z","shell.execute_reply":"2023-01-30T20:26:51.681987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss,accuracy = model.evaluate(x_test,y_test)\nprint(\"loss:\",loss)\nprint(\"Accuracy:\",accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:29:13.596105Z","iopub.execute_input":"2023-01-30T20:29:13.596376Z","iopub.status.idle":"2023-01-30T20:29:14.992812Z","shell.execute_reply.started":"2023-01-30T20:29:13.596344Z","shell.execute_reply":"2023-01-30T20:29:14.992051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install scikit-learn\n\nfrom sklearn.metrics import classification_report\nfrom sklearn.metrics import confusion_matrix,classification_report,accuracy_score","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:29:17.078577Z","iopub.execute_input":"2023-01-30T20:29:17.079095Z","iopub.status.idle":"2023-01-30T20:29:24.477792Z","shell.execute_reply.started":"2023-01-30T20:29:17.079059Z","shell.execute_reply":"2023-01-30T20:29:24.476854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Obtain the predicted probability values for the test data\ny_pred = model.predict(x_test)\n\n# Convert the predicted probability values to class labels\ny_pred = (y_pred > 0.5).astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:29:26.362403Z","iopub.execute_input":"2023-01-30T20:29:26.363268Z","iopub.status.idle":"2023-01-30T20:29:28.299185Z","shell.execute_reply.started":"2023-01-30T20:29:26.363229Z","shell.execute_reply":"2023-01-30T20:29:28.298313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:30:04.144235Z","iopub.execute_input":"2023-01-30T20:30:04.144516Z","iopub.status.idle":"2023-01-30T20:30:04.152977Z","shell.execute_reply.started":"2023-01-30T20:30:04.144485Z","shell.execute_reply":"2023-01-30T20:30:04.152207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:30:05.326956Z","iopub.execute_input":"2023-01-30T20:30:05.327523Z","iopub.status.idle":"2023-01-30T20:30:05.338136Z","shell.execute_reply.started":"2023-01-30T20:30:05.327486Z","shell.execute_reply":"2023-01-30T20:30:05.337255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mlxtend.plotting import plot_confusion_matrix\ncm = confusion_matrix(y_test,y_pred)\nplot_confusion_matrix(conf_mat = cm,figsize=(8,7),class_names = [\"Normal\",\"Cataract\"],\n                      show_normed = True);","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:30:07.244281Z","iopub.execute_input":"2023-01-30T20:30:07.245033Z","iopub.status.idle":"2023-01-30T20:30:07.468762Z","shell.execute_reply.started":"2023-01-30T20:30:07.244986Z","shell.execute_reply":"2023-01-30T20:30:07.468092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use(\"ggplot\")\nfig = plt.figure(figsize=(12,6))\nepochs = range(1,51)\nplt.subplot(1,2,1)\nplt.plot(epochs,history.history[\"accuracy\"],\"go-\")\nplt.plot(epochs,history.history[\"val_accuracy\"],\"ro-\")\nplt.title(\"Model Accuracy\")\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Accuracy\")\nplt.legend([\"Train\",\"val\"],loc = \"upper left\")\n\nplt.subplot(1,2,2)\nplt.plot(epochs,history.history[\"loss\"],\"go-\")\nplt.plot(epochs,history.history[\"val_loss\"],\"ro-\")\nplt.title(\"Model Loss\")\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss\")\nplt.legend([\"Train\",\"val\"],loc = \"upper left\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-30T20:30:41.076494Z","iopub.execute_input":"2023-01-30T20:30:41.076799Z","iopub.status.idle":"2023-01-30T20:30:41.443513Z","shell.execute_reply.started":"2023-01-30T20:30:41.076768Z","shell.execute_reply":"2023-01-30T20:30:41.442746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}