Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Shear Augmentation. #3617

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f7a3bdc
add Translate AutoAugment
v-qjqs Aug 17, 2020
b4dfc8a
reformat Translate AutoAugment
v-qjqs Aug 17, 2020
f411a6a
add doc string and bug fix
v-qjqs Aug 18, 2020
742205c
add tests for image, bboxes, masks and segmentations
v-qjqs Aug 18, 2020
bbf5c8d
fix _translate_bbox
v-qjqs Aug 18, 2020
cca16ad
add coco dummy dataset used for testing auto_aug
v-qjqs Aug 18, 2020
4eb98ff
fix redundant in _translate_bboxes
v-qjqs Aug 18, 2020
dc14f02
remain mask with type PolygonMasks to be implemented and tested
v-qjqs Aug 20, 2020
7d84ffd
Merge branch 'master' of https://github.com/open-mmlab/mmdetection in…
v-qjqs Aug 20, 2020
644c3f2
cover more cases for unit tests and add PolygonMasks test (not implem…
v-qjqs Aug 20, 2020
93ac5fc
Merge branch 'master' of https://github.com/open-mmlab/mmdetection in…
v-qjqs Aug 20, 2020
456441c
add auto augmentation Translate and TranslateOnlyBBox
v-qjqs Aug 21, 2020
895831e
corver more unit test for test_translate.py
v-qjqs Aug 21, 2020
159ea62
delete remote test_autoaug.py since it is redundant
v-qjqs Aug 21, 2020
ac6d3d2
delete Translate in auto_augment.py since it is implemented in transl…
v-qjqs Aug 21, 2020
3260e5e
fix mask translation
v-qjqs Aug 21, 2020
c678131
fix mask translation
v-qjqs Aug 21, 2020
bec1cbd
handle case when masks are all filtered
v-qjqs Aug 21, 2020
24719e3
fix case when masks are all filtered
v-qjqs Aug 21, 2020
b6a6562
Merge branch 'master' of https://github.com/open-mmlab/mmdetection in…
v-qjqs Aug 24, 2020
5d742eb
shear augmentation init
v-qjqs Aug 24, 2020
741702d
Merge branch 'master' of https://github.com/open-mmlab/mmdetection in…
v-qjqs Aug 24, 2020
c126bcf
Merge branch 'master' of https://github.com/open-mmlab/mmdetection in…
v-qjqs Aug 25, 2020
d3426ae
fix training loss nan and add more unit test
v-qjqs Aug 25, 2020
9d55e6a
Add shear augmentation
v-qjqs Aug 27, 2020
088256a
Merge branch 'master' of https://github.com/open-mmlab/mmdetection in…
v-qjqs Aug 27, 2020
1763687
remove unnecessary files
v-qjqs Aug 27, 2020
96f67b6
remove unnecessary files
v-qjqs Aug 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions mmdet/core/mask/structures.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from abc import ABCMeta, abstractmethod

import cv2
import mmcv
import numpy as np
import pycocotools.mask as maskUtils
Expand Down Expand Up @@ -132,6 +133,36 @@ def to_tensor(self, dtype, device):
"""
pass

@staticmethod
def warpAffine(data,
trans_matrix,
out_size,
fill_val,
flags=cv2.INTER_NEAREST,
borderMode=cv2.BORDER_CONSTANT):
"""Affine wrapper which transforms the source data using the given
trans_matrix.

Args:
data (np.ndarray): Source data.
trans_matrix (np.ndarray): Transformation matrix with shape (2, 3).
out_size (tuple): Size of the output data with format (w, h).
fill_val (int | float | tuple): Value used in case of a constant
border.
flags: Interpolation methods used in ``cv2.warpAffine``.
borderMode: pixel extrapolation method used in ``cv2.warpAffine``.

Returns:
np.ndarray: transformed data with the same shape as input data.
"""
return cv2.warpAffine(
data,
trans_matrix,
dsize=out_size, # dsize takes input size as order (w,h).
flags=flags,
borderMode=borderMode,
borderValue=fill_val)


class BitmapMasks(BaseInstanceMasks):
"""This class represents masks in the form of bitmaps.
Expand Down Expand Up @@ -297,6 +328,21 @@ def expand(self, expanded_h, expanded_w, top, left):
left:left + self.width] = self.masks
return BitmapMasks(expanded_mask, expanded_h, expanded_w)

