def __init__(self): super().__init__() if 'sewer' in hparams.dataset: if hparams.in_channels == 1: mean, std = [0.5], [0.5] else: mean, std = [0.5, 0.5, 0.5], [0.5, 0.5, 0.5] self.train_t = Augment.Compose([ #Augment.ToGray(p=1.0), Augment.SmallestMaxSize(max_size=hparams.image_height, interpolation=cv2.INTER_LINEAR, always_apply=True), Augment.RandomCrop(hparams.image_height, hparams.image_height, always_apply=True), Augment.HorizontalFlip(p=0.5), Augment.RandomBrightnessContrast(p=0.5), Augment.Normalize(mean, std) ]) self.test_t = Augment.Compose([ Augment.SmallestMaxSize(max_size=hparams.image_height, interpolation=cv2.INTER_LINEAR, always_apply=True), Augment.CenterCrop(hparams.image_height, hparams.image_height, always_apply=True), Augment.Normalize(mean, std) ]) else: print("> Unknown dataset. Terminating") exit()
def create_runtime_tfms(): mean, std = [0.5] * 3, [0.5] * 3 resize_to_64 = A.SmallestMaxSize(IMG_SIZE, interpolation=cv2.INTER_AREA) out = [ A.HorizontalFlip(p=0.5), A.Normalize(mean=mean, std=std), ToTensor() ] rand_crop = A.Compose([ A.SmallestMaxSize(IMG_SIZE + 8, interpolation=cv2.INTER_AREA), A.RandomCrop(IMG_SIZE, IMG_SIZE) ]) affine_1 = A.ShiftScaleRotate(shift_limit=0, scale_limit=0.1, rotate_limit=8, interpolation=cv2.INTER_CUBIC, border_mode=cv2.BORDER_REFLECT_101, p=1.0) affine_1 = A.Compose([affine_1, resize_to_64]) affine_2 = A.ShiftScaleRotate(shift_limit=0.06, scale_limit=(-0.06, 0.18), rotate_limit=6, interpolation=cv2.INTER_CUBIC, border_mode=cv2.BORDER_REFLECT_101, p=1.0) affine_2 = A.Compose([affine_2, resize_to_64]) tfm_0 = A.Compose(out) tfm_1 = A.Compose([A.OneOrOther(affine_1, rand_crop, p=1.0), *out]) tfm_2 = A.Compose([affine_2, *out]) return [tfm_0, tfm_1, tfm_2]
def __init__(self, is_train, data_root='/home/dahu/xueruini/onion_rain/pytorch/dataset/plant-pathology-2021-fgvc8/train_images/', csv_root='/home/dahu/xueruini/onion_rain/pytorch/dataset/plant-pathology-2021-fgvc8/div/', ): self.data_root = data_root if is_train: csv_data = pd.read_csv(csv_root+"trainset.csv") self.transform = A.Compose([ A.SmallestMaxSize(256), A.RandomCrop(224, 224), A.Flip(p=0.5), A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), ToTensorV2() ]) else: csv_data = pd.read_csv(csv_root+"valset.csv") self.transform = A.Compose([ A.SmallestMaxSize(256), A.CenterCrop(224, 224), A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), ToTensorV2() ]) self.img_paths = csv_data['image'].values self.labels = csv_data['t_label'].values
def setup(self, stage: str) -> None: self.transform_train = A.Compose( [ A.SmallestMaxSize(min(self.input_size[:2]), always_apply=True, interpolation=cv2.INTER_AREA), A.RandomCrop( self.input_size[0], self.input_size[1], always_apply=1), A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5), A.Rotate(p=0.5), A.GridDistortion(p=0.2), A.RandomBrightnessContrast((0, 0.5), (0, 0.5), p=0.2), A.GaussNoise(p=0.2) ], # additional_targets={'box_mask': 'mask'}, bbox_params=A.BboxParams(format='pascal_voc', label_fields=['category_ids']) ) self.transform_val = A.Compose( [ A.SmallestMaxSize(min(self.input_size[:2]), always_apply=True, interpolation=cv2.INTER_AREA), A.RandomCrop( self.input_size[0], self.input_size[1], always_apply=1), ], # additional_targets={'box_mask': 'mask'}, bbox_params=A.BboxParams(format='pascal_voc', label_fields=['category_ids']) ) # self.transform_test = A.Compose([ # A.SmallestMaxSize(min(self.input_size[:2]), always_apply=True, interpolation=cv2.INTER_AREA), # # A.CenterCrop(height=self.input_size[0], width=self.input_size[1], always_apply=True) # # A.PadIfNeeded(min_height=self.input_size[0], min_width=self.input_size[1], pad_height_divisor=16, # # pad_width_divisor=16, border_mode=cv2.BORDER_CONSTANT, always_apply=True, value=0) # A.PadIfNeeded(min_height=None, min_width=None, pad_height_divisor=16, pad_width_divisor=16, # border_mode=cv2.BORDER_CONSTANT, # always_apply=True, value=0) # # A.RandomCrop(self.input_size[0], self.input_size[1], always_apply=1), # ]) self.train_dataset = StanfordDataset( "data/stanford_drone/videos", n_frame_samples=self.in_channels, interframe_step=self.interframe_step, mode="train", part_of_dataset_to_use=self.part_of_train_dataset, dilate=True, transform=self.transform_train) self.val_dataset = StanfordDataset( "data/stanford_drone/videos", n_frame_samples=self.in_channels, interframe_step=self.interframe_step, mode="val", part_of_dataset_to_use=self.part_of_val_dataset, dilate=True, transform=self.transform_val) self.unnormalizer = UnNormalize(mean=self.train_dataset.mean, std=self.train_dataset.std)
def __init__( self, data_csv, data_root, segmentation_root, size=None, random_crop=False, interpolation="bicubic", ): self.n_labels = 182 self.data_csv = data_csv self.data_root = data_root self.segmentation_root = segmentation_root with open(self.data_csv, "r") as f: self.image_paths = f.read().splitlines() self._length = len(self.image_paths) self.labels = { "relative_file_path_": [l for l in self.image_paths], "file_path_": [ os.path.join(self.data_root + 'images', l) for l in self.image_paths ], "segmentation_path_": [ os.path.join(self.data_root + "segmentations", l.replace("image", "label")) for l in self.image_paths ], } size = None if size is not None and size <= 0 else size self.size = size if self.size is not None: self.interpolation = interpolation self.interpolation = { "nearest": cv2.INTER_NEAREST, "bilinear": cv2.INTER_LINEAR, "bicubic": cv2.INTER_CUBIC, "area": cv2.INTER_AREA, "lanczos": cv2.INTER_LANCZOS4 }[self.interpolation] self.image_rescaler = albumentations.SmallestMaxSize( max_size=self.size, interpolation=self.interpolation) self.segmentation_rescaler = albumentations.SmallestMaxSize( max_size=self.size, interpolation=cv2.INTER_NEAREST) self.center_crop = not random_crop if self.center_crop: self.cropper = albumentations.CenterCrop(height=self.size, width=self.size) else: self.cropper = albumentations.RandomCrop(height=self.size, width=self.size) self.preprocessor = self.cropper
def __init__(self, config=None, size=None, random_crop=False, interpolation="bicubic", crop_size=None): self.split = self.get_split() self.n_labels = 151 # unknown + 150 self.data_csv = {"train": "data/ade20k_train.txt", "validation": "data/ade20k_test.txt"}[self.split] self.data_root = "data/ade20k_root" with open(os.path.join(self.data_root, "sceneCategories.txt"), "r") as f: self.scene_categories = f.read().splitlines() self.scene_categories = dict(line.split() for line in self.scene_categories) with open(self.data_csv, "r") as f: self.image_paths = f.read().splitlines() self._length = len(self.image_paths) self.labels = { "relative_file_path_": [l for l in self.image_paths], "file_path_": [os.path.join(self.data_root, "images", l) for l in self.image_paths], "relative_segmentation_path_": [l.replace(".jpg", ".png") for l in self.image_paths], "segmentation_path_": [os.path.join(self.data_root, "annotations", l.replace(".jpg", ".png")) for l in self.image_paths], "scene_category": [self.scene_categories[l.split("/")[1].replace(".jpg", "")] for l in self.image_paths], } size = None if size is not None and size<=0 else size self.size = size if crop_size is None: self.crop_size = size if size is not None else None else: self.crop_size = crop_size if self.size is not None: self.interpolation = interpolation self.interpolation = { "nearest": cv2.INTER_NEAREST, "bilinear": cv2.INTER_LINEAR, "bicubic": cv2.INTER_CUBIC, "area": cv2.INTER_AREA, "lanczos": cv2.INTER_LANCZOS4}[self.interpolation] self.image_rescaler = albumentations.SmallestMaxSize(max_size=self.size, interpolation=self.interpolation) self.segmentation_rescaler = albumentations.SmallestMaxSize(max_size=self.size, interpolation=cv2.INTER_NEAREST) if crop_size is not None: self.center_crop = not random_crop if self.center_crop: self.cropper = albumentations.CenterCrop(height=self.crop_size, width=self.crop_size) else: self.cropper = albumentations.RandomCrop(height=self.crop_size, width=self.crop_size) self.preprocessor = self.cropper
class FRCNN(object): width = height = short_side = opt.scale if opt.scale else 600 divisor = 32 train_transform = A.Compose( # FRCNN [ A.SmallestMaxSize(short_side, p=1.0), # resize到短边600 A.PadIfNeeded(min_height=None, min_width=None, pad_height_divisor=divisor, pad_width_divisor=divisor, p=1.0), # A.RandomCrop(height=height, width=width, p=1.0), # 600×600 A.OneOf([ A.HueSaturationValue(hue_shift_limit=0.3, sat_shift_limit=0.3, val_shift_limit=0.3, p=0.95), A.RandomBrightnessContrast( brightness_limit=0.3, contrast_limit=0.3, p=0.95), ], p=1.0), A.ToGray(p=0.01), A.HorizontalFlip(p=0.5), ToTensorV2(p=1.0), ], p=1.0, bbox_params=A.BboxParams(format='pascal_voc', min_area=0, min_visibility=0, label_fields=['labels']), ) # FRCNN val_transform = A.Compose( [ A.SmallestMaxSize(short_side, p=1.0), # resize到短边600 A.PadIfNeeded(min_height=None, min_width=None, pad_height_divisor=divisor, pad_width_divisor=divisor, p=1.0), ToTensorV2(p=1.0), ], p=1.0, bbox_params=A.BboxParams(format='pascal_voc', min_area=0, min_visibility=0, label_fields=['labels']))
def test_smallest_max_size_keypoints(): img = np.random.randint(0, 256, [50, 10], np.uint8) keypoints = [(9, 5, 0, 0)] aug = A.SmallestMaxSize(max_size=100, p=1) result = aug(image=img, keypoints=keypoints) assert result["keypoints"] == [(90, 50, 0, 0)] aug = A.SmallestMaxSize(max_size=5, p=1) result = aug(image=img, keypoints=keypoints) assert result["keypoints"] == [(4.5, 2.5, 0, 0)] aug = A.SmallestMaxSize(max_size=10, p=1) result = aug(image=img, keypoints=keypoints) assert result["keypoints"] == [(9, 5, 0, 0)]
def __init__(self, base_dir=None, split='train', affine_augmenter=None, image_augmenter=None, target_size=224, filename=None, use_bined=False, n_class=4, debug=False): self.base_dir = base_dir self.base_dir = Path(base_dir) self.split = split self.use_bined = use_bined self.n_class = n_class self.debug = debug self.img_paths = [] self.bbox = [] self.labels = [] self.euler_binned = [] with open(self.base_dir / filename) as f: for i, line in enumerate(f.readlines()): ls = line.strip() mat_path = self.base_dir / ls.replace('.jpg', '.mat') bbox, pose = get_pt_ypr_from_mat(mat_path, pt3d=True) if True and (abs(pose[0])>99 or abs(pose[1])>99 or abs(pose[2])>99): continue if use_bined: yaw_pitch_bins = np.array([-60, -40, -20, 20, 40, 60]) roll_bins = np.array(range(-81, 82, 9)) self.euler_binned.append([np.digitize(pose[0], yaw_pitch_bins),np.digitize(pose[1], yaw_pitch_bins),np.digitize(pose[2], roll_bins)]) self.labels.append(np.array(pose)) self.bbox.append(bbox) self.img_paths.append(ls) self.labels_sort_idx = np.argsort(-np.mean(np.abs(self.labels), axis=1)) if 'train' in self.split: self.resizer = albu.Compose([albu.SmallestMaxSize(target_size, p=1.), albu.RandomScale(scale_limit=(-0.2, 0.2), p=0.1), albu.PadIfNeeded(min_height=target_size, min_width=target_size, value=0, p=1), albu.RandomCrop(target_size, target_size, p=1.)]) else: # self.resizer = albu.Compose([albu.Resize(target_size[0], target_size[1], p=1.)]) self.resizer = albu.Compose([albu.SmallestMaxSize(target_size, p=1.), albu.CenterCrop(target_size, target_size, p=1.)]) self.affine_augmenter = affine_augmenter self.image_augmenter = image_augmenter
def factory(name, image_size, p=0.5, without_norm=False): tr_func = globals().get(name, None) if tr_func is None: raise AttributeError("Transform %s doesn't exist" % (name, )) norm = { 'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225] } if not without_norm else None max_size_tr = A.SmallestMaxSize(max_size=image_size, always_apply=True) train_transform = A.Compose([ max_size_tr, tr_func(image_size, p), A.RandomCrop(image_size, image_size, always_apply=True), ToTensor(normalize=norm), ]) val_transform = A.Compose([ max_size_tr, A.CenterCrop(image_size, image_size, always_apply=True), ToTensor(normalize=norm), ]) test_transform = val_transform # TODO: tta (return list) return train_transform, val_transform, test_transform
def resize_transforms(image_size=512): BORDER_CONSTANT = 0 pre_size = int(image_size * 1.5) random_crop = albu.Compose([ albu.SmallestMaxSize(pre_size, p=1), albu.RandomCrop( image_size, image_size, p=1 ) ]) rescale = albu.Compose([albu.Resize(image_size, image_size, p=1)]) random_crop_big = albu.Compose([ albu.LongestMaxSize(pre_size, p=1), albu.RandomCrop( image_size, image_size, p=1 ) ]) result = [ albu.OneOf([ random_crop, rescale, random_crop_big ], p=1) ] return result
def get_augs_train(self): augs_train = A.Compose([ # Geometric Augs A.SmallestMaxSize(max_size=self.resize_h, interpolation=0, p=1.0), A.CenterCrop(height=self.resize_h, width=self.resize_w, p=1.0), ]) return augs_train
def __init__(self, config: dict, mode: str): super(NanodetDataset, self).__init__(config, mode) if self.augment: assert 'hyper' in config.keys( ), 'Please add the parameters for data augmentation !' self.hyp = config['hyper'] self.transforms = A.Compose( [ A.SmallestMaxSize(max_size=min(self.img_size)), A.ShiftScaleRotate(shift_limit=self.hyp.get('shift', 0.), scale_limit=self.hyp.get('scale', 0.), rotate_limit=self.hyp.get('rotate', 0.)), A.HorizontalFlip(), A.VerticalFlip(), A.RandomCrop(height=self.img_size[0], width=self.img_size[1]), # A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)) ToTensorV2() ], bbox_params=A.BboxParams(format='coco', min_area=256, min_visibility=0.2, label_fields=['gt_labels'])) else: self.transforms = A.Compose( [A.LongestMaxSize(max_size=max(self.img_size)), ToTensorV2()], bbox_params=A.BboxParams(format='coco', min_area=256, min_visibility=0.2, label_fields=['gt_labels']))
def get_transforms(input_size=600, transforms_mode="no"): mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225] trans = { "no_pad": Compose([ ToTensor(), AlbumentationsWrapper(A.SmallestMaxSize(input_size)), Normalize(mean, std), ]), "simple": Compose([ ResizeAndPad(input_size), ToTensor(), Normalize(mean, std), ]), # "light": Compose([ # RandomResizedCrop(train_size), # RandomHorizontalFlip(), # ColorJitter(0.3, 0.3, 0.3), # ToTensor(), # Normalize(mean, std), # ]), } if transforms_mode not in trans: raise ValueError( f"Invalid transformation mode. Expected one of " f"{list(trans.keys())}, got {transforms_mode} instead.") return trans[transforms_mode]
def __init__(self, base_dir=None, filename=None, n_class=3, target_size=224, affine_augmenter=None, image_augmenter=None, debug=False, paired_img=False): print("[INFO] Initing Rank300WLPDataset with HDF5 type.") print(base_dir, filename, n_class, target_size, debug) self.base_dir = Path(base_dir) self.n_class = n_class self.target_size = target_size self.affine_augmenter = affine_augmenter self.image_augmenter = image_augmenter self.debug = debug self.paired_img = paired_img self.CMU_data = False self.resizer = albu.Compose([ albu.SmallestMaxSize(target_size, p=1.), albu.CenterCrop(target_size, target_size, p=1.) ]) # CMU_dataset_64x64 # self.dbs = self.load_dbs(base_dir, filename) self.dbs = self.load_all_hdf5(base_dir) # print(self.dbs[0]["labels"].shape[0]) self.size_dbs = [db["labels"].shape[0] for db in self.dbs] self.numImages = sum(self.size_dbs) print(f"[INFO] Size dbs {self.numImages}")
def get_preprocessor(size=None, random_crop=False, additional_targets=None, crop_size=None): if size is not None and size > 0: transforms = list() rescaler = albumentations.SmallestMaxSize(max_size = size) transforms.append(rescaler) if not random_crop: cropper = albumentations.CenterCrop(height=size,width=size) transforms.append(cropper) else: cropper = albumentations.RandomCrop(height=size,width=size) transforms.append(cropper) flipper = albumentations.HorizontalFlip() transforms.append(flipper) preprocessor = albumentations.Compose(transforms, additional_targets=additional_targets) elif crop_size is not None and crop_size > 0: if not random_crop: cropper = albumentations.CenterCrop(height=crop_size,width=crop_size) else: cropper = albumentations.RandomCrop(height=crop_size,width=crop_size) transforms = [cropper] preprocessor = albumentations.Compose(transforms, additional_targets=additional_targets) else: preprocessor = lambda **kwargs: kwargs return preprocessor
def __init__(self, config): self.data = self.get_base_data(config) self.size = retrieve(config, "spatial_size", default=32) self.rescaler = albumentations.SmallestMaxSize(max_size = self.size) self.cropper = albumentations.CenterCrop(height=self.size,width=self.size) self.preprocessor = albumentations.Compose([self.rescaler, self.cropper])
def test_dataloader(self): """Summary Returns: TYPE: Description """ ds_test = MultiLabelDataset(folder=self.hparams.data_path, is_train='test', fname='test.csv', types=self.hparams.types, pathology=self.hparams.pathology, resize=int(self.hparams.shape)) ds_test.reset_state() ag_test = [ imgaug.Albumentations( AB.SmallestMaxSize(self.hparams.shape, p=1.0)), iimgaug.ColorSpace(mode=cv2.COLOR_GRAY2RGB), imgaug.Albumentations(AB.CLAHE(p=1)), imgaug.ToFloat32(), ] ds_test = AugmentImageComponent(ds_test, ag_test, 0) ds_test = BatchData(ds_test, self.hparams.batch, remainder=True) # ds_test = MultiProcessRunner(ds_test, num_proc=4, num_prefetch=16) ds_test = PrintData(ds_test) ds_test = MapData(ds_test, lambda dp: [torch.tensor(np.transpose(dp[0], (0, 3, 1, 2))), torch.tensor(dp[1]).float()]) return ds_test
def __init__(self, dims, outputPath, dataKey="images", bufSize=1000): # check to see if the output path exists, and if so, raise # an exception if os.path.exists(outputPath): raise ValueError( "The supplied ‘outputPath‘ already exists and cannot be overwritten. Manually delete the file before continuing.", outputPath) # open the HDF5 database for writing and create two datasets: # one to store the images/features and another to store the # class labels self.db = h5py.File(outputPath, "w") self.data = self.db.create_dataset(dataKey, dims, dtype=np.uint8) # self.size_imgs = self.db.create_dataset("raw_size_data", (dims[0], 3) , dtype=np.uint8) self.labels = self.db.create_dataset("labels", (dims[0], 3), dtype="float") self.size_data = (dims[1], dims[2], dims[3]) print( f"[Wranning] If input data is larger size {self.size_data}, auto resizer is called to target size: {self.size_data}." ) target_size = max(dims[1], dims[2]) self.resizer = albu.Compose([ albu.SmallestMaxSize(target_size + 1, p=1.), albu.CenterCrop(target_size, target_size, p=1.) ]) # self.resizer = albu.Compose([albu.SmallestMaxSize(target_size, p=1.)]) # self.centerCrop = albu.Compose([albu.CenterCrop(target_size, target_size, p=1.)]) # store the buffer size, then initialize the buffer itself # along with the index into the datasets self.bufSize = bufSize self.buffer = {"data": [], "raw_size_data": [], "labels": []} self.idx = 0
def get_lung_transforms(*, augment, args): transforms_train = A.Compose([ A.SmallestMaxSize(max_size=args.img_size), A.Transpose(p=0.5), A.VerticalFlip(p=0.5), A.HorizontalFlip(p=0.5), A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.75), A.Resize(args.img_size, args.img_size), A.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ), ToTensorV2() ]) transforms_val = A.Compose([ A.Resize(args.img_size, args.img_size), A.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ), ToTensorV2() ]) if augment == 'augment': return transforms_train else: return transforms_val
def forward_img(self, sample_ims: List[np.array], smallest_size=640): """ Process sample grayscale images (n_images == self.input_size) and generates one segmentation (for central input img) """ transform = A.Compose([ A.SmallestMaxSize(smallest_size, always_apply=True, interpolation=cv2.INTER_AREA), A.PadIfNeeded(min_height=None, min_width=None, pad_height_divisor=16, pad_width_divisor=16, border_mode=cv2.BORDER_CONSTANT, always_apply=True, value=0) ]) sample_ims = np.stack(sample_ims, axis=-1) sample_ims = transform(image=sample_ims)["image"] self.net.eval() with torch.no_grad(): x = sample_ims x = self.val_dataset.torch_transform(x) x = torch.unsqueeze(x, 0) x = x.to(self.device) out_mask = self.forward(x) out_mask = out_mask[0].permute(1, 2, 0).cpu().numpy() out_mask = np.argmax(out_mask, axis=-1) masked_img = self.val_dataset.generate_masked_image( sample_ims[:, :, sample_ims.shape[-1] // 2], out_mask, gray_img=True) return masked_img
def resize_transforms(image_size=224): BORDER_CONSTANT = 0 pre_size = int(image_size * 1.5) random_crop = albu.Compose([ albu.SmallestMaxSize(pre_size, p=1), albu.RandomCrop(image_size, image_size, p=1) ]) rescale = albu.Compose([albu.Resize(image_size, image_size, p=1)]) random_crop_big = albu.Compose([ albu.LongestMaxSize(pre_size, p=1), albu.RandomCrop(image_size, image_size, p=1) ]) # Converts the image to a square of size image_size x image_size # result = [ # albu.OneOf([ # random_crop, # rescale, # random_crop_big # ], p=1) # ] result = [albu.RandomCrop(image_size, image_size, p=1)] return result
def get_train_transforms(config): return A.Compose( [ A.OneOf([ A.HueSaturationValue(hue_shift_limit=0.1, sat_shift_limit= 0.1, val_shift_limit=0.1, p=0.8), A.RandomBrightnessContrast(brightness_limit=0.3, contrast_limit=0.2, p=0.8), ],p=0.7), A.Rotate (limit=15, interpolation=1, border_mode=4, value=None, mask_value=None, p=0.8), A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5), A.RandomResizedCrop (config.preprocess.height, config.preprocess.width, scale=(0.8, 0.8), ratio=(0.75, 1.3333333333333333), interpolation=1, always_apply=False, p=0.1), A.OneOf([ A.Resize(height=config.preprocess.height, width=config.preprocess.width, p=0.2), A.LongestMaxSize(max_size=config.preprocess.longest_max_size, p=0.2), A.SmallestMaxSize(max_size=config.preprocess.smallest_max_size, p=0.2), ], p=1), A.CLAHE(clip_limit=[1,4],p=1), ], p=1.0, bbox_params=A.BboxParams( format='coco', min_area=0.5, min_visibility=0.5, label_fields=['category_id'] ) )
def _get_train_transform(self): print("Using train transform") return A.Compose([ A.HorizontalFlip(p=0.5), A.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0, p=0.5), A.OneOf([ A.Compose([ A.SmallestMaxSize(max_size=self.target_size[0], interpolation=cv2.INTER_LINEAR, p=1.0), A.RandomCrop(height=self.target_size[0], width=self.target_size[0], p=1.0) ], p=1.0), A.RandomResizedCrop(height=self.target_size[0], width=self.target_size[1], scale=(0.25, 1.0), ratio=(3. / 4., 4. / 3.), interpolation=cv2.INTER_LINEAR, p=1.0), A.Resize(height=self.target_size[0], width=self.target_size[1], interpolation=cv2.INTER_LINEAR, p=1.0) ], p=1.0) ])
def __init__(self, config): width = height = config.DATA.SCALE # 300/512 self.train_transform = A.Compose( # Yolo [ # A.RandomSizedCrop(min_max_height=(800, 1024), height=1024, width=1024, p=0.5), # A.RandomScale(scale_limit=0.3, p=1.0), # 这个有问题 C.RandomResize(scale_limit=0.3, p=1.0), # 调节长宽比 [1/1.3, 1.3] A.OneOf( [ A.Sequential( [ A.SmallestMaxSize(min(height, width), p=1.0), A.RandomCrop( height, width, p=1.0) # 先resize到短边544,再crop成544×544 ], p=0.4), A.LongestMaxSize(max(height, width), p=0.6), # resize到长边544 ], p=1.0), # A.LongestMaxSize(max(height, width), p=1.0), A.OneOf([ A.HueSaturationValue(hue_shift_limit=0.4, sat_shift_limit=0.4, val_shift_limit=0.4, p=0.9), A.RandomBrightnessContrast( brightness_limit=0.3, contrast_limit=0.3, p=0.9), ], p=0.9), # A.PadIfNeeded(min_height=height, min_width=width, border_mode=0, value=(0.5,0.5,0.5), p=1.0), C.RandomPad(min_height=height, min_width=width, border_mode=0, value=(123 / 255, 117 / 255, 104 / 255), p=1.0), A.HorizontalFlip(p=0.5), ToTensorV2(p=1.0), ], p=1.0, bbox_params=A.BboxParams(format='pascal_voc', min_area=0, min_visibility=0, label_fields=['labels']), ) self.val_transform = A.Compose([ A.Resize(height=height, width=width, p=1.0), ToTensorV2(p=1.0), ], p=1.0, bbox_params=A.BboxParams( format='pascal_voc', min_area=0, min_visibility=0, label_fields=['labels']))
def get_test_augmentation(): augmentations_val = A.Compose([ A.SmallestMaxSize(256), A.CenterCrop(224, 224), A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ToTensorV2(), ], ) return lambda img: augmentations_val(image=np.array(img))
def resize_image_test(img_arr, smallest_max_size): # create resize transform pipeline transform = albumentations.Compose([ albumentations.SmallestMaxSize(max_size=smallest_max_size, always_apply=True) ]) return transform(image=img_arr)
def test_smallest_max_size_list(): img = np.random.randint(0, 256, [50, 10], np.uint8) keypoints = [(9, 5, 0, 0)] aug = A.SmallestMaxSize(max_size=[50, 100], p=1) result = aug(image=img, keypoints=keypoints) assert result["image"].shape in [(250, 50), (500, 100)] assert result["keypoints"] in [[(45, 25, 0, 0)], [(90, 50, 0, 0)]]
def __init__(self, size=None, crop_size=None, random_crop=False, up_factor=None, hr_factor=None, keep_mode="bicubic"): self.base = self.get_base() self.size = size self.crop_size = crop_size if crop_size is not None else self.size self.random_crop = random_crop self.up_factor = up_factor self.hr_factor = hr_factor self.keep_mode = keep_mode transforms = list() if self.size is not None and self.size > 0: rescaler = albumentations.SmallestMaxSize(max_size=self.size) self.rescaler = rescaler transforms.append(rescaler) if self.crop_size is not None and self.crop_size > 0: if len(transforms) == 0: self.rescaler = albumentations.SmallestMaxSize( max_size=self.crop_size) if not self.random_crop: cropper = albumentations.CenterCrop(height=self.crop_size, width=self.crop_size) else: cropper = albumentations.RandomCrop(height=self.crop_size, width=self.crop_size) transforms.append(cropper) if len(transforms) > 0: if self.up_factor is not None: additional_targets = {"lr": "image"} else: additional_targets = None self.preprocessor = albumentations.Compose( transforms, additional_targets=additional_targets) else: self.preprocessor = lambda **kwargs: kwargs
def data_sample(): return A.Compose([ A.SmallestMaxSize(max_size=200), A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.05, rotate_limit=15, p=0.5), A.RandomCrop(height=size, width=size), A.RGBShift(r_shift_limit=15, g_shift_limit=15, b_shift_limit=15, p=0.5), A.RandomBrightnessContrast(p=0.5), A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), ToTensorV2() ])