{"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":"markdown","source":"inference\n\nhttps://www.kaggle.com/rluethy/efficientnet3d-with-one-mri-type\n\nhttps://www.kaggle.com/chumajin/brain-tumor-eda-for-starter-version\n\nhttps://www.kaggle.com/davidbroberts/determining-mr-image-planes","metadata":{}},{"cell_type":"code","source":"#defaul libraries\n#https://docs.python.org/ja/\nimport os\nimport sys\nimport io\n\nimport math\nimport random\nimport pprint\nimport time\nimport datetime\nimport typing\nimport json\nimport glob\nimport requests\nimport warnings\nimport gc\nfrom pprint import pprint\nimport re\n\nimport numpy as np #https://numpy.org/\nimport pandas as pd #https://pandas.pydata.org/\nimport sklearn #https://scikit-learn.org/stable/\n\nimport matplotlib.pyplot as plt #https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.html\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')\n%matplotlib inline\n\nimport seaborn as sns\nsns.set()\n\nfrom tqdm.auto import tqdm #https://tqdm.github.io/\n\nimport torch #https://pytorch.org/\n\nsys.path.append(\"../input/efficientnetpyttorch3d/EfficientNet-PyTorch-3D\")\nfrom efficientnet_pytorch_3d import EfficientNet3D\n\n# import torchvision\nimport cv2\nfrom PIL import Image\n\nimport pydicom","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG():\n    \n    data_path=\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/\"\n    debug=False\n    seed=0\n\n    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n    batch_size=4\n    epochs=30\n    learning_rate=0.0001\n    kFold=5\n    \n    \n    image_size=64\n\n    #高速化関連\n    #https://qiita.com/sugulu_Ogawa_ISID/items/62f5f7adee083d96a587\n\n    #GPU 遅くなるらしい↓\n    torch.backends.cudnn.deterministic = True\n\n    #イテレーションごとのnnの順伝搬および誤差関数の 計算手法がある程度一定であれば、torch.backends.cudnn.benchmark = Trueで GPU での計算が高速化\n    #torch.backends.cudnn.benchmark = False\n\n\ndef set_seed(seed=0):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    #tf.random.set_seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n\nset_seed(CFG.seed)\n\nprint(CFG.device)\nif torch.cuda.is_available():\n    print(torch.cuda.current_device())\n    print(torch.cuda.get_device_name())","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:07.5798Z","iopub.status.busy":"2021-08-25T12:07:07.578891Z","iopub.status.idle":"2021-08-25T12:07:07.586354Z","shell.execute_reply":"2021-08-25T12:07:07.586809Z","shell.execute_reply.started":"2021-08-25T12:03:59.818209Z"},"papermill":{"duration":0.040654,"end_time":"2021-08-25T12:07:07.587008","exception":false,"start_time":"2021-08-25T12:07:07.546354","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def color(string,fg='DEFAULT',bg='DEFAULT',fg_rgb=None,bg_rgb=None,style='END'):\n    colors=['BLACK','RED','GREEN','YELLOW','BLUE','PURPLE','CYAN','WHITE','8','DEFAULT']\n    styles=['END','BOLD','2','3','UNDERLINE','5','6','REVERSE','INVISIBLE','9']\n\n    fg=f'\\033[3{colors.index(fg)}m'\n    bg=f'\\033[4{colors.index(bg)}m'\n    style=f'\\033[0{styles.index(style)}m'\n\n    if fg_rgb:fg=f\"\\033[38;2;{fg_rgb[0]};{fg_rgb[1]};{fg_rgb[2]}m\"\n    if bg_rgb:bg=f\"\\033[48;2;{bg_rgb[0]};{bg_rgb[1]};{bg_rgb[2]}m\"\n\n    return style+fg+bg+str(string)+'\\033[0m'","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:07.642788Z","iopub.status.busy":"2021-08-25T12:07:07.641835Z","iopub.status.idle":"2021-08-25T12:07:07.645105Z","shell.execute_reply":"2021-08-25T12:07:07.644488Z","shell.execute_reply.started":"2021-08-25T12:03:59.835438Z"},"papermill":{"duration":0.034023,"end_time":"2021-08-25T12:07:07.645235","exception":false,"start_time":"2021-08-25T12:07:07.611212","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels={'train':None,'val':None,'test':None}\nlabels['train']=pd.read_csv(os.path.join(CFG.data_path,\"train_labels.csv\"),index_col='BraTS21ID').drop([109, 123, 709])\nlabels['test']=pd.read_csv(os.path.join(CFG.data_path,\"sample_submission.csv\"),index_col='BraTS21ID')\n\nif CFG.debug:\n    labels['train']=labels['train'].sample(frac=0.1)","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:07.700071Z","iopub.status.busy":"2021-08-25T12:07:07.699256Z","iopub.status.idle":"2021-08-25T12:07:07.729161Z","shell.execute_reply":"2021-08-25T12:07:07.729695Z","shell.execute_reply.started":"2021-08-25T12:03:59.852613Z"},"papermill":{"duration":0.060809,"end_time":"2021-08-25T12:07:07.7299","exception":false,"start_time":"2021-08-25T12:07:07.669091","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nlabels['train'], labels['val'] = sklearn.model_selection.train_test_split(\n    labels['train'],\n    test_size=0.2,\n    random_state=CFG.seed, \n    stratify=labels['train'][\"MGMT_value\"],\n)","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:07.781517Z","iopub.status.busy":"2021-08-25T12:07:07.78046Z","iopub.status.idle":"2021-08-25T12:07:07.874216Z","shell.execute_reply":"2021-08-25T12:07:07.87473Z","shell.execute_reply.started":"2021-08-25T12:03:59.885574Z"},"papermill":{"duration":0.121235,"end_time":"2021-08-25T12:07:07.874914","exception":false,"start_time":"2021-08-25T12:07:07.753679","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for phase in ['train','val','test']:\n    phase_dir='test' if phase=='test' else 'train'\n    labels[phase][\"path\"] = [\n        os.path.join(*[CFG.data_path,phase_dir,str(index).zfill(5)]) for index in labels[phase].index\n    ]","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:07.926582Z","iopub.status.busy":"2021-08-25T12:07:07.925902Z","iopub.status.idle":"2021-08-25T12:07:07.936266Z","shell.execute_reply":"2021-08-25T12:07:07.936752Z","shell.execute_reply.started":"2021-08-25T12:03:59.920277Z"},"papermill":{"duration":0.037833,"end_time":"2021-08-25T12:07:07.936958","exception":false,"start_time":"2021-08-25T12:07:07.899125","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def BraTS21ID_phase(BraTS21ID):\n    if BraTS21ID in labels['train'].index:return 'train'\n    if BraTS21ID in labels['val'].index  :return 'val'\n    if BraTS21ID in labels['test'].index :return 'test'\n\nBraTS21ID_phase(102)","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:07.991218Z","iopub.status.busy":"2021-08-25T12:07:07.990584Z","iopub.status.idle":"2021-08-25T12:07:07.999097Z","shell.execute_reply":"2021-08-25T12:07:07.999549Z","shell.execute_reply.started":"2021-08-25T12:03:59.935832Z"},"papermill":{"duration":0.038657,"end_time":"2021-08-25T12:07:07.999725","exception":false,"start_time":"2021-08-25T12:07:07.961068","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['train']","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:08.051729Z","iopub.status.busy":"2021-08-25T12:07:08.051002Z","iopub.status.idle":"2021-08-25T12:07:08.068398Z","shell.execute_reply":"2021-08-25T12:07:08.068888Z","shell.execute_reply.started":"2021-08-25T12:03:59.9571Z"},"papermill":{"duration":0.045345,"end_time":"2021-08-25T12:07:08.069072","exception":false,"start_time":"2021-08-25T12:07:08.023727","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5, 5))\nsns.countplot(data=labels['train'], x=\"MGMT_value\");","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:08.121609Z","iopub.status.busy":"2021-08-25T12:07:08.120975Z","iopub.status.idle":"2021-08-25T12:07:08.306132Z","shell.execute_reply":"2021-08-25T12:07:08.30658Z","shell.execute_reply.started":"2021-08-25T12:03:59.982842Z"},"papermill":{"duration":0.212899,"end_time":"2021-08-25T12:07:08.306746","exception":false,"start_time":"2021-08-25T12:07:08.093847","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset(torch.utils.data.Dataset):\n    def __init__(self,labels,phase='train',frame_per_brain=None):\n        self.labels=labels\n        self.phase=phase #train/val/test\n        self.remove_black_boundary=True\n        self.frame_per_brain=frame_per_brain\n        \n    def __len__(self):\n        return len(self.labels)\n    \n    def images_path(self,BraTS21ID):\n        \"\"\"BraTS21ID に対応する患者のMRI画像のpathをまとめた DataFrame を返す\"\"\"\n        df={\"FLAIR\":None,\"T1w\":None,\"T1wCE\":None,\"T2w\":None}\n        for mri_type in [\"FLAIR\",\"T1w\",\"T1wCE\",\"T2w\"]:\n            #print(os.path.join(self.labels.loc[BraTS21ID].path,mri_type))\n            folder_path=os.path.join(self.labels.loc[BraTS21ID].path,mri_type)\n            dcm_files = os.listdir(folder_path)\n            tmp_df = pd.DataFrame(\n                {'image_path':[os.path.join(folder_path,dcm_file) for dcm_file in dcm_files]},\n                index=[int(re.search('(\\d+)',a).group()) for a in dcm_files],\n            )\n            tmp_df.sort_index(inplace=True)\n            df[mri_type]=tmp_df\n        return df\n    \n    def load_dicom(self,path,voi_lut=True,remove_black_boundary=True):\n        \"\"\"path で指定した MRI画像 を返す\"\"\"\n        dicom = pydicom.read_file(path)\n        data = dicom.pixel_array\n        \n        # VOI LUT (if available by DICOM device) is used to\n        # transform raw DICOM data to \"human-friendly\" view\n        if voi_lut:data = pydicom.pixel_data_handlers.util.apply_voi_lut(data, dicom)#??????????????\n        \n        # depending on this value, X-ray may look inverted - fix that:\n        if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n            print('inverted')\n            data = np.amax(data) - data\n        \n        data=data.astype(np.float32)\n        data = data-np.min(data)\n        if np.max(data)!=0 : data/=np.max(data)\n        #data = (data * 255).astype(np.uint8)\n        return cv2.resize(data, (CFG.image_size, CFG.image_size))\n\n    def __getitem__(self,BraTS21ID):\n        #print(f'fetching:{BraTS21ID}')\n        df=self.images_path(BraTS21ID)\n        mri_images={\n            \"FLAIR\":np.ndarray((0,CFG.image_size,CFG.image_size),dtype=np.int),\n            \"T1w\"  :np.ndarray((0,CFG.image_size,CFG.image_size),dtype=np.int),\n            \"T1wCE\":np.ndarray((0,CFG.image_size,CFG.image_size),dtype=np.int),\n            \"T2w\"  :np.ndarray((0,CFG.image_size,CFG.image_size),dtype=np.int)\n        }\n        #3次元ndarrayに画像を一枚ずつ追加\n        for mri_type in [\"FLAIR\",\"T1w\",\"T1wCE\",\"T2w\"]:\n            \n            for image_path in df[mri_type].image_path:\n                image=self.load_dicom(image_path)\n                image=np.reshape(image,(1,CFG.image_size,CFG.image_size)) # reshapeで追加先と同じ次元数にする\n                mri_images[mri_type] = np.append(mri_images[mri_type], np.atleast_3d(image), axis=0)\n                \n            #真っ黒な画像　＆　周りの黒い領域を消す\n            if self.remove_black_boundary:\n                t,x,y=np.where(mri_images[mri_type]>0)\n                ###train 123 T1w T2w 真っ暗\n                if (len(t) == 0 or len(x) == 0 or len(y) == 0) : pass\n                #if self.frame_per_brain and (np.max(t) - np.min(t)) < self.frame_per_brain : pass\n                #if (np.max(x) - np.min(x)) < 10 : pass\n                #if (np.max(y) - np.min(y)) < 10 : pass\n                mri_images[mri_type]=mri_images[mri_type][np.min(t):np.max(t),np.min(x):np.max(x),np.min(y):np.max(y)]\n                \n                #xかyに合わせて拡大\n                t,x,y=np.shape(mri_images[mri_type])\n                enlarged_mri_images = np.zeros((t,int(x/max(x,y)*CFG.image_size),int(y/max(x,y)*CFG.image_size)))\n                for idx,image in enumerate(mri_images[mri_type]):\n                    enlarged_mri_images[idx,:,:] = cv2.resize(image.astype('float32') ,(int(y/max(x,y)*CFG.image_size),int(x/max(x,y)*CFG.image_size)),interpolation=cv2.INTER_CUBIC)\n                mri_images[mri_type]=enlarged_mri_images\n                \n                #切り取ったあと周りを追加して256×256に\n                _,x,y=np.shape(mri_images[mri_type])\n                padding=[(0,0),((CFG.image_size-x)//2,(CFG.image_size-x+1)//2),((CFG.image_size-y)//2,(CFG.image_size-y+1)//2)]#paddingの大きさ\n                mri_images[mri_type]=np.pad(mri_images[mri_type],padding,'constant')\n            \n            #画像枚数をframe_per_brainに変換\n            if self.frame_per_brain:\n                modified_mri_images = np.zeros((self.frame_per_brain,CFG.image_size,CFG.image_size))\n                \n                for i in range(self.frame_per_brain):\n                    frame=(t-1)/(self.frame_per_brain-1)*i\n                    rate=frame-int(frame)\n                    #print(i,frame,rate)\n                    \n                    if rate==0:modified_mri_images[i,:,:]=mri_images[mri_type][int(frame)]\n                    else:modified_mri_images[i,:,:]=mri_images[mri_type][int(frame)]*(1-rate)+mri_images[mri_type][int(frame)+1]*rate\n                \n                mri_images[mri_type]=modified_mri_images\n                \n            \n            #print(np.shape(mri_images[mri_type]))\n        \n        return mri_images,self.labels.MGMT_value.loc[BraTS21ID]","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:08.361708Z","iopub.status.busy":"2021-08-25T12:07:08.360993Z","iopub.status.idle":"2021-08-25T12:07:08.389977Z","shell.execute_reply":"2021-08-25T12:07:08.389367Z","shell.execute_reply.started":"2021-08-25T12:04:00.123782Z"},"papermill":{"duration":0.057722,"end_time":"2021-08-25T12:07:08.390117","exception":false,"start_time":"2021-08-25T12:07:08.332395","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset={\n    'train':Dataset(labels['train'],'train',frame_per_brain=CFG.image_size),\n    'val':Dataset(labels['val'],'val',frame_per_brain=CFG.image_size),\n    'test':Dataset(labels['test'],'test',frame_per_brain=CFG.image_size),\n}","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:08.446488Z","iopub.status.busy":"2021-08-25T12:07:08.445814Z","iopub.status.idle":"2021-08-25T12:07:08.448591Z","shell.execute_reply":"2021-08-25T12:07:08.449091Z","shell.execute_reply.started":"2021-08-25T12:04:00.149361Z"},"papermill":{"duration":0.03368,"end_time":"2021-08-25T12:07:08.449262","exception":false,"start_time":"2021-08-25T12:07:08.415582","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Sampler(torch.utils.data.Sampler):\n    def __init__(self, indice):\n        self.indice=indice\n    def __iter__(self):\n        for index in self.indice:yield index\n\ndef collate_fn(batch):\n    #print(\"collate_fn\")\n    mri_images,MGMT_value= list(zip(*batch))\n    #mri_images[0][\"FLAIR\"]\n    all_mri_images=[]#(4,frame_per_brain,256,256)のtensorのリスト\n    \n    for idx in range(len(MGMT_value)):\n        mri_image_tensor=[]\n        for mri_type in [\"FLAIR\",\"T1w\",\"T1wCE\",\"T2w\"]:\n            mri_image_tensor.append(torch.tensor(mri_images[idx][mri_type]))\n        all_mri_images.append(torch.stack(mri_image_tensor))\n\n    batch_image=all_mri_images[0].unsqueeze(0) if len(all_mri_images)==1 else torch.stack(all_mri_images)\n    #(batch_size,4,frame_per_brain,256,256)\n    return batch_image.float(),torch.tensor(MGMT_value).float()\n\ndef set_dataloader():\n    global dataloader\n    dataloader={\n        'train':\n        torch.utils.data.DataLoader(\n            dataset['train'],\n            batch_size=CFG.batch_size,\n            sampler=Sampler(list(labels['train'].index)),\n            collate_fn=collate_fn,\n            num_workers=2,#os.cpu_count(),\n            pin_memory=True\n        ),\n        'val':\n        torch.utils.data.DataLoader(\n            dataset['val'],\n            batch_size=CFG.batch_size,\n            sampler=Sampler(list(labels['val'].index)),\n            collate_fn=collate_fn,\n            num_workers=2,#os.cpu_count(),\n            pin_memory=True\n        ),\n        'test':\n        torch.utils.data.DataLoader(\n            dataset['test'],\n            batch_size=CFG.batch_size,\n            sampler=Sampler(list(labels['test'].index)),\n            collate_fn=collate_fn,\n            num_workers=2,#os.cpu_count(),\n            pin_memory=True\n        )\n    }\nset_dataloader()","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:08.513968Z","iopub.status.busy":"2021-08-25T12:07:08.513261Z","iopub.status.idle":"2021-08-25T12:07:08.516331Z","shell.execute_reply":"2021-08-25T12:07:08.515766Z","shell.execute_reply.started":"2021-08-25T12:04:00.172432Z"},"papermill":{"duration":0.041133,"end_time":"2021-08-25T12:07:08.516481","exception":false,"start_time":"2021-08-25T12:07:08.475348","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images,MGMT_value=dataset['test'][13]","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:08.571686Z","iopub.status.busy":"2021-08-25T12:07:08.571055Z","iopub.status.idle":"2021-08-25T12:07:18.106984Z","shell.execute_reply":"2021-08-25T12:07:18.106193Z","shell.execute_reply.started":"2021-08-25T12:04:00.192156Z"},"papermill":{"duration":9.565606,"end_time":"2021-08-25T12:07:18.10716","exception":false,"start_time":"2021-08-25T12:07:08.541554","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(images['FLAIR'][30])","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:18.191718Z","iopub.status.busy":"2021-08-25T12:07:18.190602Z","iopub.status.idle":"2021-08-25T12:07:18.378535Z","shell.execute_reply":"2021-08-25T12:07:18.379043Z","shell.execute_reply.started":"2021-08-25T12:04:03.970035Z"},"papermill":{"duration":0.243947,"end_time":"2021-08-25T12:07:18.37923","exception":false,"start_time":"2021-08-25T12:07:18.135283","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot(mri_images):\n    max_len=max([\n        len(mri_images[\"FLAIR\"]),\n        len(mri_images[\"T1w\"]),\n        len(mri_images[\"T1wCE\"]),\n        len(mri_images[\"T2w\"])\n    ])\n\n    fig, axes = plt.subplots(max_len,4,figsize=(2*4,2*max_len))\n    \n    for col,mri_type in enumerate([\"FLAIR\",\"T1w\",\"T1wCE\",\"T2w\"]):\n    \n        for row in tqdm( range(len(mri_images[mri_type])) ):\n            img=mri_images[mri_type][row,:,:]\n            fig.add_subplot(max_len,4,4*row+col+1)\n            plt.imshow(img)\n            plt.axis(\"off\")\n            \n            if row==0:plt.title(mri_type,fontsize=9)\n            if col==0:plt.ylabel(\"row=\" + str(row),fontsize=9)\n    \n    plt.show()\n\ndef create_animation(mri_images,mri_type):\n    images=[]\n    for image in mri_images[mri_type]:\n        if image.max() != 0:images.append(image)\n\n    fig = plt.figure(figsize=(6, 6))\n    plt.axis('off')\n    im = plt.imshow(images[0], cmap=\"gray\")\n\n    def animate_func(i):\n        im.set_array(images[i])\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames = len(images), interval = 1000//24)","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:18.445478Z","iopub.status.busy":"2021-08-25T12:07:18.444244Z","iopub.status.idle":"2021-08-25T12:07:18.447814Z","shell.execute_reply":"2021-08-25T12:07:18.44719Z","shell.execute_reply.started":"2021-08-25T12:04:04.142197Z"},"papermill":{"duration":0.041931,"end_time":"2021-08-25T12:07:18.447965","exception":false,"start_time":"2021-08-25T12:07:18.406034","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(images)","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:07:18.513705Z","iopub.status.busy":"2021-08-25T12:07:18.506639Z","iopub.status.idle":"2021-08-25T12:08:12.054769Z","shell.execute_reply":"2021-08-25T12:08:12.055585Z","shell.execute_reply.started":"2021-08-25T12:04:04.152853Z"},"papermill":{"duration":53.581293,"end_time":"2021-08-25T12:08:12.05585","exception":false,"start_time":"2021-08-25T12:07:18.474557","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#images['FLAIR']=images['FLAIR'].transpose(1,0,2)\ncreate_animation(images,\"FLAIR\")","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:08:12.283506Z","iopub.status.busy":"2021-08-25T12:08:12.282779Z","iopub.status.idle":"2021-08-25T12:08:15.971383Z","shell.execute_reply":"2021-08-25T12:08:15.971917Z","shell.execute_reply.started":"2021-08-25T12:04:55.309723Z"},"papermill":{"duration":3.802119,"end_time":"2021-08-25T12:08:15.972122","exception":false,"start_time":"2021-08-25T12:08:12.170003","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images['FLAIR']\n#images['FLAIR'][30][100]","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:08:16.24379Z","iopub.status.busy":"2021-08-25T12:08:16.242894Z","iopub.status.idle":"2021-08-25T12:08:16.251019Z","shell.execute_reply":"2021-08-25T12:08:16.251529Z","shell.execute_reply.started":"2021-08-25T12:04:59.34004Z"},"papermill":{"duration":0.143597,"end_time":"2021-08-25T12:08:16.251698","exception":false,"start_time":"2021-08-25T12:08:16.108101","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class Model(torch.nn.Module):\n#     def __init__(self):\n#         super().__init__()\n#         self.net = EfficientNet3D.from_name(\"efficientnet-b0\", override_params={'num_classes': 2}, in_channels=1)\n#         n_features = self.net._fc.in_features\n#         self.net._fc = torch.nn.Linear(in_features=n_features, out_features=1, bias=True)\n#         #self.sigmoid=torch.nn.Sigmoid()\n        \n    \n#     def forward(self, x):\n        \n#         #x (batch_size,channel,x,y,z)?\n#         out = self.net(x)\n#         return out#self.sigmoid(out)\n\n# model=Model()\nmodel=EfficientNet3D.from_name(\"efficientnet-b0\", override_params={'num_classes': 2}, in_channels=1)\nn_features = model._fc.in_features\nmodel._fc = torch.nn.Linear(in_features=n_features, out_features=1, bias=True)\nmodel.to(CFG.device)\n#model","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:08:16.517904Z","iopub.status.busy":"2021-08-25T12:08:16.517236Z","iopub.status.idle":"2021-08-25T12:08:16.637886Z","shell.execute_reply":"2021-08-25T12:08:16.637266Z","shell.execute_reply.started":"2021-08-25T12:04:59.351197Z"},"papermill":{"duration":0.255358,"end_time":"2021-08-25T12:08:16.638037","exception":false,"start_time":"2021-08-25T12:08:16.382679","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # del model\n# # torch.cuda.empty_cache()\n# # gc.collect()\n\n# class Model(torch.nn.Module):\n\n\n#     def __init__(self):\n#         super().__init__()\n#         self.conv=torch.nn.Sequential(\n#             torch.nn.Conv3d(1,1,3,stride=2),\n#             torch.nn.ReLU(),\n#             torch.nn.Conv3d(1,2,5,stride=2),\n#             torch.nn.ReLU(),\n#             torch.nn.Conv3d(2,2,7,stride=2),\n#             torch.nn.ReLU(),\n#             torch.nn.Conv3d(2,2,7),\n#             torch.nn.ReLU(),\n#             torch.nn.Conv3d(2,4,3),\n#             torch.nn.ReLU(),\n#         )\n\n#         self.fc=torch.nn.Sequential(\n#             torch.nn.Linear(256,50),\n#             torch.nn.ReLU(),\n#             torch.nn.Linear(50,10),\n#             torch.nn.ReLU(),\n#             torch.nn.Linear(10,1),\n#         )\n        \n#     def extract_feature(self,x):\n#         return self.conv(x)\n    \n#     def forward(self,x):\n#         x=self.conv(x)\n#         x=x.flatten(start_dim=1)\n#         x=self.fc(x)\n        \n#         return x\n    \n# model=Model()\n# model.to(CFG.device)","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:08:16.909244Z","iopub.status.busy":"2021-08-25T12:08:16.908559Z","iopub.status.idle":"2021-08-25T12:08:16.91217Z","shell.execute_reply":"2021-08-25T12:08:16.912721Z","shell.execute_reply.started":"2021-08-25T12:04:59.503146Z"},"papermill":{"duration":0.141326,"end_time":"2021-08-25T12:08:16.912883","exception":false,"start_time":"2021-08-25T12:08:16.771557","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.135192,"end_time":"2021-08-25T12:08:17.179757","exception":false,"start_time":"2021-08-25T12:08:17.044565","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #model.eval()\n\n# #torch.randn(1,1,64,512,512).dtype\n\n# input_image=torch.tensor(images['T1w'],dtype=torch.float32).to(CFG.device).unsqueeze(0).unsqueeze(0)\n# input_image.shape\n# #o=model.extract_feature(input_image)\n# plt.imshow(input_image[0,0,30,:,:])\n\n# model.train()\n# input_image,target=dataset['train'][3]\n# input_image=input_image['FLAIR']\n# input_image=torch.tensor(input_image,dtype=torch.float32).unsqueeze(0).unsqueeze(0)\n# input_image.shape\n\n# input_image=torch.randn(2,1,64,64,64).float()\n# print(input_image.shape[0])\n# o=model(input_image)\n# o.shape\n\n# # a=model.extract_features(input_image)\n# # print(a.shape)\n# # plt.imshow(a[0,4,0,:,:].detach().numpy())\n\n# # print(model(torch.randn(1,1,64,512,512).to(CFG.device)))","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:08:17.447675Z","iopub.status.busy":"2021-08-25T12:08:17.446599Z","iopub.status.idle":"2021-08-25T12:08:17.448896Z","shell.execute_reply":"2021-08-25T12:08:17.448372Z","shell.execute_reply.started":"2021-08-25T12:04:59.510051Z"},"papermill":{"duration":0.138613,"end_time":"2021-08-25T12:08:17.449061","exception":false,"start_time":"2021-08-25T12:08:17.310448","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_val_test(model,dataloader,phase,optimizer=None,scheduler=None,scaler=None):# モデルを学習させる関数\n    \n    #assert((phase=='train')==bool(optimizer))#学習時にoptimizer必須\n    \n    model.train() if phase=='train' else torch.no_grad()#model.eval()   # モデルのモード\n    \n    y=[]\n    preds=[]\n    losses=[]\n    scores=[]\n    \n#     print('preprocess images')\n#     #preprocessed=list(dataloader[phase])\n#     print('end')\n    \n    # データローダーからミニバッチを取り出すループ\n    for images,targets in tqdm(dataloader[phase]):\n        \n        if images.shape[0]==1:\n            #なぜかバッチサイズが1になるとバグる\n            print('batchsize==1')\n            continue\n        \n        # optimizerを初期化\n        if phase=='train':optimizer.zero_grad()\n       \n        # 順伝搬（forward）計算\n        with torch.set_grad_enabled(phase=='train'):\n\n#             with torch.cuda.amp.autocast():#HalfTensorになる？\n\n            # non_blocking=TrueでPinned MemoryからGPUに転送中もCPUが動作できるらしい。\n            images = images[:,0,:,:,:].unsqueeze(1).to(CFG.device,non_blocking=True)\n            targets = targets.to(CFG.device,non_blocking=True)\n\n            outputs = model(images).squeeze(1)\n#             print(targets)\n#             print(torch.nn.Sigmoid()(outputs))\n            \n            if phase!='test':\n                loss_val = loss_fun(outputs, targets)  # 損失を計算\n                losses.append(loss_val.item())\n\n                #score.append(score)\n\n                # 訓練時はバックプロパゲーション\n                if phase == 'train':\n                    loss_val.backward()\n                    optimizer.step()\n            \n\n        preds.extend(outputs.detach().cpu().numpy())\n        y.extend(targets.detach().cpu().numpy())\n    \n        \n        \n        del images,targets,outputs\n        if phase!='test':del loss_val\n        torch.cuda.empty_cache()\n        gc.collect()\n    \n    return preds,losses,scores","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:08:17.715674Z","iopub.status.busy":"2021-08-25T12:08:17.714652Z","iopub.status.idle":"2021-08-25T12:08:17.727387Z","shell.execute_reply":"2021-08-25T12:08:17.726807Z","shell.execute_reply.started":"2021-08-25T12:04:59.529693Z"},"papermill":{"duration":0.147746,"end_time":"2021-08-25T12:08:17.727525","exception":false,"start_time":"2021-08-25T12:08:17.579779","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_fun=torch.nn.BCEWithLogitsLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=CFG.learning_rate)#, momentum=0.9)","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:08:17.998573Z","iopub.status.busy":"2021-08-25T12:08:17.997813Z","iopub.status.idle":"2021-08-25T12:08:18.00064Z","shell.execute_reply":"2021-08-25T12:08:18.001136Z","shell.execute_reply.started":"2021-08-25T12:04:59.557459Z"},"papermill":{"duration":0.14281,"end_time":"2021-08-25T12:08:18.001323","exception":false,"start_time":"2021-08-25T12:08:17.858513","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(3):\n    preds,losses,scores=train_val_test(model,dataloader,'train',optimizer)\n    plt.plot(losses)\n    plt.show()","metadata":{"execution":{"iopub.execute_input":"2021-08-25T12:08:18.274073Z","iopub.status.busy":"2021-08-25T12:08:18.273318Z","iopub.status.idle":"2021-08-25T13:38:14.723286Z","shell.execute_reply":"2021-08-25T13:38:14.722623Z","shell.execute_reply.started":"2021-08-25T12:04:59.591771Z"},"papermill":{"duration":5396.590328,"end_time":"2021-08-25T13:38:14.723497","exception":false,"start_time":"2021-08-25T12:08:18.133169","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model, 'model.pt')","metadata":{"execution":{"iopub.execute_input":"2021-08-25T13:38:15.009152Z","iopub.status.busy":"2021-08-25T13:38:15.008462Z","iopub.status.idle":"2021-08-25T13:38:15.076411Z","shell.execute_reply":"2021-08-25T13:38:15.075806Z"},"papermill":{"duration":0.213526,"end_time":"2021-08-25T13:38:15.076571","exception":false,"start_time":"2021-08-25T13:38:14.863045","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#set_dataloader()\npreds,losses,scores=train_val_test(model,dataloader,'test')","metadata":{"execution":{"iopub.execute_input":"2021-08-25T13:38:15.363044Z","iopub.status.busy":"2021-08-25T13:38:15.36234Z","iopub.status.idle":"2021-08-25T13:44:05.849394Z","shell.execute_reply":"2021-08-25T13:44:05.848737Z"},"papermill":{"duration":350.633708,"end_time":"2021-08-25T13:44:05.84956","exception":false,"start_time":"2021-08-25T13:38:15.215852","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#preds=[1.0]*87\nsubmission=labels['test'].drop('path',axis=1)\n\nsigmoid=torch.nn.Sigmoid()\nsubmission['MGMT_value']=sigmoid(torch.tensor(preds))\nsubmission.to_csv(\"submission.csv\",index='BraTS21ID')\nsubmission","metadata":{"execution":{"iopub.execute_input":"2021-08-25T13:44:06.141481Z","iopub.status.busy":"2021-08-25T13:44:06.14073Z","iopub.status.idle":"2021-08-25T13:44:06.167304Z","shell.execute_reply":"2021-08-25T13:44:06.166762Z"},"papermill":{"duration":0.177622,"end_time":"2021-08-25T13:44:06.167465","exception":false,"start_time":"2021-08-25T13:44:05.989843","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}