Source code for cerebras.modelzoo.data.vision.segmentation.transforms.resample_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/batchgenerators (commit id: 01f225d)
#
# Copyright 2021 Division of Medical Image Computing, German Cancer Research Center (DKFZ)
# and Applied Computer Vision Lab, Helmholtz Imaging Platform
#
# 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.
import numpy as np
from cerebras.modelzoo.data.vision.segmentation.transforms.resample_augmentations import (
augment_linear_downsampling_scipy,
)
[docs]class SimulateLowResolutionTransform:
"""Downsamples each sample (linearly) by a random factor and upsamples to original resolution again
(nearest neighbor)
Info:
* Uses scipy zoom for resampling.
* Resamples all dimensions (channels, x, y, z) with same downsampling factor (like isotropic=True from
linear_downsampling_generator_nilearn)
Args:
zoom_range: can be either tuple/list/np.ndarray or tuple of tuple. If tuple/list/np.ndarray, then the zoom
factor will be sampled from zoom_range[0], zoom_range[1] (zoom < 0 = downsampling!). If tuple of tuple then
each inner tuple will give a sampling interval for each axis (allows for different range of zoom values for
each axis
p_per_channel:
per_channel (bool): whether to draw a new zoom_factor for each channel or keep one for all channels
channels (list, tuple): if None then all channels can be augmented. If list then only the channel indices can
be augmented (but may not always be depending on p_per_channel)
order_downsample:
order_upsample:
"""
[docs] def __init__(
self,
zoom_range=(0.5, 1),
per_channel=False,
p_per_channel=1,
channels=None,
order_downsample=1,
order_upsample=0,
data_key="data",
p_per_sample=1,
ignore_axes=None,
):
self.order_upsample = order_upsample
self.order_downsample = order_downsample
self.channels = channels
self.per_channel = per_channel
self.p_per_channel = p_per_channel
self.p_per_sample = p_per_sample
self.data_key = data_key
self.zoom_range = zoom_range
self.ignore_axes = ignore_axes
[docs] def __call__(self, **data_dict):
for b in range(len(data_dict[self.data_key])):
if np.random.uniform() < self.p_per_sample:
data_dict[self.data_key][b] = augment_linear_downsampling_scipy(
data_dict[self.data_key][b],
zoom_range=self.zoom_range,
per_channel=self.per_channel,
p_per_channel=self.p_per_channel,
channels=self.channels,
order_downsample=self.order_downsample,
order_upsample=self.order_upsample,
ignore_axes=self.ignore_axes,
)
return data_dict