|
@@ -160,7 +160,9 @@ class Compose(DetTransform):
|
|
|
transform_names = [type(x).__name__ for x in self.transforms]
|
|
transform_names = [type(x).__name__ for x in self.transforms]
|
|
|
for aug in augmenters:
|
|
for aug in augmenters:
|
|
|
if type(aug).__name__ in transform_names:
|
|
if type(aug).__name__ in transform_names:
|
|
|
- logging.error("{} is already in ComposedTransforms, need to remove it from add_augmenters().".format(type(aug).__name__))
|
|
|
|
|
|
|
+ logging.error(
|
|
|
|
|
+ "{} is already in ComposedTransforms, need to remove it from add_augmenters().".
|
|
|
|
|
+ format(type(aug).__name__))
|
|
|
self.transforms = augmenters + self.transforms
|
|
self.transforms = augmenters + self.transforms
|
|
|
|
|
|
|
|
|
|
|
|
@@ -220,15 +222,13 @@ class ResizeByShort(DetTransform):
|
|
|
im_short_size = min(im.shape[0], im.shape[1])
|
|
im_short_size = min(im.shape[0], im.shape[1])
|
|
|
im_long_size = max(im.shape[0], im.shape[1])
|
|
im_long_size = max(im.shape[0], im.shape[1])
|
|
|
scale = float(self.short_size) / im_short_size
|
|
scale = float(self.short_size) / im_short_size
|
|
|
- if self.max_size > 0 and np.round(scale *
|
|
|
|
|
- im_long_size) > self.max_size:
|
|
|
|
|
|
|
+ if self.max_size > 0 and np.round(scale * im_long_size) > self.max_size:
|
|
|
scale = float(self.max_size) / float(im_long_size)
|
|
scale = float(self.max_size) / float(im_long_size)
|
|
|
resized_width = int(round(im.shape[1] * scale))
|
|
resized_width = int(round(im.shape[1] * scale))
|
|
|
resized_height = int(round(im.shape[0] * scale))
|
|
resized_height = int(round(im.shape[0] * scale))
|
|
|
im_resize_info = [resized_height, resized_width, scale]
|
|
im_resize_info = [resized_height, resized_width, scale]
|
|
|
im = cv2.resize(
|
|
im = cv2.resize(
|
|
|
- im, (resized_width, resized_height),
|
|
|
|
|
- interpolation=cv2.INTER_LINEAR)
|
|
|
|
|
|
|
+ im, (resized_width, resized_height), interpolation=cv2.INTER_LINEAR)
|
|
|
im_info['im_resize_info'] = np.array(im_resize_info).astype(np.float32)
|
|
im_info['im_resize_info'] = np.array(im_resize_info).astype(np.float32)
|
|
|
if label_info is None:
|
|
if label_info is None:
|
|
|
return (im, im_info)
|
|
return (im, im_info)
|
|
@@ -268,8 +268,7 @@ class Padding(DetTransform):
|
|
|
if not isinstance(target_size, tuple) and not isinstance(
|
|
if not isinstance(target_size, tuple) and not isinstance(
|
|
|
target_size, list):
|
|
target_size, list):
|
|
|
raise TypeError(
|
|
raise TypeError(
|
|
|
- "Padding: Type of target_size must in (int|list|tuple)."
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ "Padding: Type of target_size must in (int|list|tuple).")
|
|
|
elif len(target_size) != 2:
|
|
elif len(target_size) != 2:
|
|
|
raise ValueError(
|
|
raise ValueError(
|
|
|
"Padding: Length of target_size must equal 2.")
|
|
"Padding: Length of target_size must equal 2.")
|
|
@@ -454,8 +453,7 @@ class RandomHorizontalFlip(DetTransform):
|
|
|
ValueError: 数据长度不匹配。
|
|
ValueError: 数据长度不匹配。
|
|
|
"""
|
|
"""
|
|
|
if not isinstance(im, np.ndarray):
|
|
if not isinstance(im, np.ndarray):
|
|
|
- raise TypeError(
|
|
|
|
|
- "RandomHorizontalFlip: image is not a numpy array.")
|
|
|
|
|
|
|
+ raise TypeError("RandomHorizontalFlip: image is not a numpy array.")
|
|
|
if len(im.shape) != 3:
|
|
if len(im.shape) != 3:
|
|
|
raise ValueError(
|
|
raise ValueError(
|
|
|
"RandomHorizontalFlip: image is not 3-dimensional.")
|
|
"RandomHorizontalFlip: image is not 3-dimensional.")
|
|
@@ -736,7 +734,7 @@ class MixupImage(DetTransform):
|
|
|
gt_poly2 = im_info['mixup'][2]['gt_poly']
|
|
gt_poly2 = im_info['mixup'][2]['gt_poly']
|
|
|
is_crowd1 = label_info['is_crowd']
|
|
is_crowd1 = label_info['is_crowd']
|
|
|
is_crowd2 = im_info['mixup'][2]['is_crowd']
|
|
is_crowd2 = im_info['mixup'][2]['is_crowd']
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if 0 not in gt_class1 and 0 not in gt_class2:
|
|
if 0 not in gt_class1 and 0 not in gt_class2:
|
|
|
gt_bbox = np.concatenate((gt_bbox1, gt_bbox2), axis=0)
|
|
gt_bbox = np.concatenate((gt_bbox1, gt_bbox2), axis=0)
|
|
|
gt_class = np.concatenate((gt_class1, gt_class2), axis=0)
|
|
gt_class = np.concatenate((gt_class1, gt_class2), axis=0)
|
|
@@ -785,9 +783,7 @@ class RandomExpand(DetTransform):
|
|
|
fill_value (list): 扩张图像的初始填充值(0-255)。默认为[123.675, 116.28, 103.53]。
|
|
fill_value (list): 扩张图像的初始填充值(0-255)。默认为[123.675, 116.28, 103.53]。
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
- def __init__(self,
|
|
|
|
|
- ratio=4.,
|
|
|
|
|
- prob=0.5,
|
|
|
|
|
|
|
+ def __init__(self, ratio=4., prob=0.5,
|
|
|
fill_value=[123.675, 116.28, 103.53]):
|
|
fill_value=[123.675, 116.28, 103.53]):
|
|
|
super(RandomExpand, self).__init__()
|
|
super(RandomExpand, self).__init__()
|
|
|
assert ratio > 1.01, "expand ratio must be larger than 1.01"
|
|
assert ratio > 1.01, "expand ratio must be larger than 1.01"
|
|
@@ -1281,21 +1277,25 @@ class ComposedRCNNTransforms(Compose):
|
|
|
min_max_size(list): 图像在缩放时,最小边和最大边的约束条件
|
|
min_max_size(list): 图像在缩放时,最小边和最大边的约束条件
|
|
|
mean(list): 图像均值
|
|
mean(list): 图像均值
|
|
|
std(list): 图像方差
|
|
std(list): 图像方差
|
|
|
|
|
+ random_horizontal_flip(bool): 是否以0.5的概率使用随机水平翻转增强,该仅在mode为`train`时生效,默认为True
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self,
|
|
def __init__(self,
|
|
|
mode,
|
|
mode,
|
|
|
min_max_size=[800, 1333],
|
|
min_max_size=[800, 1333],
|
|
|
mean=[0.485, 0.456, 0.406],
|
|
mean=[0.485, 0.456, 0.406],
|
|
|
- std=[0.229, 0.224, 0.225]):
|
|
|
|
|
|
|
+ std=[0.229, 0.224, 0.225],
|
|
|
|
|
+ random_horizontal_flip=True):
|
|
|
if mode == 'train':
|
|
if mode == 'train':
|
|
|
# 训练时的transforms,包含数据增强
|
|
# 训练时的transforms,包含数据增强
|
|
|
transforms = [
|
|
transforms = [
|
|
|
- RandomHorizontalFlip(prob=0.5), Normalize(
|
|
|
|
|
|
|
+ Normalize(
|
|
|
mean=mean, std=std), ResizeByShort(
|
|
mean=mean, std=std), ResizeByShort(
|
|
|
short_size=min_max_size[0], max_size=min_max_size[1]),
|
|
short_size=min_max_size[0], max_size=min_max_size[1]),
|
|
|
Padding(coarsest_stride=32)
|
|
Padding(coarsest_stride=32)
|
|
|
]
|
|
]
|
|
|
|
|
+ if random_horizontal_flip:
|
|
|
|
|
+ transforms.insert(0, RandomHorizontalFlip())
|
|
|
else:
|
|
else:
|
|
|
# 验证/预测时的transforms
|
|
# 验证/预测时的transforms
|
|
|
transforms = [
|
|
transforms = [
|
|
@@ -1325,9 +1325,14 @@ class ComposedYOLOv3Transforms(Compose):
|
|
|
Args:
|
|
Args:
|
|
|
mode(str): 图像处理流程所处阶段,训练/验证/预测,分别对应'train', 'eval', 'test'
|
|
mode(str): 图像处理流程所处阶段,训练/验证/预测,分别对应'train', 'eval', 'test'
|
|
|
shape(list): 输入模型中图像的大小,输入模型的图像会被Resize成此大小
|
|
shape(list): 输入模型中图像的大小,输入模型的图像会被Resize成此大小
|
|
|
- mixup_epoch(int): 模型训练过程中,前mixup_epoch会使用mixup策略
|
|
|
|
|
|
|
+ mixup_epoch(int): 模型训练过程中,前mixup_epoch会使用mixup策略, 若设为-1,则表示不使用该策略
|
|
|
mean(list): 图像均值
|
|
mean(list): 图像均值
|
|
|
std(list): 图像方差
|
|
std(list): 图像方差
|
|
|
|
|
+ random_distort(bool): 数据增强方式,参数仅在mode为`train`时生效,表示是否在训练过程中随机扰动图像,默认为True
|
|
|
|
|
+ random_expand(bool): 数据增强方式,参数仅在mode为`train`时生效,表示是否在训练过程中随机扩张图像,默认为True
|
|
|
|
|
+ random_crop(bool): 数据增强方式,参数仅在mode为`train`时生效,表示是否在训练过程中随机裁剪图像,默认为True
|
|
|
|
|
+ random_horizontal_flip(bool): 数据增强方式,参数仅在mode为`train`时生效,表示是否在训练过程中随机水平翻转图像,默认为True
|
|
|
|
|
+
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self,
|
|
def __init__(self,
|
|
@@ -1335,7 +1340,11 @@ class ComposedYOLOv3Transforms(Compose):
|
|
|
shape=[608, 608],
|
|
shape=[608, 608],
|
|
|
mixup_epoch=250,
|
|
mixup_epoch=250,
|
|
|
mean=[0.485, 0.456, 0.406],
|
|
mean=[0.485, 0.456, 0.406],
|
|
|
- std=[0.229, 0.224, 0.225]):
|
|
|
|
|
|
|
+ std=[0.229, 0.224, 0.225],
|
|
|
|
|
+ random_distort=True,
|
|
|
|
|
+ random_expand=True,
|
|
|
|
|
+ random_crop=True,
|
|
|
|
|
+ random_horizontal_flip=True):
|
|
|
width = shape
|
|
width = shape
|
|
|
if isinstance(shape, list):
|
|
if isinstance(shape, list):
|
|
|
if shape[0] != shape[1]:
|
|
if shape[0] != shape[1]:
|
|
@@ -1350,12 +1359,18 @@ class ComposedYOLOv3Transforms(Compose):
|
|
|
if mode == 'train':
|
|
if mode == 'train':
|
|
|
# 训练时的transforms,包含数据增强
|
|
# 训练时的transforms,包含数据增强
|
|
|
transforms = [
|
|
transforms = [
|
|
|
- MixupImage(mixup_epoch=mixup_epoch), RandomDistort(),
|
|
|
|
|
- RandomExpand(), RandomCrop(), Resize(
|
|
|
|
|
- target_size=width,
|
|
|
|
|
- interp='RANDOM'), RandomHorizontalFlip(), Normalize(
|
|
|
|
|
|
|
+ MixupImage(mixup_epoch=mixup_epoch), Resize(
|
|
|
|
|
+ target_size=width, interp='RANDOM'), Normalize(
|
|
|
mean=mean, std=std)
|
|
mean=mean, std=std)
|
|
|
]
|
|
]
|
|
|
|
|
+ if random_horizontal_flip:
|
|
|
|
|
+ transforms.insert(1, RandomHorizontalFlip())
|
|
|
|
|
+ if random_crop:
|
|
|
|
|
+ transforms.insert(1, RandomCrop())
|
|
|
|
|
+ if random_expand:
|
|
|
|
|
+ transforms.insert(1, RandomExpand())
|
|
|
|
|
+ if random_distort:
|
|
|
|
|
+ transforms.insert(1, RandomDistort())
|
|
|
else:
|
|
else:
|
|
|
# 验证/预测时的transforms
|
|
# 验证/预测时的transforms
|
|
|
transforms = [
|
|
transforms = [
|