{"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 pandas as pd \nimport os ","metadata":{"execution":{"iopub.status.busy":"2022-08-18T07:19:02.719606Z","iopub.execute_input":"2022-08-18T07:19:02.720080Z","iopub.status.idle":"2022-08-18T07:19:02.727146Z","shell.execute_reply.started":"2022-08-18T07:19:02.720021Z","shell.execute_reply":"2022-08-18T07:19:02.726180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = '../input/hubmap-organ-segmentation/train_images/*.tiff'\n\ndf = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2022-08-18T07:19:02.732508Z","iopub.execute_input":"2022-08-18T07:19:02.733321Z","iopub.status.idle":"2022-08-18T07:19:02.992421Z","shell.execute_reply.started":"2022-08-18T07:19:02.733284Z","shell.execute_reply":"2022-08-18T07:19:02.991179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['organ'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T07:19:02.994336Z","iopub.execute_input":"2022-08-18T07:19:02.995090Z","iopub.status.idle":"2022-08-18T07:19:03.004821Z","shell.execute_reply.started":"2022-08-18T07:19:02.995020Z","shell.execute_reply":"2022-08-18T07:19:03.003754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Kidney CSV","metadata":{}},{"cell_type":"code","source":"out_id = []\nout_organs = []\nout_rle = []\nresult = dict()\nfor index, row in df.iterrows():\n    organ = row['organ']\n    img_id = row['id']\n    rle = row['rle']\n    if organ == 'kidney':\n            out_id.append(img_id)\n            out_organs.append(organ)\n            out_rle.append(rle)\nresult['id'] = out_id\nresult['organ'] = out_organs\nresult['rle'] = out_rle\nresult = pd.DataFrame.from_dict(result)\nresult.to_csv('kidney.csv', index=False)\nresult\n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-18T07:19:03.008023Z","iopub.execute_input":"2022-08-18T07:19:03.008768Z","iopub.status.idle":"2022-08-18T07:19:03.117234Z","shell.execute_reply.started":"2022-08-18T07:19:03.008730Z","shell.execute_reply":"2022-08-18T07:19:03.116205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Lung CSV","metadata":{}},{"cell_type":"code","source":"out_id = []\nout_organs = []\nout_rle = []\nresult = dict()\nfor index, row in df.iterrows():\n    organ = row['organ']\n    img_id = row['id']\n    rle = row['rle']\n    if organ == 'lung':\n            out_id.append(img_id)\n            out_organs.append(organ)\n            out_rle.append(rle)\nresult['id'] = out_id\nresult['organ'] = out_organs\nresult['rle'] = out_rle\nresult = pd.DataFrame.from_dict(result)\nresult.to_csv('lung.csv', index=False)\nresult","metadata":{"execution":{"iopub.status.busy":"2022-08-18T07:19:03.122230Z","iopub.execute_input":"2022-08-18T07:19:03.122644Z","iopub.status.idle":"2022-08-18T07:19:03.217652Z","shell.execute_reply.started":"2022-08-18T07:19:03.122596Z","shell.execute_reply":"2022-08-18T07:19:03.216357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# largeintestine CSV","metadata":{}},{"cell_type":"code","source":"out_id = []\nout_organs = []\nout_rle = []\nresult = dict()\nfor index, row in df.iterrows():\n    organ = row['organ']\n    img_id = row['id']\n    rle = row['rle']\n    if organ == 'largeintestine':\n            out_id.append(img_id)\n            out_organs.append(organ)\n            out_rle.append(rle)\nresult['id'] = out_id\nresult['organ'] = out_organs\nresult['rle'] = out_rle\nresult = pd.DataFrame.from_dict(result)\nresult.to_csv('largeintestine.csv', index=False)\nresult","metadata":{"execution":{"iopub.status.busy":"2022-08-18T07:19:03.219156Z","iopub.execute_input":"2022-08-18T07:19:03.219536Z","iopub.status.idle":"2022-08-18T07:19:03.623742Z","shell.execute_reply.started":"2022-08-18T07:19:03.219499Z","shell.execute_reply":"2022-08-18T07:19:03.622814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# prostate CSV","metadata":{}},{"cell_type":"code","source":"out_id = []\nout_organs = []\nout_rle = []\nresult = dict()\nfor index, row in df.iterrows():\n    organ = row['organ']\n    img_id = row['id']\n    rle = row['rle']\n    if organ == 'prostate':\n            out_id.append(img_id)\n            out_organs.append(organ)\n            out_rle.append(rle)\nresult['id'] = out_id\nresult['organ'] = out_organs\nresult['rle'] = out_rle\nresult = pd.DataFrame.from_dict(result)\nresult.to_csv('prostate.csv', index=False)\nresult","metadata":{"execution":{"iopub.status.busy":"2022-08-18T07:19:03.627858Z","iopub.execute_input":"2022-08-18T07:19:03.630453Z","iopub.status.idle":"2022-08-18T07:19:03.854015Z","shell.execute_reply.started":"2022-08-18T07:19:03.630407Z","shell.execute_reply":"2022-08-18T07:19:03.853110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_id = []\nout_organs = []\nout_rle = []\nresult = dict()\nfor index, row in df.iterrows():\n    organ = row['organ']\n    img_id = row['id']\n    rle = row['rle']\n    if organ == 'spleen':\n            out_id.append(img_id)\n            out_organs.append(organ)\n            out_rle.append(rle)\nresult['id'] = out_id\nresult['organ'] = out_organs\nresult['rle'] = out_rle\nresult = pd.DataFrame.from_dict(result)\nresult.to_csv('spleen.csv', index=False)\nresult","metadata":{"execution":{"iopub.status.busy":"2022-08-18T07:19:03.855318Z","iopub.execute_input":"2022-08-18T07:19:03.855668Z","iopub.status.idle":"2022-08-18T07:19:03.928545Z","shell.execute_reply.started":"2022-08-18T07:19:03.855632Z","shell.execute_reply":"2022-08-18T07:19:03.927500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import load_img\nfrom tqdm import tqdm\nimport keras\n\ntrain_img_path = '../input/hubmap-organ-segmentation/train_images'\ndf = pd.read_csv('./spleen.csv')\nids = df['id'].values\n\ndef load_img(id):\n    img = tiff.imread(os.path.join(train_img_path, f\"{id}.tiff\"))\n    return img\n\ndef rle2mask(id, shape):\n    rle = df[df['id'] == id]['rle'].iloc[-1]\n    s = rle.split()\n    starts, lengths = [np.asarray(x, dtype='int') for x in (s[0:][::2], s[1:][::2])]\n    starts = starts - 1\n    mask = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for s, l in zip(starts, lengths):\n        mask[s:s+l] = 1\n    mask = mask.reshape((shape[0], shape[1])).T\n#     we need at least three channels to save an image so here expand mask to (3000, 3000, 1)\n    mask = mask[:, :, tf.newaxis]\n    return mask","metadata":{"execution":{"iopub.status.busy":"2022-08-18T07:19:03.931717Z","iopub.execute_input":"2022-08-18T07:19:03.932100Z","iopub.status.idle":"2022-08-18T07:19:09.747388Z","shell.execute_reply.started":"2022-08-18T07:19:03.932050Z","shell.execute_reply":"2022-08-18T07:19:09.746346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport tifffile as tiff\nimport numpy as np\ntrain_img_output_path = './HuBMAP_train_spleen/images'\ntrain_mask_output_path = './HuBMAP_train_spleen/masks'\nif not os.path.exists(train_img_output_path):\n    os.makedirs(train_img_output_path)\nif not os.path.exists(train_mask_output_path):\n    os.makedirs(train_mask_output_path)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T07:19:09.749144Z","iopub.execute_input":"2022-08-18T07:19:09.749504Z","iopub.status.idle":"2022-08-18T07:19:09.886643Z","shell.execute_reply.started":"2022-08-18T07:19:09.749469Z","shell.execute_reply":"2022-08-18T07:19:09.885619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow\nfor id in tqdm(ids):\n    img = load_img(id)\n    tensorflow.keras.utils.save_img(f'{train_img_output_path}/{id}.png', img)\n    mask = rle2mask(id, img.shape)\n    tensorflow.keras.utils.save_img(f'{train_mask_output_path}/{id}.png', mask, scale=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T07:19:09.889516Z","iopub.execute_input":"2022-08-18T07:19:09.890458Z","iopub.status.idle":"2022-08-18T07:22:28.033165Z","shell.execute_reply.started":"2022-08-18T07:19:09.890411Z","shell.execute_reply":"2022-08-18T07:22:28.032123Z"},"trusted":true},"execution_count":null,"outputs":[]}]}