{"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\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-15T12:17:01.599966Z","iopub.execute_input":"2022-07-15T12:17:01.600464Z","iopub.status.idle":"2022-07-15T12:17:01.835783Z","shell.execute_reply.started":"2022-07-15T12:17:01.600361Z","shell.execute_reply":"2022-07-15T12:17:01.834543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 0. Utility","metadata":{}},{"cell_type":"code","source":"# https://www.kaggle.com/paulorzp/rle-functions-run-length-encode-decode\ndef mask2rle(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels= img.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n \ndef rle2mask(mask_rle, shape):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:01.837732Z","iopub.execute_input":"2022-07-15T12:17:01.838091Z","iopub.status.idle":"2022-07-15T12:17:01.849402Z","shell.execute_reply.started":"2022-07-15T12:17:01.838058Z","shell.execute_reply":"2022-07-15T12:17:01.848216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Check data","metadata":{}},{"cell_type":"markdown","source":"## Train data","metadata":{}},{"cell_type":"code","source":"# load train.csv\ntrain_df = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:01.851497Z","iopub.execute_input":"2022-07-15T12:17:01.851877Z","iopub.status.idle":"2022-07-15T12:17:02.246472Z","shell.execute_reply.started":"2022-07-15T12:17:01.851848Z","shell.execute_reply":"2022-07-15T12:17:02.245308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:02.249612Z","iopub.execute_input":"2022-07-15T12:17:02.250467Z","iopub.status.idle":"2022-07-15T12:17:02.281051Z","shell.execute_reply.started":"2022-07-15T12:17:02.250419Z","shell.execute_reply":"2022-07-15T12:17:02.280091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.sex.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:02.282863Z","iopub.execute_input":"2022-07-15T12:17:02.283252Z","iopub.status.idle":"2022-07-15T12:17:02.293675Z","shell.execute_reply.started":"2022-07-15T12:17:02.283216Z","shell.execute_reply":"2022-07-15T12:17:02.292389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(train_df.age)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:02.295136Z","iopub.execute_input":"2022-07-15T12:17:02.295621Z","iopub.status.idle":"2022-07-15T12:17:02.536606Z","shell.execute_reply.started":"2022-07-15T12:17:02.295573Z","shell.execute_reply":"2022-07-15T12:17:02.535271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.organ.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:02.538208Z","iopub.execute_input":"2022-07-15T12:17:02.538707Z","iopub.status.idle":"2022-07-15T12:17:02.549251Z","shell.execute_reply.started":"2022-07-15T12:17:02.538647Z","shell.execute_reply":"2022-07-15T12:17:02.547788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.data_source.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:02.551508Z","iopub.execute_input":"2022-07-15T12:17:02.552956Z","iopub.status.idle":"2022-07-15T12:17:02.565550Z","shell.execute_reply.started":"2022-07-15T12:17:02.552906Z","shell.execute_reply":"2022-07-15T12:17:02.564218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.pixel_size.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:02.567586Z","iopub.execute_input":"2022-07-15T12:17:02.568531Z","iopub.status.idle":"2022-07-15T12:17:02.581072Z","shell.execute_reply.started":"2022-07-15T12:17:02.568479Z","shell.execute_reply":"2022-07-15T12:17:02.579659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.tissue_thickness.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:02.586723Z","iopub.execute_input":"2022-07-15T12:17:02.587733Z","iopub.status.idle":"2022-07-15T12:17:02.599765Z","shell.execute_reply.started":"2022-07-15T12:17:02.587680Z","shell.execute_reply":"2022-07-15T12:17:02.598779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[['img_height', 'img_width']].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:02.601150Z","iopub.execute_input":"2022-07-15T12:17:02.602341Z","iopub.status.idle":"2022-07-15T12:17:02.618345Z","shell.execute_reply.started":"2022-07-15T12:17:02.602290Z","shell.execute_reply":"2022-07-15T12:17:02.617117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0,10): # index of train_df 0~350\n    train_img_base = '/kaggle/input/hubmap-organ-segmentation/train_images'\n    train_img_id = '/' + str(train_df.id.iloc[i]) + '.tiff'\n    train_img = cv2.imread(train_img_base + train_img_id,flags = cv2.IMREAD_UNCHANGED)\n    train_mask = rle2mask(train_df.rle.iloc[i], tuple(train_df[['img_height', 'img_width']].iloc[i].values))\n    print(train_df.id.iloc[i], train_df.organ.iloc[i])\n    plt.imshow(train_img)\n    plt.imshow(train_mask, alpha = 0.3)\n    plt.show()\n    # print(train_img.shape, train_mask.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:02.620078Z","iopub.execute_input":"2022-07-15T12:17:02.621467Z","iopub.status.idle":"2022-07-15T12:17:28.712243Z","shell.execute_reply.started":"2022-07-15T12:17:02.621416Z","shell.execute_reply":"2022-07-15T12:17:28.710931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img.dtype","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:28.713561Z","iopub.execute_input":"2022-07-15T12:17:28.713890Z","iopub.status.idle":"2022-07-15T12:17:28.722718Z","shell.execute_reply.started":"2022-07-15T12:17:28.713858Z","shell.execute_reply":"2022-07-15T12:17:28.721234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check blank area --> values of the blank area are different\nprint(train_img[10,10,:])\nprint(train_img[12,12,:])","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:28.724501Z","iopub.execute_input":"2022-07-15T12:17:28.724946Z","iopub.status.idle":"2022-07-15T12:17:28.738497Z","shell.execute_reply.started":"2022-07-15T12:17:28.724857Z","shell.execute_reply":"2022-07-15T12:17:28.737408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test data","metadata":{}},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/hubmap-organ-segmentation/test.csv')\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:28.740139Z","iopub.execute_input":"2022-07-15T12:17:28.740532Z","iopub.status.idle":"2022-07-15T12:17:28.766094Z","shell.execute_reply.started":"2022-07-15T12:17:28.740485Z","shell.execute_reply":"2022-07-15T12:17:28.765229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = cv2.imread('/kaggle/input/hubmap-organ-segmentation/test_images/10078.tiff', cv2.IMREAD_UNCHANGED)\nplt.imshow(test_img)\nplt.show()\nprint(test_img.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:17:28.767078Z","iopub.execute_input":"2022-07-15T12:17:28.767429Z","iopub.status.idle":"2022-07-15T12:17:29.564996Z","shell.execute_reply.started":"2022-07-15T12:17:28.767397Z","shell.execute_reply":"2022-07-15T12:17:29.563700Z"},"trusted":true},"execution_count":null,"outputs":[]}]}