本文整理汇总了Python中albumentations.Compose方法的典型用法代码示例。如果您正苦于以下问题:Python albumentations.Compose方法的具体用法?Python albumentations.Compose怎么用?Python albumentations.Compose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类albumentations
的用法示例。
在下文中一共展示了albumentations.Compose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def __init__(
self,
data_root: str,
split: str,
tokenizer: SentencePieceBPETokenizer,
image_transform: Callable = T.DEFAULT_IMAGE_TRANSFORM,
max_caption_length: int = 30,
use_single_caption: bool = False,
percentage: float = 100.0,
):
lmdb_path = os.path.join(data_root, f"serialized_{split}.lmdb")
self.reader = LmdbReader(lmdb_path, percentage=percentage)
self.image_transform = image_transform
self.caption_transform = alb.Compose(
[
T.NormalizeCaption(),
T.TokenizeCaption(tokenizer),
T.TruncateCaptionTokens(max_caption_length),
]
)
self.use_single_caption = use_single_caption
self.padding_idx = tokenizer.token_to_id("<unk>")
示例2: get_training_augmentation
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def get_training_augmentation(resize_to=(320,640), crop_size=(288,576)):
print('[get_training_augmentation] crop_size:', crop_size, ', resize_to:', resize_to)
train_transform = [
albu.HorizontalFlip(p=0.5),
albu.VerticalFlip(p=0.5),
albu.ShiftScaleRotate(scale_limit=0.20, rotate_limit=10, shift_limit=0.1, p=0.5, border_mode=cv2.BORDER_CONSTANT, value=0),
albu.GridDistortion(p=0.5),
albu.Resize(*resize_to),
albu.RandomCrop(*crop_size),
albu.ChannelShuffle(),
albu.InvertImg(),
albu.ToGray(),
albu.Normalize(),
]
return albu.Compose(train_transform)
示例3: test_transform_pipeline_serialization
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
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),
]
)
serialized_aug = A.to_dict(aug)
deserialized_aug = A.from_dict(serialized_aug)
set_seed(seed)
aug_data = aug(image=image, mask=mask)
set_seed(seed)
deserialized_aug_data = deserialized_aug(image=image, mask=mask)
assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
assert np.array_equal(aug_data["mask"], deserialized_aug_data["mask"])
示例4: test_transform_pipeline_serialization_with_keypoints
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def test_transform_pipeline_serialization_with_keypoints(seed, image, keypoints, keypoint_format, labels):
aug = A.Compose(
[
A.OneOrOther(
A.Compose([A.RandomRotate90(), A.OneOf([A.HorizontalFlip(p=0.5), A.VerticalFlip(p=0.5)])]),
A.Compose([A.Rotate(p=0.5), A.OneOf([A.HueSaturationValue(p=0.5), A.RGBShift(p=0.7)], p=1)]),
),
A.HorizontalFlip(p=1),
A.RandomBrightnessContrast(p=0.5),
],
keypoint_params={"format": keypoint_format, "label_fields": ["labels"]},
)
serialized_aug = A.to_dict(aug)
deserialized_aug = A.from_dict(serialized_aug)
set_seed(seed)
aug_data = aug(image=image, keypoints=keypoints, labels=labels)
set_seed(seed)
deserialized_aug_data = deserialized_aug(image=image, keypoints=keypoints, labels=labels)
assert np.array_equal(aug_data["image"], deserialized_aug_data["image"])
assert np.array_equal(aug_data["keypoints"], deserialized_aug_data["keypoints"])
示例5: __init__
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def __init__(self,
transforms,
bbox_params=None,
keymap=None,
update_pad_shape=False,
skip_img_without_anno=False):
if Compose is None:
raise RuntimeError('albumentations is not installed')
self.transforms = transforms
self.filter_lost_elements = False
self.update_pad_shape = update_pad_shape
self.skip_img_without_anno = skip_img_without_anno
# A simple workaround to remove masks without boxes
if (isinstance(bbox_params, dict) and 'label_fields' in bbox_params
and 'filter_lost_elements' in bbox_params):
self.filter_lost_elements = True
self.origin_label_fields = bbox_params['label_fields']
bbox_params['label_fields'] = ['idx_mapper']
del bbox_params['filter_lost_elements']
self.bbox_params = (
self.albu_builder(bbox_params) if bbox_params else None)
self.aug = Compose([self.albu_builder(t) for t in self.transforms],
bbox_params=self.bbox_params)
if not keymap:
self.keymap_to_albu = {
'img': 'image',
'gt_masks': 'masks',
'gt_bboxes': 'bboxes'
}
else:
self.keymap_to_albu = keymap
self.keymap_back = {v: k for k, v in self.keymap_to_albu.items()}
示例6: augment
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def augment(image, boxes):
h, w, _ = image.shape
labels, boxes_coord = boxes[:, 0], boxes[:, 1:]
labels = labels.tolist()
boxes_coord = boxes_coord * h # 得到原图尺寸下的坐标(未归一化的坐标)
boxes_coord[:, 0] = np.clip(boxes_coord[:, 0]-boxes_coord[:, 2]/2, a_min=0, a_max=None) # 确保x_min和y_min有效
boxes_coord[:, 1] = np.clip(boxes_coord[:, 1]-boxes_coord[:, 3]/2, a_min=0, a_max=None)
boxes_coord = boxes_coord.tolist() # [x_min, y_min, width, height]
# 在这里设置数据增强的方法
aug = A.Compose([
A.HorizontalFlip(p=0.5),
# A.HueSaturationValue(hue_shift_limit=10, sat_shift_limit=10, val_shift_limit=10, p=0.5),
# A.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.1, rotate_limit=5, border_mode=0, p=0.5)
], bbox_params={'format':'coco', 'label_fields': ['category_id']})
augmented = aug(image=image, bboxes=boxes_coord, category_id=labels)
# 经过aug之后,如果把boxes变没了,则返回原来的图片
if augmented['bboxes']:
image = augmented['image']
boxes_coord = np.array(augmented['bboxes']) # x_min, y_min, w, h → x, y, w, h
boxes_coord[:, 0] = boxes_coord[:, 0] + boxes_coord[:, 2]/2
boxes_coord[:, 1] = boxes_coord[:, 1] + boxes_coord[:, 3]/2
boxes_coord = boxes_coord / h
labels = np.array(augmented['category_id'])[:, None]
boxes = np.concatenate((labels, boxes_coord), 1)
return image, boxes
示例7: get_transform
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def get_transform(train: bool) -> Callable:
train_initial_size = 2048
crop_min_max_height = (400, 533)
crop_width = 512
crop_height = 384
if train:
transforms = [
A.LongestMaxSize(max_size=train_initial_size),
A.RandomSizedCrop(
min_max_height=crop_min_max_height,
width=crop_width,
height=crop_height,
w2h_ratio=crop_width / crop_height,
),
A.HueSaturationValue(
hue_shift_limit=7,
sat_shift_limit=10,
val_shift_limit=10,
),
A.RandomBrightnessContrast(),
A.RandomGamma(),
]
else:
test_size = int(train_initial_size *
crop_height / np.mean(crop_min_max_height))
print(f'Test image max size {test_size} px')
transforms = [
A.LongestMaxSize(max_size=test_size),
]
transforms.extend([
ToTensor(),
])
return A.Compose(
transforms,
bbox_params={
'format': 'coco',
'min_area': 0,
'min_visibility': 0.5,
'label_fields': ['labels'],
},
)
示例8: main
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def main():
image = cv2.imread("images/image_1.jpg")
keypoints = cv2.goodFeaturesToTrack(
cv2.cvtColor(image, cv2.COLOR_RGB2GRAY), maxCorners=100, qualityLevel=0.5, minDistance=5
).squeeze(1)
bboxes = [(kp[0] - 10, kp[1] - 10, kp[0] + 10, kp[1] + 10) for kp in keypoints]
disp_image = visualize(image, keypoints, bboxes)
plt.figure(figsize=(10, 10))
plt.imshow(cv2.cvtColor(disp_image, cv2.COLOR_RGB2BGR))
plt.tight_layout()
plt.show()
aug = A.Compose(
[A.ShiftScaleRotate(scale_limit=0.1, shift_limit=0.2, rotate_limit=10, always_apply=True)],
bbox_params=A.BboxParams(format="pascal_voc", label_fields=["bbox_labels"]),
keypoint_params=A.KeypointParams(format="xy"),
)
for _i in range(10):
data = aug(image=image, keypoints=keypoints, bboxes=bboxes, bbox_labels=np.ones(len(bboxes)))
aug_image = data["image"]
aug_image = visualize(aug_image, data["keypoints"], data["bboxes"])
plt.figure(figsize=(10, 10))
plt.imshow(cv2.cvtColor(aug_image, cv2.COLOR_RGB2BGR))
plt.tight_layout()
plt.show()
示例9: get_test_augmentation
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def get_test_augmentation(resize_to=(320,640)):
"""Add paddings to make image shape divisible by 32"""
test_transform = [
albu.Resize(*resize_to),
albu.Normalize(),
]
return albu.Compose(test_transform)
示例10: pad_mask_image
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def pad_mask_image(self, mask, image, img_id, crop_shape):
composed = Compose([PadIfNeeded(crop_shape[0], crop_shape[1], p=1),
RandomCrop(crop_shape[0], crop_shape[1], p=1)], p=1)
if np.sum(mask) != 0:
s = 0
tries = 0
while s == 0:
# crop = composed(crop_shape[0], crop_shape[1])
croped = composed(image=image, mask=mask)
image_padded = croped['image']
mask_padded = croped['mask']
# print(mask_padded.shape)
s = np.sum(mask_padded)
tries += 1
if tries > 5:
break
else:
croped = composed(image=image, mask=mask)
image_padded = croped['image']
mask_padded = croped['mask']
return mask_padded, image_padded
示例11: get_test_transforms
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def get_test_transforms():
return A.Compose([A.Normalize(mean=(0.485, ), std=(0.229, ))])
示例12: parse_albu
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def parse_albu(configs: List[dict]):
res = []
for config in configs:
assert 'name' in config, f'name is required in {config}'
config = config.copy()
name = config.pop('name')
if name == 'Compose':
items = config.pop('items')
aug = A.Compose(parse_albu(items), **config)
else:
aug = getattr(A, name)(**config)
res.append(aug)
return res
示例13: __init__
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def __init__(self, base_dir='../data/cityscapes', split='train',
affine_augmenter=None, image_augmenter=None, target_size=(1024, 2048),
net_type='unet', ignore_index=255, debug=False):
self.debug = debug
self.base_dir = Path(base_dir)
assert net_type in ['unet', 'deeplab']
self.net_type = net_type
self.ignore_index = ignore_index
self.split = 'val' if split == 'valid' else split
self.img_paths = sorted(self.base_dir.glob(f'leftImg8bit/{self.split}/*/*leftImg8bit.png'))
self.lbl_paths = sorted(self.base_dir.glob(f'gtFine/{self.split}/*/*gtFine_labelIds.png'))
assert len(self.img_paths) == len(self.lbl_paths)
# Resize
if isinstance(target_size, str):
target_size = eval(target_size)
if self.split == 'train':
if self.net_type == 'deeplab':
target_size = (target_size[0] + 1, target_size[1] + 1)
self.resizer = albu.Compose([albu.RandomScale(scale_limit=(-0.5, 0.5), p=1.0),
PadIfNeededRightBottom(min_height=target_size[0], min_width=target_size[1],
value=0, ignore_index=self.ignore_index, p=1.0),
albu.RandomCrop(height=target_size[0], width=target_size[1], p=1.0)])
else:
self.resizer = None
# Augment
if self.split == 'train':
self.affine_augmenter = affine_augmenter
self.image_augmenter = image_augmenter
else:
self.affine_augmenter = None
self.image_augmenter = None
示例14: test_force_apply
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def test_force_apply():
"""
Unit test for https://github.com/albumentations-team/albumentations/issues/189
"""
aug = A.Compose(
[
A.OneOrOther(
A.Compose(
[
A.RandomSizedCrop(min_max_height=(256, 1025), 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.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),
]
)
res = aug(image=np.zeros((1248, 1248, 3), dtype=np.uint8))
assert res["image"].shape[0] in (256, 384, 512)
assert res["image"].shape[1] in (256, 384, 512)
示例15: test_additional_targets_for_image_only
# 需要导入模块: import albumentations [as 别名]
# 或者: from albumentations import Compose [as 别名]
def test_additional_targets_for_image_only(augmentation_cls, params):
aug = A.Compose([augmentation_cls(always_apply=True, **params)], additional_targets={"image2": "image"})
for _i in range(10):
image1 = np.random.randint(low=0, high=256, size=(100, 100, 3), dtype=np.uint8)
image2 = image1.copy()
res = aug(image=image1, image2=image2)
aug1 = res["image"]
aug2 = res["image2"]
assert np.array_equal(aug1, aug2)