{"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 segmentation_models_pytorch -q\n!pip install timm -q","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-27T11:27:54.343718Z","iopub.execute_input":"2021-08-27T11:27:54.344169Z","iopub.status.idle":"2021-08-27T11:28:14.286778Z","shell.execute_reply.started":"2021-08-27T11:27:54.344085Z","shell.execute_reply":"2021-08-27T11:28:14.285687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nimport os\nimport cv2\nfrom glob import glob\nimport numpy as np\nimport pandas as pd\nimport re\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-08-27T11:28:14.288435Z","iopub.execute_input":"2021-08-27T11:28:14.288736Z","iopub.status.idle":"2021-08-27T11:28:15.684308Z","shell.execute_reply.started":"2021-08-27T11:28:14.288704Z","shell.execute_reply":"2021-08-27T11:28:15.683485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')\ntest=sample_submission\ntest['BraTS21ID5'] = [format(x, '05d') for x in test.BraTS21ID]\ntest.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T11:28:15.685737Z","iopub.execute_input":"2021-08-27T11:28:15.685997Z","iopub.status.idle":"2021-08-27T11:28:15.721999Z","shell.execute_reply.started":"2021-08-27T11:28:15.685972Z","shell.execute_reply":"2021-08-27T11:28:15.721041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def avg_img(img_size, img_list):\n    n = len(img_list)\n    arr = np.zeros((img_size, img_size), np.float32)\n    \n    for img_array in img_list:\n        arr = arr + img_array/n\n\n    arr=np.array(np.round(arr),dtype=np.uint8)\n\n    return arr\n\n\ndef load_slices_3d(path_to_scan_dir, num_imgs=16, img_size=256):\n    \"\"\"\n        root_dir: the path to the dataset\n        scan_id: (integer/string) the id of the scan\n        num_imgs: (integer) the number of the slices to get from the scan returns 3d tensor of shape (num_imgs, img_size, img_size)        \n    \"\"\"\n    path_to_scan_dir = os.path.join(path_to_scan_dir, \"*\")\n    slices_path = sorted(glob(path_to_scan_dir), key=lambda var:[int(x) if x.isdigit() else x for x in re.findall(r'[^0-9]|[0-9]+', var)])\n        \n    slices = [cv2.imread(f, cv2.IMREAD_GRAYSCALE) for f in slices_path]\n    slices = [cv2.resize(img, (img_size, img_size)) for img in slices]\n        \n    chunk_size = len(slices) // num_imgs #if chunk size if \n    \n    averaged_slices = []\n    \n    for i in range(num_imgs):\n        \n        img_list = slices[i*chunk_size: (i+1)*chunk_size]\n        averaged_slices.append(avg_img(img_size, img_list))\n\n    return np.array(averaged_slices)","metadata":{"execution":{"iopub.status.busy":"2021-08-27T11:39:46.907328Z","iopub.execute_input":"2021-08-27T11:39:46.907879Z","iopub.status.idle":"2021-08-27T11:39:46.916624Z","shell.execute_reply.started":"2021-08-27T11:39:46.907816Z","shell.execute_reply":"2021-08-27T11:39:46.915917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from glob import glob\nfrom tqdm import tqdm\nmri_types = ['T1w','T1wCE','T2w','FLAIR']\nfor mri_type in mri_types:\n    \n    scan_paths = sorted(glob(\"../input/rsna-miccai-png/test/*\"))\n    scan_paths = [i+\"/\"+mri_type for i in scan_paths]\n\n    for scan_path in tqdm(scan_paths): #../input/rsna-miccai-png/test/00001/FLAIR\n\n        img = load_slices_3d(scan_path, num_imgs=1, img_size=384)\n        folder = scan_path.split('/')[4]\n        mr_type = scan_path.split('/')[5]\n        cv2.imwrite(f'{folder}_{mr_type}.png', img[0])\n        ","metadata":{"execution":{"iopub.status.busy":"2021-08-27T11:59:26.025330Z","iopub.execute_input":"2021-08-27T11:59:26.025795Z","iopub.status.idle":"2021-08-27T12:04:33.860215Z","shell.execute_reply.started":"2021-08-27T11:59:26.025767Z","shell.execute_reply":"2021-08-27T12:04:33.859201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(cv2.imread('./00702_T1wCE.png'))","metadata":{"execution":{"iopub.status.busy":"2021-08-27T12:09:06.829151Z","iopub.execute_input":"2021-08-27T12:09:06.829680Z","iopub.status.idle":"2021-08-27T12:09:07.005880Z","shell.execute_reply.started":"2021-08-27T12:09:06.829648Z","shell.execute_reply":"2021-08-27T12:09:07.004837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(cv2.imread('./00592_T1wCE.png'))","metadata":{"execution":{"iopub.status.busy":"2021-08-27T12:08:47.651776Z","iopub.execute_input":"2021-08-27T12:08:47.652306Z","iopub.status.idle":"2021-08-27T12:08:47.822292Z","shell.execute_reply.started":"2021-08-27T12:08:47.652272Z","shell.execute_reply":"2021-08-27T12:08:47.821642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(cv2.imread('./00256_T1wCE.png'))","metadata":{"execution":{"iopub.status.busy":"2021-08-27T12:05:47.846561Z","iopub.execute_input":"2021-08-27T12:05:47.846922Z","iopub.status.idle":"2021-08-27T12:05:48.019677Z","shell.execute_reply.started":"2021-08-27T12:05:47.846892Z","shell.execute_reply":"2021-08-27T12:05:48.018681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('OK')","metadata":{"execution":{"iopub.status.busy":"2021-08-27T12:12:18.197980Z","iopub.execute_input":"2021-08-27T12:12:18.198333Z","iopub.status.idle":"2021-08-27T12:12:18.203218Z","shell.execute_reply.started":"2021-08-27T12:12:18.198304Z","shell.execute_reply":"2021-08-27T12:12:18.202484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}