{"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":"# RSNA Breast Cancer Detection Baseline - Inference\n\nThis notebook shows a simple PyTorch inference pipeline based on the pregenerated datasets.\n\n**Training Datasets:**  \n* https://www.kaggle.com/code/mourinho384/training-rsna\n\n**Reference:**  \n* Main ref : https://www.kaggle.com/code/theoviel/rsna-breast-baseline-inference\n* https://www.kaggle.com/code/tachyon777/hubmap2-tachyon-v5-sub-d4hand-512/notebook\n\n**Changes :**  \n* v-:","metadata":{}},{"cell_type":"markdown","source":"- Official -\nThe mammograms, in dicom format. You can expect roughly 8,000 patients in the hidden test set. There are usually but not always 4 images per patient. Note that many of the images use the jpeg 2000 format which may you may need special libraries to load.","metadata":{}},{"cell_type":"markdown","source":"### ver42. Trainで試す。pfbetaは出ない？","metadata":{}},{"cell_type":"code","source":"DEBUG =False\nimport time\ntime_point1 = time.time()\nprint(\"Debug:\", DEBUG)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T11:35:31.877348Z","iopub.execute_input":"2023-01-27T11:35:31.878147Z","iopub.status.idle":"2023-01-27T11:35:31.905278Z","shell.execute_reply.started":"2023-01-27T11:35:31.878067Z","shell.execute_reply":"2023-01-27T11:35:31.904275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Initialization\n\npylibjpeg  \nhttps://github.com/pydicom/pylibjpeg","metadata":{}},{"cell_type":"code","source":"#Internet Off -- pydicom / pylibjpeg/ python_gdcm/ \n!pip install /kaggle/input/rsna-2022-whl/{pydicom-2.3.0-py3-none-any.whl,pylibjpeg-1.4.0-py3-none-any.whl,python_gdcm-3.0.15-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl}","metadata":{"execution":{"iopub.status.busy":"2023-01-27T11:35:31.907323Z","iopub.execute_input":"2023-01-27T11:35:31.908011Z","iopub.status.idle":"2023-01-27T11:36:05.484486Z","shell.execute_reply.started":"2023-01-27T11:35:31.907963Z","shell.execute_reply":"2023-01-27T11:36:05.483261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp ../input/segmentationmodelspytorch/segmentation_models/efficientnet_pytorch-0.6.3.xyz /tmp/pip/cache/efficientnet_pytorch-0.6.3.tar.gz\n\n!mkdir -p /tmp/pip/cache/\n!cp ../input/segmentationmodelspytorch/segmentation_models/efficientnet_pytorch-0.6.3.xyz /tmp/pip/cache/efficientnet_pytorch-0.6.3.tar.gz\n!cp ../input/segmentationmodelspytorch/segmentation_models/pretrainedmodels-0.7.4.xyz /tmp/pip/cache/pretrainedmodels-0.7.4.tar.gz\n!cp ../input/segmentationmodelspytorch/segmentation_models/segmentation-models-pytorch-0.1.2.xyz /tmp/pip/cache/segmentation_models_pytorch-0.1.2.tar.gz\n!cp ../input/segmentationmodelspytorch/segmentation_models/timm-0.1.20-py3-none-any.whl /tmp/pip/cache/\n!cp ../input/segmentationmodelspytorch/segmentation_models/timm-0.2.1-py3-none-any.whl /tmp/pip/cache/\n!pip install --no-index --find-links /tmp/pip/cache/ efficientnet-pytorch\n!pip install --no-index --find-links /tmp/pip/cache/ segmentation-models-pytorch\n\n#!pip install torchinfo","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-01-27T11:36:05.486607Z","iopub.execute_input":"2023-01-27T11:36:05.486991Z","iopub.status.idle":"2023-01-27T11:36:36.9944Z","shell.execute_reply.started":"2023-01-27T11:36:05.486952Z","shell.execute_reply":"2023-01-27T11:36:36.993245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import pylibjpeg\n\nimport os\nimport sys\nimport cv2\nimport glob\nimport gdcm\nimport json\nimport pydicom\nimport gc\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport random\nimport matplotlib.pyplot as plt\n\nfrom tqdm.notebook import tqdm\nfrom joblib import Parallel, delayed\nimport torch\nfrom torch.utils.data import Dataset\nfrom torch.utils.data import DataLoader\nfrom torch import nn, optim\n\nimport albumentations as albu\nfrom albumentations.pytorch import ToTensorV2\nfrom efficientnet_pytorch import model as enet","metadata":{"execution":{"iopub.status.busy":"2023-01-27T11:36:36.999209Z","iopub.execute_input":"2023-01-27T11:36:36.999542Z","iopub.status.idle":"2023-01-27T11:36:40.327039Z","shell.execute_reply.started":"2023-01-27T11:36:36.999511Z","shell.execute_reply":"2023-01-27T11:36:40.326089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data","metadata":{}},{"cell_type":"code","source":"test_image_dcm=glob.glob('/kaggle/input/rsna-breast-cancer-detection/train_images/*/*.dcm')[:5000]\n#test_image_dcm=glob.glob('/kaggle/input/rsna-breast-cancer-detection/test_images/*/*.dcm')\nprint(\"Number of images :\", len(test_image_dcm))","metadata":{"execution":{"iopub.status.busy":"2023-01-27T11:36:40.328282Z","iopub.execute_input":"2023-01-27T11:36:40.329702Z","iopub.status.idle":"2023-01-27T11:37:24.058251Z","shell.execute_reply.started":"2023-01-27T11:36:40.329665Z","shell.execute_reply":"2023-01-27T11:37:24.057072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Device and seed","metadata":{}},{"cell_type":"code","source":"class CFG:\n    model_name = \"RSNA_Sahashi_v1\"\n    backbone = 'efficientnet-b1'\n    Progress_Bar = True\n    image_size = 128\n    batch_size = 64\n    \n    n_fold = 4\n    n_seed = 1\n    seed = 42\n    num_workers = 4","metadata":{"execution":{"iopub.status.busy":"2023-01-27T11:37:24.059808Z","iopub.execute_input":"2023-01-27T11:37:24.060847Z","iopub.status.idle":"2023-01-27T11:37:24.067114Z","shell.execute_reply.started":"2023-01-27T11:37:24.060805Z","shell.execute_reply":"2023-01-27T11:37:24.066006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nprint(device)\n\ndef seed_everything(seed=CFG.seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    \nseed_everything()","metadata":{"execution":{"iopub.status.busy":"2023-01-27T11:37:24.068847Z","iopub.execute_input":"2023-01-27T11:37:24.069632Z","iopub.status.idle":"2023-01-27T11:37:24.160887Z","shell.execute_reply.started":"2023-01-27T11:37:24.069589Z","shell.execute_reply":"2023-01-27T11:37:24.159694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**ROI Extraction**\n\nhttps://www.kaggle.com/code/snnclsr/roi-extraction-using-opencv","metadata":{}},{"cell_type":"code","source":"def crop_coords(img):\n    \"\"\"\n    Crop ROI from image\n    \"\"\"\n    #GaussianBlur before Otsu thresholding\n    #http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_imgproc/py_filtering/py_filtering.html\n    blur = cv2.GaussianBlur(img,(5,5),0)\n    # Otsu's thresholding after Gaussian filtering 三つ目の方法は，5x5のサイズのgaussianフィルタでノイズを抑制した画像に対して大津の二値化を適用します． 平滑化処理によってノイズの影響が軽減されていることが分かります．\n    #http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html\n    _,breast_mask=cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)\n    \n    #境界検出　https://axa.biopapyrus.jp/ia/opencv/detect-contours.html\n    cnts, _ = cv2.findContours(breast_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n    cnt = max(cnts, key = cv2.contourArea)\n    x, y, w, h = cv2.boundingRect(cnt)\n    return (x, y, w, h)\n\ndef truncation_normalization(img):\n    \"\"\"\n    Clip and normalize pixels in the breast ROI.\n    @img : numpy array image\n    return: numpy array of the normalized image\n    \"\"\"\n    Pmin = np.percentile(img[img!=0], 5)\n    Pmax = np.percentile(img[img!=0], 99)\n    truncated = np.clip(img,Pmin, Pmax)  \n    normalized = (truncated - Pmin)/(Pmax - Pmin)\n    normalized[img==0]=0\n    return normalized\n\ndef clahe(img, clip):\n    \"\"\"\n    Image enhancement.\n    @img : numpy array image\n    @clip : float, clip limit for CLAHE algorithm\n    return: numpy array of the enhanced image\n    \"\"\"\n    clahe = cv2.createCLAHE(clipLimit=clip)\n    cl = clahe.apply(np.array(img*255, dtype=np.uint8))\n    return cl","metadata":{"execution":{"iopub.status.busy":"2023-01-27T11:37:24.164576Z","iopub.execute_input":"2023-01-27T11:37:24.164844Z","iopub.status.idle":"2023-01-27T11:37:24.177278Z","shell.execute_reply.started":"2023-01-27T11:37:24.164819Z","shell.execute_reply":"2023-01-27T11:37:24.176049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Docim to PNG**","metadata":{}},{"cell_type":"code","source":"%%capture\n!pip install /kaggle/input/rsnamodules/dicomsdl-0.109.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl \nimport dicomsdl as dicoml","metadata":{"execution":{"iopub.status.busy":"2023-01-27T11:37:24.182768Z","iopub.execute_input":"2023-01-27T11:37:24.183043Z","iopub.status.idle":"2023-01-27T11:37:54.207032Z","shell.execute_reply.started":"2023-01-27T11:37:24.183018Z","shell.execute_reply":"2023-01-27T11:37:54.205793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Dicom to Png converter**","metadata":{}},{"cell_type":"code","source":"def process(f, size=128, save_folder=None, dicom_process = True, extension=\"png\"):\n    patient = f.split('/')[-2]\n    image_name = f.split('/')[-1][:-4]\n    if dicom_process:\n        dicom = pydicom.dcmread(f)\n        img = dicom.pixel_array\n        img = (img - img.min()) / (img.max() - img.min())\n        if dicom.PhotometricInterpretation == \"MONOCHROME1\":  \n            img = 1 - img\n        image = (img * 255).astype(np.uint8)\n        \n    else:\n        dicom = dicoml.open(f)\n        img = dicom.pixelData()\n        img = (img - img.min()) / (img.max() - img.min())\n        if dicom.getPixelDataInfo()['PhotometricInterpretation'] == \"MONOCHROME1\":\n            img = 1 - img\n        image = (img * 255).astype(np.uint8)\n    img = cv2.resize(image, (size, size))\n    file_name = f'{save_folder}' + f\"{patient}_{image_name}.{extension}\"\n    cv2.imwrite(file_name, img)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T11:37:54.209186Z","iopub.execute_input":"2023-01-27T11:37:54.209626Z","iopub.status.idle":"2023-01-27T11:37:54.221257Z","shell.execute_reply.started":"2023-01-27T11:37:54.209577Z","shell.execute_reply":"2023-01-27T11:37:54.220286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir_dicomsdl = '/kaggle/working/png_file_dic/'\nos.makedirs(image_dir_dicomsdl, exist_ok=True)\n#SAVE_FOLDER = \"/kaggle/tmp/output/\" #https://www.kaggle.com/competitions/rsna-breast-cancer-detection/discussion/370156","metadata":{"execution":{"iopub.status.busy":"2023-01-27T11:37:54.222642Z","iopub.execute_input":"2023-01-27T11:37:54.223099Z","iopub.status.idle":"2023-01-27T11:37:54.233688Z","shell.execute_reply.started":"2023-01-27T11:37:54.223059Z","shell.execute_reply":"2023-01-27T11:37:54.232662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**img2tensor / pfbeta_torch**　","metadata":{}},{"cell_type":"code","source":"def img2tensor(img,dtype:np.dtype=np.float32):\n    if img.ndim==2 : img = np.expand_dims(img,2)\n    img = np.transpose(img,(2,0,1))\n    return torch.from_numpy(img.astype(dtype, copy=False))\n\ndef pfbeta_torch(labels, preds, beta=1):\n    '''\n    labels, preds: numpy.array\n    '''\n    preds = preds.clip(0, 1)\n    y_true_count = labels.sum()\n    ctp = preds[labels==1].sum()\n    cfp = preds[labels==0].sum()\n    \n    beta_squared = beta * beta\n    c_precision = ctp / (ctp + cfp)\n    c_recall = ctp / y_true_count\n    if (c_precision > 0 and c_recall > 0):\n        result = (1 + beta_squared) * (c_precision * c_recall) / (beta_squared * c_precision + c_recall)\n        return result\n    else:\n        return 0.00","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:34:12.658616Z","iopub.execute_input":"2023-01-27T12:34:12.659002Z","iopub.status.idle":"2023-01-27T12:34:12.668352Z","shell.execute_reply.started":"2023-01-27T12:34:12.658965Z","shell.execute_reply":"2023-01-27T12:34:12.667408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**pydicom vs dicomsdl time**","metadata":{}},{"cell_type":"code","source":"#image_dir_dicomsdl\nstart_time = time.time()  \nParallel(n_jobs=4)(\n    delayed(process)(f, size = 256, save_folder = image_dir_dicomsdl, dicom_process = False)\n    for f in tqdm(test_image_dcm))\n\nprint(\"image_dir_dicomsdl\",image_dir_dicomsdl)\nprint(\"Saving to -image_dir_dicomsdl- : dicomsdl time\",time.time() - start_time)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T11:37:54.248022Z","iopub.execute_input":"2023-01-27T11:37:54.2484Z","iopub.status.idle":"2023-01-27T12:13:28.439336Z","shell.execute_reply.started":"2023-01-27T11:37:54.248355Z","shell.execute_reply":"2023-01-27T12:13:28.437898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-----------------------------","metadata":{}},{"cell_type":"code","source":"BASE_PATH = \"/kaggle/input/rsna-breast-cancer-detection\"\nTEST_PATH = os.path.join(BASE_PATH, \"test_image_dcm\")\nMODELS_PATH = \"/kaggle/input/rsna-training-debug-true\"\ntrain_csv = pd.read_csv(os.path.join(BASE_PATH, \"train.csv\"))\ntest_csv =pd.read_csv(os.path.join(BASE_PATH, \"test.csv\"))\ndf_sample = pd.read_csv(os.path.join(BASE_PATH, \"sample_submission.csv\"))\n\ntest_csv['cancer']=0\ntest_csv['path'] = image_dir_dicomsdl + test_csv[\"patient_id\"].astype(str) + \"_\" + test_csv[\"image_id\"].astype(str) + \".png\"\ntest_csv.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:19:27.412121Z","iopub.execute_input":"2023-01-27T12:19:27.412517Z","iopub.status.idle":"2023-01-27T12:19:27.555064Z","shell.execute_reply.started":"2023-01-27T12:19:27.412484Z","shell.execute_reply":"2023-01-27T12:19:27.554162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ver42 新設\ntrain_csv['cancer']=0\ntrain_csv['path'] = image_dir_dicomsdl + train_csv[\"patient_id\"].astype(str) + \"_\" + train_csv[\"image_id\"].astype(str) + \".png\"\ntrain_csv.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:19:28.20433Z","iopub.execute_input":"2023-01-27T12:19:28.20524Z","iopub.status.idle":"2023-01-27T12:19:28.302033Z","shell.execute_reply.started":"2023-01-27T12:19:28.205196Z","shell.execute_reply":"2023-01-27T12:19:28.300972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images= glob.glob('/kaggle/working/png_file_dic/*.png')\nlen(test_images)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:20:05.364975Z","iopub.execute_input":"2023-01-27T12:20:05.365339Z","iopub.status.idle":"2023-01-27T12:20:05.389285Z","shell.execute_reply.started":"2023-01-27T12:20:05.365309Z","shell.execute_reply":"2023-01-27T12:20:05.388417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"condition= train_csv['path'].isin(test_images)\ntrain_csv=train_csv[condition]\ntrain_csv.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:20:16.88982Z","iopub.execute_input":"2023-01-27T12:20:16.890248Z","iopub.status.idle":"2023-01-27T12:20:16.922479Z","shell.execute_reply.started":"2023-01-27T12:20:16.890215Z","shell.execute_reply":"2023-01-27T12:20:16.921657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**prediction**","metadata":{}},{"cell_type":"code","source":"class RSNADataset(Dataset):    \n    def __init__(self,tfms=None,df=train_csv):\n        self.df=df\n        self.ids = self.df.image_id.values   \n        self.paths = df['path'].values\n        self.fnames = [fname.split('/')[-1][:-4] for fname in test_image_dcm if int(fname.split('/')[-1][:-4]) in self.ids]        \n        self.targets = df['cancer'].values\n        self.tfms = tfms\n        \n    def __len__(self):\n        return len(self.fnames)\n    \n    def __getitem__(self, idx):\n        img=cv2.imread(self.paths[idx], cv2.IMREAD_GRAYSCALE)\n        (x,y,w,h) = crop_coords(img)\n        img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        img = img[y:y+h+100, x:x+w+100]\n        img = cv2.resize(img, (CFG.image_size, CFG.image_size)) \n        label = torch.tensor([self.targets[idx]], dtype=torch.float)\n        \n        return img2tensor(img/255.0) , label #img.shape()","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:20:20.998141Z","iopub.execute_input":"2023-01-27T12:20:20.998529Z","iopub.status.idle":"2023-01-27T12:20:21.00907Z","shell.execute_reply.started":"2023-01-27T12:20:20.998497Z","shell.execute_reply":"2023-01-27T12:20:21.008079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained_model = {\n        'efficientnet-b0': '../input/efficientnet-pytorch/efficientnet-b0-08094119.pth',\n        'efficientnet-b1': '../input/efficientnet-pytorch/efficientnet-b1-dbc7070a.pth',\n        'efficientnet-b2': '../input/efficientnet-pytorch/efficientnet-b2-27687264.pth',\n        'efficientnet-b3': '../input/efficientnet-pytorch/efficientnet-b3-c8376fa2.pth',\n        'efficientnet-b4': '../input/efficientnet-pytorch/efficientnet-b4-e116e8b3.pth',\n        'efficientnet-b5': '../input/efficientnet-pytorch/efficientnet-b5-586e6cc6.pth'}\n\nclass enetv2(nn.Module):\n    def __init__(self, backbone, out_dim=1):\n        super(enetv2, self).__init__()\n        self.enet = enet.EfficientNet.from_name(backbone)\n        self.enet.load_state_dict(torch.load(pretrained_model[backbone]))\n        self.myfc = nn.Linear(self.enet._fc.in_features, out_dim)\n        self.enet._fc = nn.Identity()\n        self.sigmoid = nn.Sigmoid()\n\n    def extract(self, x):\n        return self.enet(x)\n\n    def forward(self, x):\n        x = self.extract(x)\n        x = self.myfc(x)\n        #x = self.sigmoid(x)\n        return x\n    \n    \ncls_models = [None]*len(os.listdir(MODELS_PATH))\nfor path in os.listdir(MODELS_PATH):\n    model = enetv2(backbone=CFG.backbone)\n    params = torch.load(os.path.join(MODELS_PATH,path),map_location=torch.device('cpu'))\n    model.load_state_dict(params)\n    model.float()\n    model.eval()\n    model.to(device)\n    \n    for i in range(len(os.listdir(MODELS_PATH))):\n        if \"f\"+str(i) in path:\n            cls_models[i] = model\n            print(str(i),path)\n\nprint(\"len model path\", len(os.listdir(MODELS_PATH)))","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:20:22.195123Z","iopub.execute_input":"2023-01-27T12:20:22.195518Z","iopub.status.idle":"2023-01-27T12:20:27.075101Z","shell.execute_reply.started":"2023-01-27T12:20:22.195481Z","shell.execute_reply":"2023-01-27T12:20:27.074069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model_pred:\n    def __init__(self, cls_models, dl, tta:bool=False):#本番はtta=True\n        self.cls_models = cls_models\n        self.dl = dl\n        self.tta = tta\n    def __iter__(self):\n        with torch.no_grad():\n            for batch in iter(self.dl):\n                x = batch[0].to(device)\n                y = batch[1].to(device)\n                #classification\n                cpy = None\n                for i, cls_model in enumerate(self.cls_models):\n                    cp = cls_model(x)\n                    cp = torch.sigmoid(cp).detach()#q\n                    #print(\"i\",i)\n                    #print(\"cpy\",cpy)\n                    #print(\"cp\",cp)\n                    \n                    if cpy is None:\n                        cpy = cp\n                    else:\n                        cpy += cp #.squeeze(-1)\n                    \n                    if self.tta:\n                        #x,y,xy flips as TTA\n                        flips = [[-1],[-2],[-2,-1]]\n                        for f in flips:\n                            xf = torch.flip(x,f)#q\n                            for cls_model in self.cls_models:\n                                cp = cls_model(xf)\n                                cp = torch.sigmoid(cp).detach()\n                                cpy += cp\n                        cpy /= (1+len(flips))\n                        \n                cpy /= len(self.cls_models)\n                cpy = cpy.squeeze(-1) \n                \n                #print(\"cpy\",cpy)\n                #print(\"len cpy\", len(cpy))\n                yield cpy, y \n                    \n    def __len__(self):\n        return len(self.dl.dataset)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:31:06.37508Z","iopub.execute_input":"2023-01-27T12:31:06.375687Z","iopub.status.idle":"2023-01-27T12:31:06.389638Z","shell.execute_reply.started":"2023-01-27T12:31:06.375646Z","shell.execute_reply":"2023-01-27T12:31:06.388545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = []\nlabels =[]\npfbetas=[]\n\nif len(test_csv) != 2:\n    print(\"len(test_csv) != 2\")\n    ds=RSNADataset(df=train_csv)\n    dl=DataLoader(ds,CFG.batch_size,num_workers=2,shuffle=False,pin_memory=True)\n    mp=Model_pred(cls_models,dl)\n    for p, label in iter(mp):\n        preds.append(p.cpu().numpy().tolist())\n        labels.append(label.cpu().numpy().tolist())\n        #pfbetas.apped(pfbeta.cpu().numpy().tolist()) #add 42\n        \n    del ds,dl\n    gc.collect()\n        \nelse:\n    print(\"len(test_csv) = 2\")\n    for idx,row in tqdm(test_csv.iterrows(),total=len(df_sample)):\n        preds.append(\"\")\n        \npfbeta=pfbeta_torch(labels=labels,preds=preds,beta=1)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:34:17.206621Z","iopub.execute_input":"2023-01-27T12:34:17.207222Z","iopub.status.idle":"2023-01-27T12:34:30.293973Z","shell.execute_reply.started":"2023-01-27T12:34:17.207186Z","shell.execute_reply":"2023-01-27T12:34:30.292573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:40:53.963999Z","iopub.execute_input":"2023-01-27T12:40:53.964611Z","iopub.status.idle":"2023-01-27T12:40:54.105451Z","shell.execute_reply.started":"2023-01-27T12:40:53.964568Z","shell.execute_reply":"2023-01-27T12:40:54.10455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(labels[1])","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:36:06.523439Z","iopub.execute_input":"2023-01-27T12:36:06.523824Z","iopub.status.idle":"2023-01-27T12:36:06.53251Z","shell.execute_reply.started":"2023-01-27T12:36:06.523792Z","shell.execute_reply":"2023-01-27T12:36:06.531388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(np.mean(preds, 0)>0.001).astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:37:53.049529Z","iopub.execute_input":"2023-01-27T12:37:53.049899Z","iopub.status.idle":"2023-01-27T12:37:53.058746Z","shell.execute_reply.started":"2023-01-27T12:37:53.049869Z","shell.execute_reply":"2023-01-27T12:37:53.057604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"THRESHOLD = 0.001\n\nprint(\"len check:\",len(np.mean(preds, 0)))\npred_m = np.mean(preds, 0)\npred_m = (pred_m > THRESHOLD).astype(int)\ntrain_csv[\"cancer\"] = pred_m\ntrain_csv","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:39:29.365778Z","iopub.execute_input":"2023-01-27T12:39:29.366151Z","iopub.status.idle":"2023-01-27T12:39:29.40128Z","shell.execute_reply.started":"2023-01-27T12:39:29.366119Z","shell.execute_reply":"2023-01-27T12:39:29.400304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv[train_csv[\"cancer\"]==1]","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:39:19.685216Z","iopub.execute_input":"2023-01-27T12:39:19.685537Z","iopub.status.idle":"2023-01-27T12:39:19.698174Z","shell.execute_reply.started":"2023-01-27T12:39:19.685509Z","shell.execute_reply":"2023-01-27T12:39:19.697099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Submit**","metadata":{}},{"cell_type":"code","source":"#Delete\nimport shutil\nimport os\nshutil.rmtree('/kaggle/working/png_file_dic/')","metadata":{"execution":{"iopub.status.busy":"2023-01-24T13:05:09.414489Z","iopub.execute_input":"2023-01-24T13:05:09.415316Z","iopub.status.idle":"2023-01-24T13:05:09.423846Z","shell.execute_reply.started":"2023-01-24T13:05:09.41528Z","shell.execute_reply":"2023-01-24T13:05:09.422987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv['prediction_id'] = test_csv['patient_id'].astype(str) + \"_\" + test_csv['laterality']\nsub = test_csv[['prediction_id', 'cancer']].groupby(\"prediction_id\").mean().reset_index()\nsub.to_csv('/kaggle/working/submission.csv', index=False)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-24T13:05:09.428418Z","iopub.execute_input":"2023-01-24T13:05:09.428676Z","iopub.status.idle":"2023-01-24T13:05:09.453554Z","shell.execute_reply.started":"2023-01-24T13:05:09.428652Z","shell.execute_reply":"2023-01-24T13:05:09.45229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## pfbeta sample try","metadata":{}},{"cell_type":"code","source":"def pfbeta_torch(labels, preds, beta=1):\n    '''\n    labels, preds: numpy.array\n    '''\n    preds = preds.clip(0, 1)\n    y_true_count = labels.sum()\n    ctp = preds[labels==1].sum()\n    cfp = preds[labels==0].sum()\n    \n    beta_squared = beta * beta\n    c_precision = ctp / (ctp + cfp)\n    c_recall = ctp / y_true_count\n    if (c_precision > 0 and c_recall > 0):\n        result = (1 + beta_squared) * (c_precision * c_recall) / (beta_squared * c_precision + c_recall)\n        return result\n    else:\n        return 0.00\n    \nlabel_sample=np.array([1,1,1,1,1,0,0,0,0,0,1,1,1,0,0])\npreds_sample=np.array([0.4,0.35,0.62,0.22,0.56,0.23,0.14,0.02,0.03,0.04,0.24,0.56,0.19,0.12,0.16])\npfbeta_torch(labels=label_sample, preds=preds_sample)","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:40:07.031103Z","iopub.execute_input":"2023-01-27T12:40:07.031492Z","iopub.status.idle":"2023-01-27T12:40:07.04503Z","shell.execute_reply.started":"2023-01-27T12:40:07.031458Z","shell.execute_reply":"2023-01-27T12:40:07.04366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array([1,1,1,1,1,0,0,0,0,0,1,1,1,0,0])","metadata":{"execution":{"iopub.status.busy":"2023-01-27T12:39:57.92802Z","iopub.execute_input":"2023-01-27T12:39:57.928426Z","iopub.status.idle":"2023-01-27T12:39:57.939256Z","shell.execute_reply.started":"2023-01-27T12:39:57.928387Z","shell.execute_reply":"2023-01-27T12:39:57.938107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}