Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
[A.PadIfNeeded(333, 512), ATorch.PadIfNeededTorch(333, 512)],
[A.Crop(11, 5, 72, 36), ATorch.CropTorch(11, 5, 72, 36)],
[A.VerticalFlip(), ATorch.VerticalFlipTorch()],
[A.HorizontalFlip(), ATorch.HorizontalFlipTorch()],
[A.Flip(), ATorch.FlipTorch()],
[A.Transpose(), ATorch.TransposeTorch()],
[
A.LongestMaxSize(interpolation=cv2.INTER_NEAREST),
ATorch.LongestMaxSizeTorch(interpolation=cv2.INTER_NEAREST),
],
[
A.SmallestMaxSize(interpolation=cv2.INTER_NEAREST),
ATorch.SmallestMaxSizeTorch(interpolation=cv2.INTER_NEAREST),
],
[
A.Resize(100, 100, interpolation=cv2.INTER_NEAREST),
ATorch.ResizeTorch(100, 100, interpolation=cv2.INTER_NEAREST),
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.HorizontalFlip(p=0.5),
normalization,
],
p=1,
)
val_augmentations = albu.Compose(
[
albu.PadIfNeeded(
min_height=1024, min_width=2048, border_mode=cv2.BORDER_CONSTANT, mask_value=ignore_index, p=1
),
normalization,
],
p=1,
)
test_augmentations = albu.Compose([normalization], p=1)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[150, 200, 250], gamma=0.1)
train_image_path = Path("data/train/images")
train_mask_path = Path("data/train/masks")
val_image_path = Path("data/val/images")
val_mask_path = Path("data/val/masks")
def valid_transform(image_size=224):
transforms = [
LongestMaxSize(max_size=image_size),
PadIfNeeded(image_size, image_size, border_mode=cv2.BORDER_CONSTANT),
post_transform()
]
transforms = Compose(transforms)
return transforms
# Augmentation:
basic_aug = Compose(
[
Normalize(mean=(config.TRAIN.MEAN,), std=(config.TRAIN.STD,), max_pixel_value=1),
PadIfNeeded(
min_height=config.TRAIN.PATCH_SIZE,
min_width=config.TRAIN.PATCH_SIZE,
border_mode=config.OPENCV_BORDER_CONSTANT,
always_apply=True,
mask_value=255,
value=0,
),
Resize(
config.TRAIN.AUGMENTATIONS.RESIZE.HEIGHT, config.TRAIN.AUGMENTATIONS.RESIZE.WIDTH, always_apply=True,
),
PadIfNeeded(
min_height=config.TRAIN.AUGMENTATIONS.PAD.HEIGHT,
min_width=config.TRAIN.AUGMENTATIONS.PAD.WIDTH,
border_mode=config.OPENCV_BORDER_CONSTANT,
always_apply=True,
mask_value=255,
),
]
)
if config.TRAIN.AUGMENTATION:
train_aug = Compose([basic_aug, HorizontalFlip(p=0.5)])
val_aug = basic_aug
else:
train_aug = val_aug = basic_aug
# Training and Validation Loaders:
TrainPatchLoader = get_patch_loader(config)
"LongestMaxSizeTorch",
"SmallestMaxSizeTorch",
"ResizeTorch",
"RandomRotate90Torch",
"RotateTorch",
"RandomScaleTorch",
"ShiftScaleRotateTorch",
"CenterCropTorch",
"RandomCropTorch",
"RandomCropNearBBoxTorch",
"RandomSizedCropTorch",
"RandomResizedCropTorch",
]
class PadIfNeededTorch(BasicTransformTorch, A.PadIfNeeded):
"""Pad side of the image / max if side is less than desired number.
Args:
min_height (int): minimal result image height.
min_width (int): minimal result image width.
border_mode (str): ``'constant'``, ``'reflect'``, ``'replicate'`` or ``'circular'``. Default: ``'reflect'``.
value (int, float, list of int, lisft of float): padding value if border_mode is ```'constant'```.
mask_value (int, float,
list of int,
lisft of float): padding value for mask if border_mode is cv2.BORDER_CONSTANT.
p (float): probability of applying the transform. Default: 1.0.
Targets:
image, mask, bbox, keypoints
Image types:
),
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),
albu.Blur(p=0.5),
albu.CoarseDropout(p=0.5, max_height=26, max_width=16),
albu.OneOf([albu.HueSaturationValue(p=0.5), albu.RGBShift(p=0.5)], p=0.5),
normalization,
],
p=1,
)
val_augmentations = albu.Compose(
[
albu.PadIfNeeded(
min_height=1024, min_width=2048, border_mode=cv2.BORDER_CONSTANT, mask_value=ignore_index, p=1
),
normalization,
],
p=1,
)
test_augmentations = albu.Compose([normalization], p=1)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[150, 200, 250], gamma=0.1)
train_image_path = Path("data/train/images")
train_mask_path = Path("data/train/masks")
val_image_path = Path("data/val/images")
val_mask_path = Path("data/val/masks")
),
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),
albu.Blur(p=0.5),
albu.CoarseDropout(p=0.5, max_height=26, max_width=16),
albu.OneOf([albu.HueSaturationValue(p=0.5), albu.RGBShift(p=0.5)], p=0.5),
normalization,
],
p=1,
)
val_augmentations = albu.Compose(
[
albu.PadIfNeeded(
min_height=1024, min_width=2048, border_mode=cv2.BORDER_CONSTANT, mask_value=ignore_index, p=1
),
normalization,
],
p=1,
)
test_augmentations = albu.Compose([normalization], p=1)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[150, 200, 250], gamma=0.1)
train_image_path = Path("data/train/images")
train_mask_path = Path("data/train/masks")
val_image_path = Path("data/val/images")
val_mask_path = Path("data/val/masks")
def do_tta(img, TTA, preprocess):
imgs = []
if TTA == 'flip':
augmentation = HorizontalFlip(p=1)
data = {'image': img}
img2 = augmentation(**data)['image']
for im in [img, img2]:
im = np.array(im, np.float32)
im = cv2.resize(im, (args.resize_size, args.resize_size))
augmentation = PadIfNeeded(min_height=args.input_size, min_width=args.input_size, p=1.0, border_mode=4)
data = {"image": im}
im = augmentation(**data)["image"]
im = np.array(im, np.float32)
imgs.append(preprocess(im))
return imgs
def pre_transforms(image_size=224):
return Compose(
[
LongestMaxSize(max_size=image_size),
PadIfNeeded(
image_size, image_size, border_mode=cv2.BORDER_CONSTANT
),