{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"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":"2023-08-14T11:51:43.390434Z","iopub.execute_input":"2023-08-14T11:51:43.391499Z","iopub.status.idle":"2023-08-14T11:52:01.045367Z","shell.execute_reply.started":"2023-08-14T11:51:43.391459Z","shell.execute_reply":"2023-08-14T11:52:01.043786Z"},"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":"2023-08-14T11:52:01.04723Z","iopub.execute_input":"2023-08-14T11:52:01.047624Z","iopub.status.idle":"2023-08-14T11:52:11.085144Z","shell.execute_reply.started":"2023-08-14T11:52:01.047588Z","shell.execute_reply":"2023-08-14T11:52:11.084256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_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":"2023-08-14T11:52:11.086398Z","iopub.execute_input":"2023-08-14T11:52:11.087767Z","iopub.status.idle":"2023-08-14T11:52:11.09391Z","shell.execute_reply.started":"2023-08-14T11:52:11.087732Z","shell.execute_reply":"2023-08-14T11:52:11.091918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_listdir0 = sorted(os.listdir(images_dir))\nimages_listdir=[]\nfor i in range(len(images_listdir0)):\n    if i%4==0:\n        images_listdir+=[images_listdir0[i]]\nimages_listdir= images_listdir[50:99]\nmasks_listdir = sorted(os.listdir(masks_dir))\nN=list(range(9))\nrandom_N = N\n#np.random.choice(N, size = 9, replace = False)","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":"2023-08-14T11:52:11.096874Z","iopub.execute_input":"2023-08-14T11:52:11.097279Z","iopub.status.idle":"2023-08-14T11:52:11.226202Z","shell.execute_reply.started":"2023-08-14T11:52:11.097248Z","shell.execute_reply":"2023-08-14T11:52:11.224738Z"},"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":{"papermill":{"duration":0.027463,"end_time":"2023-07-11T04:08:56.301016","exception":false,"start_time":"2023-07-11T04:08:56.273553","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-14T11:52:11.988856Z","iopub.status.idle":"2023-08-14T11:52:11.989252Z","shell.execute_reply.started":"2023-08-14T11:52:11.98906Z","shell.execute_reply":"2023-08-14T11:52:11.989078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_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":"2023-08-14T11:52:11.99093Z","iopub.status.idle":"2023-08-14T11:52:11.991329Z","shell.execute_reply.started":"2023-08-14T11:52:11.991137Z","shell.execute_reply":"2023-08-14T11:52:11.991155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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","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":"2023-08-14T11:52:11.992487Z","iopub.status.idle":"2023-08-14T11:52:11.993112Z","shell.execute_reply.started":"2023-08-14T11:52:11.992909Z","shell.execute_reply":"2023-08-14T11:52:11.99293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"number=120","metadata":{"papermill":{"duration":0.018704,"end_time":"2023-07-11T04:08:56.391595","exception":false,"start_time":"2023-07-11T04:08:56.372891","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-14T11:52:11.994085Z","iopub.status.idle":"2023-08-14T11:52:11.995941Z","shell.execute_reply.started":"2023-08-14T11:52:11.995722Z","shell.execute_reply":"2023-08-14T11:52:11.995745Z"},"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":"rows = 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":"2023-08-14T11:52:11.996881Z","iopub.status.idle":"2023-08-14T11:52:11.997718Z","shell.execute_reply.started":"2023-08-14T11:52:11.997491Z","shell.execute_reply":"2023-08-14T11:52:11.997512Z"},"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":"fig, ax = plt.subplots(rows, cols, figsize = (10,10))\nfor i, ax in enumerate(ax.flat):\n    if i < len(random_N):\n        if os.path.exists(os.path.join(masks_dir,masks_listdir[i])):\n            img = read_image(f\"{masks_dir}/{masks_listdir[i]}\")\n            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n            ax.set_title(f\"{masks_listdir[i]}\")\n            ax.imshow(img)\n            ax.axis('off')\n        else:\n            print('not exist')","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":"2023-08-14T11:52:11.999135Z","iopub.status.idle":"2023-08-14T11:52:11.999519Z","shell.execute_reply.started":"2023-08-14T11:52:11.99933Z","shell.execute_reply":"2023-08-14T11:52:11.999347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MASKS=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":{"papermill":{"duration":1.160612,"end_time":"2023-07-11T04:09:00.38412","exception":false,"start_time":"2023-07-11T04:08:59.223508","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-14T11:52:12.00127Z","iopub.status.idle":"2023-08-14T11:52:12.0024Z","shell.execute_reply.started":"2023-08-14T11:52:12.002073Z","shell.execute_reply":"2023-08-14T11:52:12.002103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images=np.array(IMAGES)[1:number+1]\nmasks=np.array(MASKS)[1:number+1]\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":"2023-08-14T11:52:12.003752Z","iopub.status.idle":"2023-08-14T11:52:12.004344Z","shell.execute_reply.started":"2023-08-14T11:52:12.004048Z","shell.execute_reply":"2023-08-14T11:52:12.004077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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.040675,"end_time":"2023-07-11T04:09:00.511171","exception":false,"start_time":"2023-07-11T04:09:00.470496","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-14T11:52:12.006835Z","iopub.status.idle":"2023-08-14T11:52:12.0075Z","shell.execute_reply.started":"2023-08-14T11:52:12.007185Z","shell.execute_reply":"2023-08-14T11:52:12.007215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(images_train), len(masks_train))","metadata":{"papermill":{"duration":0.032989,"end_time":"2023-07-11T04:09:00.573399","exception":false,"start_time":"2023-07-11T04:09:00.54041","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-14T11:52:12.009041Z","iopub.status.idle":"2023-08-14T11:52:12.009615Z","shell.execute_reply.started":"2023-08-14T11:52:12.009325Z","shell.execute_reply":"2023-08-14T11:52:12.009352Z"},"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 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(\"relu\")(conv)\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(\"relu\")(conv)\n    return conv\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\ndef Unet(input_shape):\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    outputs = tf.keras.layers.Conv2D(1, 1, padding=\"same\", activation=\"sigmoid\")(decode4)\n    model = tf.keras.models.Model(inputs, outputs, name=\"U-Net\")\n    return model\n\nunet_model = Unet((512,512,3))\nunet_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n#unet_model.summary()","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":"2023-08-14T11:52:12.011401Z","iopub.status.idle":"2023-08-14T11:52:12.012487Z","shell.execute_reply.started":"2023-08-14T11:52:12.012167Z","shell.execute_reply":"2023-08-14T11:52:12.012197Z"},"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":"unet_result = unet_model.fit(\n    images_train, masks_train, \n    validation_split = 0.2, batch_size = 4, epochs = 800)","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":"2023-08-14T11:52:12.01397Z","iopub.status.idle":"2023-08-14T11:52:12.014386Z","shell.execute_reply.started":"2023-08-14T11:52:12.014188Z","shell.execute_reply":"2023-08-14T11:52:12.014207Z"},"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":"2023-08-14T11:52:12.01536Z","iopub.status.idle":"2023-08-14T11:52:12.015758Z","shell.execute_reply.started":"2023-08-14T11:52:12.015552Z","shell.execute_reply":"2023-08-14T11:52:12.01557Z"},"trusted":true},"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":"2023-08-14T11:52:12.017635Z","iopub.status.idle":"2023-08-14T11:52:12.018292Z","shell.execute_reply.started":"2023-08-14T11:52:12.018092Z","shell.execute_reply":"2023-08-14T11:52:12.018113Z"},"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":"2023-08-14T11:52:12.019519Z","iopub.status.idle":"2023-08-14T11:52:12.019926Z","shell.execute_reply.started":"2023-08-14T11:52:12.019733Z","shell.execute_reply":"2023-08-14T11:52:12.019751Z"},"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":"2023-08-14T11:52:12.021249Z","iopub.status.idle":"2023-08-14T11:52:12.021654Z","shell.execute_reply.started":"2023-08-14T11:52:12.021446Z","shell.execute_reply":"2023-08-14T11:52:12.021464Z"},"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":"2023-08-14T11:52:12.022679Z","iopub.status.idle":"2023-08-14T11:52:12.023061Z","shell.execute_reply.started":"2023-08-14T11:52:12.022866Z","shell.execute_reply":"2023-08-14T11:52:12.022884Z"},"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":"2023-08-14T11:52:12.024199Z","iopub.status.idle":"2023-08-14T11:52:12.024581Z","shell.execute_reply.started":"2023-08-14T11:52:12.024382Z","shell.execute_reply":"2023-08-14T11:52:12.0244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.088366,"end_time":"2023-07-11T04:11:41.641552","exception":false,"start_time":"2023-07-11T04:11:41.553186","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}