{"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":"%cd ../input/icevision-080","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:27.569527Z","iopub.execute_input":"2021-06-18T12:26:27.569897Z","iopub.status.idle":"2021-06-18T12:26:27.579824Z","shell.execute_reply.started":"2021-06-18T12:26:27.569863Z","shell.execute_reply":"2021-06-18T12:26:27.578892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install timm-0.4.9-py3-none-any.whl -f ./ --no-index --no-deps\n!pip install effdet-0.2.4-py3-none-any.whl -f ./ --no-index --no-deps\n!pip install omegaconf-2.0.6-py3-none-any.whl -f ./ --no-index --no-deps","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:28.741938Z","iopub.execute_input":"2021-06-18T12:26:28.742228Z","iopub.status.idle":"2021-06-18T12:26:34.4749Z","shell.execute_reply.started":"2021-06-18T12:26:28.742194Z","shell.execute_reply":"2021-06-18T12:26:34.4739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:34.476699Z","iopub.execute_input":"2021-06-18T12:26:34.476971Z","iopub.status.idle":"2021-06-18T12:26:34.485177Z","shell.execute_reply.started":"2021-06-18T12:26:34.476944Z","shell.execute_reply":"2021-06-18T12:26:34.482817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd pycocotools202/","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:34.487799Z","iopub.execute_input":"2021-06-18T12:26:34.488043Z","iopub.status.idle":"2021-06-18T12:26:34.498357Z","shell.execute_reply.started":"2021-06-18T12:26:34.48802Z","shell.execute_reply":"2021-06-18T12:26:34.497505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pycocotools-2.0.2-cp37-cp37m-linux_x86_64.whl -f ./ --no-index --no-deps","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:34.500118Z","iopub.execute_input":"2021-06-18T12:26:34.500494Z","iopub.status.idle":"2021-06-18T12:26:36.099833Z","shell.execute_reply.started":"2021-06-18T12:26:34.500458Z","shell.execute_reply":"2021-06-18T12:26:36.098926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:36.101443Z","iopub.execute_input":"2021-06-18T12:26:36.1018Z","iopub.status.idle":"2021-06-18T12:26:36.108146Z","shell.execute_reply.started":"2021-06-18T12:26:36.10176Z","shell.execute_reply":"2021-06-18T12:26:36.107221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport re\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\nimport albumentations as A\nfrom tqdm.notebook import tqdm\nimport gc\n\nimport timm\nfrom fastai.vision.all import *\nfrom fastai.medical.imaging import *\nfrom fastai.vision.learner import create_head","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:36.109752Z","iopub.execute_input":"2021-06-18T12:26:36.110262Z","iopub.status.idle":"2021-06-18T12:26:40.774328Z","shell.execute_reply.started":"2021-06-18T12:26:36.110224Z","shell.execute_reply":"2021-06-18T12:26:40.773472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from effdet import get_efficientdet_config, create_model_from_config, unwrap_bench, create_model, load_checkpoint\nfrom effdet.bench import _post_process, _batch_detection\nfrom effdet.config import set_config_readonly, set_config_writeable\nfrom effdet.efficientdet import get_feature_info, BiFpn, BiFpnLayer, HeadNet, _init_weight, _init_weight_alt\nfrom effdet.loss import DetectionLoss\nfrom effdet.anchors import Anchors, AnchorLabeler","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:40.775761Z","iopub.execute_input":"2021-06-18T12:26:40.776133Z","iopub.status.idle":"2021-06-18T12:26:40.941778Z","shell.execute_reply.started":"2021-06-18T12:26:40.776098Z","shell.execute_reply":"2021-06-18T12:26:40.941061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader\nimport torchvision.transforms as transforms","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:40.94457Z","iopub.execute_input":"2021-06-18T12:26:40.944889Z","iopub.status.idle":"2021-06-18T12:26:40.949876Z","shell.execute_reply.started":"2021-06-18T12:26:40.944861Z","shell.execute_reply":"2021-06-18T12:26:40.948891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing data ","metadata":{}},{"cell_type":"code","source":"sz = 256\nbs = 32\nmean = [0.485, 0.456, 0.406] \nstd = [0.229, 0.224, 0.225]","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:40.951845Z","iopub.execute_input":"2021-06-18T12:26:40.95211Z","iopub.status.idle":"2021-06-18T12:26:40.961947Z","shell.execute_reply.started":"2021-06-18T12:26:40.952085Z","shell.execute_reply":"2021-06-18T12:26:40.961087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"box_class = {'background': 0, 'opacity': 1, 'none': 2}\nclassi_class = {'Typical Appearance': 0, 'Negative for Pneumonia': 1, 'Atypical Appearance': 2, 'Indeterminate Appearance': 3}","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:40.964839Z","iopub.execute_input":"2021-06-18T12:26:40.965276Z","iopub.status.idle":"2021-06-18T12:26:40.971867Z","shell.execute_reply.started":"2021-06-18T12:26:40.965244Z","shell.execute_reply":"2021-06-18T12:26:40.971142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cc_i2o = {classi_class[k]:k for k in classi_class.keys()}","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:40.973355Z","iopub.execute_input":"2021-06-18T12:26:40.973703Z","iopub.status.idle":"2021-06-18T12:26:40.980512Z","shell.execute_reply.started":"2021-06-18T12:26:40.97367Z","shell.execute_reply":"2021-06-18T12:26:40.979742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bc_i2o = {box_class[k]:k for k in box_class.keys()}","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:40.983324Z","iopub.execute_input":"2021-06-18T12:26:40.983571Z","iopub.status.idle":"2021-06-18T12:26:40.989084Z","shell.execute_reply.started":"2021-06-18T12:26:40.983548Z","shell.execute_reply":"2021-06-18T12:26:40.988276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_tst_img = Path('../input/siim-covid19-detection/test'); path_tst_img.ls()","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:40.990341Z","iopub.execute_input":"2021-06-18T12:26:40.990689Z","iopub.status.idle":"2021-06-18T12:26:41.058372Z","shell.execute_reply.started":"2021-06-18T12:26:40.990656Z","shell.execute_reply":"2021-06-18T12:26:41.057467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pairs = []\nfor stu in path_tst_img.ls():\n    study = str(stu).split('/')[-1]\n    for images in stu.ls(): \n        for im in images.ls():\n            pairs.append([study, str(im).split('/')[-1], im])","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:41.059607Z","iopub.execute_input":"2021-06-18T12:26:41.059946Z","iopub.status.idle":"2021-06-18T12:26:44.646803Z","shell.execute_reply.started":"2021-06-18T12:26:41.059912Z","shell.execute_reply":"2021-06-18T12:26:44.645879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(pairs,columns=['Study', 'Image', 'fname'])","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:44.648139Z","iopub.execute_input":"2021-06-18T12:26:44.648521Z","iopub.status.idle":"2021-06-18T12:26:44.659843Z","shell.execute_reply.started":"2021-06-18T12:26:44.648485Z","shell.execute_reply":"2021-06-18T12:26:44.659053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Ori_x'] = None\ndf['Ori_y'] = None\nfor i, fn in tqdm(enumerate(df['fname'].values)):\n    dcm = Path(fn).dcmread()\n    y = dcm.Rows\n    x = dcm.Columns\n    df.loc[i, 'Ori_x'] = x\n    df.loc[i, 'Ori_y'] = y","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:26:44.6628Z","iopub.execute_input":"2021-06-18T12:26:44.663108Z","iopub.status.idle":"2021-06-18T12:27:09.287143Z","shell.execute_reply.started":"2021-06-18T12:26:44.663083Z","shell.execute_reply":"2021-06-18T12:27:09.286424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['x_scale'] = df['Ori_x']/256\ndf['y_scale'] = df['Ori_y']/256","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:27:09.289888Z","iopub.execute_input":"2021-06-18T12:27:09.290139Z","iopub.status.idle":"2021-06-18T12:27:09.299394Z","shell.execute_reply.started":"2021-06-18T12:27:09.290112Z","shell.execute_reply":"2021-06-18T12:27:09.298666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss = pd.read_csv('../input/siim-covid19-detection/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:27:09.300372Z","iopub.execute_input":"2021-06-18T12:27:09.300689Z","iopub.status.idle":"2021-06-18T12:27:09.330741Z","shell.execute_reply.started":"2021-06-18T12:27:09.300664Z","shell.execute_reply":"2021-06-18T12:27:09.330019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(ss) == (len(path_tst_img.ls()) + len(df))","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:27:09.331779Z","iopub.execute_input":"2021-06-18T12:27:09.332099Z","iopub.status.idle":"2021-06-18T12:27:09.342955Z","shell.execute_reply.started":"2021-06-18T12:27:09.332062Z","shell.execute_reply":"2021-06-18T12:27:09.341792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ndef dicom2array(path, voi_lut=True, fix_monochrome=True):\n    dicom = pydicom.read_file(path)\n    # VOI LUT (if available by DICOM device) is used to\n    # transform raw DICOM data to \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and 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    return data\n'''","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:27:09.34424Z","iopub.execute_input":"2021-06-18T12:27:09.34473Z","iopub.status.idle":"2021-06-18T12:27:09.350804Z","shell.execute_reply.started":"2021-06-18T12:27:09.344695Z","shell.execute_reply":"2021-06-18T12:27:09.349939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" def dicom2array(path, voi_lut=True, fix_monochrome=True):\n        dicom = pydicom.read_file(path)\n        \n        if voi_lut:\n            data = apply_voi_lut(dicom.pixel_array, dicom)\n        else:\n            data = dicom.pixel_array\n        \n        if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n            data = np.amax(data) - data\n        \n        data = data - np.min(data)\n        data = data / np.max(data)\n        data = (data * 255).astype(np.uint8)\n        \n        #image = self.transform(date)\n        \n        return data","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:27:09.352268Z","iopub.execute_input":"2021-06-18T12:27:09.352812Z","iopub.status.idle":"2021-06-18T12:27:09.360491Z","shell.execute_reply.started":"2021-06-18T12:27:09.352778Z","shell.execute_reply":"2021-06-18T12:27:09.359581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CDataset(Dataset):\n    def __init__(self, df, tfms=None):\n        self.df = df\n        self.tfms = tfms\n        \n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, index):\n        img_path  = self.df.loc[index, 'fname']\n        img = dicom2array(img_path)\n        img = PILImage.create(img)\n        tfm_img = self.tfms(img)\n        \n        return torch.cat([tfm_img, tfm_img, tfm_img]) ","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:27:09.361664Z","iopub.execute_input":"2021-06-18T12:27:09.362053Z","iopub.status.idle":"2021-06-18T12:27:09.371781Z","shell.execute_reply.started":"2021-06-18T12:27:09.362018Z","shell.execute_reply":"2021-06-18T12:27:09.370988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfms = transforms.Compose([transforms.Resize((sz, sz)), \n                           transforms.ToTensor(), \n                           transforms.Normalize(mean=[0.485], std=[0.229])])","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:27:09.375278Z","iopub.execute_input":"2021-06-18T12:27:09.375599Z","iopub.status.idle":"2021-06-18T12:27:09.380656Z","shell.execute_reply.started":"2021-06-18T12:27:09.375566Z","shell.execute_reply":"2021-06-18T12:27:09.379663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = CDataset(df, tfms=tfms)","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:27:09.381908Z","iopub.execute_input":"2021-06-18T12:27:09.382569Z","iopub.status.idle":"2021-06-18T12:27:09.389541Z","shell.execute_reply.started":"2021-06-18T12:27:09.382472Z","shell.execute_reply":"2021-06-18T12:27:09.388686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loader = DataLoader(dataset=dataset, batch_size=bs, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:27:09.390833Z","iopub.execute_input":"2021-06-18T12:27:09.391234Z","iopub.status.idle":"2021-06-18T12:27:09.39987Z","shell.execute_reply.started":"2021-06-18T12:27:09.391145Z","shell.execute_reply":"2021-06-18T12:27:09.398965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"next(iter(loader))","metadata":{"execution":{"iopub.status.busy":"2021-06-18T12:27:09.400801Z","iopub.execute_input":"2021-06-18T12:27:09.401036Z","iopub.status.idle":"2021-06-18T12:27:09.733562Z","shell.execute_reply.started":"2021-06-18T12:27:09.401014Z","shell.execute_reply":"2021-06-18T12:27:09.731807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing and loading model/bench","metadata":{}},{"cell_type":"code","source":"#modeified effdet's EfficientDet to calculate classification output from backbone's output\nclass EfficientDetClassi(nn.Module):\n    def __init__(self, config, pretrained_backbone=True, classi_class=None, alternate_init=False):\n        super(EfficientDetClassi, self).__init__()\n        self.config = config\n        set_config_readonly(self.config)\n        self.backbone = timm.create_model(\n            config.backbone_name, features_only=True,\n            out_indices=self.config.backbone_indices or (2, 3, 4),\n            pretrained=pretrained_backbone, **config.backbone_args)\n        feature_info = get_feature_info(self.backbone)\n        self.fpn = BiFpn(self.config, feature_info)\n        self.class_net = HeadNet(self.config, num_outputs=self.config.num_classes)\n        self.box_net = HeadNet(self.config, num_outputs=4)\n\n        #INSERTED CODE STARTS\n        backbone_features = num_features_model(nn.Sequential(*self.backbone.children()))\n        self.classifier = create_head(backbone_features, classi_class) #fastai's create_head\n        #INSERTED CODE ENDS\n        '''\n        self.classifier = nn.Sequential(\n                                        nn.AdaptiveMaxPool2d(output_size=1),\n                                        nn.Flatten(),\n                                        nn.BatchNorm1d(backbone_features),\n                                        nn.Dropout(p=0.25, inplace=False),\n                                        nn.Linear(backbone_features, 512),\n                                        nn.ReLU(inplace=True),\n                                        nn.BatchNorm1d(512),\n                                        nn.Dropout(p=0.25, inplace=False),\n                                        nn.Linear(512, classi_class), \n        )\n  \n        '''\n        for n, m in self.named_modules():\n            if 'backbone' not in n:\n                if alternate_init:\n                    _init_weight_alt(m, n)\n                else:\n                    _init_weight(m, n)\n\n    @torch.jit.ignore()\n    def reset_head(self, num_classes=None, aspect_ratios=None, num_scales=None, alternate_init=False):\n        reset_class_head = False\n        reset_box_head = False\n        set_config_writeable(self.config)\n        if num_classes is not None:\n            reset_class_head = True\n            self.config.num_classes = num_classes\n        if aspect_ratios is not None:\n            reset_box_head = True\n            self.config.aspect_ratios = aspect_ratios\n        if num_scales is not None:\n            reset_box_head = True\n            self.config.num_scales = num_scales\n        set_config_readonly(self.config)\n\n        if reset_class_head:\n            self.class_net = HeadNet(self.config, num_outputs=self.config.num_classes)\n            for n, m in self.class_net.named_modules(prefix='class_net'):\n                if alternate_init:\n                    _init_weight_alt(m, n)\n                else:\n                    _init_weight(m, n)\n\n        if reset_box_head:\n            self.box_net = HeadNet(self.config, num_outputs=4)\n            for n, m in self.box_net.named_modules(prefix='box_net'):\n                if alternate_init:\n                    _init_weight_alt(m, n)\n                else:\n                    _init_weight(m, n)\n\n    @torch.jit.ignore()\n    def toggle_head_bn_level_first(self):\n        \"\"\" Toggle the head batchnorm layers between being access with feature_level first vs repeat\n        \"\"\"\n        self.class_net.toggle_bn_level_first()\n        self.box_net.toggle_bn_level_first()\n\n    def forward(self, x):\n        x_b = self.backbone(x)\n        x = self.fpn(x_b)\n        x_class = self.class_net(x)\n        x_box = self.box_net(x)\n        x_classi = self.classifier(x_b[2]) #INSERTED CODE\n        return x_class, x_box, x_classi #returns x_classi on top of original","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DetClassiBenchPredict(nn.Module):\n    def __init__(self, model):\n        super(DetClassiBenchPredict, self).__init__()\n        self.model = model\n        self.config = model.config  # FIXME remove this when we can use @property (torchscript limitation)\n        self.num_levels = model.config.num_levels\n        self.num_classes = model.config.num_classes\n        self.anchors = Anchors.from_config(model.config)\n        self.max_detection_points = model.config.max_detection_points\n        self.max_det_per_image = model.config.max_det_per_image\n        self.soft_nms = model.config.soft_nms\n\n    def forward(self, x, img_info=None):\n        class_out, box_out, classi_out = self.model(x)\n        class_out, box_out, indices, classes = _post_process(\n            class_out, box_out, num_levels=self.num_levels, num_classes=self.num_classes,\n            max_detection_points=self.max_detection_points)\n        if img_info is None:\n            img_scale, img_size = None, None\n        else:\n            img_scale, img_size = img_info['img_scale'], img_info['img_size']\n        return _batch_detection(x.shape[0], class_out, box_out, self.anchors.boxes, indices, classes,\n                                img_scale, img_size, max_det_per_image=3, soft_nms=self.soft_nms\n                                ), classi_out","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model_m(model_name, bench_task='', num_classes=None, pretrained=False,\n                 checkpoint_path='', checkpoint_ema=False, img_size=None, **kwargs):\n\n    config = get_efficientdet_config(model_name)\n    config.image_size = (img_size, img_size) if isinstance(img_size, int) else img_size\n\n    return create_model_from_config_m(config, bench_task=bench_task, num_classes=num_classes, pretrained=pretrained,\n                                      checkpoint_path=checkpoint_path, checkpoint_ema=checkpoint_ema, **kwargs)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model_from_config_m(\n        config, bench_task='', num_classes=None, pretrained=False,\n        checkpoint_path='', checkpoint_ema=False, **kwargs):\n\n    pretrained_backbone = kwargs.pop('pretrained_backbone', True)\n    if pretrained or checkpoint_path:\n        pretrained_backbone = False  # no point in loading backbone weights\n\n    # Config overrides, override some config values via kwargs.\n    overrides = (\n        'redundant_bias', 'label_smoothing', 'legacy_focal', 'jit_loss', 'soft_nms', 'max_det_per_image', 'image_size')\n    for ov in overrides:\n        value = kwargs.pop(ov, None)\n        if value is not None:\n            setattr(config, ov, value)\n\n    labeler = kwargs.pop('bench_labeler', False)\n\n    # create the base model\n    model = EfficientDetClassi(config, pretrained_backbone=pretrained_backbone, **kwargs)\n    \n    # pretrained weights are always spec'd for original config, load them before we change the model\n    if pretrained:\n        load_pretrained(model, config.url)\n\n    # reset model head if num_classes doesn't match configs\n    if num_classes is not None and num_classes != config.num_classes:\n        model.reset_head(num_classes=num_classes)\n\n    # load an argument specified training checkpoint\n    if checkpoint_path:\n        load_checkpoint(model, checkpoint_path, use_ema=checkpoint_ema)\n\n    # wrap model in task specific training/prediction bench if set\n    if bench_task == 'train':\n        model = DetClassiBenchTrain(model, create_labeler=True)\n    elif bench_task == 'predict':\n        model = DetClassiBenchPredict(model)\n    return model","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def splitter(m):\n    s = nn.Sequential(*m.model.children())\n    return L(s[0], s[1:]).map(params)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bench = create_model_m('tf_efficientdet_d3', \n                        'predict',  \n                        num_classes=2,\n                        classi_class=4,\n                        img_size=256)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load checkpoint","metadata":{}},{"cell_type":"code","source":"PATH = Path('../input/siim-covid-dc/tf_d3.2.pth')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bench.load_state_dict(torch.load(PATH))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\nbench.to(device)\nbench.model.eval()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = None\nclassis = None\nfor batch_idx, data in tqdm(enumerate(loader), total=len(loader)):\n    data = data.to(device=device)\n    with torch.no_grad():\n        pred, classi = bench(data)\n        if preds == None:\n            preds = pred.detach().cpu()\n            classis = classi.detach().cpu()\n        else:\n            preds = torch.cat([preds, pred.detach().cpu()], 0)\n            classis = torch.cat([classis, classi.detach().cpu()], 0)\n    gc.collect()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing submission file","metadata":{}},{"cell_type":"code","source":"df['box_1'] = list(np.array(preds)[:,0,:])\ndf['box_2'] = list(np.array(preds)[:,1,:])\ndf['box_3'] = list(np.array(preds)[:,2,:])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['conf_1'] = list(np.array(preds)[:,0,4])\ndf['conf_2'] = list(np.array(preds)[:,1,4])\ndf['conf_3'] = list(np.array(preds)[:,2,4])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['cls_1'] = list(np.array(preds)[:,0,5])\ndf['cls_2'] = list(np.array(preds)[:,1,5])\ndf['cls_3'] = list(np.array(preds)[:,2,5])\ndf['cls_1'] = df['cls_1'].map(bc_i2o)\ndf['cls_2'] = df['cls_2'].map(bc_i2o)\ndf['cls_3'] = df['cls_3'].map(bc_i2o)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def scale(row, box):\n    x_s = row.x_scale\n    y_s = row.x_scale\n    scale = [x_s, y_s, x_s, y_s]\n    \n    return row[box][:4]*scale","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['bbox_1'] = df.apply(lambda x: scale(x, 'box_1'), axis=1)\ndf['bbox_2'] = df.apply(lambda x: scale(x, 'box_2'), axis=1)\ndf['bbox_3'] = df.apply(lambda x: scale(x, 'box_3'), axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sm = nn.Softmax()\ndf['classification'] = np.array(torch.argmax(sm(classis),1).unsqueeze(-1))\ndf['classification'] = df['classification'].map(cc_i2o)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediciton_im(row, conf_th):\n    \n    predstr = ''\n    \n    if row.cls_1 == 'none':\n        predstr = 'none 1 0 0 1 1'\n        \n        return predstr\n        \n    elif row.cls_1 != 'none':\n        if row.conf_1 >= conf_th:\n            bb = [str(f'{b:.3f}') for b in row.bbox_1]\n            predstr += ' '.join(['opacity', str(f'{row.conf_1:.3f}'), str(bb[0]), str(bb[1]), str(bb[2]), str(bb[3]), ' '])\n            \n        if row.conf_2 >= conf_th:\n            bb = [str(f'{b:.3f}') for b in row.bbox_2]\n            predstr += ' '.join(['opacity', str(f'{row.conf_2:.3f}'), str(bb[0]), str(bb[1]), str(bb[2]), str(bb[3]), ' '])\n            \n        if row.conf_3 >= conf_th:\n            bb = [str(f'{b:.3f}') for b in row.bbox_3]\n            predstr += ' '.join(['opacity', str(f'{row.conf_3:.3f}'), str(bb[0]), str(bb[1]), str(bb[2]), str(bb[3])])\n    \n        if len(predstr)>0 and predstr[-1] == ' ':\n            predstr = predstr[:-1]\n        \n        if len(predstr)>0 and predstr[-1] == ' ':\n            predstr = predstr[:-1]\n        \n        elif row.conf_1 < conf_th:\n            predstr = 'none 1 0 0 1 1'\n    \n        return predstr","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prediction_stu(row):\n    p = row.classification.lower().split()[0]\n    predstr = ' '.join([p,'1 0 0 1 1'])\n    return predstr","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['PredStrIm'] = df.apply(lambda row: prediciton_im(row, 0.25), 1)\ndf['PredStrStu'] = df.apply(lambda row: prediction_stu(row), 1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stu_ = []\nfor stu in df['Study'].unique():\n    mask = df[df['Study'] == stu]['PredStrStu'].values\n    comb_str = ''\n\n    for i, e in enumerate(mask):\n        comb_str += ''.join([e, ' '])\n    comb_str = comb_str[:-1]\n    stu_.append([f'{stu}_study', comb_str])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_stu = pd.DataFrame(stu_, columns=['id', 'PredictionString'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_stu['PredictionString'].value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_img = df[['Image', 'PredStrIm']]\ndf_img['id'] = df_img['Image'].apply(lambda x: f'{x.split(\".dcm\")[0]}_image')\ndf_img.drop('Image', axis=1, inplace=True)\ndf_img.columns = ['PredictionString', 'id']\ndf_img = df_img[['id', 'PredictionString']]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub = pd.concat([df_stu, df_img])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub = df_sub[['id', 'PredictionString']]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analysing Predicitons","metadata":{}},{"cell_type":"code","source":"#pred ---> boxes[:4], scores[4], class[5]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_bboxes(preds, x_scale, y_scale):\n    bboxes = []\n    bconfs = []\n    bclses = []\n    for pred in preds:\n        bbox = pred[:4]\n        bbox = bbox * [x_scale, y_scale, x_scale, y_scale]\n        bboxes.append(bbox)\n        \n        bconfs.append(pred[4])\n        bclses.append(pred[5])\n        \n    return bboxes, bconfs, bclses ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_bbox(img, bboxes, bconfs, bclses, color=(255, 0, 0), thickness=5):\n    \n    img1 = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)    \n\n    for i, bbox in enumerate(bboxes):\n        x_min, y_min, x_max, y_max = map(int, bbox)\n        cv2.rectangle(img1, (x_min, y_min), (x_max, y_max), color=color, thickness=thickness)\n        cv2.putText(img1, bc_i2o[bclses[i]], (x_min, y_min-20), \n                    cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, \n                    thickness=thickness, color=(255,255,255))\n        cv2.putText(img1, str(f'{bconfs[i]:.4f}'), (x_min, y_min-120), \n                    cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, \n                    thickness=thickness, color=(255,255,255))\n        '''\n        if target is not None:\n            y_min, x_min, y_max, x_max = map(int, target)\n            cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color=(0,255,0), thickness=thickness)\n        '''\n    return img1","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_prediction(index, preds, classi, vocab, sm=nn.Softmax()):\n    \n    preds = preds[index]\n    preds = np.array(preds.detach().cpu())\n    classi = classi[index].detach().cpu()\n    \n    classi = np.argmax(np.array(sm(classi)))\n    predicted_label = vocab[classi]\n    \n    fn = df.fname[index]\n    x_scale = df.x_scale[index]\n    y_scale = df.y_scale[index]\n    \n    img = dicom2array(fn)\n    bboxes, bconfs, bclses = prepare_bboxes(preds, x_scale, y_scale)\n    img_bboxes = draw_bbox(img, bboxes, bconfs, bclses)\n    \n    plt.figure(figsize=(12,10))\n    plt.imshow(img_bboxes, cmap='gray')\n    plt.title(predicted_label)\n    \n    #return img_bboxes","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_prediction(6, pred, classi, cc_i20)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing submission file","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}