{"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":"# 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","execution":{"iopub.status.busy":"2023-06-11T15:04:21.783132Z","iopub.execute_input":"2023-06-11T15:04:21.783493Z","iopub.status.idle":"2023-06-11T15:04:26.439562Z","shell.execute_reply.started":"2023-06-11T15:04:21.783466Z","shell.execute_reply":"2023-06-11T15:04:26.438446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport cv2\nimport os\nfrom zipfile import ZipFile\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.utils import img_to_array\nfrom keras.utils import np_utils\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:04:44.555924Z","iopub.execute_input":"2023-06-11T15:04:44.556311Z","iopub.status.idle":"2023-06-11T15:04:53.845964Z","shell.execute_reply.started":"2023-06-11T15:04:44.556279Z","shell.execute_reply":"2023-06-11T15:04:53.844406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = []\nlabels = []","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:05:30.627795Z","iopub.execute_input":"2023-06-11T15:05:30.628611Z","iopub.status.idle":"2023-06-11T15:05:30.633850Z","shell.execute_reply.started":"2023-06-11T15:05:30.628576Z","shell.execute_reply":"2023-06-11T15:05:30.632892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_Images(label,path):\n    img=cv2.imread(path,cv2.IMREAD_COLOR)\n    try:\n      img_res=cv2.resize(img,(256,256))\n      img_array = img_to_array(img_res)\n      img_array = img_array/255.0\n      dataset.append(img_array)\n      if str(label) == '0':\n        labels.append('0')\n      elif str(label) == '1' :\n            labels.append('1')\n      elif str(label) == '2' :\n            labels.append('2')\n      elif str(label) == '3' :\n            labels.append('3')\n      else :\n            labels.append('4')\n    except:\n      print(\"error\")","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:05:52.968314Z","iopub.execute_input":"2023-06-11T15:05:52.968709Z","iopub.status.idle":"2023-06-11T15:05:52.976181Z","shell.execute_reply.started":"2023-06-11T15:05:52.968681Z","shell.execute_reply":"2023-06-11T15:05:52.974942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_Data = pd.read_csv(\"/kaggle/input/aptos2019-blindness-detection/train.csv\")\ntrain_Data.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:05:58.248327Z","iopub.execute_input":"2023-06-11T15:05:58.248717Z","iopub.status.idle":"2023-06-11T15:05:58.293140Z","shell.execute_reply.started":"2023-06-11T15:05:58.248691Z","shell.execute_reply":"2023-06-11T15:05:58.291944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_code_Data = train_Data['id_code']\ndiagnosis_Data = train_Data['diagnosis']","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:06:04.182028Z","iopub.execute_input":"2023-06-11T15:06:04.182389Z","iopub.status.idle":"2023-06-11T15:06:04.187814Z","shell.execute_reply.started":"2023-06-11T15:06:04.182334Z","shell.execute_reply":"2023-06-11T15:06:04.186627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for id_code,diagnosis in tqdm(zip(id_code_Data,diagnosis_Data)):\n    path = os.path.join('/kaggle/input/aptos2019-blindness-detection/train_images','{}.png'.format(id_code))\n    prepare_Images(diagnosis,path)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:11:43.874667Z","iopub.execute_input":"2023-06-11T15:11:43.875093Z","iopub.status.idle":"2023-06-11T15:16:40.858996Z","shell.execute_reply.started":"2023-06-11T15:11:43.875061Z","shell.execute_reply":"2023-06-11T15:16:40.858093Z"},"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=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","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:18:51.288771Z","iopub.execute_input":"2023-06-11T15:18:51.289161Z","iopub.status.idle":"2023-06-11T15:18:51.300169Z","shell.execute_reply.started":"2023-06-11T15:18:51.289135Z","shell.execute_reply":"2023-06-11T15:18:51.299145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:19:15.195081Z","iopub.execute_input":"2023-06-11T15:19:15.195674Z","iopub.status.idle":"2023-06-11T15:19:15.203165Z","shell.execute_reply.started":"2023-06-11T15:19:15.195638Z","shell.execute_reply":"2023-06-11T15:19:15.201880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = np.array(dataset)\nlabel_arr = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:19:19.054118Z","iopub.execute_input":"2023-06-11T15:19:19.054530Z","iopub.status.idle":"2023-06-11T15:19:20.060191Z","shell.execute_reply.started":"2023-06-11T15:19:19.054498Z","shell.execute_reply":"2023-06-11T15:19:20.057973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from 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        # 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    ","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:19:28.650012Z","iopub.execute_input":"2023-06-11T15:19:28.650397Z","iopub.status.idle":"2023-06-11T15:19:28.656824Z","shell.execute_reply.started":"2023-06-11T15:19:28.650356Z","shell.execute_reply":"2023-06-11T15:19:28.655758Z"},"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_Data.shape[0]\nx_train = np.empty((N, IMG_SIZE, IMG_SIZE, 3), dtype=np.uint8)   # Use the constant","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:19:36.149582Z","iopub.execute_input":"2023-06-11T15:19:36.150149Z","iopub.status.idle":"2023-06-11T15:19:36.157012Z","shell.execute_reply.started":"2023-06-11T15:19:36.150112Z","shell.execute_reply":"2023-06-11T15:19:36.155355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = pd.get_dummies(train_Data['diagnosis']).values\n\n\nprint(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:19:40.327977Z","iopub.execute_input":"2023-06-11T15:19:40.328627Z","iopub.status.idle":"2023-06-11T15:19:40.335307Z","shell.execute_reply.started":"2023-06-11T15:19:40.328594Z","shell.execute_reply":"2023-06-11T15:19:40.334347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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))","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:19:48.297695Z","iopub.execute_input":"2023-06-11T15:19:48.298806Z","iopub.status.idle":"2023-06-11T15:19:48.307414Z","shell.execute_reply.started":"2023-06-11T15:19:48.298751Z","shell.execute_reply":"2023-06-11T15:19:48.305972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ndatagen =  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-06-11T15:20:04.601745Z","iopub.execute_input":"2023-06-11T15:20:04.602145Z","iopub.status.idle":"2023-06-11T15:20:04.607740Z","shell.execute_reply.started":"2023-06-11T15:20:04.602121Z","shell.execute_reply":"2023-06-11T15:20:04.606479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import cohen_kappa_score, accuracy_score\n","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:20:17.630186Z","iopub.execute_input":"2023-06-11T15:20:17.630538Z","iopub.status.idle":"2023-06-11T15:20:18.376595Z","shell.execute_reply.started":"2023-06-11T15:20:17.630514Z","shell.execute_reply":"2023-06-11T15:20:18.375032Z"},"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-06-11T15:20:22.912512Z","iopub.execute_input":"2023-06-11T15:20:22.912924Z","iopub.status.idle":"2023-06-11T15:20:22.918045Z","shell.execute_reply.started":"2023-06-11T15:20:22.912893Z","shell.execute_reply":"2023-06-11T15:20:22.917058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(true_labels, pred_labels)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:20:26.642201Z","iopub.execute_input":"2023-06-11T15:20:26.642616Z","iopub.status.idle":"2023-06-11T15:20:26.654729Z","shell.execute_reply.started":"2023-06-11T15:20:26.642586Z","shell.execute_reply":"2023-06-11T15:20:26.653429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cohen_kappa_score(true_labels, pred_labels)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T12:50:25.434955Z","iopub.execute_input":"2023-06-11T12:50:25.435359Z","iopub.status.idle":"2023-06-11T12:50:25.445435Z","shell.execute_reply.started":"2023-06-11T12:50:25.435329Z","shell.execute_reply":"2023-06-11T12:50:25.444705Z"},"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        y_pred = self.model.predict(X_val) > 0.5\n        y_pred = y_pred.astype(int).sum(axis=1) - 1\n        _val_kappa = cohen_kappa_score(\n        y_val,\n        y_pred, \n        weights='quadratic')\n        \n        \n\n        self.val_kappas.append(_val_kappa)\n\n        print(f\"val_kappa: {_val_kappa:.4f}\")\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                \n                json_file.write(model_json)\n                json_file.close()\n\n        return","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:20:33.113542Z","iopub.execute_input":"2023-06-11T15:20:33.114030Z","iopub.status.idle":"2023-06-11T15:20:33.125111Z","shell.execute_reply.started":"2023-06-11T15:20:33.113994Z","shell.execute_reply":"2023-06-11T15:20:33.123274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:20:44.518357Z","iopub.execute_input":"2023-06-11T15:20:44.518811Z","iopub.status.idle":"2023-06-11T15:20:44.526421Z","shell.execute_reply.started":"2023-06-11T15:20:44.518767Z","shell.execute_reply":"2023-06-11T15:20:44.525404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor i in label_arr:\n  if i == '4':\n    count = count+1\nprint(\"no of Proliferative DR[4]: \",count)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:20:52.872045Z","iopub.execute_input":"2023-06-11T15:20:52.872490Z","iopub.status.idle":"2023-06-11T15:20:52.881246Z","shell.execute_reply.started":"2023-06-11T15:20:52.872461Z","shell.execute_reply":"2023-06-11T15:20:52.879752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor i in label_arr:\n  if i == '3':\n    count = count+1\nprint(\"no of Severe[3]: \",count)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:21:00.782062Z","iopub.execute_input":"2023-06-11T15:21:00.782489Z","iopub.status.idle":"2023-06-11T15:21:00.789721Z","shell.execute_reply.started":"2023-06-11T15:21:00.782459Z","shell.execute_reply":"2023-06-11T15:21:00.788740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor i in label_arr:\n  if i == '2':\n    count = count+1\nprint(\"no of Moderate[2]: \",count)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:21:08.984622Z","iopub.execute_input":"2023-06-11T15:21:08.985792Z","iopub.status.idle":"2023-06-11T15:21:08.996216Z","shell.execute_reply.started":"2023-06-11T15:21:08.985755Z","shell.execute_reply":"2023-06-11T15:21:08.994278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor i in label_arr:\n  if i == '1':\n    count = count+1\nprint(\"no of Mild[1]: \",count)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:21:14.859135Z","iopub.execute_input":"2023-06-11T15:21:14.859619Z","iopub.status.idle":"2023-06-11T15:21:14.867034Z","shell.execute_reply.started":"2023-06-11T15:21:14.859588Z","shell.execute_reply":"2023-06-11T15:21:14.866061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 0\nfor i in label_arr:\n  if i == '0':\n    count = count+1\nprint(\"no of healthy eyes: \",count)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:21:18.566751Z","iopub.execute_input":"2023-06-11T15:21:18.567137Z","iopub.status.idle":"2023-06-11T15:21:18.576135Z","shell.execute_reply.started":"2023-06-11T15:21:18.567107Z","shell.execute_reply":"2023-06-11T15:21:18.574730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels_ = list(set(train_Data['diagnosis'])) \nprint(\"Number of target classes: {}\".format(class_labels_))","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:21:24.653241Z","iopub.execute_input":"2023-06-11T15:21:24.653699Z","iopub.status.idle":"2023-06-11T15:21:24.661373Z","shell.execute_reply.started":"2023-06-11T15:21:24.653667Z","shell.execute_reply":"2023-06-11T15:21:24.659479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels = {0: 'No DR[0]',1: 'Mild[1]', 2: 'Moderate[2]', 3: 'Severe[3]', 4: 'Proliferative DR[4]'}\nclass_sizes = []\nfor i in range(0,5):\n    class_sizes.append(list(train_Data['diagnosis']).count(i))\nlabels = class_labels.values()\ncolors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue','darkgreen']\nplt.pie(class_sizes,explode = [0.1,0,0,0,0], labels= labels, shadow = True,autopct='%1.1f%%', startangle = 35)\nplt.title('Pie Chart Analysis of Number of Images on each target label:')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:21:43.196179Z","iopub.execute_input":"2023-06-11T15:21:43.196560Z","iopub.status.idle":"2023-06-11T15:21:43.356821Z","shell.execute_reply.started":"2023-06-11T15:21:43.196534Z","shell.execute_reply":"2023-06-11T15:21:43.355821Z"},"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(images,label_arr,stratify=label_arr,test_size=0.20,random_state=44)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:21:58.016695Z","iopub.execute_input":"2023-06-11T15:21:58.017107Z","iopub.status.idle":"2023-06-11T15:21:58.943357Z","shell.execute_reply.started":"2023-06-11T15:21:58.017075Z","shell.execute_reply":"2023-06-11T15:21:58.942588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train= np_utils.to_categorical(y_train, num_classes=5)\ny_test = np_utils.to_categorical(y_test, num_classes=5)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:22:01.699828Z","iopub.execute_input":"2023-06-11T15:22:01.700218Z","iopub.status.idle":"2023-06-11T15:22:01.707907Z","shell.execute_reply.started":"2023-06-11T15:22:01.700188Z","shell.execute_reply":"2023-06-11T15:22:01.706623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(filters=16,kernel_size=2,padding=\"same\",activation=\"relu\",input_shape=(256,256,3)))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=32,kernel_size=2,padding=\"same\",activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=64,kernel_size=2,padding=\"same\",activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=128,kernel_size=2,padding=\"same\",activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(512,activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(5,activation=\"sigmoid\"))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:22:05.691904Z","iopub.execute_input":"2023-06-11T15:22:05.692312Z","iopub.status.idle":"2023-06-11T15:22:06.229770Z","shell.execute_reply.started":"2023-06-11T15:22:05.692280Z","shell.execute_reply":"2023-06-11T15:22:06.228765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow\n\nmodel.compile(loss='categorical_crossentropy',\n              optimizer='adam', metrics=['accuracy'])\nhist = model.fit(x_train,y_train,validation_split=0.10,batch_size=32,epochs=50,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T15:22:22.351353Z","iopub.execute_input":"2023-06-11T15:22:22.351805Z","iopub.status.idle":"2023-06-11T17:13:50.388547Z","shell.execute_reply.started":"2023-06-11T15:22:22.351780Z","shell.execute_reply":"2023-06-11T17:13:50.383417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(x_test)\n\nmodel.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:13:54.072149Z","iopub.execute_input":"2023-06-11T17:13:54.072908Z","iopub.status.idle":"2023-06-11T17:14:16.123163Z","shell.execute_reply.started":"2023-06-11T17:13:54.072860Z","shell.execute_reply":"2023-06-11T17:14:16.122032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, classification_report,confusion_matrix\nscore = round(accuracy_score(y_test.argmax(axis=1), pred.argmax(axis=1)),2)\nprint(score)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:14:29.909559Z","iopub.execute_input":"2023-06-11T17:14:29.909970Z","iopub.status.idle":"2023-06-11T17:14:29.921674Z","shell.execute_reply.started":"2023-06-11T17:14:29.909943Z","shell.execute_reply":"2023-06-11T17:14:29.920154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"report = classification_report(y_test.argmax(axis=1), pred.argmax(axis=1))\nprint(report)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = hist.history['accuracy']\nloss = hist.history['loss']\n\nplt.figure(figsize = (8,8))\nplt.subplot(1,2,1)\nplt.plot(range(50),acc,label='Training Accuracy')\nplt.legend(loc=\"lower right\")\nplt.title(\"Training over 50 epochs\")","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:14:39.436552Z","iopub.execute_input":"2023-06-11T17:14:39.436949Z","iopub.status.idle":"2023-06-11T17:14:39.774667Z","shell.execute_reply.started":"2023-06-11T17:14:39.436919Z","shell.execute_reply":"2023-06-11T17:14:39.773641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (8,8))\nplt.subplot(1,2,1)\nplt.plot(range(50),loss,label='Training Loss')\nplt.legend(loc=\"upper right\")\nplt.title(\"Training Loss over 50 epochs\")","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:14:44.977879Z","iopub.execute_input":"2023-06-11T17:14:44.978239Z","iopub.status.idle":"2023-06-11T17:14:45.190049Z","shell.execute_reply.started":"2023-06-11T17:14:44.978213Z","shell.execute_reply":"2023-06-11T17:14:45.188467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:15:27.288816Z","iopub.execute_input":"2023-06-11T17:15:27.289214Z","iopub.status.idle":"2023-06-11T17:15:27.631656Z","shell.execute_reply.started":"2023-06-11T17:15:27.289184Z","shell.execute_reply":"2023-06-11T17:15:27.629933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:15:50.873667Z","iopub.execute_input":"2023-06-11T17:15:50.874050Z","iopub.status.idle":"2023-06-11T17:15:50.981989Z","shell.execute_reply.started":"2023-06-11T17:15:50.874021Z","shell.execute_reply":"2023-06-11T17:15:50.980332Z"},"trusted":true},"execution_count":null,"outputs":[]}]}