def shear(self,
shear_matrix,
out_shape,
fill_val=0,
flags=cv2.INTER_NEAREST,
borderMode=cv2.BORDER_CONSTANT):
if len(self.masks) == 0:
sheared_masks = np.empty((0, *out_shape), dtype=np.uint8)
else:
sheared_masks = np.stack([
self.warpAffine(mask, shear_matrix, out_shape[::-1], fill_val,
flags, borderMode) for mask in self.masks
]).astype(self.masks.dtype)
return BitmapMasks(sheared_masks, *out_shape)

@property
def areas(self):
"""See :py:attr:`BaseInstanceMasks.areas`."""
Expand Down Expand Up @@ -498,6 +544,14 @@ def crop_and_resize(self,
resized_masks.append(resized_mask)
return PolygonMasks(resized_masks, *out_shape)

def shear(self,
shear_matrix,
out_shape,
fill_val=0,
flags=cv2.INTER_NEAREST,
borderMode=cv2.BORDER_CONSTANT):
raise NotImplementedError

def to_bitmap(self):
"""convert polygon masks to bitmap masks."""
bitmap_masks = self.to_ndarray()
Expand Down
36 changes: 30 additions & 6 deletions mmdet/datasets/pipelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,40 @@
from .instaboost import InstaBoost
from .loading import (LoadAnnotations, LoadImageFromFile,
LoadMultiChannelImageFromFiles, LoadProposals)
from .shear import Shear
from .test_time_aug import MultiScaleFlipAug
from .transforms import (Albu, CutOut, Expand, MinIoURandomCrop, Normalize,
Pad, PhotoMetricDistortion, RandomCenterCropPad,
RandomCrop, RandomFlip, Resize, SegRescale)

__all__ = [
'Compose', 'to_tensor', 'ToTensor', 'ImageToTensor', 'ToDataContainer',
'Transpose', 'Collect', 'LoadAnnotations', 'LoadImageFromFile',
'LoadMultiChannelImageFromFiles', 'LoadProposals', 'MultiScaleFlipAug',
'Resize', 'RandomFlip', 'Pad', 'RandomCrop', 'Normalize', 'SegRescale',
'MinIoURandomCrop', 'Expand', 'PhotoMetricDistortion', 'Albu',
'InstaBoost', 'RandomCenterCropPad', 'AutoAugment', 'CutOut'
'Compose',
'to_tensor',
'ToTensor',
'ImageToTensor',
'ToDataContainer',
'Transpose',
'Collect',
'LoadAnnotations',
'LoadImageFromFile',
'LoadMultiChannelImageFromFiles',
'LoadProposals',
'MultiScaleFlipAug',
'Resize',
'RandomFlip',
'Pad',
'RandomCrop',
'Normalize',
'SegRescale',
'MinIoURandomCrop',
'Expand',
'PhotoMetricDistortion',
'Albu',
'InstaBoost',
'RandomCenterCropPad',
'AutoAugment',
'CutOut',
'Translate',
'TranslateOnlyBBox',
'Shear',
]
17 changes: 13 additions & 4 deletions mmdet/datasets/pipelines/auto_augment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ class AutoAugment(object):
augment images.

Examples:
>>> replace = (104, 116, 124)
>>> fill_val = (104, 116, 124)
>>> policies = [
>>> [
>>> dict(type='Sharpness', prob=0.0, level=8),
>>> dict(
>>> type='Shear',
>>> prob=0.4,
>>> level=0,
>>> replace=replace,
>>> fill_val=fill_val,
>>> axis='x')
>>> ],
>>> [
>>> dict(
>>> type='Rotate',
>>> prob=0.6,
>>> level=10,
>>> replace=replace),
>>> fill_val=fill_val),
>>> dict(type='Color', prob=1.0, level=6)
>>> ]
>>> ]
Expand All @@ -65,8 +65,17 @@ def __init__(self, policies):
self.transforms = [Compose(policy) for policy in self.policies]

def __call__(self, results):
"""Call function to randomly select transforms and perform
transformation.

Args:
results (dict): Result dict from loading pipeline.

Returns:
dict: Transformed results.
"""
transform = np.random.choice(self.transforms)
return transform(results)

def __repr__(self):
return f'{self.__class__.__name__}(policies={self.policies}'
return f'{self.__class__.__name__}(policies={self.policies})'
Loading