{"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":"from kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nsecret_value_0 = user_secrets.get_secret(\"pw\")\nsecret_value_1 = user_secrets.get_secret(\"username\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-11T07:28:45.782793Z","iopub.execute_input":"2022-07-11T07:28:45.783268Z","iopub.status.idle":"2022-07-11T07:28:46.177917Z","shell.execute_reply.started":"2022-07-11T07:28:45.783175Z","shell.execute_reply":"2022-07-11T07:28:46.176885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://{secret_value_0}@github.com/{secret_value_1}/kaggle_uwm.git","metadata":{"execution":{"iopub.status.busy":"2022-07-11T07:28:46.179611Z","iopub.execute_input":"2022-07-11T07:28:46.180364Z","iopub.status.idle":"2022-07-11T07:28:47.90793Z","shell.execute_reply.started":"2022-07-11T07:28:46.180311Z","shell.execute_reply":"2022-07-11T07:28:47.906391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile kaggle_uwm/custom_modules/datasets/__init__.py\n\n# flake8: noqa:F401\nfrom . import pipelines\nfrom .external_dataset import CHAOSSeqDataset\nfrom .monai_dataset import MonaiDataset\nfrom .uwm_dataset import UWMDataset\nfrom .uwm_seq_dataset import UWMSeqDataset, UWMSeqNiftiDataset","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile kaggle_uwm/custom_modules/datasets/pipelines/aug.py\n\nimport random\n\nimport numpy as np\nimport pandas as pd\nimport torch\nimport torch.nn.functional as F\nfrom mmseg.datasets import PIPELINES\nfrom mmseg.datasets.pipelines.formatting import DC, to_tensor\nfrom monai import transforms as monai_transforms\n\n\n@PIPELINES.register_module(force=True)\nclass ToTorchTensor(object):\n    def __call__(self, results):\n        img = torch.from_numpy(results['img']).float()\n        results['img'] = img\n        if 'gt_semantic_seg' in results:\n            mask = torch.from_numpy(results['gt_semantic_seg']).float()\n            results['gt_semantic_seg'] = mask\n        return results\n\n\n@PIPELINES.register_module(force=True)\nclass Resize3D(object):\n    def __init__(self, size):\n        self.size = size\n\n    def __call__(self, results):\n        size = self.size\n        img = results['img'].unsqueeze(0).float()\n\n        new_h, new_w = size\n\n        n, c, d, h, w = img.size()\n        img = img.permute(0, 2, 1, 3, 4).reshape(n * d, c, h, w)\n        img = F.interpolate(img, size, mode='bilinear')\n        img = img.reshape(n, d, c, new_h, new_w).permute(0, 2, 1, 3, 4)\n        results['img'] = img.squeeze(0)\n        results['ori_shape'] = (d, h, w)\n\n        if 'gt_semantic_seg' in results:\n            mask = results['gt_semantic_seg'].unsqueeze(0).float()\n            n, c, d, h, w = mask.size()\n            mask = mask.permute(0, 2, 1, 3, 4).reshape(n * d, c, h, w)\n            mask = F.interpolate(mask, size, mode='bilinear')\n            mask = mask.reshape(n, d, c, new_h, new_w).permute(0, 2, 1, 3, 4)\n\n            results['gt_semantic_seg'] = mask.squeeze(0)\n        return results\n\n\n@PIPELINES.register_module(force=True)\nclass Normalize3D(object):\n    def __call__(self, results):\n        img = results['img']\n        img = (img - img.mean()) / img.std()\n        results['img'] = img\n        return results\n\n\n@PIPELINES.register_module(force=True)\nclass Normalize3DV2(object):\n    def __call__(self, results):\n        img = results['img']\n\n        # mask = img > 0\n        # assert mask.any(), 'mask is empty'\n        # img[mask] = (img[mask] / img[mask].max()) * 2 - 1\n        # print(img.min(), img.max())\n        # print(img.shape)\n        # exit()\n        # img = img / (img.max(1, keepdims=True).values + 1e-7)\n\n        results['img'] = img\n        return results\n\n\n@PIPELINES.register_module(force=True)\nclass FormatBundle3D(object):\n    def __call__(self, results):\n\n        if 'img' in results:\n            img = results['img']\n            results['img'] = DC(to_tensor(img), stack=True)\n        if 'gt_semantic_seg' in results:\n            mask = results['gt_semantic_seg']\n            results['gt_semantic_seg'] = DC(to_tensor(mask), stack=True)\n        return results\n\n    def __repr__(self):\n        return self.__class__.__name__\n\n\n@PIPELINES.register_module(force=True)\nclass RandomCutOut3D(object):\n    def __init__(self,\n                 prob,\n                 n_holes,\n                 cutout_shape=None,\n                 cutout_ratio=None,\n                 fill_in=(0, 0, 0),\n                 seg_fill_in=None):\n\n        assert 0 <= prob and prob <= 1\n        assert (cutout_shape is None) ^ (cutout_ratio is None), \\\n            'Either cutout_shape or cutout_ratio should be specified.'\n        assert (isinstance(cutout_shape, (list, tuple))\n                or isinstance(cutout_ratio, (list, tuple)))\n        if isinstance(n_holes, tuple):\n            assert len(n_holes) == 2 and 0 <= n_holes[0] < n_holes[1]\n        else:\n            n_holes = (n_holes, n_holes)\n        if seg_fill_in is not None:\n            assert (isinstance(seg_fill_in, int) and 0 <= seg_fill_in\n                    and seg_fill_in <= 255)\n        self.prob = prob\n        self.n_holes = n_holes\n        self.fill_in = fill_in\n        self.seg_fill_in = seg_fill_in\n        self.with_ratio = cutout_ratio is not None\n        self.candidates = cutout_ratio if self.with_ratio else cutout_shape\n        if not isinstance(self.candidates, list):\n            self.candidates = [self.candidates]\n\n    def __call__(self, results):\n        \"\"\"Call function to drop some regions of image.\"\"\"\n        cutout = True if np.random.rand() < self.prob else False\n        if cutout:\n            n, d, h, w = results['img'].shape\n            n_holes = np.random.randint(self.n_holes[0], self.n_holes[1] + 1)\n            for _ in range(n_holes):\n                x1 = np.random.randint(0, w)\n                y1 = np.random.randint(0, h)\n                index = np.random.randint(0, len(self.candidates))\n                if not self.with_ratio:\n                    cutout_w, cutout_h = self.candidates[index]\n                else:\n                    cutout_w = int(self.candidates[index][0] * w)\n                    cutout_h = int(self.candidates[index][1] * h)\n\n                x2 = np.clip(x1 + cutout_w, 0, w)\n                y2 = np.clip(y1 + cutout_h, 0, h)\n                results['img'][:, :, y1:y2, x1:x2] = self.fill_in\n\n                if self.seg_fill_in is not None:\n                    for key in results.get('seg_fields', []):\n                        results[key][:, :, y1:y2, x1:x2] = self.seg_fill_in\n\n        return results\n\n    def __repr__(self):\n        repr_str = self.__class__.__name__\n        repr_str += f'(prob={self.prob}, '\n        repr_str += f'n_holes={self.n_holes}, '\n        repr_str += (f'cutout_ratio={self.candidates}, ' if self.with_ratio\n                     else f'cutout_shape={self.candidates}, ')\n        repr_str += f'fill_in={self.fill_in}, '\n        repr_str += f'seg_fill_in={self.seg_fill_in})'\n        return\n\n\n@PIPELINES.register_module(force=True)\nclass MonaiTransform(object):\n    def __init__(self, name, **kwargs):\n        self.transform = getattr(monai_transforms, name)(**kwargs)\n\n    def __call__(self, results):\n        # torch.cuda.empty_cache()\n        img = results['img']\n        seg = results['gt_semantic_seg']\n\n        inp = dict(img=img, seg=seg)\n        out = self.transform(inp)\n\n        results['img'] = out['img']\n        results['gt_semantic_seg'] = out['seg']\n        return results\n\n\n@PIPELINES.register_module(force=True)\nclass CHWD2CDHW(object):\n    def __call__(self, results):\n\n        results['img'] = results['img'].permute(0, 3, 1, 2)\n        if 'gt_semantic_seg' in results:\n            results['gt_semantic_seg'] = results['gt_semantic_seg'].permute(\n                0, 3, 1, 2)\n        return results\n\n\n@PIPELINES.register_module(force=True)\nclass CDHW2CHWD(object):\n    def __call__(self, results):\n\n        results['img'] = results['img'].permute(0, 2, 3, 1)\n        if 'gt_semantic_seg' in results:\n            results['gt_semantic_seg'] = results['gt_semantic_seg'].permute(\n                0, 2, 3, 1)\n        return results\n\n\n@PIPELINES.register_module(force=True)\nclass ToCuda(object):\n    def __call__(self, results):\n        torch.cuda.empty_cache()\n        results['img'] = results['img'].cuda(non_blocking=True)\n        results['gt_semantic_seg'] = results['gt_semantic_seg'].cuda(\n            non_blocking=True)\n        return results\n\n\n@PIPELINES.register_module(force=True)\nclass ToCpu(object):\n    def __call__(self, results):\n\n        results['img'] = results['img'].cpu()\n        results['gt_semantic_seg'] = results['gt_semantic_seg'].cpu()\n        return results\n\n\n@PIPELINES.register_module(force=True)\nclass CropByBoxes:\n    def __init__(self, box_ann_file, prob=0.):\n        df = pd.read_csv(box_ann_file)\n        self.anns = dict(list(df.groupby('id')))\n        self.prob = prob\n\n    def __call__(self, results):\n        if (results['id'] in self.anns) and (random.random() < self.prob):\n            ann = self.anns[results['id']].iloc[0]\n            y1, x1, y2, x2 = ann['xmin'], ann['ymin'], ann['xmax'], ann['ymax']\n            x1 = max(0, x1)\n            x2 = min(results['img'].shape[3], x2)\n            y1 = max(0, y1)\n            y2 = min(results['img'].shape[2], y2)\n            results['img'] = results['img'][:, :, y1:y2, x1:x2]\n            results['gt_semantic_seg'] = results['gt_semantic_seg'][:, :,\n                                                                    y1:y2,\n                                                                    x1:x2]\n            results['box'] = [x1, y1, x2, y2]\n            results['img_shape'] = results['img'].shape\n        else:\n            c, d, h, w = results['img'].shape\n            results['box'] = [0, 0, w, h]\n        return results\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile kaggle_uwm/custom_modules/datasets/pipelines/__init__.py\n\n# flake8: noqa:F401\n# from .albu import Albu\nfrom .aug import (CDHW2CHWD, CHWD2CDHW, FormatBundle3D, MonaiTransform,\n                  Normalize3D, RandomCutOut3D, Resize3D, ToTorchTensor)\nfrom .dump_image import DumpImage3D\nfrom .formatting import (BinaryDefaultFormatBundle,\n                         BinaryDefaultFormatBundle3D, ChannelFirst3D,\n                         ImageToTensor3D, SeqTranspose, ToTensor3D,\n                         ToTensor3DFloat)\nfrom .loading import (LoadAnnotationsFromRLE, LoadMultiAnnotations,\n                      LoadMultiAnnotationsFromRLE, LoadMultiImagesFromFile)\nfrom .monai import Monai, MonaiAug\nfrom .test_time_aug import MultiScaleFlipAug3D\nfrom .transforms import (ConcatImgs3D, DivMax, Pad3d,\n                         PadSeg, SeqAddC, SeqDivMax, SeqGray, SeqNormalize,\n                         SeqPad, SeqPhotoMetricDistortion, SeqRandomFlip,\n                         SeqResize, SliceDrop, SliceEmb)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile kaggle_uwm/custom_modules/datasets/pipelines/transforms.py\n\nimport copy\n\nimport mmcv\nimport numpy as np\nfrom mmseg.datasets.builder import PIPELINES\nfrom mmseg.datasets.pipelines.transforms import (Normalize, Pad, RandomFlip,\n                                                 Resize)\n\n\n@PIPELINES.register_module()\nclass DivMax(object):\n    def __call__(self, results, max_value=None):\n        if max_value is None:\n            max_value = results['img'].max()\n        img = results['img'] / (max_value + 1e-7)\n        if len(img.shape) == 2:\n            img = img[:, :, np.newaxis].repeat(3, axis=-1)\n        if img.shape[2] == 1:\n            img = img.repeat(3, axis=-1)\n        results['img'] = img\n        return results\n\n\n@PIPELINES.register_module()\nclass SeqDivMax(DivMax):\n    def __call__(self, results):\n        outs = []\n        max_values = []\n        for _results in results:\n            max_values.append(_results['img'].max())\n        for _results in results:\n            _results = super().__call__(_results, np.max(max_values))\n            outs.append(_results)\n        return outs\n\n\n@PIPELINES.register_module()\nclass SeqGray(object):\n    def __call__(self, results):\n        outs = []\n        for _results in results:\n            _results['img'] = _results['img'][:, :, 0][:, :, np.newaxis]\n            outs.append(_results)\n        return outs\n\n\n@PIPELINES.register_module()\nclass SeqAddC(object):\n    def __call__(self, results):\n        outs = []\n        for _results in results:\n            _results['img'] = _results['img'][np.newaxis, :, :]\n            outs.append(_results)\n        return outs\n\n\n@PIPELINES.register_module()\nclass SeqMaskFloat(object):\n    def __call__(self, results):\n        outs = []\n        for _results in results:\n            for key in results.get('seg_fields', []):\n                _results[key] = _results[key].astype(np.float32)\n            outs.append(_results)\n        return outs\n\n\n@PIPELINES.register_module()\nclass BilinearResize(Resize):\n    def _resize_seg(self, results):\n        \"\"\"Resize semantic segmentation map with ``results['scale']``.\"\"\"\n        for key in results.get('seg_fields', []):\n            if self.keep_ratio:\n                gt_seg = mmcv.imrescale(results[key].astype(np.float32),\n                                        results['scale'],\n                                        interpolation='bilinear')\n            else:\n                gt_seg = mmcv.imresize(results[key].astype(np.float32),\n                                       results['scale'],\n                                       interpolation='bilinear')\n            results[key] = gt_seg\n\n\n@PIPELINES.register_module()\nclass SeqResize(BilinearResize):\n    \"\"\"Resize images.\n\n    Please refer to `mmdet.datasets.pipelines.transforms.py:Resize` for\n    detailed docstring.\n    Args:\n        share_params (bool): If True, share the resize parameters for all\n            images. Defaults to True.\n    \"\"\"\n    def __init__(self, share_params=True, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        self.share_params = share_params\n\n    def __call__(self, results):\n        \"\"\"Call function.\n\n        For each dict in results, call the call function of `Resize` to resize\n        image and corresponding annotations.\n        Args:\n            results (list[dict]): List of dict that from\n                :obj:`mmtrack.CocoVideoDataset`.\n        Returns:\n            list[dict]: List of dict that contains resized results,\n            'img_shape', 'pad_shape', 'scale_factor', 'keep_ratio' keys\n            are added into result dict.\n        \"\"\"\n        outs, scale = [], None\n        for i, _results in enumerate(results):\n            if self.share_params and i > 0:\n                _results['scale'] = scale\n            _results = super().__call__(_results)\n            if self.share_params and i == 0:\n                scale = _results['scale']\n            outs.append(_results)\n        return outs\n\n\n@PIPELINES.register_module()\nclass SeqRandomFlip(RandomFlip):\n    \"\"\"Randomly flip for images.\n\n    Please refer to `mmdet.datasets.pipelines.transforms.py:RandomFlip` for\n    detailed docstring.\n    Args:\n        share_params (bool): If True, share the flip parameters for all images.\n            Defaults to True.\n    \"\"\"\n    def __init__(self, share_params, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n        self.share_params = share_params\n\n    def __call__(self, results):\n        \"\"\"Call function.\n\n        For each dict in results, call `RandomFlip` to randomly flip image.\n        Args:\n            results (list[dict]): List of dict that from\n                :obj:`mmtrack.CocoVideoDataset`.\n        Returns:\n            list[dict]: List of dict that contains flipped results, 'flip',\n            'flip_direction' keys are added into the dict.\n        \"\"\"\n        if self.share_params:\n            if isinstance(self.direction, list):\n                # None means non-flip\n                direction_list = self.direction + [None]\n            else:\n                # None means non-flip\n                direction_list = [self.direction, None]\n\n            if isinstance(self.prob, list):\n                non_flip_ratio = 1 - sum(self.prob)\n                flip_ratio_list = self.flip_ratio + [non_flip_ratio]\n            else:\n                non_flip_ratio = 1 - self.prob\n                # exclude non-flip\n                single_ratio = self.prob / (len(direction_list) - 1)\n                flip_ratio_list = [single_ratio] * (len(direction_list) -\n                                                    1) + [non_flip_ratio]\n\n            cur_dir = np.random.choice(direction_list, p=flip_ratio_list)\n            flip = cur_dir is not None\n            flip_direction = cur_dir\n\n            for _results in results:\n                _results['flip'] = flip\n                _results['flip_direction'] = flip_direction\n\n        outs = []\n        for _results in results:\n            _results = super().__call__(_results)\n            outs.append(_results)\n        return outs\n\n\n@PIPELINES.register_module()\nclass SeqPhotoMetricDistortion(object):\n    \"\"\"Apply photometric distortion to image sequentially, every transformation\n    is applied with a probability of 0.5.\n\n    The position of random contrast is in\n    second or second to last.\n    1. random brightness\n    2. random contrast (mode 0)\n    3. convert color from BGR to HSV\n    4. random saturation\n    5. random hue\n    6. convert color from HSV to BGR\n    7. random contrast (mode 1)\n    8. randomly swap channels\n    Args:\n        brightness_delta (int): delta of brightness.\n        contrast_range (tuple): range of contrast.\n        saturation_range (tuple): range of saturation.\n        hue_delta (int): delta of hue.\n    \"\"\"\n    def __init__(self,\n                 share_params=True,\n                 brightness_delta=32,\n                 contrast_range=(0.5, 1.5),\n                 saturation_range=(0.5, 1.5),\n                 hue_delta=18):\n        self.share_params = share_params\n        self.brightness_delta = brightness_delta\n        self.contrast_lower, self.contrast_upper = contrast_range\n        self.saturation_lower, self.saturation_upper = saturation_range\n        self.hue_delta = hue_delta\n\n    def get_params(self):\n        \"\"\"Generate parameters.\"\"\"\n        params = dict()\n        # delta\n        if np.random.randint(2):\n            params['delta'] = np.random.uniform(-self.brightness_delta,\n                                                self.brightness_delta)\n        else:\n            params['delta'] = None\n        # mode\n        mode = np.random.randint(2)\n        params['contrast_first'] = True if mode == 1 else 0\n        # alpha\n        if np.random.randint(2):\n            params['alpha'] = np.random.uniform(self.contrast_lower,\n                                                self.contrast_upper)\n        else:\n            params['alpha'] = None\n        # saturation\n        if np.random.randint(2):\n            params['saturation'] = np.random.uniform(self.saturation_lower,\n                                                     self.saturation_upper)\n        else:\n            params['saturation'] = None\n        # hue\n        if np.random.randint(2):\n            params['hue'] = np.random.uniform(-self.hue_delta, self.hue_delta)\n        else:\n            params['hue'] = None\n        # swap\n        if np.random.randint(2):\n            params['permutation'] = np.random.permutation(3)\n        else:\n            params['permutation'] = None\n        return params\n\n    def photo_metric_distortion(self, results, params=None):\n        \"\"\"Call function to perform photometric distortion on images.\n\n        Args:\n            results (dict): Result dict from loading pipeline.\n            params (dict, optional): Pre-defined parameters. Default to None.\n        Returns:\n            dict: Result dict with images distorted.\n        \"\"\"\n        if params is None:\n            params = self.get_params()\n        results['img_info']['color_jitter'] = params\n\n        if 'img_fields' in results:\n            assert results['img_fields'] == ['img'], \\\n                'Only single img_fields is allowed'\n        img = results['img']\n        assert img.dtype == np.float32, \\\n            'PhotoMetricDistortion needs the input image of dtype np.float32,'\\\n            ' please set \"to_float32=True\" in \"LoadImageFromFile\" pipeline'\n        # random brightness\n        if params['delta'] is not None:\n            img += params['delta']\n\n        # mode == 0 --> do random contrast first\n        # mode == 1 --> do random contrast last\n        if params['contrast_first']:\n            if params['alpha'] is not None:\n                img *= params['alpha']\n\n        # convert color from BGR to HSV\n        img = mmcv.bgr2hsv(img)\n\n        # random saturation\n        if params['saturation'] is not None:\n            img[..., 1] *= params['saturation']\n\n        # random hue\n        if params['hue'] is not None:\n            img[..., 0] += params['hue']\n            img[..., 0][img[..., 0] > 360] -= 360\n            img[..., 0][img[..., 0] < 0] += 360\n\n        # convert color from HSV to BGR\n        img = mmcv.hsv2bgr(img)\n\n        # random contrast\n        if not params['contrast_first']:\n            if params['alpha'] is not None:\n                img *= params['alpha']\n\n        # randomly swap channels\n        if params['permutation'] is not None:\n            img = img[..., params['permutation']]\n\n        results['img'] = img\n        return results\n\n    def __call__(self, results):\n        \"\"\"Call function to perform photometric distortion on images.\n\n        Args:\n            results (dict): Result dict from loading pipeline.\n        Returns:\n            dict: Result dict with images distorted.\n        \"\"\"\n        if self.share_params:\n            params = self.get_params()\n        else:\n            params = None\n\n        outs = []\n        for _results in results:\n            _results = self.photo_metric_distortion(_results, params)\n            outs.append(_results)\n\n        return outs\n\n    def __repr__(self):\n        repr_str = self.__class__.__name__\n        repr_str += f'(\\nbrightness_delta={self.brightness_delta},\\n'\n        repr_str += 'contrast_range='\n        repr_str += f'{(self.contrast_lower, self.contrast_upper)},\\n'\n        repr_str += 'saturation_range='\n        repr_str += f'{(self.saturation_lower, self.saturation_upper)},\\n'\n        repr_str += f'hue_delta={self.hue_delta})'\n        return repr_str\n\n\n@PIPELINES.register_module()\nclass SeqPad(Pad):\n    \"\"\"Pad images.\n\n    Please refer to `mmdet.datasets.pipelines.transforms.py:Pad` for detailed\n    docstring.\n    \"\"\"\n    def __init__(self, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n\n    def __call__(self, results):\n        \"\"\"Call function.\n\n        For each dict in results, call the call function of `Pad` to pad image.\n        Args:\n            results (list[dict]): List of dict that from\n                :obj:`mmtrack.CocoVideoDataset`.\n        Returns:\n            list[dict]: List of dict that contains padding results,\n            'pad_shape', 'pad_fixed_size' and 'pad_size_divisor' keys are\n            added into the dict.\n        \"\"\"\n        outs = []\n        for _results in results:\n            _results = super().__call__(_results)\n            outs.append(_results)\n        return outs\n\n\n@PIPELINES.register_module()\nclass ConcatImgs3D(object):\n    def __call__(self, results):\n        new_results = dict()\n        if 'img' in results[0]:\n            imgs = [r['img'][:, :, :, np.newaxis] for r in results]\n            new_results['img_fields'] = ['img']\n            new_results['img'] = np.concatenate(imgs, axis=-1)\n\n            new_results['filename'] = results[0]['filename']\n            new_results['ori_filename'] = results[0]['img_info']['filename']\n            new_results['img_shape'] = results[0]['img_shape']\n            new_results['ori_shape'] = results[0]['ori_shape']\n            new_results['pad_shape'] = results[0]['pad_shape']\n            new_results['scale_factor'] = results[0]['scale_factor']\n            new_results['img_norm_cfg'] = results[0]['img_norm_cfg']\n            new_results[\n                'flip'] = results[0]['flip'] if 'flip' in results[0] else None\n            new_results['flip_direction'] = results[0][\n                'flip_direction'] if 'flip' in results[0] else None\n\n        if 'gt_semantic_seg' in results[0]:\n            gt_semantic_segs = [\n                r['gt_semantic_seg'][:, :, :, np.newaxis] for r in results\n            ]\n            new_results['seg_fields'] = ['gt_semantic_seg']\n            new_results['gt_semantic_seg'] = np.concatenate(gt_semantic_segs,\n                                                            axis=-1)\n\n        return new_results\n\n\n@PIPELINES.register_module()\nclass PadSeg(object):\n    \"\"\"Pad the image & mask.\n\n    There are two padding modes: (1) pad to a fixed size and (2) pad to the\n    minimum size that is divisible by some number.\n    Added keys are \"pad_shape\", \"pad_fixed_size\", \"pad_size_divisor\",\n    Args:\n        size (tuple, optional): Fixed padding size.\n        size_divisor (int, optional): The divisor of padded size.\n        pad_val (float, optional): Padding value. Default: 0.\n        seg_pad_val (float, optional): Padding value of segmentation map.\n            Default: 255.\n    \"\"\"\n    def __init__(self, size=None, size_divisor=None, seg_pad_val=255):\n        self.size = size\n        self.size_divisor = size_divisor\n        self.seg_pad_val = seg_pad_val\n        # only one of size and size_divisor should be valid\n        assert size is not None or size_divisor is not None\n        assert size is None or size_divisor is None\n\n    def _pad_seg(self, results):\n        \"\"\"Pad masks according to ``results['pad_shape']``.\"\"\"\n        for key in results.get('seg_fields', []):\n            if self.size is not None:\n                results[key] = mmcv.impad(results[key],\n                                          shape=self.size,\n                                          pad_val=self.seg_pad_val)\n            elif self.size_divisor is not None:\n                results[key] = mmcv.impad_to_multiple(results[key],\n                                                      self.size_divisor,\n                                                      pad_val=self.seg_pad_val)\n\n    def __call__(self, results):\n        \"\"\"Call function to pad images, masks, semantic segmentation maps.\n\n        Args:\n            results (dict): Result dict from loading pipeline.\n        Returns:\n            dict: Updated result dict.\n        \"\"\"\n\n        self._pad_seg(results)\n        return results\n\n    def __repr__(self):\n        repr_str = self.__class__.__name__\n        repr_str += f'(size={self.size}, size_divisor={self.size_divisor}'\n        return repr_str\n\n\n@PIPELINES.register_module()\nclass SeqNormalize(Normalize):\n    def __init__(self, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n\n    def __call__(self, results):\n        outs = []\n        for _results in results:\n            _results = super().__call__(_results)\n            outs.append(_results)\n        return outs\n\n\n@PIPELINES.register_module()\nclass Pad3d(object):\n    \"\"\"Pad the image & mask.\n\n    There are two padding modes: (1) pad to a fixed size and (2) pad to the\n    minimum size that is divisible by some number.\n    Added keys are \"pad_shape\", \"pad_fixed_size\", \"pad_size_divisor\",\n    Args:\n        size (tuple, optional): Fixed padding size.\n        size_divisor (int, optional): The divisor of padded size.\n        pad_val (float, optional): Padding value. Default: 0.\n        seg_pad_val (float, optional): Padding value of segmentation map.\n            Default: 255.\n    \"\"\"\n    def __init__(self, img_size, mask_size, pad_val=0, seg_pad_val=255):\n        self.img_size = img_size\n        self.mask_size = mask_size\n        self.pad_val = pad_val\n        self.seg_pad_val = seg_pad_val\n\n    def __call__(self, results):\n        \"\"\"Call function to pad images, masks, semantic segmentation maps.\n\n        Args:\n            results (dict): Result dict from loading pipeline.\n        Returns:\n            dict: Updated result dict.\n        \"\"\"\n        results['ori_seq_len'] = results['img'].shape[-1]\n        if 'img' in results:\n            _, h, w, d = results['img'].shape\n            pad_h = np.max(self.img_size[0] - h, 0)\n            pad_w = np.max(self.img_size[1] - w, 0)\n            pad_d = np.max(self.img_size[2] - d, 0)\n\n            results['img'] = np.pad(results['img'], ((0, 0), (0, pad_h),\n                                                     (0, pad_w), (0, pad_d)),\n                                    constant_values=self.pad_val)\n        if 'gt_semantic_seg' in results:\n            _, h, w, d = results['gt_semantic_seg'].shape\n            pad_h = np.max(self.mask_size[0] - h, 0)\n            pad_w = np.max(self.mask_size[1] - w, 0)\n            pad_d = np.max(self.mask_size[2] - d, 0)\n\n            results['gt_semantic_seg'] = np.pad(\n                results['gt_semantic_seg'],\n                ((0, 0), (0, pad_h), (0, pad_w), (0, pad_d)),\n                constant_values=self.seg_pad_val)\n        results['pad_shape'] = results['img'].shape[1:-1]\n        results['pad_fixed_size'] = self.img_size[:-1]\n        return results\n\n    def __repr__(self):\n        repr_str = self.__class__.__name__\n        repr_str += f'(size={self.size}, size_divisor={self.size_divisor}'\n        return repr_str\n\n\n@PIPELINES.register_module()\nclass SliceDrop(object):\n    def __init__(self, slices):\n        self.slices = slices\n\n    def __call__(self, results):\n        \"\"\"Call function to pad images, masks, semantic segmentation maps.\n\n        Args:\n            results (dict): Result dict from loading pipeline.\n        Returns:\n            dict: Updated result dict.\n        \"\"\"\n        if 'img' in results:\n            results['img'] = results['img'][:, :, :, :self.slices]\n        if 'gt_semantic_seg' in results:\n            results['gt_semantic_seg'] = results[\n                'gt_semantic_seg'][:, :, :, :self.slices]\n        return results\n\n\n@PIPELINES.register_module()\nclass Pad3dOld(object):\n    \"\"\"Pad the image & mask.\n\n    There are two padding modes: (1) pad to a fixed size and (2) pad to the\n    minimum size that is divisible by some number.\n    Added keys are \"pad_shape\", \"pad_fixed_size\", \"pad_size_divisor\",\n    Args:\n        size (tuple, optional): Fixed padding size.\n        size_divisor (int, optional): The divisor of padded size.\n        pad_val (float, optional): Padding value. Default: 0.\n        seg_pad_val (float, optional): Padding value of segmentation map.\n            Default: 255.\n    \"\"\"\n    def __init__(self, size=None, pad_val=0, seg_pad_val=255):\n        self.size = size\n        self.pad_val = pad_val\n        self.seg_pad_val = seg_pad_val\n\n    def __call__(self, results):\n        \"\"\"Call function to pad images, masks, semantic segmentation maps.\n\n        Args:\n            results (dict): Result dict from loading pipeline.\n        Returns:\n            dict: Updated result dict.\n        \"\"\"\n        results['ori_seq_len'] = results['img'].shape[-1]\n        if 'img' in results:\n            pad = np.ones(\n                (*results['img'].shape[:-1],\n                 self.size - results['img'].shape[-1])) * self.pad_val\n            pad = pad.astype(results['img'].dtype)\n            results['img'] = np.concatenate([results['img'], pad], axis=-1)\n        if 'gt_semantic_seg' in results:\n            pad = np.ones(\n                (*results['gt_semantic_seg'].shape[:-1], self.size -\n                 results['gt_semantic_seg'].shape[-1])) * self.seg_pad_val\n            pad = pad.astype(results['gt_semantic_seg'].dtype)\n            results['gt_semantic_seg'] = np.concatenate(\n                [results['gt_semantic_seg'], pad], axis=-1)\n        return results\n\n    def __repr__(self):\n        repr_str = self.__class__.__name__\n        repr_str += f'(size={self.size}, size_divisor={self.size_divisor}'\n        return repr_str\n\n\n@PIPELINES.register_module()\nclass SliceEmb(object):\n    def __call__(self, results):\n        results['ori_seq_len'] = results['img'].shape[-1]\n        if 'img' in results:\n            slice_emb = np.zeros_like(results['img'])\n            d = slice_emb.shape[-1]\n            slice_emb = slice_emb + np.linspace(-1, 1, d).reshape(1, 1, 1, -1)\n\n            results['img'] = np.concatenate(\n                [results['img'],\n                 slice_emb.astype(results['img'].dtype)],\n                axis=0)\n        return results\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile kaggle_uwm/custom_modules/models/backbones/__init__.py\n\n# flake8: noqa:F401\nfrom .monai_unet import MonaiDynUNet, MonaiUnet","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile kaggle_uwm/custom_modules/models/decode_heads/__init__.py\n\n# flake8: noqa:F401\nfrom .identity_head import IdentityHead, IdentityHead3D, PosOnlyIdentityHead3D","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile kaggle_uwm/custom_modules/models/segmentors/__init__.py\n\nfrom .encoder_decoder_3d import EncoderDecoder3D\nfrom .unet3d import UNet3DOrig","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r kaggle_uwm working/","metadata":{"execution":{"iopub.status.busy":"2022-07-11T07:31:42.735819Z","iopub.execute_input":"2022-07-11T07:31:42.736309Z","iopub.status.idle":"2022-07-11T07:31:43.51127Z","shell.execute_reply.started":"2022-07-11T07:31:42.736273Z","shell.execute_reply":"2022-07-11T07:31:43.509885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}