{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        #print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-12T05:34:56.495758Z","iopub.execute_input":"2021-10-12T05:34:56.496081Z","iopub.status.idle":"2021-10-12T05:34:56.501551Z","shell.execute_reply.started":"2021-10-12T05:34:56.496054Z","shell.execute_reply":"2021-10-12T05:34:56.500382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport os\nimport re\nfrom skimage import io\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport matplotlib as mpl\n\n#filepath='../data/rsna2png/train/00000/FLAIR/'\ntrainpath = '../input/rsna-miccai-png/train/'\ntestpath = '../input/rsna-miccai-png/test/'\nsavepath = './data3d/train/'\n\n# 转三维数组\ndef matrix3d(filepath):\n    files=os.listdir(filepath)\n    files.sort(key=lambda x:re.findall(\"\\d+\",x)[0])\n    count=0\n    im3d=np.zeros(shape=(len(files),io.imread(os.path.join(filepath,files[0])).shape[0],\n                io.imread(os.path.join(filepath,files[0])).shape[1]),\n                dtype='uint16')\n\n\n    count=0\n    for file_ in files:\n        im2d=io.imread(os.path.join(filepath,file_))\n        im3d[count]=im2d\n        count+=1\n    \n    return im3d\n\n# 存储为.npy文件，格式为:样本号_mri(shape).npy，例：0000_FLAIR(288,512,512).npy\ndef saveFile(data,savepath,name):\n    np.save(savepath+name+\".npy\",data)\n\n    print(\"sample: {0} save done\".format(name))\n    print(\"shape: {0}\".format(str(data.shape)))\n\nif __name__ == \"__main__\":\n    #data = matrix3d(filepath)\n    #saveFile(data,savepath,\"0000_FLAIR.npy\")\n    \n    samples=os.listdir(trainpath)\n    for sample in samples:\n        mris=os.listdir(trainpath+sample+'/')\n        for mri in mris:\n            filepath = trainpath+sample+'/'+mri+'/'\n            data = matrix3d(filepath)\n            saveFile(data,savepath,sample+'_'+mri)\n    \n    # 统计\n    #saves = os.listdir(savepath)","metadata":{"execution":{"iopub.status.busy":"2021-08-18T05:16:52.091569Z","iopub.execute_input":"2021-08-18T05:16:52.09189Z","iopub.status.idle":"2021-08-18T05:16:52.101344Z","shell.execute_reply.started":"2021-08-18T05:16:52.09186Z","shell.execute_reply":"2021-08-18T05:16:52.100183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch import nn \nimport numpy as np\n# 用于将图片读取为ndarray格式\nfrom skimage import io\nfrom torchvision import utils as vutils\nfrom PIL import Image\nfrom matplotlib import pyplot as plt\nimport os\nimport re\n\ntrainpath = '../input/rsna-miccai-png/train/'\ntestpath = '../input/rsna-miccai-png/test/'\nsavepath = './data3d/train/'\n\n#############################################################################################################\n#############################################################################################################\n#   下采样模块本文采用了通用卷积进行搭建，在语义分割中用的较多的空洞卷积，以及残差结构对网络性能都是有提升效果的。\n#                BN层的作用是网络节点输出更加稳定，一定程度上能够缓解梯度爆炸和梯度消失问题。\n#       激活函数这里使用了Relu6，同样也是考虑到了数据分布，因为通常图片数据在进入网络模型前会进行标准化处理。\n#############################################################################################################\n#############################################################################################################\n#                                          下采样模块\n#############################################################################################################\nclass block_down(nn.Module):\n    \n    def __init__(self,inp_channel,out_channel):\n        super(block_down,self).__init__()\n        self.conv1=nn.Conv2d(inp_channel,out_channel,3,padding=1)\n        self.conv2=nn.Conv2d(out_channel,out_channel,3,padding=1)\n        self.bn=nn.BatchNorm2d(out_channel)\n        self.relu=nn.ReLU6(inplace=True)\n        \n    def forward(self,x):\n        x=self.conv1(x)\n        x=self.bn(x)\n        x=self.relu(x)\n        x=self.conv2(x)\n        x=self.bn(x)\n        x=self.relu(x)\n        return x\n\n#############################################################################################################\n#                                             上采样模块\n#############################################################################################################\nclass block_up(nn.Module):\n    \n    def __init__(self,inp_channel,out_channel,y):\n        super(block_up,self).__init__()\n        # 转置卷积\n        self.up=nn.ConvTranspose2d(inp_channel,out_channel,2,stride=2)\n        self.conv1=nn.Conv2d(inp_channel,out_channel,3,padding=1)\n        self.conv2=nn.Conv2d(out_channel,out_channel,3,padding=1)\n        self.bn=nn.BatchNorm2d(out_channel)\n        self.relu=nn.ReLU6(inplace=True)\n        self.y=y\n \n    def forward(self,x):\n        x=self.up(x)\n        x=torch.cat([x,self.y],dim=1)\n        x=self.conv1(x)\n        x=self.bn(x)\n        x=self.relu(x)\n        x=self.conv2(x)\n        x=self.bn(x)\n        x=self.relu(x)\n        return x\n\n# 用模块搭建整体网络\nclass U_net(nn.Module):\n    \n    def __init__(self,out_channel,input_channel):\n        super(U_net,self).__init__()\n        self.out=nn.Conv2d(64,out_channel,1)\n        self.maxpool=nn.MaxPool2d(2)\n        self.input_channel = input_channel\n        \n    def forward(self,x):\n        # 此处修改图像通道数\n        block1=block_down(self.input_channel,64)\n        x1_use=block1(x)\n        x1=self.maxpool(x1_use)\n        block2=block_down(64,128)\n        x2_use=block2(x1)\n        x2=self.maxpool(x2_use)\n        block3=block_down(128,256)\n        x3_use=block3(x2)\n        x3=self.maxpool(x3_use)\n        block4=block_down(256,512)\n        x4_use=block4(x3)\n        x4=self.maxpool(x4_use)\n        block5=block_down(512,1024)\n        x5=block5(x4)\n \n        block6=block_up(1024,512,x4_use)\n        x6=block6(x5)\n        block7=block_up(512,256,x3_use)\n        x7=block7(x6)\n        block8=block_up(256,128,x2_use)\n        x8=block8(x3_use)\n        block9=block_up(128,64,x1_use)\n        x9=block9(x8)\n        x10=self.out(x9)\n        out=nn.Softmax2d()(x10)\n        return out\n\nclass Plotfig():\n\n    def __init__(self,data):\n        self.data = data\n\n    def plot(self):\n        fig = plt.figure()\n        ax = fig.add_subplot(111)\n        im = ax.imshow(self.data, cmap=plt.cm.hot_r)\n        plt.colorbar(im)\n        plt.show()\n\nclass Reshape():\n    \n    def __init__(self,path):\n        self.path = path\n    \n    def matrix3d(self):\n        files=os.listdir(self.path)\n        files.sort(key=lambda x:re.findall(\"\\d+\",x)[0])\n        count=0\n        im3d=np.zeros(shape=(len(files),io.imread(os.path.join(self.path,files[0])).shape[0],\n                    io.imread(os.path.join(self.path,files[0])).shape[1]),\n                    dtype='uint16')\n        count=0\n        for file_ in files:\n            im2d=io.imread(os.path.join(self.path,file_))\n            im3d[count]=im2d\n            count+=1\n\n        return im3d\n        \n\nif __name__==\"__main__\":\n    \"\"\"\n    test = io.imread('../input/rsna-miccai-png/train/00005/FLAIR/Image-114.png')\n    test_input=torch.from_numpy(test)\n    test2 = test_input.reshape(1,1,list(test_input.shape)[0],list(test_input.shape)[0])\n    # 默认转int类型，训练时需要将其转为float类型\n    test2 = test2.float()\n    model=U_net(out_channel=1)\n    output = model(test2)\n    # print(output.size())\n    output2 = output.reshape(list(output.shape)[2],list(output.shape)[3])\n    # 转int类型\n    output2 = output2.int()\n    # 转numpy类型\n    output3 = output2.numpy()\n    \"\"\"\n    # 绘图\n    #fig = Plotfig(test)\n    #fig.plot()\n    samples=os.listdir(trainpath)\n    for sample in samples:\n        mris=os.listdir(trainpath+sample+'/')\n        for mri in mris:\n            filepath = trainpath+sample+'/'+mri+'/'\n            reshape = Reshape(filepath)\n            data = reshape.matrix3d()\n            data = data.astype(np.float)\n            train_input = torch.from_numpy(data)\n            train_input = train_input.reshape(1,list(train_input.shape)[0],list(train_input.shape)[1],list(train_input.shape)[2])\n            train_input = train_input.float()\n            model=U_net(out_channel=int(list(train_input.shape)[1]),input_channel=int(list(train_input.shape)[1]))\n            #model.cuda()\n            model.train()\n            output = model(train_input)\n            output = output.reshape(list(output.shape)[1],list(output.shape)[2],list(output.shape)[3])\n            output = output.int()\n            output = output.numpy()\n            # 统计元素个数\n            #stat = np.unique(output,return_counts=True)\n            stat = np.unique(output)\n            print(sample,\"_\",mri,\":\",stat,\", data.shape:\",data.shape,\", out.shape:\",output.shape,\"\\n\")","metadata":{"execution":{"iopub.status.busy":"2021-10-12T05:35:15.328677Z","iopub.execute_input":"2021-10-12T05:35:15.329011Z","iopub.status.idle":"2021-10-12T05:46:49.735409Z","shell.execute_reply.started":"2021-10-12T05:35:15.328983Z","shell.execute_reply":"2021-10-12T05:46:49.733014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nfrom torch.utils.data import Dataset\n\nwith open(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\",\"r\")as handle:\n    lables = handle.readlines()\nprint(type(lables[1].split(\",\")[0]),type(lables[1].split(\",\")[1]))\n\nclass MyDataSet(Dataset):\n    def __init__(self, dataset_type, transform=None, update_dataset=False):\n        \"\"\"\n        dataset_type: ['train', 'test']\n        \"\"\"\n \n        dataset_path = '/home/muzhan/projects/dataset/'\n \n        if update_dataset:\n            make_txt_file(dataset_path)  # update datalist\n \n        self.transform = transform\n        self.sample_list = list()\n        self.dataset_type = dataset_type\n        f = open(dataset_path + self.dataset_type + '/datalist.txt')\n        lines = f.readlines()\n        for line in lines:\n            self.sample_list.append(line.strip())\n        f.close()\n \n    def __getitem__(self, index):\n        item = self.sample_list[index]\n        # img = cv2.imread(item.split(' _')[0])\n        img = Image.open(item.split(' _')[0])\n        if self.transform is not None:\n            img = self.transform(img)\n        label = int(item.split(' _')[-1])\n        return img, label\n \n    def __len__(self):\n        return len(self.sample_list)\n    ","metadata":{"execution":{"iopub.status.busy":"2021-09-22T05:44:48.652357Z","iopub.execute_input":"2021-09-22T05:44:48.652689Z","iopub.status.idle":"2021-09-22T05:44:48.665905Z","shell.execute_reply.started":"2021-09-22T05:44:48.65266Z","shell.execute_reply":"2021-09-22T05:44:48.66494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\npath = \"../input/rsna-miccai-png/train\"\nfiles=os.listdir(path)\nprint(files)","metadata":{},"execution_count":null,"outputs":[]}]}