{"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 os\nimport cv2\nimport glob\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-19T06:41:30.602212Z","iopub.execute_input":"2022-07-19T06:41:30.602925Z","iopub.status.idle":"2022-07-19T06:41:30.783287Z","shell.execute_reply.started":"2022-07-19T06:41:30.602823Z","shell.execute_reply":"2022-07-19T06:41:30.782115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train CSV","metadata":{}},{"cell_type":"code","source":"train_csv = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')\ntrain_csv","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:30.785134Z","iopub.execute_input":"2022-07-19T06:41:30.785592Z","iopub.status.idle":"2022-07-19T06:41:31.132978Z","shell.execute_reply.started":"2022-07-19T06:41:30.785556Z","shell.execute_reply":"2022-07-19T06:41:31.131596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The below cell shows the image heights and widths. Although it is mentioned in the data description that all the image sizes are 3000 x 3000 for HPA data it seems there are some images with different sizes as well. But in all the images, the height and width is same","metadata":{}},{"cell_type":"code","source":"print(train_csv['img_height'].unique())\nprint(train_csv['img_width'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:31.137105Z","iopub.execute_input":"2022-07-19T06:41:31.138223Z","iopub.status.idle":"2022-07-19T06:41:31.152282Z","shell.execute_reply.started":"2022-07-19T06:41:31.138172Z","shell.execute_reply":"2022-07-19T06:41:31.150569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_csv['img_height']==train_csv['img_width']).value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:31.156745Z","iopub.execute_input":"2022-07-19T06:41:31.157340Z","iopub.status.idle":"2022-07-19T06:41:31.170919Z","shell.execute_reply.started":"2022-07-19T06:41:31.157290Z","shell.execute_reply":"2022-07-19T06:41:31.169650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['pixel_size'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:31.173058Z","iopub.execute_input":"2022-07-19T06:41:31.173659Z","iopub.status.idle":"2022-07-19T06:41:31.190210Z","shell.execute_reply.started":"2022-07-19T06:41:31.173610Z","shell.execute_reply":"2022-07-19T06:41:31.188607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['data_source'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:31.192430Z","iopub.execute_input":"2022-07-19T06:41:31.193399Z","iopub.status.idle":"2022-07-19T06:41:31.205759Z","shell.execute_reply.started":"2022-07-19T06:41:31.193352Z","shell.execute_reply":"2022-07-19T06:41:31.204196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['tissue_thickness'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:31.207873Z","iopub.execute_input":"2022-07-19T06:41:31.208857Z","iopub.status.idle":"2022-07-19T06:41:31.220408Z","shell.execute_reply.started":"2022-07-19T06:41:31.208809Z","shell.execute_reply":"2022-07-19T06:41:31.219245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['sex'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:31.222051Z","iopub.execute_input":"2022-07-19T06:41:31.222821Z","iopub.status.idle":"2022-07-19T06:41:31.235074Z","shell.execute_reply.started":"2022-07-19T06:41:31.222783Z","shell.execute_reply":"2022-07-19T06:41:31.233911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['age'].hist()\nplt.xlabel('Age')\nplt.ylabel('Number of Records')","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:31.236712Z","iopub.execute_input":"2022-07-19T06:41:31.237974Z","iopub.status.idle":"2022-07-19T06:41:31.564145Z","shell.execute_reply.started":"2022-07-19T06:41:31.237923Z","shell.execute_reply":"2022-07-19T06:41:31.563138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv['organ'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:31.569311Z","iopub.execute_input":"2022-07-19T06:41:31.570024Z","iopub.status.idle":"2022-07-19T06:41:31.580164Z","shell.execute_reply.started":"2022-07-19T06:41:31.569972Z","shell.execute_reply":"2022-07-19T06:41:31.578848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('../input/hubmap-organ-segmentation/sample_submission.csv')\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:31.582003Z","iopub.execute_input":"2022-07-19T06:41:31.582795Z","iopub.status.idle":"2022-07-19T06:41:31.606286Z","shell.execute_reply.started":"2022-07-19T06:41:31.582747Z","shell.execute_reply":"2022-07-19T06:41:31.605117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Visualization","metadata":{}},{"cell_type":"code","source":"def display_image(image):\n    img = cv2.cvtColor(cv2.imread(image),cv2.COLOR_BGR2RGB)\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:31.608176Z","iopub.execute_input":"2022-07-19T06:41:31.608961Z","iopub.status.idle":"2022-07-19T06:41:31.616247Z","shell.execute_reply.started":"2022-07-19T06:41:31.608914Z","shell.execute_reply":"2022-07-19T06:41:31.614594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_image('../input/hubmap-organ-segmentation/train_images/12471.tiff')","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:31.618066Z","iopub.execute_input":"2022-07-19T06:41:31.618940Z","iopub.status.idle":"2022-07-19T06:41:33.335094Z","shell.execute_reply.started":"2022-07-19T06:41:31.618889Z","shell.execute_reply":"2022-07-19T06:41:33.333779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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\n\ndef rle2mask(mask_rle, shape=(3000,3000)):\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-19T06:41:33.336905Z","iopub.execute_input":"2022-07-19T06:41:33.337349Z","iopub.status.idle":"2022-07-19T06:41:33.351706Z","shell.execute_reply.started":"2022-07-19T06:41:33.337302Z","shell.execute_reply":"2022-07-19T06:41:33.350504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"organs = np.unique(train_csv.organ)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:33.353461Z","iopub.execute_input":"2022-07-19T06:41:33.354116Z","iopub.status.idle":"2022-07-19T06:41:33.364055Z","shell.execute_reply.started":"2022-07-19T06:41:33.354084Z","shell.execute_reply":"2022-07-19T06:41:33.362472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,len(organs),figsize=(20,20))\n\nfor i in range(len(organs)):\n    organ_df = train_csv[train_csv['organ']==organs[i]].reset_index()\n    idx = np.random.randint(organ_df.shape[0])\n    image = plt.imread(\"../input/hubmap-organ-segmentation/train_images/\"+str(organ_df.id[idx])+\".tiff\")\n    mask = rle2mask(organ_df.rle[idx],shape=(organ_df.img_height[idx],organ_df.img_width[idx]))\n    ax[i].imshow(image)\n    ax[i].imshow(mask,alpha=0.4)\n    ax[i].set_title(organ_df.organ[idx])\n    ax[i].axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:33.367431Z","iopub.execute_input":"2022-07-19T06:41:33.367956Z","iopub.status.idle":"2022-07-19T06:41:44.210454Z","shell.execute_reply.started":"2022-07-19T06:41:33.367895Z","shell.execute_reply":"2022-07-19T06:41:44.209292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"organ_df","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:44.212100Z","iopub.execute_input":"2022-07-19T06:41:44.212778Z","iopub.status.idle":"2022-07-19T06:41:44.261183Z","shell.execute_reply.started":"2022-07-19T06:41:44.212742Z","shell.execute_reply":"2022-07-19T06:41:44.260232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:44.262460Z","iopub.execute_input":"2022-07-19T06:41:44.263674Z","iopub.status.idle":"2022-07-19T06:41:44.271492Z","shell.execute_reply.started":"2022-07-19T06:41:44.263618Z","shell.execute_reply":"2022-07-19T06:41:44.270431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv = pd.read_csv('../input/hubmap-organ-segmentation/test.csv')\ntest_csv","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:41:44.272966Z","iopub.execute_input":"2022-07-19T06:41:44.273784Z","iopub.status.idle":"2022-07-19T06:41:44.296331Z","shell.execute_reply.started":"2022-07-19T06:41:44.273750Z","shell.execute_reply":"2022-07-19T06:41:44.295369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"All the image pixel values are in the range [0,255]","metadata":{}},{"cell_type":"code","source":"for img in os.listdir('../input/hubmap-organ-segmentation/train_images/'):\n    image = cv2.imread('../input/hubmap-organ-segmentation/train_images/12471.tiff')\n    print(image.max())","metadata":{"execution":{"iopub.status.busy":"2022-07-19T06:45:54.499719Z","iopub.execute_input":"2022-07-19T06:45:54.500125Z","iopub.status.idle":"2022-07-19T06:46:19.077845Z","shell.execute_reply.started":"2022-07-19T06:45:54.500092Z","shell.execute_reply":"2022-07-19T06:46:19.076580Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}