Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_transform_pipeline_serialization(seed, image, mask):
aug = A.Compose(
[
A.OneOrOther(
A.Compose(
[
A.Resize(1024, 1024),
A.RandomSizedCrop(min_max_height=(256, 1024), height=512, width=512, p=1),
A.OneOf(
[
A.RandomSizedCrop(min_max_height=(256, 512), height=384, width=384, p=0.5),
A.RandomSizedCrop(min_max_height=(256, 512), height=512, width=512, p=0.5),
]
),
]
),
A.Compose(
[
A.Resize(1024, 1024),
A.RandomSizedCrop(min_max_height=(256, 1025), height=256, width=256, p=1),
A.OneOf([A.HueSaturationValue(p=0.5), A.RGBShift(p=0.7)], p=1),
]
),
),
A.HorizontalFlip(p=1),
A.RandomBrightnessContrast(p=0.5),
]
def transforms_train():
return A.Compose([
A.Resize(height=307, width=950),
A.RandomSizedCrop(min_max_height=(230, 307), height=256, width=896, w2h_ratio=950 / 307),
A.HorizontalFlip(),
# A.JpegCompression(quality_lower=60, quality_upper=100),
# A.RandomGamma(),
A.Normalize(mean=(Experiment.mean, Experiment.mean, Experiment.mean), std=(Experiment.std, Experiment.std, Experiment.std)),
ChannelTranspose()
])
def get_medium_augmentations(image_size):
return A.Compose([
A.OneOf([
A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.1,
rotate_limit=15,
border_mode=cv2.BORDER_CONSTANT, value=0),
A.OpticalDistortion(distort_limit=0.11, shift_limit=0.15,
border_mode=cv2.BORDER_CONSTANT,
value=0),
A.NoOp()
]),
A.RandomSizedCrop(min_max_height=(int(image_size[0] * 0.75), image_size[0]),
height=image_size[0],
width=image_size[1], p=0.3),
A.OneOf([
A.RandomBrightnessContrast(brightness_limit=0.5,
contrast_limit=0.4),
IndependentRandomBrightnessContrast(brightness_limit=0.25,
contrast_limit=0.24),
A.RandomGamma(gamma_limit=(50, 150)),
A.NoOp()
]),
A.OneOf([
A.RGBShift(r_shift_limit=20, b_shift_limit=15, g_shift_limit=15),
A.HueSaturationValue(hue_shift_limit=5,
sat_shift_limit=5),
A.NoOp()
]),
optimizer = RAdam(
[
{"params": model.decoder.parameters(), "lr": train_parameters["lr"]},
# decrease lr for encoder in order not to permute
# pre-trained weights with large gradients on training start
{"params": model.encoder.parameters(), "lr": train_parameters["lr"] / 100},
],
weight_decay=1e-4,
)
normalization = albu.Normalize(mean=mean, std=std, p=1)
train_augmentations = albu.Compose(
[
albu.RandomSizedCrop(
min_max_height=(
int(0.5 * (train_parameters["height_crop_size"])),
int(2 * (train_parameters["height_crop_size"])),
),
height=train_parameters["height_crop_size"],
width=train_parameters["width_crop_size"],
w2h_ratio=1.0,
p=1,
),
albu.ShiftScaleRotate(
border_mode=cv2.BORDER_CONSTANT, rotate_limit=10, scale_limit=0, p=0.5, mask_value=ignore_index
),
albu.HorizontalFlip(p=0.5),
normalization,
],
p=1,
def apply(self, img, h_start=0, w_start=0, **params):
return F.random_crop(img, self.height, self.width, h_start, w_start)
class RandomCropNearBBoxTorch(BasicTransformTorch, A.RandomCropNearBBox):
def apply(self, img, x_min=0, x_max=0, y_min=0, y_max=0, **params):
return F.clamping_crop(img, x_min, y_min, x_max, y_max)
class _BaseRandomSizedCropTorch(BasicTransformTorch, _BaseRandomSizedCrop):
def apply(self, img, crop_height=0, crop_width=0, h_start=0, w_start=0, interpolation=cv2.INTER_LINEAR, **params):
crop = F.random_crop(img, crop_height, crop_width, h_start, w_start)
return F.resize(crop, self.height, self.width, interpolation)
class RandomSizedCropTorch(_BaseRandomSizedCropTorch, A.RandomSizedCrop):
pass
class RandomResizedCropTorch(_BaseRandomSizedCropTorch, A.RandomResizedCrop):
def get_params_dependent_on_targets(self, params):
img = params["image"]
height, width = img.shape[-2:]
area = height * width
for _attempt in range(10):
target_area = random.uniform(*self.scale) * area
log_ratio = (math.log(self.ratio[0]), math.log(self.ratio[1]))
aspect_ratio = math.exp(random.uniform(*log_ratio))
w = int(round(math.sqrt(target_area * aspect_ratio)))
h = int(round(math.sqrt(target_area / aspect_ratio)))
def crop_transform_xview2(image_size: Tuple[int, int], min_scale=0.4, max_scale=0.75, input_size=1024):
return A.OneOrOther(
A.RandomSizedCrop(
(int(image_size[0] * min_scale), int(min(input_size, image_size[0] * max_scale))),
image_size[0],
image_size[1],
),
A.Compose(
[A.Resize(input_size * 2, input_size * 2), A.CropNonEmptyMaskIfExists(image_size[0], image_size[1])]
),
optimizer = RAdam(
[
{"params": model.decoder.parameters(), "lr": train_parameters["lr"]},
# decrease lr for encoder in order not to permute
# pre-trained weights with large gradients on training start
{"params": model.encoder.parameters(), "lr": train_parameters["lr"] / 100},
],
weight_decay=1e-2,
)
normalization = albu.Normalize(mean=mean, std=std, p=1)
train_augmentations = albu.Compose(
[
albu.RandomSizedCrop(
min_max_height=(
int(0.5 * (train_parameters["height_crop_size"])),
int(2 * (train_parameters["height_crop_size"])),
),
height=train_parameters["height_crop_size"],
width=train_parameters["width_crop_size"],
w2h_ratio=1.0,
p=1,
),
albu.ShiftScaleRotate(
border_mode=cv2.BORDER_CONSTANT, rotate_limit=10, scale_limit=0, p=0.5, mask_value=ignore_index
),
albu.RandomBrightnessContrast(p=0.5),
albu.RandomGamma(p=0.5),
albu.ImageCompression(quality_lower=20, quality_upper=100, p=0.5),
albu.GaussNoise(p=0.5),
optimizer = RAdam(
[
{"params": model.decoder.parameters(), "lr": train_parameters["lr"]},
# decrease lr for encoder in order not to permute
# pre-trained weights with large gradients on training start
{"params": model.encoder.parameters(), "lr": train_parameters["lr"] / 100},
],
weight_decay=1e-3,
)
normalization = albu.Normalize(mean=mean, std=std, p=1)
train_augmentations = albu.Compose(
[
albu.RandomSizedCrop(
min_max_height=(
int(0.5 * (train_parameters["height_crop_size"])),
int(2 * (train_parameters["height_crop_size"])),
),
height=train_parameters["height_crop_size"],
width=train_parameters["width_crop_size"],
w2h_ratio=1.0,
p=1,
),
albu.ShiftScaleRotate(
border_mode=cv2.BORDER_CONSTANT, rotate_limit=10, scale_limit=0, p=0.5, mask_value=ignore_index
),
albu.RandomBrightnessContrast(p=0.5),
albu.RandomGamma(p=0.5),
albu.ImageCompression(quality_lower=20, quality_upper=100, p=0.5),
albu.GaussNoise(p=0.5),