{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":52254,"databundleVersionId":8756537,"sourceType":"competition"},{"sourceId":139134997,"sourceType":"kernelVersion"},{"sourceId":139881797,"sourceType":"kernelVersion"}],"dockerImageVersionId":30528,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Abdominal Trauma UNET - Target Organ\nfrom segmentation image of target organ, predict it in CT scan images<br/> \nhttps://www.kaggle.com/code/stpeteishii/abdominal-trauma-unet","metadata":{"papermill":{"duration":0.009655,"end_time":"2023-07-11T04:08:31.528281","exception":false,"start_time":"2023-07-11T04:08:31.518626","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"images: https://www.kaggle.com/code/stpeteishii/one-patient-images-slide-show <br/>\nmasks: https://www.kaggle.com/code/stpeteishii/segmentation-image-of-the-matched-id","metadata":{}},{"cell_type":"markdown","source":"https://arxiv.org/pdf/1505.04597.pdf","metadata":{"papermill":{"duration":0.008294,"end_time":"2023-07-11T04:08:31.545451","exception":false,"start_time":"2023-07-11T04:08:31.537157","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!pip install imantics --quiet","metadata":{"papermill":{"duration":15.417197,"end_time":"2023-07-11T04:08:46.970986","exception":false,"start_time":"2023-07-11T04:08:31.553789","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:36.352919Z","iopub.execute_input":"2024-09-13T16:40:36.353292Z","iopub.status.idle":"2024-09-13T16:40:47.555427Z","shell.execute_reply.started":"2024-09-13T16:40:36.353259Z","shell.execute_reply":"2024-09-13T16:40:47.554096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport json\nimport random\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport imantics\nfrom PIL import Image\nfrom skimage.transform import resize\nfrom sklearn.model_selection import train_test_split\n%matplotlib inline","metadata":{"papermill":{"duration":8.983267,"end_time":"2023-07-11T04:08:55.963588","exception":false,"start_time":"2023-07-11T04:08:46.980321","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:47.558491Z","iopub.execute_input":"2024-09-13T16:40:47.558899Z","iopub.status.idle":"2024-09-13T16:40:47.570107Z","shell.execute_reply.started":"2024-09-13T16:40:47.558858Z","shell.execute_reply":"2024-09-13T16:40:47.568945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Định nghĩa đường dẫn đến thư mục chứa dữ liệu\nimages_dir ='/kaggle/input/one-patient-images-slide-show'\nmasks_dir = '/kaggle/input/segmentation-image-of-the-matched-id/4' # left kidney\norgans=['bowel','liver','spleen','right kidney','left kidney']","metadata":{"papermill":{"duration":0.02292,"end_time":"2023-07-11T04:08:55.996944","exception":false,"start_time":"2023-07-11T04:08:55.974024","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:47.571403Z","iopub.execute_input":"2024-09-13T16:40:47.571727Z","iopub.status.idle":"2024-09-13T16:40:47.580778Z","shell.execute_reply.started":"2024-09-13T16:40:47.571701Z","shell.execute_reply":"2024-09-13T16:40:47.579841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tạo danh sách ảnh và mặt nạ\nimages_listdir0 = sorted(os.listdir(images_dir))\nimages_listdir = [images_listdir0[i] for i in range(len(images_listdir0)) if i % 4 == 0]\nimages_listdir = images_listdir[50:99]\nmasks_listdir = sorted(os.listdir(masks_dir))\nN=list(range(9))\nrandom_N = N","metadata":{"papermill":{"duration":0.257623,"end_time":"2023-07-11T04:08:56.264361","exception":false,"start_time":"2023-07-11T04:08:56.006738","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:47.583485Z","iopub.execute_input":"2024-09-13T16:40:47.584221Z","iopub.status.idle":"2024-09-13T16:40:47.594227Z","shell.execute_reply.started":"2024-09-13T16:40:47.584195Z","shell.execute_reply":"2024-09-13T16:40:47.59346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(images_listdir))\nprint(len(masks_listdir))\n#number of images with left kidney is limited, so use all images in #4 folder. ","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:47.595546Z","iopub.execute_input":"2024-09-13T16:40:47.595794Z","iopub.status.idle":"2024-09-13T16:40:47.603324Z","shell.execute_reply.started":"2024-09-13T16:40:47.595772Z","shell.execute_reply":"2024-09-13T16:40:47.60233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Định nghĩa hàm để đếm số lượng ảnh\ndef count_images_in_directory(image_dir):\n    # Các phần mở rộng hợp lệ cho ảnh\n    valid_extensions = ('.png', '.jpg', '.jpeg')\n    \n    # Liệt kê tất cả các tệp trong thư mục và đếm những tệp có phần mở rộng hợp lệ\n    image_files = [f for f in os.listdir(image_dir) if f.endswith(valid_extensions)]\n    \n    return len(image_files)\n\n# Đường dẫn đến thư mục ảnh\nimage_dir = \"/kaggle/input/segmentation-image-of-the-matched-id/4/\"\n\n# Đếm số lượng ảnh\nnum_images = count_images_in_directory(image_dir)\nprint(f\"Số lượng ảnh trong thư mục: {num_images}\")","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:47.604433Z","iopub.execute_input":"2024-09-13T16:40:47.604725Z","iopub.status.idle":"2024-09-13T16:40:47.614754Z","shell.execute_reply.started":"2024-09-13T16:40:47.604701Z","shell.execute_reply":"2024-09-13T16:40:47.613813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\n# Đường dẫn đến thư mục ảnh và mặt nạ\nimage_dir = \"/kaggle/input/segmentation-image-of-the-matched-id/4/\"\nmask_dir = \"/kaggle/input/one-patient-images-slide-show/\"\n\nprint(\"Danh sách tệp ảnh:\", os.listdir(image_dir))\nprint(\"Danh sách tệp mặt nạ:\", os.listdir(mask_dir))\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:47.61585Z","iopub.execute_input":"2024-09-13T16:40:47.61621Z","iopub.status.idle":"2024-09-13T16:40:47.624245Z","shell.execute_reply.started":"2024-09-13T16:40:47.616185Z","shell.execute_reply":"2024-09-13T16:40:47.623338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\ndef check_image_size(image_dir, mask_dir):\n    image_files = [f for f in os.listdir(image_dir) if f.endswith(valid_extensions)]\n    mask_files = [f for f in os.listdir(mask_dir) if f.endswith(valid_extensions)]\n    \n    for img_file in image_files:\n        img_path = os.path.join(image_dir, img_file)\n        if img_file in mask_files:\n            mask_path = os.path.join(mask_dir, img_file)\n            img = Image.open(img_path)\n            mask = Image.open(mask_path)\n            print(f\"Ảnh: {img_file} - Kích thước ảnh: {img.size} - Kích thước mặt nạ: {mask.size}\")\n\ncheck_image_size(image_dir, mask_dir)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:47.625522Z","iopub.execute_input":"2024-09-13T16:40:47.625776Z","iopub.status.idle":"2024-09-13T16:40:47.635256Z","shell.execute_reply.started":"2024-09-13T16:40:47.625754Z","shell.execute_reply":"2024-09-13T16:40:47.634509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Định nghĩa kích thước ảnh\nimage_size=512\ninput_image_size=(512,512)","metadata":{"papermill":{"duration":0.020017,"end_time":"2023-07-11T04:08:56.336291","exception":false,"start_time":"2023-07-11T04:08:56.316274","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:47.636357Z","iopub.execute_input":"2024-09-13T16:40:47.636654Z","iopub.status.idle":"2024-09-13T16:40:47.644737Z","shell.execute_reply.started":"2024-09-13T16:40:47.63663Z","shell.execute_reply":"2024-09-13T16:40:47.643873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hàm đọc ảnh\ndef read_image(path):\n    img = cv2.imread(path)\n    img = cv2.resize(img, (image_size, image_size))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    return img\n","metadata":{"papermill":{"duration":0.018858,"end_time":"2023-07-11T04:08:56.363872","exception":false,"start_time":"2023-07-11T04:08:56.345014","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:47.649109Z","iopub.execute_input":"2024-09-13T16:40:47.649355Z","iopub.status.idle":"2024-09-13T16:40:47.654713Z","shell.execute_reply.started":"2024-09-13T16:40:47.649333Z","shell.execute_reply":"2024-09-13T16:40:47.65376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Input images ","metadata":{"papermill":{"duration":0.008863,"end_time":"2023-07-11T04:08:56.409542","exception":false,"start_time":"2023-07-11T04:08:56.400679","status":"completed"},"tags":[]}},{"cell_type":"code","source":"number=120\nrows = 3\ncols = 3\nfig, ax = plt.subplots(rows, cols, figsize = (10,10))\nfor i, ax in enumerate(ax.flat):\n    if i < len(random_N):\n        img = read_image(f\"{images_dir}/{images_listdir[i]}\")\n        ax.set_title(f\"{images_listdir[i]}\")\n        ax.imshow(img)\n        ax.axis('off')","metadata":{"papermill":{"duration":1.818676,"end_time":"2023-07-11T04:08:58.237091","exception":false,"start_time":"2023-07-11T04:08:56.418415","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:47.65586Z","iopub.execute_input":"2024-09-13T16:40:47.65612Z","iopub.status.idle":"2024-09-13T16:40:49.002389Z","shell.execute_reply.started":"2024-09-13T16:40:47.656097Z","shell.execute_reply":"2024-09-13T16:40:49.001458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ground truth masks","metadata":{"papermill":{"duration":0.020571,"end_time":"2023-07-11T04:08:58.277611","exception":false,"start_time":"2023-07-11T04:08:58.25704","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def read_mask(path, image_size):\n    mask = cv2.imread(path, cv2.IMREAD_GRAYSCALE)  # Đọc mặt nạ với chế độ grayscale\n    if mask is None:\n        raise ValueError(f\"Không thể đọc mặt nạ từ đường dẫn: {path}\")\n\n    # Đảm bảo image_size là một tuple (width, height)\n    mask = cv2.resize(mask, image_size)  # Sử dụng image_size là tuple (width, height)\n    mask = mask.astype(np.uint8)  # Đảm bảo kiểu dữ liệu là uint8\n    return mask\n\n# Đảm bảo image_size là tuple (width, height)\nimage_size = (512, 512)  # Ví dụ với kích thước 512x512\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:49.003702Z","iopub.execute_input":"2024-09-13T16:40:49.004002Z","iopub.status.idle":"2024-09-13T16:40:49.010544Z","shell.execute_reply.started":"2024-09-13T16:40:49.003975Z","shell.execute_reply":"2024-09-13T16:40:49.009492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hiển thị một số mặt nạ\nfig, ax = plt.subplots(rows, cols, figsize=(10, 10))\nfor i, ax in enumerate(ax.flat):\n    if i < len(masks_listdir):\n        if os.path.exists(os.path.join(masks_dir, masks_listdir[i])):\n            img = read_mask(f\"{masks_dir}/{masks_listdir[i]}\", image_size)  # Thêm image_size\n            ax.set_title(f\"{masks_listdir[i]}\")\n            ax.imshow(img, cmap='gray')  # Mặt nạ đã ở dạng grayscale, không cần chuyển đổi\n            ax.axis('off')\n        else:\n            print('File không tồn tại:', masks_listdir[i])\n","metadata":{"papermill":{"duration":0.905126,"end_time":"2023-07-11T04:08:59.201955","exception":false,"start_time":"2023-07-11T04:08:58.296829","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:49.011743Z","iopub.execute_input":"2024-09-13T16:40:49.012015Z","iopub.status.idle":"2024-09-13T16:40:50.062356Z","shell.execute_reply.started":"2024-09-13T16:40:49.01199Z","shell.execute_reply":"2024-09-13T16:40:50.061309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 512\nMASKS=np.zeros((1,image_size, image_size, 1), dtype=bool)\nIMAGES=np.zeros((1,image_size, image_size, 3),dtype=np.uint8)\n\nfor j,file in enumerate(images_listdir):   ##the smaller, the faster\n    try:\n        image = read_image(f\"{images_dir}/{file}\")\n        image_ex = np.expand_dims(image, axis=0)\n        IMAGES = np.vstack([IMAGES, image_ex])\n        mask = read_image(f\"{masks_dir}/{masks_listdir[j]}\") \n        mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)\n        mask = mask.reshape(512,512,1)\n        mask_ex = np.expand_dims(mask, axis=0)    \n        MASKS = np.vstack([MASKS, mask_ex])\n    except:\n        print(file)\n        continue","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:50.063514Z","iopub.execute_input":"2024-09-13T16:40:50.063783Z","iopub.status.idle":"2024-09-13T16:40:50.688941Z","shell.execute_reply.started":"2024-09-13T16:40:50.063758Z","shell.execute_reply":"2024-09-13T16:40:50.688099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Kiểm tra kiểu dữ liệu\nprint(\"Dtype của ảnh:\", IMAGES.dtype)\nprint(\"Dtype của mặt nạ:\", MASKS.dtype)\n\n# Kiểm tra NaN và Inf\nprint(\"NaN hoặc Inf trong IMAGES:\", check_data_validity(IMAGES))\nprint(\"NaN hoặc Inf trong MASKS:\", check_data_validity(MASKS))\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:50.690254Z","iopub.execute_input":"2024-09-13T16:40:50.690712Z","iopub.status.idle":"2024-09-13T16:40:50.749183Z","shell.execute_reply.started":"2024-09-13T16:40:50.690675Z","shell.execute_reply":"2024-09-13T16:40:50.74821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Chuyển đổi giá trị pixel từ [0, 1] sang [0, 255] và sau đó chuyển đổi kiểu dữ liệu thành uint8\n#IMAGES = (IMAGES * 255).astype(np.uint8)\n\n# Kiểm tra lại kiểu dữ liệu sau khi chuyển đổi\n#print(\"Dtype of sample image after conversion to uint8:\", IMAGES.dtype)\n#print(\"Dtype of sample mask:\", MASKS.dtype)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:50.750422Z","iopub.execute_input":"2024-09-13T16:40:50.750753Z","iopub.status.idle":"2024-09-13T16:40:50.755098Z","shell.execute_reply.started":"2024-09-13T16:40:50.750726Z","shell.execute_reply":"2024-09-13T16:40:50.75414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Chuyển đổi mảng thành numpy array và loại bỏ phần tử đầu tiên (rỗng)\nimages = np.array(IMAGES)[1:number+1]\nmasks = np.array(MASKS)[1:number+1]\n# Kiểm tra kích thước dữ liệu\nprint(images.shape, masks.shape)","metadata":{"papermill":{"duration":0.042314,"end_time":"2023-07-11T04:09:00.448602","exception":false,"start_time":"2023-07-11T04:09:00.406288","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:50.756484Z","iopub.execute_input":"2024-09-13T16:40:50.756859Z","iopub.status.idle":"2024-09-13T16:40:50.782746Z","shell.execute_reply.started":"2024-09-13T16:40:50.756825Z","shell.execute_reply":"2024-09-13T16:40:50.781807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\n\n# Khởi tạo Adam optimizer với learning rate thấp hơn\noptimizer = tf.keras.optimizers.Adam(learning_rate=0.001)\nunet_model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:50.785794Z","iopub.execute_input":"2024-09-13T16:40:50.786128Z","iopub.status.idle":"2024-09-13T16:40:50.800511Z","shell.execute_reply.started":"2024-09-13T16:40:50.786103Z","shell.execute_reply":"2024-09-13T16:40:50.799756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# U-Net","metadata":{"papermill":{"duration":0.02079,"end_time":"2023-07-11T04:09:00.614825","exception":false,"start_time":"2023-07-11T04:09:00.594035","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def check_data_validity(data):\n    return not (np.isnan(data).any() or np.isinf(data).any())\n\nprint(\"NaN or Inf in IMAGES:\", check_data_validity(images_train))\nprint(\"NaN or Inf in MASKS:\", check_data_validity(masks_train))\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:50.801515Z","iopub.execute_input":"2024-09-13T16:40:50.801767Z","iopub.status.idle":"2024-09-13T16:40:50.807542Z","shell.execute_reply.started":"2024-09-13T16:40:50.801743Z","shell.execute_reply":"2024-09-13T16:40:50.806608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\n# Định nghĩa các khối convolution, encoder và decoder cho mô hình U-Net\ndef conv_block(input, num_filters):\n    conv = tf.keras.layers.Conv2D(num_filters, 3, padding=\"same\")(input)\n    conv = tf.keras.layers.BatchNormalization()(conv)\n    conv = tf.keras.layers.Activation(\"elu\")(conv)  # Thay đổi thành 'elu'\n    conv = tf.keras.layers.Conv2D(num_filters, 3, padding=\"same\")(conv)\n    conv = tf.keras.layers.BatchNormalization()(conv)\n    conv = tf.keras.layers.Activation(\"elu\")(conv)  # Thay đổi thành 'elu'\n    return conv\n\n\ndef encoder_block(input, num_filters):\n    skip = conv_block(input, num_filters)\n    pool = tf.keras.layers.MaxPool2D((2, 2))(skip)\n    return skip, pool\n\ndef decoder_block(input, skip, num_filters):\n    up_conv = tf.keras.layers.Conv2DTranspose(num_filters, (2, 2), strides=2, padding=\"same\")(input)\n    conv = tf.keras.layers.Concatenate()([up_conv, skip])\n    conv = conv_block(conv, num_filters)\n    return conv\n\n# Định nghĩa mô hình U-Net\ndef Unet(input_shape, num_classes):\n    inputs = tf.keras.layers.Input(input_shape)\n    \n    skip1, pool1 = encoder_block(inputs, 64)\n    skip2, pool2 = encoder_block(pool1, 128)\n    skip3, pool3 = encoder_block(pool2, 256)\n    skip4, pool4 = encoder_block(pool3, 512)\n    \n    bridge = conv_block(pool4, 1024)\n    \n    decode1 = decoder_block(bridge, skip4, 512)\n    decode2 = decoder_block(decode1, skip3, 256)\n    decode3 = decoder_block(decode2, skip2, 128)\n    decode4 = decoder_block(decode3, skip1, 64)\n    \n    outputs = tf.keras.layers.Conv2D(num_classes, 1, padding=\"same\", activation=\"softmax\")(decode4)\n    model = tf.keras.models.Model(inputs, outputs, name=\"U-Net\")\n    return model\n\n# Số lớp đầu ra (ví dụ: 3 cho phân loại 3 lớp)\nnum_classes = 3\n\n# Tạo mô hình U-Net và biên dịch\nunet_model = Unet((512, 512, 3), num_classes)\nunet_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Hiển thị tóm tắt mô hình\nunet_model.summary()\n","metadata":{"_kg_hide-output":true,"papermill":{"duration":3.40184,"end_time":"2023-07-11T04:09:04.037345","exception":false,"start_time":"2023-07-11T04:09:00.635505","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:50.808683Z","iopub.execute_input":"2024-09-13T16:40:50.808984Z","iopub.status.idle":"2024-09-13T16:40:51.476868Z","shell.execute_reply.started":"2024-09-13T16:40:50.80896Z","shell.execute_reply":"2024-09-13T16:40:51.475914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a convolution block with fewer filters and activation functions\ndef conv_block(input, num_filters):\n    conv = tf.keras.layers.Conv2D(num_filters, (3, 3), padding=\"same\")(input)\n    conv = tf.keras.layers.Activation(\"relu\")(conv)\n    conv = tf.keras.layers.Conv2D(num_filters, (3, 3), padding=\"same\")(conv)\n    conv = tf.keras.layers.Activation(\"relu\")(conv)\n    return conv\n\n# Encoder block with Conv2D and MaxPooling\ndef encoder_block(input, num_filters):\n    skip = conv_block(input, num_filters)\n    pool = tf.keras.layers.MaxPool2D(pool_size=(2, 2))(skip)\n    return skip, pool\n\n# Decoder block with Conv2DTranspose and concatenation\ndef decoder_block(input, skip, num_filters):\n    up_conv = tf.keras.layers.Conv2DTranspose(num_filters, (2, 2), strides=(2, 2), padding=\"same\")(input)\n    merge = tf.keras.layers.Concatenate()([up_conv, skip])\n    conv = conv_block(merge, num_filters)\n    return conv\n\n# U-Net architecture with adjustable depth\ndef Unet(input_shape, num_classes):\n    inputs = tf.keras.layers.Input(input_shape)\n    \n    # Encoder path\n    skip1, pool1 = encoder_block(inputs, 32)  # Reduce filters\n    skip2, pool2 = encoder_block(pool1, 64)\n    skip3, pool3 = encoder_block(pool2, 128)\n    \n    # Bridge (lowest level in U-Net)\n    bridge = conv_block(pool3, 256)  # Reduce filters\n\n    # Decoder path\n    decode1 = decoder_block(bridge, skip3, 128)\n    decode2 = decoder_block(decode1, skip2, 64)\n    decode3 = decoder_block(decode2, skip1, 32)\n    \n    # Output layer for multi-class prediction (softmax)\n    outputs = tf.keras.layers.Conv2D(num_classes, (1, 1), padding=\"same\", activation=\"softmax\")(decode3)\n    \n    model = tf.keras.models.Model(inputs, outputs, name=\"U-Net\")\n    return model\n\n# Simpler classification model using Conv2D and Dense layers\ndef SimpleModel(input_shape, num_classes):\n    inputs = tf.keras.layers.Input(input_shape)\n    x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)\n    x = tf.keras.layers.MaxPooling2D((2, 2))(x)\n    x = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)\n    x = tf.keras.layers.MaxPooling2D((2, 2))(x)\n    x = tf.keras.layers.Flatten()(x)\n    x = tf.keras.layers.Dense(num_classes, activation='softmax')(x)\n    \n    model = tf.keras.models.Model(inputs, x)\n    return model\n\n# Create and compile the models\n\n# U-Net for multi-class segmentation\nunet_model = Unet((512, 512, 3), num_classes=10)  # Example with 10 classes\nunet_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Simpler model for classification\nsimple_model = SimpleModel((512, 512, 3), num_classes=10)  # Example with 10 classes\nsimple_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# For summary of the models, uncomment below\n# unet_model.summary()\n# simple_model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:51.485844Z","iopub.execute_input":"2024-09-13T16:40:51.486342Z","iopub.status.idle":"2024-09-13T16:40:51.80507Z","shell.execute_reply.started":"2024-09-13T16:40:51.486308Z","shell.execute_reply":"2024-09-13T16:40:51.804269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = tf.keras.optimizers.Adam(learning_rate=1e-5, clipvalue=1.0)\nunet_model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:51.806214Z","iopub.execute_input":"2024-09-13T16:40:51.806525Z","iopub.status.idle":"2024-09-13T16:40:51.818544Z","shell.execute_reply.started":"2024-09-13T16:40:51.806498Z","shell.execute_reply":"2024-09-13T16:40:51.817645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MASKS = MASKS.astype(np.uint8)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:51.819881Z","iopub.execute_input":"2024-09-13T16:40:51.820413Z","iopub.status.idle":"2024-09-13T16:40:51.827323Z","shell.execute_reply.started":"2024-09-13T16:40:51.820375Z","shell.execute_reply":"2024-09-13T16:40:51.82644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check_data_validity(data):\n    print(\"Contains NaN:\", np.isnan(data).any())\n    print(\"Contains Inf:\", np.isinf(data).any())\n    if np.isnan(data).any() or np.isinf(data).any():\n        nan_indices = np.where(np.isnan(data))\n        inf_indices = np.where(np.isinf(data))\n        print(\"NaN Indices:\", nan_indices)\n        print(\"Inf Indices:\", inf_indices)\n\ncheck_data_validity(IMAGES)\ncheck_data_validity(MASKS)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:51.828848Z","iopub.execute_input":"2024-09-13T16:40:51.829181Z","iopub.status.idle":"2024-09-13T16:40:51.884715Z","shell.execute_reply.started":"2024-09-13T16:40:51.829149Z","shell.execute_reply":"2024-09-13T16:40:51.883748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train\nSuitable number epoch range is narrow.","metadata":{"papermill":{"duration":0.021696,"end_time":"2023-07-11T04:09:04.081495","exception":false,"start_time":"2023-07-11T04:09:04.059799","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Huấn luyện mô hình\nunet_result = unet_model.fit(\n    images_train, masks_train, \n    validation_split=0.2, batch_size=8, epochs=50\n)","metadata":{"papermill":{"duration":147.201421,"end_time":"2023-07-11T04:11:31.304006","exception":false,"start_time":"2023-07-11T04:09:04.102585","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:51.895492Z","iopub.execute_input":"2024-09-13T16:40:51.895846Z","iopub.status.idle":"2024-09-13T16:40:51.986548Z","shell.execute_reply.started":"2024-09-13T16:40:51.89582Z","shell.execute_reply":"2024-09-13T16:40:51.984553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Kiểm tra đầu ra dự đoán\npredictions = unet_model.predict(images_test)","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:51.987396Z","iopub.status.idle":"2024-09-13T16:40:51.98773Z","shell.execute_reply.started":"2024-09-13T16:40:51.987569Z","shell.execute_reply":"2024-09-13T16:40:51.987585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Kiểm tra kích thước của dự đoán\nprint(\"Kích thước của dự đoán:\", predictions.shape)","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:51.988902Z","iopub.status.idle":"2024-09-13T16:40:51.989228Z","shell.execute_reply.started":"2024-09-13T16:40:51.989063Z","shell.execute_reply":"2024-09-13T16:40:51.98908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Kiểm tra giá trị tối thiểu và tối đa của dự đoán\nprint(\"Giá trị tối thiểu trong dự đoán:\", np.min(predictions))\nprint(\"Giá trị tối đa trong dự đoán:\", np.max(predictions))","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:51.99104Z","iopub.status.idle":"2024-09-13T16:40:51.991518Z","shell.execute_reply.started":"2024-09-13T16:40:51.991256Z","shell.execute_reply":"2024-09-13T16:40:51.991278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\n# Tính trung bình độ chính xác huấn luyện và kiểm thử\navg_train_acc = np.mean(unet_result.history['accuracy'])\navg_val_acc = np.mean(unet_result.history['val_accuracy'])\n\n# In kết quả\nprint(f\"Trung bình độ chính xác huấn luyện: {avg_train_acc}\")\nprint(f\"Trung bình độ chính xác kiểm thử: {avg_val_acc}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:51.993234Z","iopub.status.idle":"2024-09-13T16:40:51.993709Z","shell.execute_reply.started":"2024-09-13T16:40:51.993464Z","shell.execute_reply":"2024-09-13T16:40:51.993488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Vẽ biểu đồ độ chính xác theo epoch\nplt.plot(unet_result.history['accuracy'], label='Huấn luyện')\nplt.plot(unet_result.history['val_accuracy'], label='Kiểm thử')\nplt.title('Độ chính xác theo Epoch')\nplt.xlabel('Epoch')\nplt.ylabel('Độ chính xác')\nplt.legend(loc='lower right')\nplt.grid(True)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-13T16:40:51.994983Z","iopub.status.idle":"2024-09-13T16:40:51.995434Z","shell.execute_reply.started":"2024-09-13T16:40:51.995194Z","shell.execute_reply":"2024-09-13T16:40:51.995216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict valid images","metadata":{"papermill":{"duration":0.058091,"end_time":"2023-07-11T04:11:31.42581","exception":false,"start_time":"2023-07-11T04:11:31.367719","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def show_result(idx, og, unet, target, p):\n    \n    fig, axs = plt.subplots(1, 3, figsize=(12,12))\n    axs[0].set_title(\"Original \"+str(idx) )\n    axs[0].imshow(og)\n    axs[0].axis('off')\n    \n    axs[1].set_title(\"U-Net: p>\"+str(p))\n    axs[1].imshow(unet)\n    axs[1].axis('off')\n    \n    axs[2].set_title(\"Ground Truth\")\n    axs[2].imshow(target)\n    axs[2].axis('off')\n\n    plt.show()","metadata":{"papermill":{"duration":0.070807,"end_time":"2023-07-11T04:11:31.553458","exception":false,"start_time":"2023-07-11T04:11:31.482651","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:51.997711Z","iopub.status.idle":"2024-09-13T16:40:51.998255Z","shell.execute_reply.started":"2024-09-13T16:40:51.997961Z","shell.execute_reply":"2024-09-13T16:40:51.997989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unet_predict = unet_model.predict(images_test)","metadata":{"papermill":{"duration":5.678511,"end_time":"2023-07-11T04:11:37.295638","exception":false,"start_time":"2023-07-11T04:11:31.617127","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:51.999658Z","iopub.status.idle":"2024-09-13T16:40:52.000173Z","shell.execute_reply.started":"2024-09-13T16:40:51.999906Z","shell.execute_reply":"2024-09-13T16:40:51.999932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images_test)","metadata":{"papermill":{"duration":0.068633,"end_time":"2023-07-11T04:11:37.422135","exception":false,"start_time":"2023-07-11T04:11:37.353502","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:52.002053Z","iopub.status.idle":"2024-09-13T16:40:52.002652Z","shell.execute_reply.started":"2024-09-13T16:40:52.002314Z","shell.execute_reply":"2024-09-13T16:40:52.002339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r1,r2,r3,r4=0.7,0.8,0.9,0.99","metadata":{"papermill":{"duration":0.066144,"end_time":"2023-07-11T04:11:37.545376","exception":false,"start_time":"2023-07-11T04:11:37.479232","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:52.00409Z","iopub.status.idle":"2024-09-13T16:40:52.004484Z","shell.execute_reply.started":"2024-09-13T16:40:52.004265Z","shell.execute_reply":"2024-09-13T16:40:52.004282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unet_predict1 = (unet_predict > r1).astype(np.uint8)\nunet_predict2 = (unet_predict > r2).astype(np.uint8)\nunet_predict3 = (unet_predict > r3).astype(np.uint8)\nunet_predict4 = (unet_predict > r4).astype(np.uint8)","metadata":{"papermill":{"duration":0.075937,"end_time":"2023-07-11T04:11:37.684847","exception":false,"start_time":"2023-07-11T04:11:37.60891","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:52.006069Z","iopub.status.idle":"2024-09-13T16:40:52.006441Z","shell.execute_reply.started":"2024-09-13T16:40:52.006259Z","shell.execute_reply":"2024-09-13T16:40:52.006276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_test_idx = random.sample(range(len(unet_predict)), 3)\nfor idx in show_test_idx: \n    show_result(idx, images_test[idx], unet_predict1[idx], masks_test[idx], r1)\n    show_result(idx, images_test[idx], unet_predict2[idx], masks_test[idx], r2)\n    show_result(idx, images_test[idx], unet_predict3[idx], masks_test[idx], r3)\n    show_result(idx, images_test[idx], unet_predict4[idx], masks_test[idx], r4)\n    print()","metadata":{"papermill":{"duration":3.719208,"end_time":"2023-07-11T04:11:41.462814","exception":false,"start_time":"2023-07-11T04:11:37.743606","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-09-13T16:40:52.008409Z","iopub.status.idle":"2024-09-13T16:40:52.008751Z","shell.execute_reply.started":"2024-09-13T16:40:52.008593Z","shell.execute_reply":"2024-09-13T16:40:52.008609Z"},"trusted":true},"execution_count":null,"outputs":[]}]}