{"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"}],"dockerImageVersionId":30761,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"pip install keras-preprocessing","metadata":{"execution":{"iopub.status.busy":"2024-09-11T02:08:27.615008Z","iopub.execute_input":"2024-09-11T02:08:27.615490Z","iopub.status.idle":"2024-09-11T02:08:41.462685Z","shell.execute_reply.started":"2024-09-11T02:08:27.615452Z","shell.execute_reply":"2024-09-11T02:08:41.461570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.python.client import device_lib\ndef get_gpu():\n    a = device_lib.list_local_devices()\n    return [x.name for x in a if x.device_type == 'GPU']\n\nprint(get_gpu())\n    ","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:16:33.859537Z","iopub.execute_input":"2024-09-14T11:16:33.859950Z","iopub.status.idle":"2024-09-14T11:16:46.600228Z","shell.execute_reply.started":"2024-09-14T11:16:33.859906Z","shell.execute_reply":"2024-09-14T11:16:46.599175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\n","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:16:51.358157Z","iopub.execute_input":"2024-09-14T11:16:51.358791Z","iopub.status.idle":"2024-09-14T11:16:54.739621Z","shell.execute_reply.started":"2024-09-14T11:16:51.358739Z","shell.execute_reply":"2024-09-14T11:16:54.738706Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**IMPORT THƯ VIỆN**","metadata":{}},{"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 tensorflow.keras.models import load_model  # Để lưu và tải mô hình Keras\nfrom sklearn.metrics import confusion_matrix  # Để tính ma trận nhầm lẫn\nimport matplotlib.pyplot as plt  # Để vẽ biểu đồ\nimport seaborn as sns \nfrom sklearn.metrics import cohen_kappa_score\nfrom tensorflow.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\nprint(os.listdir(\"../input\"))","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:17:09.618657Z","iopub.execute_input":"2024-09-14T11:17:09.619725Z","iopub.status.idle":"2024-09-14T11:17:09.628338Z","shell.execute_reply.started":"2024-09-14T11:17:09.619678Z","shell.execute_reply":"2024-09-14T11:17:09.627306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**TIỀN XỬ LÝ DỮ LIỆU**","metadata":{}},{"cell_type":"code","source":"def parse_image(filename, label):\n    image = tf.io.read_file(filename)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, [300, 300])\n    image = image / 255.0\n    return image, label\ndef load_dataset(file_paths, labels, batch_size=64):\n    dataset = Dataset.from_tensor_slices((file_paths, labels))\n    dataset = dataset.map(parse_image, num_parallel_calls=tf.data.AUTOTUNE)\n    dataset = dataset.shuffle(buffer_size=len(file_paths)).batch(batch_size)\n    dataset = dataset.prefetch(buffer_size=tf.data.AUTOTUNE)\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:17:13.030148Z","iopub.execute_input":"2024-09-14T11:17:13.030924Z","iopub.status.idle":"2024-09-14T11:17:13.037665Z","shell.execute_reply.started":"2024-09-14T11:17:13.030880Z","shell.execute_reply":"2024-09-14T11:17:13.036726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainLabels = pd.read_csv(\"/kaggle/input/aptos2019-blindness-detection/train.csv\")\nfile_paths = \"/kaggle/input/aptos2019-blindness-detection/train_images\"\n","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:17:16.052686Z","iopub.execute_input":"2024-09-14T11:17:16.053705Z","iopub.status.idle":"2024-09-14T11:17:16.073418Z","shell.execute_reply.started":"2024-09-14T11:17:16.053651Z","shell.execute_reply":"2024-09-14T11:17:16.072384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_title = {\"0\" : \"No DR\",\"1\" : \"Mild\",\"2\" : \"Moderate\",\"3\" :\"Severe\",\"4\" : \"Proliferative DR\"}\nclass_labels=[\"No DR\",\"Mild\",\"Moderate\",\"Severe\",\"Proliferative DR\"]","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:17:19.045766Z","iopub.execute_input":"2024-09-14T11:17:19.046160Z","iopub.status.idle":"2024-09-14T11:17:19.051173Z","shell.execute_reply.started":"2024-09-14T11:17:19.046122Z","shell.execute_reply":"2024-09-14T11:17:19.050106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_raw_images_df(data_frame,filenamecol,labelcol,image_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-09-14T11:17:20.786989Z","iopub.execute_input":"2024-09-14T11:17:20.787668Z","iopub.status.idle":"2024-09-14T11:17:20.794474Z","shell.execute_reply.started":"2024-09-14T11:17:20.787623Z","shell.execute_reply":"2024-09-14T11:17:20.793535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_title = {\"0\" : \"No DR\",\"1\" : \"Mild\",\"2\" : \"Moderate\",\"3\" :\"Severe\",\"4\" : \"Proliferative DR\"}\nclass_labels=[\"No DR\",\"Mild\",\"Moderate\",\"Severe\",\"Proliferative DR\"]","metadata":{"execution":{"iopub.status.busy":"2024-09-11T05:46:50.370603Z","iopub.execute_input":"2024-09-11T05:46:50.371499Z","iopub.status.idle":"2024-09-11T05:46:50.376169Z","shell.execute_reply.started":"2024-09-11T05:46:50.371452Z","shell.execute_reply":"2024-09-11T05:46:50.375176Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"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\"))\ntrain_raw_data.diagnosis.hist()\nimg_size = 300\nbatch_size = 64","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:17:45.684534Z","iopub.execute_input":"2024-09-14T11:17:45.684983Z","iopub.status.idle":"2024-09-14T11:17:45.934321Z","shell.execute_reply.started":"2024-09-14T11:17:45.684937Z","shell.execute_reply":"2024-09-14T11:17:45.933344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display some images\nfigure, ax = plt.subplots(5,2)\nax = ax.flatten()\nfor i,row in train_raw_data.iloc[0:10,:].iterrows():\n    ax[i].imshow(cv2.imread(os.path.join(\"../input/aptos2019-blindness-detection/train_images\",row[\"id_code\"]+\".png\")))\n    ax[i].set_title(label_title[str(row[\"diagnosis\"])])","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:17:48.325031Z","iopub.execute_input":"2024-09-14T11:17:48.326139Z","iopub.status.idle":"2024-09-14T11:17:57.170219Z","shell.execute_reply.started":"2024-09-14T11:17:48.326083Z","shell.execute_reply":"2024-09-14T11:17:57.169266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df,val_df = train_test_split(train_raw_data,random_state=42,shuffle=True,test_size=0.15)\ntrain_df,test_df = train_test_split(train_raw_data,random_state=42,shuffle=True,test_size=0.15)\ntrain_df.reset_index(drop=True,inplace=True)\nval_df.reset_index(drop=True,inplace=True)\ntest_df.reset_index(drop = True, inplace = True)","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:18:00.435990Z","iopub.execute_input":"2024-09-14T11:18:00.436405Z","iopub.status.idle":"2024-09-14T11:18:00.453880Z","shell.execute_reply.started":"2024-09-14T11:18:00.436364Z","shell.execute_reply":"2024-09-14T11:18:00.452823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-09-14T11:18:12.687621Z","iopub.execute_input":"2024-09-14T11:18:12.688045Z","iopub.status.idle":"2024-09-14T11:24:34.515902Z","shell.execute_reply.started":"2024-09-14T11:18:12.688004Z","shell.execute_reply":"2024-09-14T11:24:34.514845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test, Y_test = load_raw_images_df(test_df, \"filename\", \"diagnosis\", img_size, 5)","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:24:44.368137Z","iopub.execute_input":"2024-09-14T11:24:44.368524Z","iopub.status.idle":"2024-09-14T11:25:37.453842Z","shell.execute_reply.started":"2024-09-14T11:24:44.368478Z","shell.execute_reply":"2024-09-14T11:25:37.452939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def load_raw_images(data_frame, filenamecol, labelcol, image_size, n_classes=None):\n#     n_images = len(data_frame)\n#     X = np.empty((n_images, image_size, image_size, 3))\n    \n#     if labelcol is not None and n_classes is not None:\n#         Y = np.zeros((n_images, n_classes))  # One-hot encoding for train/validation set\n#     else:\n#         Y = None  # No labels for test set\n\n#     for index, entry in data_frame.iterrows():\n#         if labelcol is not None and n_classes is not None:\n#             Y[index, entry[labelcol]] = 1  # One-hot encoding for training/validation labels\n        \n#         # Load and resize the image\n#         img = cv2.imread(entry[filenamecol])\n#         X[index, :] = cv2.resize(img, (image_size, image_size))\n#         X[index, :] = X[index, :] / 255.0  # Normalize the image to [0, 1]\n\n#     return X, Y\n\n# # Load the test data\n# test_raw_data = pd.read_csv(\"../input/aptos2019-blindness-detection/test.csv\")\n\n# Create the filename column for the test images\n# test_raw_data[\"filename\"] = test_raw_data[\"id_code\"].map(lambda x: os.path.join(\"../input/aptos2019-blindness-detection/test_images\", x + \".png\"))\n\n# # Load the test images (without labels)\n# X_test, _ = load_raw_images(test_raw_data, \"filename\", None, img_size)\n\n# # Now X_test contains the processed images, Y_test is None because there are no labels in the test set.\n# print(\"X_test shape:\", X_test.shape)\n\n# # Create the filename column for the test images\n# test_raw_data[\"filename\"] = test_raw_data[\"id_code\"].map(lambda x: os.path.join(\"../input/aptos2019-blindness-detection/test_images\", x + \".png\"))\n\n# # Load the test images (without labels)\n# X_test, _ = load_raw_images(test_raw_data, \"filename\", None, img_size)\n\n# # Now X_test contains the processed images, Y_test is None because there are no labels in the test set.\n# print(\"X_test shape:\", X_test.shape)\n\n\n# test_df,val_df = train_test_split(test_raw_data,random_state=42,shuffle=True,test_size=0.4)\n# test_df.reset_index(drop=True,inplace=True)\n# val_df.reset_index(drop=True,inplace=True)\n# X_val,Y_val = load_raw_images_df(val_df,\"filename\",None,img_size,5)","metadata":{"execution":{"iopub.status.busy":"2024-09-13T01:46:57.913184Z","iopub.execute_input":"2024-09-13T01:46:57.914173Z","iopub.status.idle":"2024-09-13T01:46:57.928837Z","shell.execute_reply.started":"2024-09-13T01:46:57.914118Z","shell.execute_reply":"2024-09-13T01:46:57.927819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"X_train shape:\", X_train.shape)\nprint(\"Y_train shape:\", Y_train.shape)\nprint(\"X_train shape:\", X_val.shape)\nprint(\"Y_train shape:\", Y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:26:01.301270Z","iopub.execute_input":"2024-09-14T11:26:01.302030Z","iopub.status.idle":"2024-09-14T11:26:01.307498Z","shell.execute_reply.started":"2024-09-14T11:26:01.301989Z","shell.execute_reply":"2024-09-14T11:26:01.306499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MixupGenerator():\n    def __init__(self, X_train, y_train, batch_size=32, alpha=0.2, shuffle=True, datagen=None):\n        self.X_train = X_train\n        self.y_train = y_train\n        self.batch_size = batch_size\n        self.alpha = alpha\n        self.shuffle = shuffle\n        self.sample_num = len(X_train)\n        self.datagen = datagen\n\n    def __call__(self):\n        while True:\n            indexes = self.__get_exploration_order()\n            itr_num = int(len(indexes) // (self.batch_size * 2))\n\n            for i in range(itr_num):\n                batch_ids = indexes[i * self.batch_size * 2:(i + 1) * self.batch_size * 2]\n                X, y = self.__data_generation(batch_ids)\n\n                yield X, y\n\n    def __get_exploration_order(self):\n        indexes = np.arange(self.sample_num)\n\n        if self.shuffle:\n            np.random.shuffle(indexes)\n\n        return indexes\n\n    def __data_generation(self, batch_ids):\n        _, h, w, c = self.X_train.shape\n        l = np.random.beta(self.alpha, self.alpha, self.batch_size)\n        X_l = l.reshape(self.batch_size, 1, 1, 1)\n        y_l = l.reshape(self.batch_size, 1)\n\n        X1 = self.X_train[batch_ids[:self.batch_size]]\n        X2 = self.X_train[batch_ids[self.batch_size:]]\n        X = X1 * X_l + X2 * (1 - X_l)\n\n        if self.datagen:\n            for i in range(self.batch_size):\n                X[i] = self.datagen.random_transform(X[i])\n                X[i] = self.datagen.standardize(X[i])\n\n        if isinstance(self.y_train, list):\n            y = []\n\n            for y_train_ in self.y_train:\n                y1 = y_train_[batch_ids[:self.batch_size]]\n                y2 = y_train_[batch_ids[self.batch_size:]]\n                y.append(y1 * y_l + y2 * (1 - y_l))\n        else:\n            y1 = self.y_train[batch_ids[:self.batch_size]]\n            y2 = self.y_train[batch_ids[self.batch_size:]]\n            y = y1 * y_l + y2 * (1 - y_l)\n\n        return X, y","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:26:14.261186Z","iopub.execute_input":"2024-09-14T11:26:14.261572Z","iopub.status.idle":"2024-09-14T11:26:14.275821Z","shell.execute_reply.started":"2024-09-14T11:26:14.261534Z","shell.execute_reply":"2024-09-14T11:26:14.274857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n            \n            zoom_range=0.15,  # set range for random zoom\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)\ntraining_generator = MixupGenerator(X_train, Y_train, batch_size=batch_size, alpha=0.2, datagen=datagen)()","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:26:18.042241Z","iopub.execute_input":"2024-09-14T11:26:18.042919Z","iopub.status.idle":"2024-09-14T11:26:18.047933Z","shell.execute_reply.started":"2024-09-14T11:26:18.042869Z","shell.execute_reply":"2024-09-14T11:26:18.046986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**XÂY DỰNG MÔ HÌNH ViT**","metadata":{}},{"cell_type":"code","source":"learning_rate = 0.001\nweight_decay = 0.0001\nbatch_size = 64  #Batch size là số lượng mẫu dữ liệu trong một lần huấn luyện\nnum_epochs = 50 # 1 Epoch được tính là khi chúng ta đưa tất cả dữ liệu 1 lần\nimage_size = 300  # We'll resize input images to this size\npatch_size = 64  # Size of the patches to be extract from the input images\nnum_patches = (image_size // patch_size) ** 2\nprojection_dim = 64\nnum_heads = 4\ninput_shape = (300,300,3)\nnum_classes = 5\ntransformer_units = [\n    projection_dim * 2,\n    projection_dim,\n]  # Size of the transformer layers\ntransformer_layers = 8\nmlp_head_units = [\n    2048,\n    1024,\n]\n","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:26:26.391205Z","iopub.execute_input":"2024-09-14T11:26:26.391837Z","iopub.status.idle":"2024-09-14T11:26:26.397869Z","shell.execute_reply.started":"2024-09-14T11:26:26.391784Z","shell.execute_reply":"2024-09-14T11:26:26.396969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mlp(x, hidden_units, dropout_rate):\n    for units in hidden_units:\n        x = layers.Dense(units, activation=keras.activations.gelu)(x)\n        x = layers.Dropout(dropout_rate)(x)\n    return x","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:26:30.638871Z","iopub.execute_input":"2024-09-14T11:26:30.639258Z","iopub.status.idle":"2024-09-14T11:26:30.644469Z","shell.execute_reply.started":"2024-09-14T11:26:30.639220Z","shell.execute_reply":"2024-09-14T11:26:30.643521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# batch_size, height, width, and channels -> image\nfrom keras import layers\nclass Patches(layers.Layer):\n    def __init__(self, patch_size):\n        super().__init__()\n        self.patch_size = patch_size\n\n    def call(self, images):\n        input_shape = ops.shape(images)\n        batch_size = input_shape[0]\n        height = input_shape[1]\n        width = input_shape[2]\n        channels = input_shape[3]\n        num_patches_h = height // self.patch_size\n        num_patches_w = width // self.patch_size\n        patches = keras.ops.image.extract_patches(images, size=self.patch_size)\n        patches = ops.reshape(\n            patches,\n            (\n                batch_size,\n                num_patches_h * num_patches_w,\n                self.patch_size * self.patch_size * channels,\n            ),\n        )\n        return patches\n\n    def get_config(self):\n        config = super().get_config()\n        config.update({\"patch_size\": self.patch_size})\n        return config","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:26:36.240544Z","iopub.execute_input":"2024-09-14T11:26:36.240967Z","iopub.status.idle":"2024-09-14T11:26:36.249266Z","shell.execute_reply.started":"2024-09-14T11:26:36.240925Z","shell.execute_reply":"2024-09-14T11:26:36.248199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" tf.experimental.numpy.experimental_enable_numpy_behavior()","metadata":{"execution":{"iopub.status.busy":"2024-09-11T04:58:51.478496Z","iopub.execute_input":"2024-09-11T04:58:51.479361Z","iopub.status.idle":"2024-09-11T04:58:51.483535Z","shell.execute_reply.started":"2024-09-11T04:58:51.479318Z","shell.execute_reply":"2024-09-11T04:58:51.482479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import ops\nplt.figure(figsize=(4, 4))\nimage = X_train[np.random.choice(range(X_train.shape[0]))]\nplt.imshow(image)\nplt.axis(\"off\")\n\nresized_image = ops.image.resize(\n    ops.convert_to_tensor([image]), size=(img_size, img_size)\n)\npatches = Patches(patch_size)(resized_image)\nprint(f\"Image size: {img_size} X {img_size}\")\nprint(f\"Patch size: {patch_size} X {patch_size}\")\nprint(f\"Patches per image: {patches.shape[1]}\")\nprint(f\"Elements per patch: {patches.shape[-1]}\")\n\nn = int(np.sqrt(patches.shape[1]))\nplt.figure(figsize=(4, 4))\nfor i, patch in enumerate(patches[0]):\n    ax = plt.subplot(n, n, i + 1)\n    patch_img = ops.reshape(patch, (patch_size, patch_size, 3))\n    plt.imshow(ops.convert_to_numpy(patch_img))\n    plt.axis(\"off\")\n    \n    \n    \n# ---------------------------------------------------------------------------\n# ValueError                                Traceback (most recent call last)\n# Cell In[13], line 2\n#       1 plt.figure(figsize=(4, 4))\n# ----> 2 image = x_train[np.random.choice(range(x_train.shape[0]))]\n#       3 plt.imshow(image.astype(\"uint8\"))\n#       4 plt.axis(\"off\")\n\n# File numpy/random/mtrand.pyx:951, in numpy.random.mtrand.RandomState.choice()\n\n# ValueError: 'a' cannot be empty unless no samples are taken","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:26:48.006117Z","iopub.execute_input":"2024-09-14T11:26:48.007094Z","iopub.status.idle":"2024-09-14T11:26:50.016237Z","shell.execute_reply.started":"2024-09-14T11:26:48.007039Z","shell.execute_reply":"2024-09-14T11:26:50.015276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check if x_train is not empty\nif X_train.size > 0:\n    plt.figure(figsize=(4, 4))\n    image = X_train[np.random.choice(range(X_train.shape[0]))]\n    plt.imshow(image)\n    plt.axis(\"off\")\n\n    # Resizing the image\n    resized_image = ops.image.resize(\n        ops.convert_to_tensor([image]), size=(300, 300)\n    )\n    patches = Patches(patch_size)(resized_image)\n\n    # Print patch and image information\n    print(f\"Image size: {img_size} X {img_size}\")\n    print(f\"Patch size: {patch_size} X {patch_size}\")\n    print(f\"Patches per image: {patches.shape[1]}\")\n    print(f\"Elements per patch: {patches.shape[-1]}\")\n\n    # Display patches\n    n = int(np.sqrt(patches.shape[1]))\n    plt.figure(figsize=(4, 4))\n    for i, patch in enumerate(patches[0]):\n        ax = plt.subplot(n, n, i + 1)\n        patch_img = ops.reshape(patch, (patch_size, patch_size, 3))\n        plt.imshow(ops.convert_to_numpy(patch_img))\n        plt.axis(\"off\")\nelse:\n    print(\"x_train is empty!\")\n","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:27:01.341228Z","iopub.execute_input":"2024-09-14T11:27:01.341610Z","iopub.status.idle":"2024-09-14T11:27:02.414737Z","shell.execute_reply.started":"2024-09-14T11:27:01.341572Z","shell.execute_reply":"2024-09-14T11:27:02.413755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Xử lí bước đầu\nclass PatchEncoder(layers.Layer):\n    def __init__(self, num_patches, projection_dim): # Chiều của patches\n        super().__init__()\n        self.num_patches = num_patches\n        self.projection = layers.Dense(units=projection_dim)\n        self.position_embedding = layers.Embedding(\n            input_dim=num_patches, output_dim=projection_dim\n        )\n\n    def call(self, patch):\n        positions = ops.expand_dims(\n            ops.arange(start=0, stop=self.num_patches, step=1), axis=0\n        )\n        projected_patches = self.projection(patch)\n        encoded = projected_patches + self.position_embedding(positions)\n        return encoded\n\n    def get_config(self):\n        config = super().get_config()\n        config.update({\"num_patches\": self.num_patches})\n        return config","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:27:14.151749Z","iopub.execute_input":"2024-09-14T11:27:14.152617Z","iopub.status.idle":"2024-09-14T11:27:14.159986Z","shell.execute_reply.started":"2024-09-14T11:27:14.152575Z","shell.execute_reply":"2024-09-14T11:27:14.158838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#mo rong kho dataset\ndata_augmentation = keras.Sequential(\n    [\n        layers.Normalization(),\n        layers.Resizing(image_size, image_size),\n        layers.RandomFlip(\"horizontal\"),\n        layers.RandomRotation(factor=0.02),\n        layers.RandomZoom(height_factor=0.2, width_factor=0.2),\n    ],\n    name=\"data_augmentation\",\n)\n# Compute the mean and the variance of the training data for normalization.\ndata_augmentation.layers[0].adapt(X_train)","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:27:18.006157Z","iopub.execute_input":"2024-09-14T11:27:18.006821Z","iopub.status.idle":"2024-09-14T11:27:37.885661Z","shell.execute_reply.started":"2024-09-14T11:27:18.006764Z","shell.execute_reply":"2024-09-14T11:27:37.884600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_vit_classifier():\n    inputs = keras.Input(shape=input_shape)\n    # Augment data.\n    augmented = data_augmentation(inputs)\n    # Create patches.\n    patches = Patches(patch_size)(augmented)\n    # Encode patches.\n    encoded_patches = PatchEncoder(num_patches, projection_dim)(patches)\n\n    # Create multiple layers of the Transformer block.\n    for _ in range(transformer_layers):\n        # Layer normalization 1.\n        x1 = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)\n        # Create a multi-head attention layer.\n        attention_output = layers.MultiHeadAttention(\n            num_heads=num_heads, key_dim=projection_dim, dropout=0.1\n        )(x1, x1)\n        # Skip connection 1.\n        x2 = layers.Add()([attention_output, encoded_patches])\n        # Layer normalization 2.\n        x3 = layers.LayerNormalization(epsilon=1e-6)(x2)\n        # MLP.\n        x3 = mlp(x3, hidden_units=transformer_units, dropout_rate=0.1)\n        # Skip connection 2.\n        encoded_patches = layers.Add()([x3, x2])\n\n    # Create a [batch_size, projection_dim] tensor.\n    representation = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)\n    representation = layers.Flatten()(representation)\n    representation = layers.Dropout(0.5)(representation)\n    # Add MLP.\n    features = mlp(representation, hidden_units=mlp_head_units, dropout_rate=0.5)\n    # Classify outputs.\n    logits = layers.Dense(num_classes)(features)\n    # Create the Keras model.\n    model = keras.Model(inputs=inputs, outputs=logits)\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:27:43.818697Z","iopub.execute_input":"2024-09-14T11:27:43.819112Z","iopub.status.idle":"2024-09-14T11:27:43.829135Z","shell.execute_reply.started":"2024-09-14T11:27:43.819071Z","shell.execute_reply":"2024-09-14T11:27:43.828081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def run_experiment(model):\n    optimizer = keras.optimizers.AdamW(\n        learning_rate=learning_rate, weight_decay=weight_decay\n    )\n    model.compile(\n        optimizer=optimizer,\n        loss=keras.losses.CategoricalCrossentropy(from_logits=True),\n        metrics=[\n            keras.metrics.CategoricalAccuracy(name=\"accuracy\"),\n            keras.metrics.TopKCategoricalAccuracy(5, name=\"top-5-accuracy\"),\n        ],\n    )\n\n    checkpoint_filepath = \"/tmp/checkpoint.weights.h5\"\n    checkpoint_callback = keras.callbacks.ModelCheckpoint(\n        checkpoint_filepath,\n        monitor=\"val_accuracy\",\n        save_best_only=True,\n        save_weights_only=True,\n    )\n\n    history = model.fit(\n        x=X_train,\n        y=Y_train,\n        batch_size=batch_size,\n        epochs=num_epochs,\n        validation_data=(X_val, Y_val),\n        callbacks=[checkpoint_callback],\n    )\n\n    model.load_weights(checkpoint_filepath)\n    \n    _, accuracy, top_5_accuracy = model.evaluate(X_test, Y_test)\n    print(f\"Test accuracy: {round(accuracy * 100, 2)}%\")\n    print(f\"Test top 5 accuracy: {round(top_5_accuracy * 100, 2)}%\")\n\n    return history\n\n\n# Running the experiment\nhistor = run_experiment(create_vit_classifier())\n\n# Plot history\ndef plot_history(item):\n    plt.plot(histor.history[item], label=item)\n    plt.plot(histor.history[\"val_\" + item], label=\"val_\" + item)\n    plt.xlabel(\"Epochs\")\n    plt.ylabel(item)\n    plt.title(\"Train and Validation {} Over Epochs\".format(item), fontsize=14)\n    plt.legend()\n    plt.grid()\n    plt.show()\n\nplot_history(\"loss\")\nplot_history(\"top-5-accuracy\")","metadata":{"execution":{"iopub.status.busy":"2024-09-14T11:37:09.092342Z","iopub.execute_input":"2024-09-14T11:37:09.092764Z","iopub.status.idle":"2024-09-14T11:44:50.658267Z","shell.execute_reply.started":"2024-09-14T11:37:09.092719Z","shell.execute_reply":"2024-09-14T11:44:50.657294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**CONFUSION MATRIX**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n\ndef plot_confusion_matrix(y_true, y_pred, classes,\n                          normalize=False,\n                          title=None,\n                          cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Normalization can be applied by setting `normalize=True`.\n    \"\"\"\n    if not title:\n        if normalize:\n            title = 'Normalized confusion matrix'\n        else:\n            title = 'Confusion matrix, without normalization'\n\n    # Compute confusion matrix\n    cm = confusion_matrix(y_true, y_pred)\n    \n    # Only use the labels that appear in the data\n    classes = np.array(classes)  # Ensure classes is a numpy array\n    unique_labels = np.unique(np.concatenate([y_true, y_pred]))\n    classes = classes[unique_labels]\n\n    if normalize:\n        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n        print(\"Normalized confusion matrix\")\n    else:\n        print('Confusion matrix, without normalization')\n\n    print(cm)\n\n    fig, ax = plt.subplots()\n    im = ax.imshow(cm, interpolation='nearest', cmap=cmap)\n    ax.figure.colorbar(im, ax=ax)\n    # We want to show all ticks...\n    ax.set(xticks=np.arange(cm.shape[1]),\n           yticks=np.arange(cm.shape[0]),\n           # ... and label them with the respective list entries\n           xticklabels=classes, yticklabels=classes,\n           title=title,\n           ylabel='True label',\n           xlabel='Predicted label')\n\n    # Rotate the tick labels and set their alignment.\n    plt.setp(ax.get_xticklabels(), rotation=45, ha=\"right\",\n             rotation_mode=\"anchor\")\n\n    # Loop over data dimensions and create text annotations.\n    fmt = '.2f' if normalize else 'd'\n    thresh = cm.max() / 2.\n    for i in range(cm.shape[0]):\n        for j in range(cm.shape[1]):\n            ax.text(j, i, format(cm[i, j], fmt),\n                    ha=\"center\", va=\"center\",\n                    color=\"white\" if cm[i, j] > thresh else \"black\")\n    fig.tight_layout()\n    return ax\n","metadata":{"execution":{"iopub.status.busy":"2024-09-14T12:01:39.572237Z","iopub.execute_input":"2024-09-14T12:01:39.573106Z","iopub.status.idle":"2024-09-14T12:01:39.585103Z","shell.execute_reply.started":"2024-09-14T12:01:39.573062Z","shell.execute_reply":"2024-09-14T12:01:39.584030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_vit_classifier()\nY_val_pred = model.predict(X_val)\nY_val_pred_hot = np.argmax(Y_val_pred, axis=1)\nY_val_actual_hot = np.argmax(Y_val, axis=1)\n\n# Vẽ ma trận nhầm lẫn\nplot_confusion_matrix(Y_val_actual_hot, Y_val_pred_hot, class_labels)","metadata":{"execution":{"iopub.status.busy":"2024-09-14T12:01:42.603629Z","iopub.execute_input":"2024-09-14T12:01:42.604025Z","iopub.status.idle":"2024-09-14T12:01:48.242742Z","shell.execute_reply.started":"2024-09-14T12:01:42.603984Z","shell.execute_reply":"2024-09-14T12:01:48.241871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n# Chọn một chỉ số ngẫu nhiên từ tập dữ liệu test\nrandom_index = random.randint(0, X_test.shape[0] - 1)\n\n# Lấy hình ảnh và nhãn ngẫu nhiên\nsample_image = X_test[random_index]\n\n# Thêm dimension cho batch\nsample_image_expanded = np.expand_dims(sample_image, axis=0)","metadata":{"execution":{"iopub.status.busy":"2024-09-14T12:01:55.319762Z","iopub.execute_input":"2024-09-14T12:01:55.320170Z","iopub.status.idle":"2024-09-14T12:01:55.325605Z","shell.execute_reply.started":"2024-09-14T12:01:55.320129Z","shell.execute_reply":"2024-09-14T12:01:55.324603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the label mapping\nlabel_title = {\n    \"0\": \"No DR\",\n    \"1\": \"Mild\",\n    \"2\": \"Moderate\",\n    \"3\": \"Severe\",\n    \"4\": \"Proliferative DR\"\n}\n\n# Convert label indices to class names\ndef index_to_label(index):\n    return label_title[str(index)]\n\n# Chọn một chỉ số ngẫu nhiên từ tập dữ liệu test\nrandom_index = random.randint(0, X_test.shape[0] - 1)\n\n# Lấy hình ảnh và nhãn ngẫu nhiên\nsample_image = X_test[random_index]\ntrue_label = Y_test[random_index]\n\n# Nếu true_label là dạng one-hot encoding, chuyển nó thành chỉ số lớp\ntrue_label_index = np.argmax(true_label)\n\n# Expand dimensions to match the input shape expected by the model (batch size, height, width, channels)\nsample_image_expanded = np.expand_dims(sample_image, axis=0)\nprint(sample_image_expanded.shape)\n\n# Get predictions from the model\nmodel = create_vit_classifier()  # Ensure to use the same model used for training\npredictions = model.predict(sample_image_expanded)\n\n# Tìm lớp dự đoán\npredicted_class_index = np.argmax(predictions, axis=1)\n\n# Map indices to class names\ntrue_label_name = index_to_label(true_label_index)\npredicted_label_name = index_to_label(predicted_class_index[0])\n\n# Hiển thị hình ảnh và kết quả dự đoán\nplt.figure(figsize=(4, 4))\nplt.imshow(sample_image)\nplt.title(f'Predicted class: {predicted_label_name} | True label: {true_label_name}')\nplt.axis('off')\nplt.show()\n\nprint(f'Predicted class: {predicted_label_name}')\nprint(f'True label: {true_label_name}')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-09-14T12:06:35.374495Z","iopub.execute_input":"2024-09-14T12:06:35.375180Z","iopub.status.idle":"2024-09-14T12:06:37.389727Z","shell.execute_reply.started":"2024-09-14T12:06:35.375135Z","shell.execute_reply":"2024-09-14T12:06:37.388858Z"},"trusted":true},"execution_count":null,"outputs":[]}]}