{"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":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport cv2\nimport seaborn \nimport matplotlib.pyplot as plt\nimport gc\n\nimport queue\nfrom shapely.geometry import Polygon, Point\nimport time","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-25T16:19:25.473517Z","iopub.execute_input":"2023-08-25T16:19:25.473894Z","iopub.status.idle":"2023-08-25T16:19:26.365469Z","shell.execute_reply.started":"2023-08-25T16:19:25.473857Z","shell.execute_reply":"2023-08-25T16:19:26.364512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/pycocotools/ /kaggle/working/pycocotools\n!pip install /kaggle/working/pycocotools/pycocotools-2.0.6  --no-index --find-links=/kaggle/working/pycocotools/","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:19:26.367506Z","iopub.execute_input":"2023-08-25T16:19:26.367857Z","iopub.status.idle":"2023-08-25T16:20:01.313125Z","shell.execute_reply.started":"2023-08-25T16:19:26.367806Z","shell.execute_reply":"2023-08-25T16:20:01.311929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_folder = \"/kaggle/input/hubmap-hacking-the-human-vasculature/train/\"\ntest_folder = \"/kaggle/input/hubmap-hacking-the-human-vasculature/test/\"\nwsi_fpath = \"/kaggle/input/hubmap-hacking-the-human-vasculature/wsi_meta.csv\"\ntile_fpath = \"/kaggle/input/hubmap-hacking-the-human-vasculature/tile_meta.csv\"\nsample_fpath = \"/kaggle/input/hubmap-hacking-the-human-vasculature/sample_submission.csv\"\npolygon_fpath = \"/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl\"","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:01.316259Z","iopub.execute_input":"2023-08-25T16:20:01.316595Z","iopub.status.idle":"2023-08-25T16:20:01.327594Z","shell.execute_reply.started":"2023-08-25T16:20:01.316561Z","shell.execute_reply":"2023-08-25T16:20:01.326682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tile_df = pd.read_csv(tile_fpath)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:01.330577Z","iopub.execute_input":"2023-08-25T16:20:01.332359Z","iopub.status.idle":"2023-08-25T16:20:01.340468Z","shell.execute_reply.started":"2023-08-25T16:20:01.332327Z","shell.execute_reply":"2023-08-25T16:20:01.339482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sample_df = pd.read_csv(sample_fpath)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:01.343587Z","iopub.execute_input":"2023-08-25T16:20:01.344848Z","iopub.status.idle":"2023-08-25T16:20:01.352354Z","shell.execute_reply.started":"2023-08-25T16:20:01.344803Z","shell.execute_reply":"2023-08-25T16:20:01.351334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom keras import backend as K\n\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:01.353894Z","iopub.execute_input":"2023-08-25T16:20:01.354494Z","iopub.status.idle":"2023-08-25T16:20:09.656241Z","shell.execute_reply.started":"2023-08-25T16:20:01.354463Z","shell.execute_reply":"2023-08-25T16:20:09.655202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def LeftBlock(channel, X, ksize = 3, downsample = True):\n    \n    if downsample:\n        X =  layers.MaxPooling2D(pool_size=(2, 2), strides = (2,2))(X)\n        \n    \n    X = layers.Conv2D(channel, kernel_size = ksize, strides = 1, padding = \"same\")(X)\n    X = layers.BatchNormalization()(X)\n    X = layers.ReLU()(X)\n    \n    return X\n\ndef RightBlock(channel, X, ksize = 3, X_skip = None):\n    \n    X = layers.Conv2DTranspose(channel, kernel_size=4, strides=2, padding=\"SAME\")(X)\n    \n    if X_skip is not None:\n        X = layers.Concatenate()([X, X_skip])\n    \n    X =  layers.Conv2D(channel, kernel_size = ksize, strides = 1, padding = \"same\")(X)\n    X = layers.BatchNormalization()(X)\n    X = layers.ReLU()(X)\n    #X = layers.LeakyReLU(alpha=0.2)(X)\n    \n    return X\n","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:09.660811Z","iopub.execute_input":"2023-08-25T16:20:09.663809Z","iopub.status.idle":"2023-08-25T16:20:09.676070Z","shell.execute_reply.started":"2023-08-25T16:20:09.663773Z","shell.execute_reply":"2023-08-25T16:20:09.675059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model(skip_connection =  True):\n    \n    L = 512\n    Input =  layers.Input(shape=(L, L, 3))\n\n    X0 = layers.Rescaling(scale = 1./127.5, offset= -1, )(Input)\n    \n    \n    KS = 5\n    \n    channel = 48\n    X1 = LeftBlock(channel, X0, ksize = KS, downsample = False)#512\n    #X1 = LeftBlock(channel, X1, ksize = KS, downsample = False)#512\n    \n\n    channel = channel*2 #128\n    X2 = LeftBlock(channel, X1, ksize = KS, downsample = True)#256\n    #X2 = LeftBlock(channel, X2, ksize = KS, downsample = False)\n\n    channel = channel*2 #256\n    X3 = LeftBlock(channel, X2, ksize = KS, downsample = True)#128\n    #X3 = LeftBlock(channel, X3, ksize = KS, downsample = False)#128\n    \n    channel = channel*2 #512\n    X4 = LeftBlock(channel, X3, ksize = KS, downsample = True)#64\n    #X4 = LeftBlock(channel, X4, ksize = KS, downsample = False)#64\n\n    channel = channel #512\n    X5 = LeftBlock(channel, X4, ksize = KS, downsample = True)#32\n    #X5 = LeftBlock(channel, X5, ksize = KS, downsample = False)#32\n    \n    \n    KS = 5\n    if skip_connection:\n        \n        KS = 5\n        \n        #channel 512\n        #channel = int(48*2*2*2)\n        XR = RightBlock(channel, X5, ksize = KS, X_skip = X4) #64\n        #XR = RightBlock(channel, XR, ksize = KS, X_skip = None, upsample = False)\n\n        channel = int(channel/2) #256\n        XR = RightBlock(channel, XR, ksize = KS, X_skip = X3) #128\n        #XR = RightBlock(channel, XR, ksize = KS, X_skip = None, upsample = False)\n\n        channel = int(channel/2) #128\n        XR = RightBlock(channel, XR, ksize = KS, X_skip = X2) #256\n        #XR = RightBlock(channel, XR, ksize = KS, X_skip = None, upsample = False)\n        \n        channel = int(channel/2) #64\n        XR = RightBlock(channel, XR, ksize = KS, X_skip = X1) #512\n        #XR = RightBlock(channel, XR, ksize = KS, X_skip = None, upsample = False)\n        \n\n        \n    else: #NOT USED\n        \n        #channel = int(channel/2) #512\n        XR = RightBlock(channel, X5, ksize = KS, X_skip = None) #64\n        \n\n        channel = int(channel/2) #256\n        XR = RightBlock(channel, XR, ksize = KS, X_skip = None) #128\n\n        channel = int(channel/2) #128\n        XR = RightBlock(channel, XR, ksize = KS, X_skip = None) #256\n\n        channel = int(channel/2) #64\n        XR = RightBlock(channel, XR, ksize = KS, X_skip = None) #512\n        \n\n    channel = 1\n    XR = layers.Conv2D(channel, kernel_size = 1, strides = 1, padding = \"same\")(XR)\n\n    model = Model(inputs = Input, outputs = XR)\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:09.680792Z","iopub.execute_input":"2023-08-25T16:20:09.683374Z","iopub.status.idle":"2023-08-25T16:20:09.703222Z","shell.execute_reply.started":"2023-08-25T16:20:09.683340Z","shell.execute_reply":"2023-08-25T16:20:09.702297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model(True)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:09.707720Z","iopub.execute_input":"2023-08-25T16:20:09.711625Z","iopub.status.idle":"2023-08-25T16:20:13.097182Z","shell.execute_reply.started":"2023-08-25T16:20:09.711495Z","shell.execute_reply":"2023-08-25T16:20:13.096398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_path = \"/kaggle/input/hubmap-unet-model/ckpt1\"","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:13.101717Z","iopub.execute_input":"2023-08-25T16:20:13.102068Z","iopub.status.idle":"2023-08-25T16:20:13.112176Z","shell.execute_reply.started":"2023-08-25T16:20:13.102034Z","shell.execute_reply":"2023-08-25T16:20:13.111444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights(checkpoint_path)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:13.113108Z","iopub.execute_input":"2023-08-25T16:20:13.113417Z","iopub.status.idle":"2023-08-25T16:20:15.260711Z","shell.execute_reply.started":"2023-08-25T16:20:13.113387Z","shell.execute_reply":"2023-08-25T16:20:15.259722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_probability(X):\n    \n    \n    pred = model.predict(X, verbose = 0)\n    odd = np.exp(pred)\n    prob = odd/(1+odd)\n    \n    return prob\n\ndef prob_to_mask(prob, threshold):\n    \n    mask = (prob > threshold).astype(np.uint8)\n    \n    return mask\n    \n    \ndef img_to_mask(X, threshold):\n    \n    prob = predict_probability(X)\n    \n    mask = prob_to_mask(prob, threshold)\n    \n    return mask","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:15.262222Z","iopub.execute_input":"2023-08-25T16:20:15.262557Z","iopub.status.idle":"2023-08-25T16:20:15.269114Z","shell.execute_reply.started":"2023-08-25T16:20:15.262526Z","shell.execute_reply":"2023-08-25T16:20:15.268111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\nimport numpy as np\nfrom pycocotools import _mask as coco_mask\nimport typing as t\nimport zlib","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:15.270801Z","iopub.execute_input":"2023-08-25T16:20:15.271191Z","iopub.status.idle":"2023-08-25T16:20:15.281372Z","shell.execute_reply.started":"2023-08-25T16:20:15.271160Z","shell.execute_reply":"2023-08-25T16:20:15.280441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def encode_binary_mask(mask: np.ndarray) -> t.Text:\n    \"\"\"Converts a binary mask into OID challenge encoding ascii text.\"\"\"\n\n    # check input mask --\n    if mask.dtype != np.bool:\n        raise ValueError(\n            \"encode_binary_mask expects a binary mask, received dtype == %s\" %\n            mask.dtype)\n\n    mask = np.squeeze(mask)\n    if len(mask.shape) != 2:\n        raise ValueError(\n            \"encode_binary_mask expects a 2d mask, received shape == %s\" %\n            mask.shape)\n\n    # convert input mask to expected COCO API input --\n    mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)\n    mask_to_encode = mask_to_encode.astype(np.uint8)\n    mask_to_encode = np.asfortranarray(mask_to_encode)\n\n    # RLE encode mask --\n    encoded_mask = coco_mask.encode(mask_to_encode)[0][\"counts\"]\n\n    # compress and base64 encoding --\n    binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)\n    base64_str = base64.b64encode(binary_str)\n    return base64_str","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:15.282914Z","iopub.execute_input":"2023-08-25T16:20:15.283298Z","iopub.status.idle":"2023-08-25T16:20:15.296456Z","shell.execute_reply.started":"2023-08-25T16:20:15.283268Z","shell.execute_reply":"2023-08-25T16:20:15.295418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cal_confidence(prob, mask_bool):\n    \n    return np.mean(prob[mask_bool])\n    ","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:15.297979Z","iopub.execute_input":"2023-08-25T16:20:15.298334Z","iopub.status.idle":"2023-08-25T16:20:15.311847Z","shell.execute_reply.started":"2023-08-25T16:20:15.298303Z","shell.execute_reply":"2023-08-25T16:20:15.310973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mask_encode(prob, threshold_prob, threshold_pixel=10):\n    \n    mask = prob_to_mask(prob, threshold_prob)\n    \n    n_labels, labels_mat = cv2.connectedComponents(mask)\n    \n    \n    output = \"\"\n    \n    for label in range(0, n_labels):\n        label_bool = labels_mat == label\n        sum_label = np.sum(label_bool)\n\n        if sum_label >= threshold_pixel:\n            \n            \n            \n            #calculate confidence\n            conf = cal_confidence(prob, label_bool)\n            \n            if conf > 0.7:\n            \n                #convert to string\n                label_string = encode_binary_mask(label_bool).decode('utf-8')\n\n                if output == \"\":\n                    output = \"0 \" + str(float(conf)) + \" \" + label_string\n\n                else:\n                    output += \" 0 \" + str(float(conf)) + \" \" + label_string\n\n                #print(sum_label, conf, label_string)\n    \n    return output","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:15.313383Z","iopub.execute_input":"2023-08-25T16:20:15.313815Z","iopub.status.idle":"2023-08-25T16:20:15.323621Z","shell.execute_reply.started":"2023-08-25T16:20:15.313785Z","shell.execute_reply":"2023-08-25T16:20:15.322774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Read Images","metadata":{}},{"cell_type":"code","source":"def read_images(folder):\n    \n    time1 = time.time()\n    files = os.listdir(folder)\n    \n    N = len(files)\n    L = 512\n    X_mat = np.zeros((N, L, L, 3), dtype = np.uint8)\n    \n    for i in range(N):\n        fname = folder + files[i]\n        \n        img = cv2.imread(fname)[:,:,::-1]\n        X_mat[i,:,:,:] = img\n        \n    time2 = time.time()\n    \n    time3 = (time2 - time1)//1\n    \n    print(time3, \"sec\")\n    \n    return X_mat, list(map(lambda x: x.split(\".\")[0], files))","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:15.324950Z","iopub.execute_input":"2023-08-25T16:20:15.325659Z","iopub.status.idle":"2023-08-25T16:20:15.335323Z","shell.execute_reply.started":"2023-08-25T16:20:15.325627Z","shell.execute_reply":"2023-08-25T16:20:15.334459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_folder","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:15.336884Z","iopub.execute_input":"2023-08-25T16:20:15.337203Z","iopub.status.idle":"2023-08-25T16:20:15.358458Z","shell.execute_reply.started":"2023-08-25T16:20:15.337173Z","shell.execute_reply":"2023-08-25T16:20:15.357500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_mat, X_files = read_images(test_folder)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:15.360092Z","iopub.execute_input":"2023-08-25T16:20:15.360728Z","iopub.status.idle":"2023-08-25T16:20:15.418458Z","shell.execute_reply.started":"2023-08-25T16:20:15.360689Z","shell.execute_reply":"2023-08-25T16:20:15.417477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predict Probability","metadata":{}},{"cell_type":"code","source":"def pred_augument(X, kind = 0):\n    \n    if kind == 1:\n        X = X[:,:,::-1]\n        #Y = Y[:,:,::-1]\n        \n    elif kind == 2:\n        X = X[:,::-1]\n        #Y = Y[:,::-1]\n    \n    elif kind == 3:\n        X = X[:,::-1][:,:,::-1]\n        #Y = Y[:,::-1][:,:,::-1]\n        \n    \n    prob = predict_probability(X)\n    \n    \n    if kind == 1:\n        prob = prob[:,:,::-1]\n        \n    elif kind == 2:\n        prob = prob[:,::-1]\n    \n    elif kind == 3:\n        prob = prob[:,::-1][:,:,::-1]\n    \n    return prob\n\ndef predict_emsemble(X):\n    \n    prob0 = pred_augument(X, 0)\n    prob1 = pred_augument(X, 1)\n    prob2 = pred_augument(X, 2)\n    prob3 = pred_augument(X, 3)\n    \n    p_avg = (prob0 + prob1 + prob2 + prob3)/4\n    \n    \n    return p_avg","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:15.419569Z","iopub.execute_input":"2023-08-25T16:20:15.419901Z","iopub.status.idle":"2023-08-25T16:20:15.428473Z","shell.execute_reply.started":"2023-08-25T16:20:15.419869Z","shell.execute_reply":"2023-08-25T16:20:15.427586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"thresh_prob = 0.835","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:15.430122Z","iopub.execute_input":"2023-08-25T16:20:15.430784Z","iopub.status.idle":"2023-08-25T16:20:15.438216Z","shell.execute_reply.started":"2023-08-25T16:20:15.430752Z","shell.execute_reply":"2023-08-25T16:20:15.437332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = X_mat.shape[0]\nprint(N)\n\nstring_list = []\n\nfor i in range(N):\n    \n    if i % 50 == 0:\n        print(i)\n    prob = predict_emsemble(X_mat[i].reshape(1,512,512,3))\n    pred_string = mask_encode(prob[0], thresh_prob)\n    string_list.append(pred_string)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:15.439646Z","iopub.execute_input":"2023-08-25T16:20:15.440302Z","iopub.status.idle":"2023-08-25T16:20:23.691926Z","shell.execute_reply.started":"2023-08-25T16:20:15.440271Z","shell.execute_reply":"2023-08-25T16:20:23.689818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df = pd.DataFrame({\"id\":X_files})","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:23.693227Z","iopub.execute_input":"2023-08-25T16:20:23.693603Z","iopub.status.idle":"2023-08-25T16:20:23.701426Z","shell.execute_reply.started":"2023-08-25T16:20:23.693569Z","shell.execute_reply":"2023-08-25T16:20:23.700102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df[\"height\"] = 512\nsubmit_df[\"width\"] = 512","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:23.703165Z","iopub.execute_input":"2023-08-25T16:20:23.703500Z","iopub.status.idle":"2023-08-25T16:20:23.717999Z","shell.execute_reply.started":"2023-08-25T16:20:23.703469Z","shell.execute_reply":"2023-08-25T16:20:23.717075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df[\"prediction_string\"] = string_list","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:23.719644Z","iopub.execute_input":"2023-08-25T16:20:23.720470Z","iopub.status.idle":"2023-08-25T16:20:23.729208Z","shell.execute_reply.started":"2023-08-25T16:20:23.720444Z","shell.execute_reply":"2023-08-25T16:20:23.728304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T16:20:23.730379Z","iopub.execute_input":"2023-08-25T16:20:23.730660Z","iopub.status.idle":"2023-08-25T16:20:23.743674Z","shell.execute_reply.started":"2023-08-25T16:20:23.730637Z","shell.execute_reply":"2023-08-25T16:20:23.742716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}