{"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":"# %cp -r /kaggle/input/rsna-2022-breast/efficientnet_pytorch-0.7.1 /kaggle/working/\n# %cd /kaggle/working/efficientnet_pytorch-0.7.1/efficientnet_pytorch-0.7.1\n# !make \n# !make install\n# !python setup.py install\n\n!pip install /kaggle/input/rsna-2022-breast/efficientnet_pytorch-0.7.1-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2022-12-14T03:09:55.025213Z","iopub.execute_input":"2022-12-14T03:09:55.025893Z","iopub.status.idle":"2022-12-14T03:10:27.069791Z","shell.execute_reply.started":"2022-12-14T03:09:55.025745Z","shell.execute_reply":"2022-12-14T03:10:27.068415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import DataLoader, Dataset\nfrom torchvision import datasets, transforms, models\nfrom PIL import Image\nfrom matplotlib import pyplot as plt\nimport numpy as np\nfrom torch.nn import BCELoss,CrossEntropyLoss,BCEWithLogitsLoss\nfrom torch import optim\nfrom sklearn.model_selection import KFold\nimport pdb\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.autograd import Variable\nfrom efficientnet_pytorch import EfficientNet\nimport pandas as pd\nfrom tqdm.notebook import tqdm\nimport SimpleITK as sitk\n\ndata_dir = '/kaggle/input/rsna-breast-cancer-detection/test_images'","metadata":{"execution":{"iopub.status.busy":"2022-12-14T03:10:27.073299Z","iopub.execute_input":"2022-12-14T03:10:27.073688Z","iopub.status.idle":"2022-12-14T03:10:29.926091Z","shell.execute_reply.started":"2022-12-14T03:10:27.073653Z","shell.execute_reply":"2022-12-14T03:10:29.925113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def read_dicom_file(path):\n#     dicom = pydicom.read_file(path)\n#     data = dicom.pixel_array\n#     if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n#         data = np.amax(data) - data\n#     data = data - np.min(data)\n#     data = data / np.max(data)\n#     data = (data * 255).astype(np.uint8)\n#     data = Image.fromarray(data).resize((1024,1024)).convert('RGB')\n#     data = transforms.ToTensor()(data)\n#     data = transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))(data)\n#     return data\n\ndef read_dicom_file(path):\n    itk_img = sitk.ReadImage(path)\n    data = sitk.GetArrayFromImage(itk_img)\n    data = data.transpose(1,2,0)[:,:,0] \n#     if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n#         data = np.amax(data) - data\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    data = Image.fromarray(data).resize((1024,1024)).convert('RGB')\n    data = transforms.ToTensor()(data)\n    data = transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))(data)\n    return data","metadata":{"execution":{"iopub.status.busy":"2022-12-14T03:10:29.927608Z","iopub.execute_input":"2022-12-14T03:10:29.928249Z","iopub.status.idle":"2022-12-14T03:10:29.936574Z","shell.execute_reply.started":"2022-12-14T03:10:29.92821Z","shell.execute_reply":"2022-12-14T03:10:29.935527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 2\nBATCH_SIZE = 16                    # 超参数batch大小\n\nsave_path = \"/kaggle/input/rsna-private-model/efficientnet-b40.7916181606519209.pth\"    # 模型权重参数保存位置\ndevice = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")                         # 创建GPU运算环境\nprint(device)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-14T03:10:29.940227Z","iopub.execute_input":"2022-12-14T03:10:29.940627Z","iopub.status.idle":"2022-12-14T03:10:30.016494Z","shell.execute_reply.started":"2022-12-14T03:10:29.940584Z","shell.execute_reply":"2022-12-14T03:10:30.015401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class RSNADataset(Dataset):\n    def __init__(self, data_dir, transform=None):\n        \"\"\"\n        :param data_dir: 数据集所在路径\n        :param transform: 数据预处理\n        \"\"\"\n        self.data_dir = data_dir\n        self.data = self.get_img_info(data_dir)\n        self.transform = transform\n\n    def __getitem__(self, item):\n        path = self.data.iloc[item]\n        # image = Image.fromarray(np.array(Image.open('./train_images_processed_cv2_256'+'/'+path_img+'.png').convert('L')).transpose(1,0).astype(np.float32))\n        itk_img = sitk.ReadImage('/kaggle/input/rsna-breast-cancer-detection/test_images'+'/'+path+'.dcm')\n        data = sitk.GetArrayFromImage(itk_img)\n        data = data.transpose(1,2,0)[:,:,0] \n        data = data - np.min(data)\n        data = data / np.max(data)\n        data = (data * 255).astype(np.uint8)\n        data = Image.fromarray(data).resize((1024,1024)).convert('RGB')\n        # pdb.sert_trace()\n        if self.transform is not None:\n            image = self.transform(data)\n        return image\n\n    def __len__(self):\n        return len(self.data)\n\n    @staticmethod\n    def get_img_info(data_dir):\n        data = pd.read_csv(data_dir+'/'+'test.csv')\n        # pdb.set_trace()\n        # data.sort_values(by=[\"patient_id\",\"image_id\"],axis=0,ascending=True,inplace=True)\n        # pdb.set_trace()\n        # print(label)\n        # with open(path_dir) as file:\n        #     lines = file.readlines()\n        #     for line in lines:\n        #         data_info.append(line.strip('\\n').split(' '))\n        # data[\"image_id\"] =[ 'Depth % i' % i for i in data[\"/\"]]\n        path_dir = data[\"patient_id\"].astype('str')+\"/\"+data[\"image_id\"].astype('str')\n        \n        return path_dir","metadata":{"execution":{"iopub.status.busy":"2022-12-14T03:10:30.018258Z","iopub.execute_input":"2022-12-14T03:10:30.01875Z","iopub.status.idle":"2022-12-14T03:10:30.031868Z","shell.execute_reply.started":"2022-12-14T03:10:30.01856Z","shell.execute_reply":"2022-12-14T03:10:30.03098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_transform = transforms.Compose([\n        # transforms.Grayscale(1),\n        # transforms.Resize(600),\n        # transforms.RandomHorizontalFlip(p=0.5),\n        transforms.ToTensor(),\n        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),\n        # transforms.RandomHorizontalFlip(),  #图像一半的概率翻转，一半的概率不翻转\n        # transforms.RandomRotation((-45,45)), #随机旋转\n    ])\n\n# 测试函数，传入模型和数据读取接口\ndef evalute(model, device, loader):\n    predictions = []\n    for x in loader:\n        x = x.to(device)\n        model.eval()\n        with torch.no_grad():\n            out = model(x)       # 计算测试数据的输出logits\n            # 计算出out在第一维度上最大值对应编号，得模型的预测值\n            prediction = out.argmax(dim=1)\n            predictions.append(prediction)\n    return predictions\n\ntest_sub = RSNADataset(data_dir='/kaggle/input/rsna-breast-cancer-detection',transform=data_transform)\ntest_dataloader = torch.utils.data.DataLoader(test_sub, BATCH_SIZE , False, num_workers=0)\n\n\nmodel = EfficientNet.from_name('efficientnet-b4')\n# model = EfficientNet.from_pretrained('efficientnet-b4')\n\n\nmodel._fc = nn.Linear(model._fc.in_features,out_features=num_classes,bias=True)\nmodel.to(device)                                      # 实例化网络模型并送入GPU\n\nmodel.load_state_dict(torch.load(save_path), strict=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-14T03:10:30.033328Z","iopub.execute_input":"2022-12-14T03:10:30.034063Z","iopub.status.idle":"2022-12-14T03:10:34.970682Z","shell.execute_reply.started":"2022-12-14T03:10:30.034028Z","shell.execute_reply":"2022-12-14T03:10:34.969614Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')\npatient_id = data['patient_id']\nimage_id = data['image_id']\nlaterality = data[\"laterality\"]\nprediction_id = data['prediction_id']\n\n# for row in range(1,len(data),2):\n#     prediction_ids.append(data['prediction_id'][row:row+BATCH_SIZE])\n# #     try:\n#     image = read_dicom_file(data_dir+'/'+str(data['patient_id'][row])+'/'+str(data['image_id'][row])+'.dcm',BATCH_SIZE)\n#     preds.append(evalute(model,device,image).cpu().item())\n# #     except:\n# #         preds.append(1)\n\n# for _, row in data.iterrows():\n#     prediction_ids.append(row.prediction_id)\n#     try:\n#         image = read_dicom_file(data_dir+'/'+str(row.patient_id)+'/'+str(row.image_id)+'.dcm')\n#         preds.append(evalute(model,device,image).cpu().item())\n#     except:\n#         preds.append(1)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-12-14T03:10:34.972252Z","iopub.execute_input":"2022-12-14T03:10:34.972884Z","iopub.status.idle":"2022-12-14T03:10:34.983017Z","shell.execute_reply.started":"2022-12-14T03:10:34.972842Z","shell.execute_reply":"2022-12-14T03:10:34.982012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import timeit\n# start=timeit.default_timer()\n# #中间写代码\n\npreds= torch.tensor([item.cpu().detach().numpy() for item in evalute(model,device,test_dataloader)]).cuda()\npreds = preds.cpu().numpy().flatten()\n\nsubmission = pd.DataFrame(data={'prediction_id': prediction_id, 'cancer': preds}).groupby('prediction_id').max().reset_index()\nsubmission.head()\n\n\n# end=timeit.default_timer()\n# print('Running time: %s Seconds'%(end-start))","metadata":{"execution":{"iopub.status.busy":"2022-12-14T03:10:34.984923Z","iopub.execute_input":"2022-12-14T03:10:34.985428Z","iopub.status.idle":"2022-12-14T03:10:43.545906Z","shell.execute_reply.started":"2022-12-14T03:10:34.98539Z","shell.execute_reply":"2022-12-14T03:10:43.544924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T03:10:43.547271Z","iopub.execute_input":"2022-12-14T03:10:43.548138Z","iopub.status.idle":"2022-12-14T03:10:43.556602Z","shell.execute_reply.started":"2022-12-14T03:10:43.548099Z","shell.execute_reply":"2022-12-14T03:10:43.555663Z"},"trusted":true},"execution_count":null,"outputs":[]}]}