{"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"},{"sourceId":9500725,"sourceType":"datasetVersion","datasetId":5781945}],"dockerImageVersionId":30775,"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},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_raw_data = pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")\ntrain_raw_data[\"filename\"] = train_raw_data[\"id_code\"].map(lambda x:os.path.join(\"../input/aptos2019-blindness-detection/train_images\",x+\".png\"))","metadata":{"execution":{"iopub.status.busy":"2024-10-08T14:44:20.584067Z","iopub.execute_input":"2024-10-08T14:44:20.584491Z","iopub.status.idle":"2024-10-08T14:44:20.608245Z","shell.execute_reply.started":"2024-10-08T14:44:20.584449Z","shell.execute_reply":"2024-10-08T14:44:20.606899Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_raw_data","metadata":{"execution":{"iopub.status.busy":"2024-10-08T14:44:33.301898Z","iopub.execute_input":"2024-10-08T14:44:33.302833Z","iopub.status.idle":"2024-10-08T14:44:33.327833Z","shell.execute_reply.started":"2024-10-08T14:44:33.302770Z","shell.execute_reply":"2024-10-08T14:44:33.326463Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_raw_images_df(data_frame,filenamecol,labelcol,img_size,n_classes):\n    n_images = len(data_frame)\n    X = np.empty((n_images,img_size,img_size,3))\n    Y = np.zeros((n_images,n_classes))\n    for index,entry in data_frame.iterrows():\n        Y[index,entry[labelcol]] = 1 # one hot encoding of the label\n        # Load the image and resize\n        img = cv2.imread(entry[filenamecol])\n        X[index,:] = cv2.resize(img, (img_size, img_size))\n        X[index,:] = X[index,:] / 255.0\n    return X,Y","metadata":{"execution":{"iopub.status.busy":"2024-10-08T14:46:09.236175Z","iopub.execute_input":"2024-10-08T14:46:09.236614Z","iopub.status.idle":"2024-10-08T14:46:09.245064Z","shell.execute_reply.started":"2024-10-08T14:46:09.236566Z","shell.execute_reply":"2024-10-08T14:46:09.243719Z"},"trusted":true},"outputs":[],"execution_count":null},{"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 tensorflow as tf\nfrom matplotlib import pyplot as plt\nfrom sklearn.metrics import cohen_kappa_score\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.densenet import DenseNet121\nimport keras\nimport cv2\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\nimport cv2\nimport os\nfrom keras.callbacks import Callback\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.utils.multiclass import unique_labels\nfrom sklearn.utils import class_weight","metadata":{"execution":{"iopub.status.busy":"2024-10-08T14:52:11.426164Z","iopub.execute_input":"2024-10-08T14:52:11.426758Z","iopub.status.idle":"2024-10-08T14:52:27.305072Z","shell.execute_reply.started":"2024-10-08T14:52:11.426697Z","shell.execute_reply":"2024-10-08T14:52:27.303379Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nbatch_size = 32\nimg_size = 224\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2024-10-08T14:47:34.621041Z","iopub.execute_input":"2024-10-08T14:47:34.621479Z","iopub.status.idle":"2024-10-08T14:47:34.849648Z","shell.execute_reply.started":"2024-10-08T14:47:34.621438Z","shell.execute_reply":"2024-10-08T14:47:34.848481Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df,val_df = train_test_split(train_raw_data,random_state=42,shuffle=True,test_size=0.333)\ntrain_df.reset_index(drop=True,inplace=True)\nval_df.reset_index(drop=True,inplace=True)","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train,Y_train = load_raw_images_df(train_df,\"filename\",\"diagnosis\",img_size,5)\nX_val,Y_val = load_raw_images_df(val_df,\"filename\",\"diagnosis\",img_size,5)","metadata":{},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport seaborn as sns\ndf = pd.read_csv('/kaggle/input/newggg/hudadata.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:01:35.460190Z","iopub.execute_input":"2024-09-28T10:01:35.460692Z","iopub.status.idle":"2024-09-28T10:01:38.585689Z","shell.execute_reply.started":"2024-09-28T10:01:35.460635Z","shell.execute_reply":"2024-09-28T10:01:38.584125Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat_cols = [col for col in df.columns if df[col].dtype == 'object']\nnum_cols = [col for col in df.columns if df[col].dtype != 'object']\ndf['dm']=df['dm'].replace('\\tno', 'no')\nprint(df['dm'].unique())\n\ndf['class']=df['class'].replace('ckd\\t', 'ckd')\ndf['class'].unique()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:01:44.743938Z","iopub.execute_input":"2024-09-28T10:01:44.744427Z","iopub.status.idle":"2024-09-28T10:01:44.764358Z","shell.execute_reply.started":"2024-09-28T10:01:44.744382Z","shell.execute_reply":"2024-09-28T10:01:44.762879Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\ndf['class'] = le.fit_transform(df['class'])","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:01:53.080098Z","iopub.execute_input":"2024-09-28T10:01:53.080667Z","iopub.status.idle":"2024-09-28T10:01:53.203220Z","shell.execute_reply.started":"2024-09-28T10:01:53.080608Z","shell.execute_reply":"2024-09-28T10:01:53.201777Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df['rbc'].unique())\nprint( (df['rbc']==\"normal\").sum(),\" normal    1\")\nprint( (df['rbc']==\"abnormal\").sum(),\" abnormal    0\")\ndf['rbc'] = df['rbc'].map({'normal': 1, 'abnormal': 0})\ndf['rbc'] = pd.to_numeric(df['rbc'], errors='coerce')\nprint( (df['rbc']==1).sum())\nprint( (df['rbc']==0).sum())","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:02:04.746732Z","iopub.execute_input":"2024-09-28T10:02:04.747253Z","iopub.status.idle":"2024-09-28T10:02:04.765886Z","shell.execute_reply.started":"2024-09-28T10:02:04.747204Z","shell.execute_reply":"2024-09-28T10:02:04.763325Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df['pc'].unique())\nprint( (df['pc']==\"normal\").sum(),\" normal    \")\nprint( (df['pc']==\"abnormal\").sum(),\" abnormal   \")\ndf['pc'] = df['pc'].map({'normal': 1, 'abnormal': 0})\ndf['pc'] = pd.to_numeric(df['pc'], errors='coerce')\nprint( (df['pc']==1).sum())\nprint( (df['pc']==0).sum())","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:02:08.448977Z","iopub.execute_input":"2024-09-28T10:02:08.449465Z","iopub.status.idle":"2024-09-28T10:02:08.462864Z","shell.execute_reply.started":"2024-09-28T10:02:08.449419Z","shell.execute_reply":"2024-09-28T10:02:08.461408Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df['htn'].unique())\nprint( (df['htn']==\"yes\").sum(),\" yes\")\nprint( (df['htn']==\"no\").sum(),\" no\")\ndf['htn'] = df['htn'].map({'yes': 1, 'no': 0})\ndf['htn'] = pd.to_numeric(df['htn'], errors='coerce')\nprint( (df['htn']==1).sum())\nprint( (df['htn']==0).sum())","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:02:11.407850Z","iopub.execute_input":"2024-09-28T10:02:11.408315Z","iopub.status.idle":"2024-09-28T10:02:11.421369Z","shell.execute_reply.started":"2024-09-28T10:02:11.408270Z","shell.execute_reply":"2024-09-28T10:02:11.420149Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df['dm'].unique())\nprint( (df['dm']==\"yes\").sum(),\" yes\")\nprint( (df['dm']==\"no\").sum(),\" no\")\ndf['dm'] = df['dm'].map({'yes': 1, 'no': 0})\ndf['dm'] = pd.to_numeric(df['dm'], errors='coerce')\nprint( (df['dm']==1).sum())\nprint( (df['dm']==0).sum())","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:02:14.312593Z","iopub.execute_input":"2024-09-28T10:02:14.313026Z","iopub.status.idle":"2024-09-28T10:02:14.325280Z","shell.execute_reply.started":"2024-09-28T10:02:14.312985Z","shell.execute_reply":"2024-09-28T10:02:14.323636Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df['cad'].unique())\nprint( (df['cad']==\"yes\").sum(),\" yes\")\nprint( (df['cad']==\"no\").sum(),\" no\")\ndf['cad'] = df['cad'].map({'yes': 1, 'no': 0})\ndf['cad'] = pd.to_numeric(df['cad'], errors='coerce')\nprint( (df['cad']==1).sum())\nprint( (df['cad']==0).sum())","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:02:16.862052Z","iopub.execute_input":"2024-09-28T10:02:16.862489Z","iopub.status.idle":"2024-09-28T10:02:16.874355Z","shell.execute_reply.started":"2024-09-28T10:02:16.862448Z","shell.execute_reply":"2024-09-28T10:02:16.873185Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df['pe'].unique())\nprint( (df['pe']==\"yes\").sum(),\" yes\")\nprint( (df['pe']==\"no\").sum(),\" no\")\ndf['pe'] = df['pe'].map({'yes': 1, 'no': 0})\ndf['pe'] = pd.to_numeric(df['pe'], errors='coerce')\nprint( (df['pe']==1).sum())\nprint( (df['pe']==0).sum())","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:02:19.210243Z","iopub.execute_input":"2024-09-28T10:02:19.210698Z","iopub.status.idle":"2024-09-28T10:02:19.223185Z","shell.execute_reply.started":"2024-09-28T10:02:19.210654Z","shell.execute_reply":"2024-09-28T10:02:19.221992Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df['ane'].unique())\nprint( (df['ane']==\"yes\").sum(),\" yes\")\nprint( (df['ane']==\"no\").sum(),\" no\")\ndf['ane'] = df['ane'].map({'yes': 1, 'no': 0})\ndf['ane'] = pd.to_numeric(df['ane'], errors='coerce')\nprint( (df['ane']==1).sum())\nprint( (df['ane']==0).sum())","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:02:26.524181Z","iopub.execute_input":"2024-09-28T10:02:26.524635Z","iopub.status.idle":"2024-09-28T10:02:26.536977Z","shell.execute_reply.started":"2024-09-28T10:02:26.524591Z","shell.execute_reply":"2024-09-28T10:02:26.535547Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df['appet'].unique())\nprint( (df['appet']==\"good\").sum(),\" good\")\nprint( (df['appet']==\"poor\").sum(),\" poor\")\ndf['appet'] = df['appet'].map({'good': 1, 'poor': 0})\ndf['appet'] = pd.to_numeric(df['appet'], errors='coerce')\nprint( (df['appet']==1).sum())\nprint( (df['appet']==0).sum())","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:02:30.134797Z","iopub.execute_input":"2024-09-28T10:02:30.136243Z","iopub.status.idle":"2024-09-28T10:02:30.148610Z","shell.execute_reply.started":"2024-09-28T10:02:30.136191Z","shell.execute_reply":"2024-09-28T10:02:30.146906Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df['pcc'].unique())\nprint( (df['pcc']==\"present\").sum(),\" present\")\nprint( (df['pcc']==\"notpresent\").sum(),\" notpresent\")\ndf['pcc'] = df['pcc'].map({'present': 1, 'notpresent': 0})\ndf['pcc'] = pd.to_numeric(df['pcc'], errors='coerce')\nprint( (df['pcc']==1).sum())\nprint( (df['pcc']==0).sum())","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:02:32.975585Z","iopub.execute_input":"2024-09-28T10:02:32.976026Z","iopub.status.idle":"2024-09-28T10:02:32.988274Z","shell.execute_reply.started":"2024-09-28T10:02:32.975986Z","shell.execute_reply":"2024-09-28T10:02:32.986759Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(df['ba'].unique())\nprint( (df['ba']==\"present\").sum(),\" present\")\nprint( (df['ba']==\"notpresent\").sum(),\" notpresent\")\ndf['ba'] = df['ba'].map({'present': 1, 'notpresent': 0})\ndf['ba'] = pd.to_numeric(df['ba'], errors='coerce')\nprint( (df['ba']==1).sum())\nprint( (df['ba']==0).sum())","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:02:35.674332Z","iopub.execute_input":"2024-09-28T10:02:35.674801Z","iopub.status.idle":"2024-09-28T10:02:35.687411Z","shell.execute_reply.started":"2024-09-28T10:02:35.674759Z","shell.execute_reply":"2024-09-28T10:02:35.686053Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:02:37.711641Z","iopub.execute_input":"2024-09-28T10:02:37.712744Z","iopub.status.idle":"2024-09-28T10:02:37.726397Z","shell.execute_reply.started":"2024-09-28T10:02:37.712686Z","shell.execute_reply":"2024-09-28T10:02:37.725037Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for f in df.columns:\n    print(f)\n    df[f] = df[f].fillna(df[f].median())\nprint(df.isna().sum())","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:02:41.994359Z","iopub.execute_input":"2024-09-28T10:02:41.994933Z","iopub.status.idle":"2024-09-28T10:02:42.027761Z","shell.execute_reply.started":"2024-09-28T10:02:41.994875Z","shell.execute_reply":"2024-09-28T10:02:42.026175Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ind_col = [col for col in df.columns if col != 'class']\ndep_col = 'class'\nX = df[ind_col].values.tolist()\ny = df[dep_col].tolist()","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:12:13.553170Z","iopub.execute_input":"2024-09-28T10:12:13.553895Z","iopub.status.idle":"2024-09-28T10:12:13.566530Z","shell.execute_reply.started":"2024-09-28T10:12:13.553836Z","shell.execute_reply":"2024-09-28T10:12:13.564809Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nfrom transformers import BertTokenizer\n\n# تحميل Tokenizer\ntokenizer = BertTokenizer.from_pretrained('bert-base-uncased')\n\n# تحويل النصوص إلى Token IDs\n#inputs = tokenizer(X, padding=True, truncation=True, return_tensors=\"pt\", max_length=128)\nlabels = torch.tensor(y)\ninputs = torch.tensor(X)\ninputs=inputs.long()\n\nfrom transformers import BertForSequenceClassification\n\n# تحميل نموذج BERT مصمم للتصنيف\nmodel = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=len(set(y)))\nimport torch\n\n# تحويل التصنيفات إلى Tensor\nlabels = torch.tensor(y)\n\n# إدخال البيانات في النموذج\noutputs = model(inputs, labels=labels)\n\n# حساب الخسارة\nloss = outputs.loss\nprint(f\"Loss: {loss.item()}\")\n\nfrom transformers import Trainer, TrainingArguments\n\n# إعداد معلمات التدريب\ntraining_args = TrainingArguments(\n    output_dir='/kaggle/working/',          # حفظ النتائج\n    num_train_epochs=3,              # عدد الحلقات\n    per_device_train_batch_size=16,  # حجم الدفعة\n    per_device_eval_batch_size=64,   # حجم دفعة التقييم\n    warmup_steps=500,                # خطوات التسخين\n    weight_decay=0.01,               # معامل التآكل\n    logging_dir='./logs',            # حفظ السجلات\n)\n\n# إعداد المدرب\ntrainer = Trainer(\n    model=model,                         # النموذج\n    args=training_args,                  # معلمات التدريب\n    train_dataset=inputs,                # بيانات التدريب\n    eval_dataset=inputs,                 # بيانات التقييم\n)\n\n# تدريب النموذج\ntrainer.train()\ntrainer.evaluate()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:36:18.330011Z","iopub.execute_input":"2024-09-28T10:36:18.330490Z","iopub.status.idle":"2024-09-28T10:36:35.764975Z","shell.execute_reply.started":"2024-09-28T10:36:18.330443Z","shell.execute_reply":"2024-09-28T10:36:35.762887Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import wandb\nwandb.login(key=\"your_api_keyaaaaaaaaaaaaaaaaaaaaaaa_here\")","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:38:12.686988Z","iopub.execute_input":"2024-09-28T10:38:12.688188Z","iopub.status.idle":"2024-09-28T10:38:12.973181Z","shell.execute_reply.started":"2024-09-28T10:38:12.688129Z","shell.execute_reply":"2024-09-28T10:38:12.971904Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pip install qrcode","metadata":{"execution":{"iopub.status.busy":"2024-10-20T16:52:41.493543Z","iopub.execute_input":"2024-10-20T16:52:41.493983Z","iopub.status.idle":"2024-10-20T16:52:57.223041Z","shell.execute_reply.started":"2024-10-20T16:52:41.493940Z","shell.execute_reply":"2024-10-20T16:52:57.221576Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"kagglehub.competition_download('aptos2019-blindness-detection', path='0005cfc8afb6.png')","metadata":{}},{"cell_type":"code","source":"import base64\nimport qrcode\n\n# Reading of image\nwith open(\"/kaggle/input/aptos2019-blindness-detection/test_images/010d915e229a.png\", 'rb') as f:\n    b=f.read()\n# Turn into a base64 string 'encode/decode' may seems paradoxical, but they\n# have nothing to do with each other. `b64encode` creates a base64 string, but in the form of bytes (a b-string)\n# .decode() here is just the native python method to turn a b-string to a string\nb64 = base64.standard_b64encode(b).decode()\n# Add some decoration to make it a \"URL\" (a base64 URL, that is, that \"links\"\n# to data that are directly encoded into the URL itself)\nurl = 'data:image/gif;base64,' + b64\n# Turn this url into a qr-code\nqrimg = qrcode.make(url)\n# save the image\nqrimg.save('myqrcode.png')","metadata":{"execution":{"iopub.status.busy":"2024-10-20T16:53:22.681434Z","iopub.execute_input":"2024-10-20T16:53:22.681892Z","iopub.status.idle":"2024-10-20T16:53:51.368856Z","shell.execute_reply.started":"2024-10-20T16:53:22.681848Z","shell.execute_reply":"2024-10-20T16:53:51.367165Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"/kaggle/input/aptos2019-blindness-detection/test_images","metadata":{"execution":{"iopub.status.busy":"2024-09-28T10:38:21.449622Z","iopub.execute_input":"2024-09-28T10:38:21.450126Z","iopub.status.idle":"2024-09-28T10:38:21.458318Z","shell.execute_reply.started":"2024-09-28T10:38:21.450077Z","shell.execute_reply":"2024-09-28T10:38:21.456436Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\nfrom tensorflow.keras.preprocessing.image import img_to_array\nfrom sklearn.model_selection import train_test_split\nimport numpy as np\nimport cv2\nfrom os import listdir\nfrom tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.applications.vgg19 import VGG19\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.layers import Dropout\nfrom tensorflow.keras.layers import Flatten,GlobalAveragePooling2D,MaxPooling2D\nprint(tensorflow.__version__)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:08:06.785360Z","iopub.execute_input":"2025-02-17T16:08:06.786064Z","iopub.status.idle":"2025-02-17T16:08:19.361942Z","shell.execute_reply.started":"2025-02-17T16:08:06.786030Z","shell.execute_reply":"2025-02-17T16:08:19.360961Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.applications import EfficientNetB1\nfrom tensorflow.keras.applications import EfficientNetB2\nfrom tensorflow.keras.applications import EfficientNetB3\nfrom tensorflow.keras.applications import EfficientNetB4\nfrom tensorflow.keras.applications import EfficientNetB5\nfrom tensorflow.keras.applications import EfficientNetB6\nfrom tensorflow.keras.applications import EfficientNetB7\nfrom tensorflow.keras.applications import ResNet50","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:08:19.363319Z","iopub.execute_input":"2025-02-17T16:08:19.363827Z","iopub.status.idle":"2025-02-17T16:08:19.368937Z","shell.execute_reply.started":"2025-02-17T16:08:19.363799Z","shell.execute_reply":"2025-02-17T16:08:19.367951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"EPOCHS =50\nINIT_LearningRate = 1e-3\nBatchSize = 32\ndefault_image_size = tuple((224,224))\nwidth=224\nheight=224\ndepth=3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:08:19.370244Z","iopub.execute_input":"2025-02-17T16:08:19.370650Z","iopub.status.idle":"2025-02-17T16:08:19.396666Z","shell.execute_reply.started":"2025-02-17T16:08:19.370604Z","shell.execute_reply":"2025-02-17T16:08:19.395766Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# #add dropout\nfrom tensorflow.keras import backend as K\ninputShape = (height, width, depth)\nconv_base = EfficientNetB1(weights='imagenet', include_top=False, input_shape=inputShape) ## VGG16, VGG19, DenseNet121 or ResNet50\nx=conv_base.output\nx=GlobalAveragePooling2D()(x)\nx=Dense(1024,activation='relu')(x) \nx=Dropout((0.5))(x)\n#x=Dense(256,activation='relu')(x) \npreds=Dense(5,activation='softmax')(x)  ## Number of class\nmodel=Model(inputs=conv_base.input,outputs=preds)\n# for i in range(len(conv_base.layers)):\n#     model.layers[i].trainable=False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:08:19.766924Z","iopub.execute_input":"2025-02-17T16:08:19.767266Z","iopub.status.idle":"2025-02-17T16:08:23.025207Z","shell.execute_reply.started":"2025-02-17T16:08:19.767235Z","shell.execute_reply":"2025-02-17T16:08:23.024237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(len(conv_base.layers)):\n    model.layers[i].trainable=False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T15:20:22.575793Z","iopub.execute_input":"2025-02-17T15:20:22.576520Z","iopub.status.idle":"2025-02-17T15:20:22.591198Z","shell.execute_reply.started":"2025-02-17T15:20:22.576485Z","shell.execute_reply":"2025-02-17T15:20:22.590159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Set input and output directories\ninput_dir = '/kaggle/input/aptos2019-blindness-detection/train_images/'\n\nimport pandas as pd\n# Load the CSV containing image names and labels\ncsv_path = '/kaggle/input/aptos2019-blindness-detection/train.csv'\ndf = pd.read_csv(csv_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:08:23.356007Z","iopub.execute_input":"2025-02-17T16:08:23.356954Z","iopub.status.idle":"2025-02-17T16:08:23.372447Z","shell.execute_reply.started":"2025-02-17T16:08:23.356906Z","shell.execute_reply":"2025-02-17T16:08:23.371491Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import img_to_array\nimport cv2\nimage_list=[]\nlabel_list=[]\nfor _, row in df.iterrows():\n    img = cv2.imread(f\"/kaggle/input/aptos2019-blindness-detection/train_images/{row[0]}.png\")#ing the image\n    #print(img)\n    # Getting the label (class) for the image\n    #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    # label = train[train['id_code'] == img_name]['diagnosis'].values[0]  # Assuming 'diagnosis' is the label column\n    image1 = cv2.resize(img, default_image_size)   \n    x = img_to_array(image1)\n    #x = preprocess_input(x)\n    image_list.append(x)\n    label_list.append(row[1])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:08:32.968645Z","iopub.execute_input":"2025-02-17T16:08:32.969405Z","iopub.status.idle":"2025-02-17T16:14:51.821490Z","shell.execute_reply.started":"2025-02-17T16:08:32.969358Z","shell.execute_reply":"2025-02-17T16:14:51.820736Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"aa = tensorflow.keras.utils.to_categorical(label_list, 5)\nprint(aa[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:14:51.822924Z","iopub.execute_input":"2025-02-17T16:14:51.823189Z","iopub.status.idle":"2025-02-17T16:14:51.828665Z","shell.execute_reply.started":"2025-02-17T16:14:51.823165Z","shell.execute_reply":"2025-02-17T16:14:51.827787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np_image_list = np.array(image_list, dtype=np.float16)\nprint(np_image_list.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:14:51.829851Z","iopub.execute_input":"2025-02-17T16:14:51.830145Z","iopub.status.idle":"2025-02-17T16:14:54.663784Z","shell.execute_reply.started":"2025-02-17T16:14:51.830108Z","shell.execute_reply":"2025-02-17T16:14:54.662782Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(np_image_list, aa, test_size=0.2, random_state = 42) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:14:54.666127Z","iopub.execute_input":"2025-02-17T16:14:54.666852Z","iopub.status.idle":"2025-02-17T16:14:54.997588Z","shell.execute_reply.started":"2025-02-17T16:14:54.666809Z","shell.execute_reply":"2025-02-17T16:14:54.996555Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(x_train.shape,x_test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:14:54.998788Z","iopub.execute_input":"2025-02-17T16:14:54.999075Z","iopub.status.idle":"2025-02-17T16:14:55.003942Z","shell.execute_reply.started":"2025-02-17T16:14:54.999048Z","shell.execute_reply":"2025-02-17T16:14:55.003045Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Activation, Dropout, BatchNormalization\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T15:09:01.634168Z","iopub.execute_input":"2025-02-17T15:09:01.635089Z","iopub.status.idle":"2025-02-17T15:09:01.645008Z","shell.execute_reply.started":"2025-02-17T15:09:01.635050Z","shell.execute_reply":"2025-02-17T15:09:01.644151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(aa[1])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:07:54.883304Z","iopub.execute_input":"2025-02-17T16:07:54.883691Z","iopub.status.idle":"2025-02-17T16:07:55.117520Z","shell.execute_reply.started":"2025-02-17T16:07:54.883650Z","shell.execute_reply":"2025-02-17T16:07:55.116265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# #add dropout\nfrom tensorflow.keras import backend as K\ninputShape = (height, width, depth)\nconv_base = EfficientNetB1(weights='imagenet', include_top=False, input_shape=inputShape) ## VGG16, VGG19, DenseNet121 or ResNet50\nx=conv_base.output\nx=GlobalAveragePooling2D()(x)\nx=Dense(512,activation='relu')(x) \nx=Dropout((0.7))(x)\n#x=Dense(256,activation='relu')(x) \npreds=Dense(5,activation='softmax')(x)  ## Number of class\nmodel=Model(inputs=conv_base.input,outputs=preds)\n# for i in range(len(conv_base.layers)):\n#     model.layers[i].trainable=False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:59:11.741698Z","iopub.execute_input":"2025-02-17T16:59:11.742294Z","iopub.status.idle":"2025-02-17T16:59:13.051146Z","shell.execute_reply.started":"2025-02-17T16:59:11.742260Z","shell.execute_reply":"2025-02-17T16:59:13.050205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"opt = Adam(learning_rate=INIT_LearningRate)\nmodel.compile(loss='categorical_crossentropy', optimizer=opt,metrics=[\"accuracy\"])\nhistory = model.fit(\n    x_train, y_train, batch_size=16,     validation_data=(x_test, y_test),\n     epochs=100, verbose=1\n     )\n#B1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T16:59:14.485275Z","iopub.execute_input":"2025-02-17T16:59:14.485938Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig , ax = plt.subplots(1,2)\ntrain_acc = history.history['accuracy']\ntrain_loss = history.history['loss']\nfig.set_size_inches(12,4)\nax[0].plot(history.history['accuracy'])\nax[0].plot(history.history['val_accuracy'])\nax[0].set_title('Training Accuracy vs Validation Accuracy')\nax[0].set_ylabel('Accuracy')\nax[0].set_xlabel('Epoch')\nax[0].legend(['Train', 'Validation'], loc='upper left')\nax[1].plot(history.history['loss'])\nax[1].plot(history.history['val_loss'])\nax[1].set_title('Training Loss vs Validation Loss')\nax[1].set_ylabel('Loss')\nax[1].set_xlabel('Epoch')\nax[1].legend(['Train', 'Validation'], loc='upper left')\nplt.show()\n\n#B1","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"opt = Adam(learning_rate=INIT_LearningRate)\nmodel.compile(loss='categorical_crossentropy', optimizer=opt,metrics=[\"accuracy\"])\nhistory = model.fit(\n    x_train, y_train, batch_size=16,     validation_data=(x_test, y_test),\n     epochs=100, verbose=1\n     )\n#B1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T15:33:39.630802Z","iopub.execute_input":"2025-02-17T15:33:39.631270Z","iopub.status.idle":"2025-02-17T15:45:05.717040Z","shell.execute_reply.started":"2025-02-17T15:33:39.631238Z","shell.execute_reply":"2025-02-17T15:45:05.716059Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig , ax = plt.subplots(1,2)\ntrain_acc = history.history['accuracy']\ntrain_loss = history.history['loss']\nfig.set_size_inches(12,4)\nax[0].plot(history.history['accuracy'])\nax[0].plot(history.history['val_accuracy'])\nax[0].set_title('Training Accuracy vs Validation Accuracy')\nax[0].set_ylabel('Accuracy')\nax[0].set_xlabel('Epoch')\nax[0].legend(['Train', 'Validation'], loc='upper left')\nax[1].plot(history.history['loss'])\nax[1].plot(history.history['val_loss'])\nax[1].set_title('Training Loss vs Validation Loss')\nax[1].set_ylabel('Loss')\nax[1].set_xlabel('Epoch')\nax[1].legend(['Train', 'Validation'], loc='upper left')\nplt.show()\n\n#B1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-17T15:45:43.338670Z","iopub.execute_input":"2025-02-17T15:45:43.339585Z","iopub.status.idle":"2025-02-17T15:45:43.733753Z","shell.execute_reply.started":"2025-02-17T15:45:43.339547Z","shell.execute_reply":"2025-02-17T15:45:43.732879Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    x_train, y_train, batch_size=16,     validation_data=(x_test, y_test),\n     epochs=EPOCHS, verbose=1\n     )\n#B1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-16T16:16:59.782242Z","iopub.execute_input":"2025-02-16T16:16:59.783056Z","iopub.status.idle":"2025-02-16T16:23:04.959654Z","shell.execute_reply.started":"2025-02-16T16:16:59.783022Z","shell.execute_reply":"2025-02-16T16:23:04.958864Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig , ax = plt.subplots(1,2)\ntrain_acc = history.history['accuracy']\ntrain_loss = history.history['loss']\nfig.set_size_inches(12,4)\nax[0].plot(history.history['accuracy'])\nax[0].plot(history.history['val_accuracy'])\nax[0].set_title('Training Accuracy vs Validation Accuracy')\nax[0].set_ylabel('Accuracy')\nax[0].set_xlabel('Epoch')\nax[0].legend(['Train', 'Validation'], loc='upper left')\nax[1].plot(history.history['loss'])\nax[1].plot(history.history['val_loss'])\nax[1].set_title('Training Loss vs Validation Loss')\nax[1].set_ylabel('Loss')\nax[1].set_xlabel('Epoch')\nax[1].legend(['Train', 'Validation'], loc='upper left')\nplt.show()\n\n#B1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-16T16:23:04.961383Z","iopub.execute_input":"2025-02-16T16:23:04.961661Z","iopub.status.idle":"2025-02-16T16:23:05.293409Z","shell.execute_reply.started":"2025-02-16T16:23:04.961634Z","shell.execute_reply":"2025-02-16T16:23:05.292583Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    x_train, y_train, batch_size=16,     validation_data=(x_test, y_test),\n     epochs=EPOCHS, verbose=1\n     )\n#B3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-16T16:07:11.112170Z","iopub.execute_input":"2025-02-16T16:07:11.112740Z","iopub.status.idle":"2025-02-16T16:15:03.607742Z","shell.execute_reply.started":"2025-02-16T16:07:11.112704Z","shell.execute_reply":"2025-02-16T16:15:03.606998Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig , ax = plt.subplots(1,2)\ntrain_acc = history.history['accuracy']\ntrain_loss = history.history['loss']\nfig.set_size_inches(12,4)\nax[0].plot(history.history['accuracy'])\nax[0].plot(history.history['val_accuracy'])\nax[0].set_title('Training Accuracy vs Validation Accuracy')\nax[0].set_ylabel('Accuracy')\nax[0].set_xlabel('Epoch')\nax[0].legend(['Train', 'Validation'], loc='upper left')\nax[1].plot(history.history['loss'])\nax[1].plot(history.history['val_loss'])\nax[1].set_title('Training Loss vs Validation Loss')\nax[1].set_ylabel('Loss')\nax[1].set_xlabel('Epoch')\nax[1].legend(['Train', 'Validation'], loc='upper left')\nplt.show()\n\n#B3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-16T16:15:38.171587Z","iopub.execute_input":"2025-02-16T16:15:38.172434Z","iopub.status.idle":"2025-02-16T16:15:38.501831Z","shell.execute_reply.started":"2025-02-16T16:15:38.172399Z","shell.execute_reply":"2025-02-16T16:15:38.501013Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    x_train, y_train, batch_size=16,     validation_data=(x_test, y_test),\n     epochs=EPOCHS, verbose=1\n     )\n#B2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-16T15:59:16.539990Z","iopub.execute_input":"2025-02-16T15:59:16.540677Z","iopub.status.idle":"2025-02-16T16:06:05.701328Z","shell.execute_reply.started":"2025-02-16T15:59:16.540644Z","shell.execute_reply":"2025-02-16T16:06:05.700458Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig , ax = plt.subplots(1,2)\ntrain_acc = history.history['accuracy']\ntrain_loss = history.history['loss']\nfig.set_size_inches(12,4)\nax[0].plot(history.history['accuracy'])\nax[0].plot(history.history['val_accuracy'])\nax[0].set_title('Training Accuracy vs Validation Accuracy')\nax[0].set_ylabel('Accuracy')\nax[0].set_xlabel('Epoch')\nax[0].legend(['Train', 'Validation'], loc='upper left')\nax[1].plot(history.history['loss'])\nax[1].plot(history.history['val_loss'])\nax[1].set_title('Training Loss vs Validation Loss')\nax[1].set_ylabel('Loss')\nax[1].set_xlabel('Epoch')\nax[1].legend(['Train', 'Validation'], loc='upper left')\nplt.show()\n\n#B2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-16T16:06:24.937619Z","iopub.execute_input":"2025-02-16T16:06:24.938347Z","iopub.status.idle":"2025-02-16T16:06:25.386381Z","shell.execute_reply.started":"2025-02-16T16:06:24.938311Z","shell.execute_reply":"2025-02-16T16:06:25.385524Z"}},"outputs":[],"execution_count":null}]}