{"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":"!pip install monai[cucim]==0.9.1\n!pip install scipy scikit-image cupy-cuda110","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:26:28.892127Z","iopub.execute_input":"2022-08-17T07:26:28.892892Z","iopub.status.idle":"2022-08-17T07:26:52.334832Z","shell.execute_reply.started":"2022-08-17T07:26:28.892778Z","shell.execute_reply":"2022-08-17T07:26:52.333536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install monai[itk]==0.9.1 ","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:26:52.336552Z","iopub.execute_input":"2022-08-17T07:26:52.337072Z","iopub.status.idle":"2022-08-17T07:27:18.315496Z","shell.execute_reply.started":"2022-08-17T07:26:52.337017Z","shell.execute_reply":"2022-08-17T07:27:18.313769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom monai.transforms import LoadImaged, Compose, GridPatchd\nfrom monai.data.wsi_reader import WSIReader\nfrom monai.data import Dataset, DataLoader\nfrom monai.visualize import matshow3d\nfrom monai.data.image_reader import ITKReader       \nimport matplotlib.pyplot as plt\n\nroot_dir = \"../input/hubmap-organ-segmentation/\"\n\ntrain_img_dir = os.path.join(root_dir, \"train_images\")\ntest_img_dir = os.path.join(root_dir, \"test_images\")\n\ntrain_df = pd.read_csv(os.path.join(root_dir, \"train.csv\"))\n\nlabel_dict = {\"prostate\" : 0, \"kidney\": 1,\"spleen\" : 2, \"lung\": 3,\"largeintestine\": 4}\n\ndef parse_label(x):\n    return label_dict[x]\ntrain_df[\"cls\"] = train_df[\"organ\"].apply(lambda x: parse_label(x))\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:27:48.536830Z","iopub.execute_input":"2022-08-17T07:27:48.537306Z","iopub.status.idle":"2022-08-17T07:27:48.732452Z","shell.execute_reply.started":"2022-08-17T07:27:48.537270Z","shell.execute_reply":"2022-08-17T07:27:48.731274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_id = \"10044\"  # select a small .tif file\n\nexample_img_path = os.path.join(train_img_dir, example_id + \".tiff\")\nexample_label = train_df[\"cls\"][train_df[\"id\"] == example_id]\n\nexample = {\"image\": example_img_path, \"organ\": example_label}\npatch_size = (300, 300)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:39:00.484320Z","iopub.execute_input":"2022-08-17T07:39:00.485893Z","iopub.status.idle":"2022-08-17T07:39:00.494134Z","shell.execute_reply.started":"2022-08-17T07:39:00.485829Z","shell.execute_reply":"2022-08-17T07:39:00.492694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_img_path","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:27:30.919759Z","iopub.execute_input":"2022-08-17T07:27:30.920392Z","iopub.status.idle":"2022-08-17T07:27:30.928486Z","shell.execute_reply.started":"2022-08-17T07:27:30.920345Z","shell.execute_reply":"2022-08-17T07:27:30.927098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport numpy as np\n\nimg = Image.open(example_img_path)\n#example = {\"image\": img, \"organ\": example_label}","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:27:36.188281Z","iopub.execute_input":"2022-08-17T07:27:36.188693Z","iopub.status.idle":"2022-08-17T07:27:36.198124Z","shell.execute_reply.started":"2022-08-17T07:27:36.188660Z","shell.execute_reply":"2022-08-17T07:27:36.197294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:27:39.308266Z","iopub.execute_input":"2022-08-17T07:27:39.309496Z","iopub.status.idle":"2022-08-17T07:27:40.661525Z","shell.execute_reply.started":"2022-08-17T07:27:39.309447Z","shell.execute_reply":"2022-08-17T07:27:40.660250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = Compose(\n    [\n        LoadImaged(keys=[\"image\"], reader=ITKReader, image_only=True), # backend = \"cucim\" or \"openslide\"\n        GridPatchd(\n            keys=[\"image\"],\n            patch_size=patch_size,\n        ),\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:39:02.686199Z","iopub.execute_input":"2022-08-17T07:39:02.686664Z","iopub.status.idle":"2022-08-17T07:39:02.694060Z","shell.execute_reply.started":"2022-08-17T07:39:02.686627Z","shell.execute_reply":"2022-08-17T07:39:02.692970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#filename=\"your_wsi_file.tiff\"\n#reader = WSIReader(backend=\"cuCIM\")\n#reader = ITKReader\n\n#obj = reader.read(example_img_path)\n#img, _ = reader.get_data(obj, level=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = transform(example)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:39:10.945450Z","iopub.execute_input":"2022-08-17T07:39:10.945956Z","iopub.status.idle":"2022-08-17T07:39:13.260951Z","shell.execute_reply.started":"2022-08-17T07:39:10.945913Z","shell.execute_reply":"2022-08-17T07:39:13.259829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the image has been divided into 30 patches\n\nlen(data)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:39:20.183321Z","iopub.execute_input":"2022-08-17T07:39:20.184377Z","iopub.status.idle":"2022-08-17T07:39:20.192534Z","shell.execute_reply.started":"2022-08-17T07:39:20.184315Z","shell.execute_reply":"2022-08-17T07:39:20.191242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:43:09.936665Z","iopub.execute_input":"2022-08-17T07:43:09.937117Z","iopub.status.idle":"2022-08-17T07:43:11.247904Z","shell.execute_reply.started":"2022-08-17T07:43:09.937080Z","shell.execute_reply":"2022-08-17T07:43:11.246626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row = 2\ncol = 5\nfig, axs = plt.subplots(row, col, figsize=(5, 5))\n\ncount = 0\nfor i in range(row):\n    for j in range(col):\n        axs[i, j].imshow(data[count][\"image\"][:,:,0].numpy())#.transpose([2,1, 0]))\n        count += 1","metadata":{"execution":{"iopub.status.busy":"2022-08-17T07:42:41.276744Z","iopub.execute_input":"2022-08-17T07:42:41.277267Z","iopub.status.idle":"2022-08-17T07:42:42.513393Z","shell.execute_reply.started":"2022-08-17T07:42:41.277227Z","shell.execute_reply":"2022-08-17T07:42:42.511971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}