{"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":84969,"databundleVersionId":10033515,"sourceType":"competition"},{"sourceId":206671331,"sourceType":"kernelVersion"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# ","metadata":{"papermill":{"duration":0.008195,"end_time":"2023-10-31T14:35:49.34593","exception":false,"start_time":"2023-10-31T14:35:49.337735","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"CZII 3D UNET w/Callback, Max Epochs","metadata":{"papermill":{"duration":0.008195,"end_time":"2023-10-31T14:35:49.34593","exception":false,"start_time":"2023-10-31T14:35:49.337735","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"https://www.kaggle.com/code/stpeteishii/czii-prepare-image-dataset-for-unet","metadata":{"papermill":{"duration":0.00738,"end_time":"2023-10-31T14:35:49.360958","exception":false,"start_time":"2023-10-31T14:35:49.353578","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"An effective method to find the optimal number of epochs.Save the predicted mask images generated every 3 epoch and try arranging them later. You can see the best number of epochs at a glance. If the epoch number is too large, the mask image will be lost.","metadata":{"papermill":{"duration":0.007482,"end_time":"2023-10-31T14:35:49.37588","exception":false,"start_time":"2023-10-31T14:35:49.368398","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!pip install imantics --quiet","metadata":{"papermill":{"duration":13.989536,"end_time":"2023-10-31T14:36:03.372848","exception":false,"start_time":"2023-10-31T14:35:49.383312","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-11-12T13:15:44.263040Z","iopub.execute_input":"2024-11-12T13:15:44.263871Z","iopub.status.idle":"2024-11-12T13:16:03.897244Z","shell.execute_reply.started":"2024-11-12T13:15:44.263827Z","shell.execute_reply":"2024-11-12T13:16:03.895739Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport tensorflow as tf\nimport json\nimport os\nimport imantics\nfrom PIL import Image\nfrom skimage.transform import resize\nimport random\nfrom sklearn.model_selection import train_test_split\n%matplotlib inline","metadata":{"papermill":{"duration":9.744181,"end_time":"2023-10-31T14:36:13.125092","exception":false,"start_time":"2023-10-31T14:36:03.380911","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-11-12T13:16:03.899893Z","iopub.execute_input":"2024-11-12T13:16:03.900291Z","iopub.status.idle":"2024-11-12T13:16:20.308644Z","shell.execute_reply.started":"2024-11-12T13:16:03.900259Z","shell.execute_reply":"2024-11-12T13:16:20.307456Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tracemalloc\ntracemalloc.start()\n\ndef print_memory():\n    snapshot = tracemalloc.take_snapshot()\n    total_memory = sum(stat.size for stat in snapshot.statistics('lineno')) \n    total_memory_mb = total_memory / 1024**2  \n    print(f\"{total_memory_mb:.2f} MB\")\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print_memory()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_dir = '/kaggle/input/czii-prepare-image-dataset-for-unet'\nimages_dir = f'{base_dir}/image' \nmasks_dir = f'{base_dir}/mask' \ntimages_dir = f'{base_dir}/timage' ","metadata":{"papermill":{"duration":0.014689,"end_time":"2023-10-31T14:36:13.147877","exception":false,"start_time":"2023-10-31T14:36:13.133188","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:20.310181Z","iopub.execute_input":"2024-11-12T13:16:20.310843Z","iopub.status.idle":"2024-11-12T13:16:20.316365Z","shell.execute_reply.started":"2024-11-12T13:16:20.310808Z","shell.execute_reply":"2024-11-12T13:16:20.315168Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images_listdir = os.listdir(images_dir)\nrandom_images = np.random.choice(images_listdir, size = 9, replace = False)","metadata":{"papermill":{"duration":0.022329,"end_time":"2023-10-31T14:36:13.178043","exception":false,"start_time":"2023-10-31T14:36:13.155714","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:20.317756Z","iopub.execute_input":"2024-11-12T13:16:20.318360Z","iopub.status.idle":"2024-11-12T13:16:20.450739Z","shell.execute_reply.started":"2024-11-12T13:16:20.318288Z","shell.execute_reply":"2024-11-12T13:16:20.449542Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_size=512","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:20.454476Z","iopub.execute_input":"2024-11-12T13:16:20.454946Z","iopub.status.idle":"2024-11-12T13:16:20.460180Z","shell.execute_reply.started":"2024-11-12T13:16:20.454910Z","shell.execute_reply":"2024-11-12T13:16:20.459074Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def read_image(path):\n    img = cv2.imread(path)\n    #img = cv2.imread(path,cv2.IMREAD_ANYDEPTH)\n    #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img2 = cv2.resize(img, (image_size, image_size))\n    #print(img.shape,img2.shape)\n    return img2","metadata":{"papermill":{"duration":0.014979,"end_time":"2023-10-31T14:36:13.222538","exception":false,"start_time":"2023-10-31T14:36:13.207559","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:20.461482Z","iopub.execute_input":"2024-11-12T13:16:20.461814Z","iopub.status.idle":"2024-11-12T13:16:20.475814Z","shell.execute_reply.started":"2024-11-12T13:16:20.461786Z","shell.execute_reply":"2024-11-12T13:16:20.474560Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Input images ","metadata":{"papermill":{"duration":0.007468,"end_time":"2023-10-31T14:36:13.237532","exception":false,"start_time":"2023-10-31T14:36:13.230064","status":"completed"},"tags":[]}},{"cell_type":"code","source":"rows = 3\ncols = 3\nfig, ax = plt.subplots(rows, cols, figsize = (12,12))\nfor i, ax in enumerate(ax.flat):\n    if i < len(random_images):\n        img = read_image(f\"{images_dir}/{random_images[i]}\")\n        ax.set_title(f\"{random_images[i]}\")\n        ax.imshow(img)\n        ax.axis('off')","metadata":{"papermill":{"duration":1.589493,"end_time":"2023-10-31T14:36:14.834556","exception":false,"start_time":"2023-10-31T14:36:13.245063","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:20.477334Z","iopub.execute_input":"2024-11-12T13:16:20.477786Z","iopub.status.idle":"2024-11-12T13:16:22.363808Z","shell.execute_reply.started":"2024-11-12T13:16:20.477746Z","shell.execute_reply":"2024-11-12T13:16:22.362735Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Ground truth masks","metadata":{"papermill":{"duration":0.01276,"end_time":"2023-10-31T14:36:14.860722","exception":false,"start_time":"2023-10-31T14:36:14.847962","status":"completed"},"tags":[]}},{"cell_type":"code","source":"fig, ax = plt.subplots(rows, cols, figsize = (12,12))\nfor i, ax in enumerate(ax.flat):\n    if i < len(random_images):\n        file = random_images[i]\n        img = read_image(f\"{masks_dir}/{file}\")       \n        #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        ax.set_title(f\"{random_images[i]}\")\n        ax.imshow(img)\n        ax.axis('off')","metadata":{"papermill":{"duration":1.425215,"end_time":"2023-10-31T14:36:16.298726","exception":false,"start_time":"2023-10-31T14:36:14.873511","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:22.365246Z","iopub.execute_input":"2024-11-12T13:16:22.365601Z","iopub.status.idle":"2024-11-12T13:16:24.004066Z","shell.execute_reply.started":"2024-11-12T13:16:22.365571Z","shell.execute_reply":"2024-11-12T13:16:24.002805Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#MASKS=np.zeros((1,image_size, image_size, 1), dtype=bool) \nMASKS=np.zeros((1,image_size,image_size,3),dtype=np.uint8)\nIMAGES=np.zeros((1,image_size,image_size,3),dtype=np.uint8)\n\nfor j,file in enumerate(images_listdir[0:81]): ##the smaller, the faster\n    #print(j)\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    \n    file2=file\n    mask = read_image(f\"{masks_dir}/{file2}\")\n    #mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY) #####\n    mask_ex = np.expand_dims(mask, axis=0)    \n    MASKS = np.vstack([MASKS, mask_ex])","metadata":{"papermill":{"duration":1.769615,"end_time":"2023-10-31T14:36:18.112401","exception":false,"start_time":"2023-10-31T14:36:16.342786","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:24.005536Z","iopub.execute_input":"2024-11-12T13:16:24.005906Z","iopub.status.idle":"2024-11-12T13:16:28.044219Z","shell.execute_reply.started":"2024-11-12T13:16:24.005874Z","shell.execute_reply":"2024-11-12T13:16:28.043203Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images=np.array(IMAGES)[1:81]\nmasks=np.array(MASKS)[1:81]\nprint(images.shape,masks.shape)","metadata":{"papermill":{"duration":0.040793,"end_time":"2023-10-31T14:36:18.168251","exception":false,"start_time":"2023-10-31T14:36:18.127458","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:28.045527Z","iopub.execute_input":"2024-11-12T13:16:28.045844Z","iopub.status.idle":"2024-11-12T13:16:28.109167Z","shell.execute_reply.started":"2024-11-12T13:16:28.045817Z","shell.execute_reply":"2024-11-12T13:16:28.107728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images_train, images_test, masks_train, masks_test = train_test_split(\n    images, masks, test_size=0.4, random_state=42)","metadata":{"_kg_hide-input":true,"papermill":{"duration":0.038918,"end_time":"2023-10-31T14:36:18.221744","exception":false,"start_time":"2023-10-31T14:36:18.182826","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:28.110757Z","iopub.execute_input":"2024-11-12T13:16:28.111140Z","iopub.status.idle":"2024-11-12T13:16:28.170042Z","shell.execute_reply.started":"2024-11-12T13:16:28.111108Z","shell.execute_reply":"2024-11-12T13:16:28.168570Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(len(images_train), len(masks_train))\nprint(len(images_test), len(masks_test))\n\nimages_train = images_train.reshape(-1, 1, 512, 512, 3)\nmasks_train = masks_train.reshape(-1, 1, 512, 512, 1) \nimages_test = images_test.reshape(-1, 1, 512, 512, 3)\nmasks_test = masks_test.reshape(-1, 1, 512, 512, 1) ","metadata":{"papermill":{"duration":0.022288,"end_time":"2023-10-31T14:36:18.259006","exception":false,"start_time":"2023-10-31T14:36:18.236718","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:28.172618Z","iopub.execute_input":"2024-11-12T13:16:28.173039Z","iopub.status.idle":"2024-11-12T13:16:28.181159Z","shell.execute_reply.started":"2024-11-12T13:16:28.172979Z","shell.execute_reply":"2024-11-12T13:16:28.179979Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# U-Net","metadata":{"papermill":{"duration":0.014232,"end_time":"2023-10-31T14:36:18.287583","exception":false,"start_time":"2023-10-31T14:36:18.273351","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import tensorflow as tf\n\ndef print_shape(name, tensor):\n    \"\"\"Shape printing function for debugging\"\"\"\n    print(f\"{name} shape:\", tensor.shape)\n    return tensor\n\ndef encoder_block(input, num_filters, name):\n    conv = conv_block(input, num_filters, f\"{name}_conv\")\n    pool = tf.keras.layers.MaxPooling3D((2, 2, 2), strides=(2, 2, 2), padding=\"same\", name=f\"{name}_pool\")(conv)\n    print_shape(f\"{name}_conv\", conv)\n    print_shape(f\"{name}_pool\", pool)\n    return conv, pool\n\ndef decoder_block(input, skip, num_filters, name):\n    print_shape(f\"{name}_input\", input)\n    print_shape(f\"{name}_skip\", skip)\n    \n    # Upsample to match the shape of the skip connection\n    target_shape = skip.shape\n    current_shape = input.shape\n    up_size = (\n        target_shape[1] // current_shape[1],\n        target_shape[2] // current_shape[2],\n        target_shape[3] // current_shape[3]\n    )\n    \n    up = tf.keras.layers.UpSampling3D(size=up_size, name=f\"{name}_up\")(input)\n    print_shape(f\"{name}_up\", up)\n    \n    # Concatenate with the skip connection\n    concat = tf.keras.layers.Concatenate(axis=-1, name=f\"{name}_concat\")([up, skip])\n    print_shape(f\"{name}_concat\", concat)\n    \n    conv = conv_block(concat, num_filters, f\"{name}_conv\")\n    print_shape(f\"{name}_conv_out\", conv)\n    return conv\n\ndef conv_block(input, num_filters, name):\n    conv = tf.keras.layers.Conv3D(\n        num_filters, 3, padding=\"same\", name=f\"{name}_conv1\")(input)\n    conv = tf.keras.layers.BatchNormalization(name=f\"{name}_bn1\")(conv)\n    conv = tf.keras.layers.ReLU(name=f\"{name}_relu1\")(conv)\n    \n    conv = tf.keras.layers.Conv3D(\n        num_filters, 3, padding=\"same\", name=f\"{name}_conv2\")(conv)\n    conv = tf.keras.layers.BatchNormalization(name=f\"{name}_bn2\")(conv)\n    conv = tf.keras.layers.ReLU(name=f\"{name}_relu2\")(conv)\n    return conv\n\ndef Unet3D(input_shape):\n    inputs = tf.keras.layers.Input(input_shape)\n    print_shape(\"input\", inputs)\n    \n    # Encoder path\n    skip1, pool1 = encoder_block(inputs, 64, \"encoder1\")\n    skip2, pool2 = encoder_block(pool1, 128, \"encoder2\")\n    skip3, pool3 = encoder_block(pool2, 256, \"encoder3\")\n    skip4, pool4 = encoder_block(pool3, 512, \"encoder4\")\n    \n    # Bridge\n    bridge = conv_block(pool4, 1024, \"bridge\")\n    print_shape(\"bridge\", bridge)\n    \n    # Decoder path\n    decode1 = decoder_block(bridge, skip4, 512, \"decoder1\")\n    decode2 = decoder_block(decode1, skip3, 256, \"decoder2\")\n    decode3 = decoder_block(decode2, skip2, 128, \"decoder3\")\n    decode4 = decoder_block(decode3, skip1, 64, \"decoder4\")\n    \n    # Output layer\n    outputs = tf.keras.layers.Conv3D(\n        1, 1, padding=\"same\", activation=\"sigmoid\", name=\"output\")(decode4)\n    print_shape(\"output\", outputs)\n    \n    model = tf.keras.models.Model(inputs, outputs, name=\"U-Net3D\")\n    return model\n","metadata":{"papermill":{"duration":0.014232,"end_time":"2023-10-31T14:36:18.287583","exception":false,"start_time":"2023-10-31T14:36:18.273351","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-11-12T13:16:28.182548Z","iopub.execute_input":"2024-11-12T13:16:28.182889Z","iopub.status.idle":"2024-11-12T13:16:28.201355Z","shell.execute_reply.started":"2024-11-12T13:16:28.182859Z","shell.execute_reply":"2024-11-12T13:16:28.200116Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"input_shape = (1, 512, 512, 3)  # (depth, height, width, channels)\nmodel = Unet3D(input_shape)\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:28.206217Z","iopub.execute_input":"2024-11-12T13:16:28.206645Z","iopub.status.idle":"2024-11-12T13:16:30.428906Z","shell.execute_reply.started":"2024-11-12T13:16:28.206611Z","shell.execute_reply":"2024-11-12T13:16:30.427087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print_memory()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Callback","metadata":{"papermill":{"duration":0.028918,"end_time":"2023-10-31T14:36:22.128723","exception":false,"start_time":"2023-10-31T14:36:22.099805","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#!rm -rf output_masks","metadata":{"papermill":{"duration":0.035785,"end_time":"2023-10-31T14:36:22.193477","exception":false,"start_time":"2023-10-31T14:36:22.157692","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:30.429762Z","iopub.status.idle":"2024-11-12T13:16:30.430218Z","shell.execute_reply.started":"2024-11-12T13:16:30.429985Z","shell.execute_reply":"2024-11-12T13:16:30.430025Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow import keras\n\nimport numpy as np\nimport keras\n\nclass SaveMaskImagesCallback(keras.callbacks.Callback):\n    def __init__(self, test_images, output_dir):\n        super().__init__()\n        self.test_images = test_images\n        self.output_dir = output_dir\n        \n    def on_epoch_end(self, epoch, logs=None):\n        predictions = self.model.predict(self.test_images)\n        for i, pred_mask in enumerate(predictions):\n            # Squeeze out extra dimensions to get shape (512, 512, 1)\n            pred_mask = np.squeeze(pred_mask)\n            # Add channel dimension back if needed\n            if pred_mask.ndim == 2:\n                pred_mask = pred_mask[..., np.newaxis]\n                \n            output_path = os.path.join(self.output_dir, f\"mask_{i:02d}_ep_{epoch + 1:03d}.png\")\n            pred_mask = (pred_mask * 255).astype(np.uint8)\n            keras.preprocessing.image.save_img(output_path, pred_mask)\n            \noutput_directory = \"output_masks\"  \nos.makedirs(output_directory, exist_ok=True)\nmask_callback = SaveMaskImagesCallback(images_train, output_directory) ","metadata":{"papermill":{"duration":0.039875,"end_time":"2023-10-31T14:36:22.262429","exception":false,"start_time":"2023-10-31T14:36:22.222554","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:30.431795Z","iopub.status.idle":"2024-11-12T13:16:30.432260Z","shell.execute_reply.started":"2024-11-12T13:16:30.432047Z","shell.execute_reply":"2024-11-12T13:16:30.432070Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images_train.shape","metadata":{"papermill":{"duration":0.039875,"end_time":"2023-10-31T14:36:22.262429","exception":false,"start_time":"2023-10-31T14:36:22.222554","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:30.434020Z","iopub.status.idle":"2024-11-12T13:16:30.434453Z","shell.execute_reply.started":"2024-11-12T13:16:30.434249Z","shell.execute_reply":"2024-11-12T13:16:30.434269Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"masks_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:30.436968Z","iopub.status.idle":"2024-11-12T13:16:30.437490Z","shell.execute_reply.started":"2024-11-12T13:16:30.437273Z","shell.execute_reply":"2024-11-12T13:16:30.437295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:30.438641Z","iopub.status.idle":"2024-11-12T13:16:30.439084Z","shell.execute_reply.started":"2024-11-12T13:16:30.438847Z","shell.execute_reply":"2024-11-12T13:16:30.438867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print_memory()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Train","metadata":{"papermill":{"duration":0.028873,"end_time":"2023-10-31T14:36:22.320261","exception":false,"start_time":"2023-10-31T14:36:22.291388","status":"completed"},"tags":[]}},{"cell_type":"code","source":"unet_result = model.fit(\n    images_train, masks_train, \n    validation_split=0.2, batch_size=1, epochs=30,\n    callbacks=[mask_callback]\n)","metadata":{"papermill":{"duration":380.649972,"end_time":"2023-10-31T14:42:42.999007","exception":false,"start_time":"2023-10-31T14:36:22.349035","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:30.442372Z","iopub.status.idle":"2024-11-12T13:16:30.442754Z","shell.execute_reply.started":"2024-11-12T13:16:30.442564Z","shell.execute_reply":"2024-11-12T13:16:30.442581Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"unet_result","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:30.444264Z","iopub.status.idle":"2024-11-12T13:16:30.444831Z","shell.execute_reply.started":"2024-11-12T13:16:30.444531Z","shell.execute_reply":"2024-11-12T13:16:30.444557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print_memory()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predict","metadata":{"papermill":{"duration":0.103907,"end_time":"2023-10-31T14:42:43.416004","exception":false,"start_time":"2023-10-31T14:42:43.312097","status":"completed"},"tags":[]}},{"cell_type":"code","source":"unet_predict = model.predict(images_test)","metadata":{"papermill":{"duration":12.243025,"end_time":"2023-10-31T14:42:55.762948","exception":false,"start_time":"2023-10-31T14:42:43.519923","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:30.446071Z","iopub.status.idle":"2024-11-12T13:16:30.446627Z","shell.execute_reply.started":"2024-11-12T13:16:30.446345Z","shell.execute_reply":"2024-11-12T13:16:30.446371Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_result(idx, og, unet, target, p):\n\n    print(unet.flatten().min(),unet.flatten().max())\n    \n    fig, axs = plt.subplots(1, 3, figsize=(12,12))\n    axs[0].set_title(\"Original \"+str(idx))\n    axs[0].imshow(og.squeeze())\n    axs[0].axis('off')\n    \n    axs[1].set_title(\"U-Net: p>\"+str(p))\n    axs[1].imshow(unet.squeeze()*255)\n    axs[1].axis('off')\n    \n    axs[2].set_title(\"Ground Truth\")\n    axs[2].imshow(target.squeeze())\n    axs[2].axis('off')\n\n    plt.show()","metadata":{"papermill":{"duration":0.114414,"end_time":"2023-10-31T14:42:55.982901","exception":false,"start_time":"2023-10-31T14:42:55.868487","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:30.448627Z","iopub.status.idle":"2024-11-12T13:16:30.449069Z","shell.execute_reply.started":"2024-11-12T13:16:30.448837Z","shell.execute_reply":"2024-11-12T13:16:30.448855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"r1,r2,r3,r4=0.7,0.8,0.9,0.99","metadata":{"papermill":{"duration":0.112198,"end_time":"2023-10-31T14:42:56.200267","exception":false,"start_time":"2023-10-31T14:42:56.088069","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:30.450489Z","iopub.status.idle":"2024-11-12T13:16:30.450894Z","shell.execute_reply.started":"2024-11-12T13:16:30.450699Z","shell.execute_reply":"2024-11-12T13:16:30.450718Z"}},"outputs":[],"execution_count":null},{"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.134428,"end_time":"2023-10-31T14:42:56.440017","exception":false,"start_time":"2023-10-31T14:42:56.305589","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:30.452527Z","iopub.status.idle":"2024-11-12T13:16:30.453140Z","shell.execute_reply.started":"2024-11-12T13:16:30.452822Z","shell.execute_reply":"2024-11-12T13:16:30.452850Z"}},"outputs":[],"execution_count":null},{"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":5.183271,"end_time":"2023-10-31T14:43:01.729239","exception":false,"start_time":"2023-10-31T14:42:56.545968","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T13:16:30.455061Z","iopub.status.idle":"2024-11-12T13:16:30.455637Z","shell.execute_reply.started":"2024-11-12T13:16:30.455350Z","shell.execute_reply":"2024-11-12T13:16:30.455378Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print_memory()","metadata":{"papermill":{"duration":0.125499,"end_time":"2023-10-31T14:43:01.9836","exception":false,"start_time":"2023-10-31T14:43:01.858101","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null}]}