{"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":"none","dataSources":[{"sourceId":14774,"databundleVersionId":875431,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-09-12T03:28:59.006993Z","iopub.execute_input":"2024-09-12T03:28:59.007948Z","iopub.status.idle":"2024-09-12T03:29:06.246427Z","shell.execute_reply.started":"2024-09-12T03:28:59.007843Z","shell.execute_reply":"2024-09-12T03:29:06.245067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"kaggle competitions download -c aptos2019-blindness-detection","metadata":{"execution":{"iopub.status.busy":"2024-09-06T16:11:19.468663Z","iopub.execute_input":"2024-09-06T16:11:19.469097Z","iopub.status.idle":"2024-09-06T16:11:19.510679Z","shell.execute_reply.started":"2024-09-06T16:11:19.469058Z","shell.execute_reply":"2024-09-06T16:11:19.508896Z"}}},{"cell_type":"code","source":"import cv2\nimport keras\nfrom keras import layers\nfrom keras import ops\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport os\nimport pandas as pd\n\nfrom tensorflow.image import resize","metadata":{"execution":{"iopub.status.busy":"2024-09-12T03:29:06.248776Z","iopub.execute_input":"2024-09-12T03:29:06.249471Z","iopub.status.idle":"2024-09-12T03:29:21.51671Z","shell.execute_reply.started":"2024-09-12T03:29:06.249414Z","shell.execute_reply":"2024-09-12T03:29:21.515652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport random\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\ndef show_random_image_from_folder(folder_path):\n    # Lấy danh sách tất cả các tệp ảnh trong thư mục\n    all_files = os.listdir(folder_path)\n    image_files = [f for f in all_files if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]\n    \n    if not image_files:\n        print(\"Không tìm thấy ảnh trong thư mục.\")\n        return\n    \n    # Chọn ngẫu nhiên một tệp ảnh từ danh sách\n    random_image_file = random.choice(image_files)\n    image_path = os.path.join(folder_path, random_image_file)\n    \n    # Mở và hiển thị ảnh\n    with Image.open(image_path) as img:\n        plt.figure(figsize=(6, 6))\n        plt.imshow(img)\n        plt.axis('off')  # Tắt trục để hiển thị ảnh đẹp hơn\n        plt.title(f\"Ảnh ngẫu nhiên: {random_image_file}\")\n        plt.show()\n\n# Thay thế bằng đường dẫn đến thư mục chứa ảnh của bạn\nfolder_path = '../input/aptos2019-blindness-detection/test_images'\nshow_random_image_from_folder(folder_path)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-12T03:29:21.518207Z","iopub.execute_input":"2024-09-12T03:29:21.519004Z","iopub.status.idle":"2024-09-12T03:29:22.002668Z","shell.execute_reply.started":"2024-09-12T03:29:21.518945Z","shell.execute_reply":"2024-09-12T03:29:22.001315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport tensorflow as tf\n\ndef load_images_and_labels(image_dir, labels_df, target_size=(900, 600), num_samples=100):\n    # Đảm bảo labels_df là một DataFrame\n    if isinstance(labels_df, str):\n        labels_df = pd.read_csv(labels_df)\n    \n    # Lấy mẫu 100 dòng từ tập dữ liệu\n    labels_df = labels_df.sample(n=num_samples)\n    \n    # Append '.png' to id_code to match file names\n    labels_df['id_code'] = labels_df['id_code'].apply(lambda x: x + '.png')\n\n    images = []\n    labels = []\n    \n    for _, row in labels_df.iterrows():\n        img_path = os.path.join(image_dir, row['id_code'])\n        if os.path.exists(img_path):\n            img = tf.io.read_file(img_path)\n            img = tf.image.decode_jpeg(img, channels=3)  # Decoding image\n            img = tf.image.resize(img, target_size)\n            images.append(img)\n            labels.append(row['diagnosis'])  # Load labels for training data\n    \n    # Convert to TensorFlow tensors\n    images = tf.stack(images)\n    labels = tf.convert_to_tensor(labels)\n    \n    return images, labels\n\n\ndef load_images_without_labels(image_dir, labels_df, target_size=(900, 600), num_samples=100):\n    # Đảm bảo labels_df là một DataFrame\n    if isinstance(labels_df, str):\n        labels_df = pd.read_csv(labels_df)\n    \n    # Lấy mẫu 100 dòng từ tập dữ liệu\n    labels_df = labels_df.sample(n=num_samples)\n    \n    # Append '.png' to id_code to match file names\n    labels_df['id_code'] = labels_df['id_code'].apply(lambda x: x + '.png')\n\n    images = []\n    \n    for _, row in labels_df.iterrows():\n        img_path = os.path.join(image_dir, row['id_code'])\n        if os.path.exists(img_path):\n            img = tf.io.read_file(img_path)\n            img = tf.image.decode_jpeg(img, channels=3)  # Decoding image\n            img = tf.image.resize(img, target_size)\n            images.append(img)\n    \n    # Convert to TensorFlow tensors\n    images = tf.stack(images)\n    \n    return images\n\n\n# Đường dẫn đến tệp CSV và thư mục ảnh\ntrain_labels_csv_path = \"../input/aptos2019-blindness-detection/train.csv\"\ntest_labels_csv_path = \"../input/aptos2019-blindness-detection/test.csv\"\ntrain_dir = '../input/aptos2019-blindness-detection/train_images'\ntest_dir = '../input/aptos2019-blindness-detection/test_images'\n\n# Load 100 training samples (ảnh và nhãn)\nx_train, y_train = load_images_and_labels(train_dir, train_labels_csv_path, num_samples=100)\n\n# Load 100 test samples (chỉ ảnh)\nx_test = load_images_without_labels(test_dir, test_labels_csv_path, num_samples=100)\n\n# Kiểm tra kích thước của ảnh và nhãn\nprint(f\"Shape of x_train: {x_train.shape}\")\nprint(f\"Shape of y_train: {y_train.shape}\")\nprint(f\"Shape of x_test: {x_test.shape}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-09-12T03:38:36.885413Z","iopub.execute_input":"2024-09-12T03:38:36.885947Z","iopub.status.idle":"2024-09-12T03:39:05.744822Z","shell.execute_reply.started":"2024-09-12T03:38:36.885895Z","shell.execute_reply":"2024-09-12T03:39:05.743261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import ops\n\n# Model parameters\nlearning_rate = 0.001\nweight_decay = 0.0001\nbatch_size = 256  # Batch size\nnum_epochs = 10  # Number of epochs\nwidth = 900\nheight = 600  # Image size\npatch_size = 64  # Patch size for images\nnum_patches = (height // patch_size) * (width // patch_size)  # Total patches\nprojection_dim = 64\nnum_heads = 4\ntransformer_units = [\n    projection_dim * 2,\n    projection_dim,\n]  # Size of the transformer layers\ntransformer_layers = 8\nmlp_head_units = [2048, 1024]  # Dense layer sizes for MLP head\n","metadata":{"execution":{"iopub.status.busy":"2024-09-12T03:45:26.880625Z","iopub.execute_input":"2024-09-12T03:45:26.88134Z","iopub.status.idle":"2024-09-12T03:45:26.979653Z","shell.execute_reply.started":"2024-09-12T03:45:26.881269Z","shell.execute_reply":"2024-09-12T03:45:26.978405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define MLP function\ndef 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-12T03:45:28.659587Z","iopub.execute_input":"2024-09-12T03:45:28.660876Z","iopub.status.idle":"2024-09-12T03:45:28.666686Z","shell.execute_reply.started":"2024-09-12T03:45:28.660818Z","shell.execute_reply":"2024-09-12T03:45:28.665477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the Patches layer\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 = tf.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 = tf.image.extract_patches(\n            images=images,\n            sizes=[1, self.patch_size, self.patch_size, 1],\n            strides=[1, self.patch_size, self.patch_size, 1],\n            rates=[1, 1, 1, 1],\n            padding=\"VALID\",\n        )\n        patches = tf.reshape(\n            patches,\n            [batch_size, num_patches_h * num_patches_w, self.patch_size * self.patch_size * channels],\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\n","metadata":{"execution":{"iopub.status.busy":"2024-09-12T03:45:40.792791Z","iopub.execute_input":"2024-09-12T03:45:40.793268Z","iopub.status.idle":"2024-09-12T03:45:40.805716Z","shell.execute_reply.started":"2024-09-12T03:45:40.793227Z","shell.execute_reply":"2024-09-12T03:45:40.804314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" tf.experimental.numpy.experimental_enable_numpy_behavior()","metadata":{"execution":{"iopub.status.busy":"2024-09-12T03:45:48.985016Z","iopub.execute_input":"2024-09-12T03:45:48.985537Z","iopub.status.idle":"2024-09-12T03:45:48.991868Z","shell.execute_reply.started":"2024-09-12T03:45:48.985491Z","shell.execute_reply":"2024-09-12T03:45:48.990513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom tensorflow.keras import ops\n\n# Randomly select an image from the training set\nplt.figure(figsize=(4, 4))\nimage = x_train[np.random.choice(range(x_train.shape[0]))]\nplt.imshow(image.numpy().astype(\"uint8\"))  # Ensure proper numpy conversion\nplt.axis(\"off\")\n\n# Resize the image\nresized_image = ops.image.resize(\n    ops.convert_to_tensor([image]), size=(900, 600)\n)\n\n# Extract patches\npatches = Patches(patch_size)(resized_image)\n\n# Print patch information\nprint(f\"Image size: {900} X {600}\")\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\n# Calculate n based on the number of patches\nn = int(np.ceil(np.sqrt(patches.shape[1])))\n\n# Display patches\nplt.figure(figsize=(8, 8))\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(tf.convert_to_tensor(patch_img).numpy().astype(\"uint8\"))\n    plt.axis(\"off\")\n","metadata":{"execution":{"iopub.status.busy":"2024-09-12T03:51:04.743621Z","iopub.execute_input":"2024-09-12T03:51:04.744204Z","iopub.status.idle":"2024-09-12T03:51:10.47072Z","shell.execute_reply.started":"2024-09-12T03:51:04.744156Z","shell.execute_reply":"2024-09-12T03:51:10.469357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install tensorflow-addons\n","metadata":{"execution":{"iopub.status.busy":"2024-09-12T03:51:10.473188Z","iopub.execute_input":"2024-09-12T03:51:10.473727Z","iopub.status.idle":"2024-09-12T03:51:30.526552Z","shell.execute_reply.started":"2024-09-12T03:51:10.473672Z","shell.execute_reply":"2024-09-12T03:51:30.524561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install keras\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import ops\nimport tensorflow_addons as tfa\n\n# Assuming Patches is a custom class already defined\npatch_size = 64  # Size of each patch\n\n# Function to display an image\nplt.figure(figsize=(4, 4))\nimage = x_train[np.random.choice(range(x_train.shape[0]))]\nplt.imshow(image.numpy().astype(\"uint8\"))  # Convert Tensor to NumPy for display\nplt.axis(\"off\")\n\n# Resize the image to the desired dimensions (900x600)\nresized_image = tf.image.resize(image, size=(900, 600))\n\n# Extract patches using TensorFlow Addons (tfa)\npatches = tf.image.extract_patches(\n    images=tf.expand_dims(resized_image, axis=0),  # Add batch dimension\n    sizes=[1, patch_size, patch_size, 1],\n    strides=[1, patch_size, patch_size, 1],\n    rates=[1, 1, 1, 1],\n    padding='VALID'\n)\n\n# Calculate number of patches\nnum_patches = patches.shape[1] * patches.shape[2]\n\nprint(f\"Image size: {900} X {600}\")\nprint(f\"Patch size: {patch_size} X {patch_size}\")\nprint(f\"Patches per image: {num_patches}\")\nprint(f\"Elements per patch: {patches.shape[-1]}\")\n\n# Display patches in a grid\nn = int(np.ceil(np.sqrt(num_patches)))  # Determine grid size for displaying patches\nplt.figure(figsize=(12, 12))\n\npatches_reshaped = tf.reshape(patches, [num_patches, patch_size, patch_size, 3])\n\nfor i, patch in enumerate(patches_reshaped):\n    if i >= n * n:\n        break\n    ax = plt.subplot(n, n, i + 1)\n    plt.imshow(patch.numpy().astype(\"uint8\"))  # Convert each patch to NumPy\n    plt.axis(\"off\")\nplt.show()\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-09-12T03:58:15.69726Z","iopub.execute_input":"2024-09-12T03:58:15.697791Z","iopub.status.idle":"2024-09-12T03:58:32.169248Z","shell.execute_reply.started":"2024-09-12T03:58:15.697741Z","shell.execute_reply":"2024-09-12T03:58:32.167262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define PatchEncoder class\nclass PatchEncoder(layers.Layer):\n    def __init__(self, num_patches, projection_dim):\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-12T03:53:48.309658Z","iopub.execute_input":"2024-09-12T03:53:48.310173Z","iopub.status.idle":"2024-09-12T03:53:48.321074Z","shell.execute_reply.started":"2024-09-12T03:53:48.310104Z","shell.execute_reply":"2024-09-12T03:53:48.319574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}