Source code for cerebras.modelzoo.data.vision.segmentation.transforms.custom_transforms
# Copyright 2022 Cerebras Systems.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Adapted from: https://github.com/MIC-DKFZ/nnUNet/blob/master/nnunet/training/data_augmentation/
# custom_transforms.py (commit id: f2282ed)
# Copyright 2020 Division of Medical Image Computing, German Cancer Research Center (DKFZ), Heidelberg, Germany
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
[docs]class MaskTransform:
def __init__(
self,
dct_for_where_it_was_used,
mask_idx_in_seg=1,
set_outside_to=0,
data_key="data",
seg_key="seg",
):
"""
data[mask < 0] = 0
Sets everything outside the mask to 0. CAREFUL! outside is defined as < 0, not =0 (in the Mask)!!!
:param dct_for_where_it_was_used:
:param mask_idx_in_seg:
:param set_outside_to:
:param data_key:
:param seg_key:
"""
self.dct_for_where_it_was_used = dct_for_where_it_was_used
self.seg_key = seg_key
self.data_key = data_key
self.set_outside_to = set_outside_to
self.mask_idx_in_seg = mask_idx_in_seg
def __call__(self, **data_dict):
seg = data_dict.get(self.seg_key)
if seg is None or seg.shape[1] < self.mask_idx_in_seg:
raise Warning(
"mask not found, seg may be missing or seg[:, mask_idx_in_seg] may not exist"
)
data = data_dict.get(self.data_key)
for b in range(data.shape[0]):
mask = seg[b, self.mask_idx_in_seg]
for c in range(data.shape[1]):
if self.dct_for_where_it_was_used[c]:
data[b, c][mask < 0] = self.set_outside_to
data_dict[self.data_key] = data
return data_dict