Skip to content

Added vertical flip augmentation, along with its random version. #21

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
140 changes: 93 additions & 47 deletions .ipynb_checkpoints/quick-start-checkpoint.ipynb

Large diffs are not rendered by default.

Binary file added Images/vflip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data_aug/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file added data_aug/__pycache__/bbox_util.cpython-37.pyc
Binary file not shown.
Binary file added data_aug/__pycache__/data_aug.cpython-37.pyc
Binary file not shown.
99 changes: 90 additions & 9 deletions data_aug/data_aug.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,19 @@ def __init__(self, p=0.5):
self.p = p

def __call__(self, img, bboxes):
img_center = np.array(img.shape[:2])[::-1]/2
img_center = np.hstack((img_center, img_center))
if random.random() < self.p:
img = img[:, ::-1, :]
bboxes[:, [0, 2]] += 2*(img_center[[0, 2]] - bboxes[:, [0, 2]])
img_center = np.array(img.shape[:2])[::-1]/2
img_center = np.hstack((img_center, img_center))

box_w = abs(bboxes[:, 0] - bboxes[:, 2])
if random.random() < self.p:
img = img[:, ::-1, :]
bboxes[:, [0, 2]] += 2*(img_center[[0, 2]] - bboxes[:, [0, 2]])

bboxes[:, 0] -= box_w
bboxes[:, 2] += box_w
box_w = abs(bboxes[:, 0] - bboxes[:, 2])

return img, bboxes
bboxes[:, 0] -= box_w
bboxes[:, 2] += box_w

return img, bboxes


class HorizontalFlip(object):
Expand Down Expand Up @@ -90,6 +91,86 @@ def __call__(self, img, bboxes):
return img, bboxes


class RandomVerticalFlip(object):

"""Randomly vertically flips the Image with the probability *p*

Parameters
----------
p: float
The probability with which the image is flipped


Returns
-------

numpy.ndaaray
Flipped image in the numpy format of shape `HxWxC`

numpy.ndarray
Tranformed bounding box co-ordinates of the format `n x 4` where n is
number of bounding boxes and 4 represents `x1,y1,x2,y2` of the box

"""

def __init__(self, p=0.5):
self.p = p

def __call__(self, img, bboxes):
img_center = np.array(img.shape[:2])[::-1]/2
img_center = np.hstack((img_center, img_center))

if random.random() < self.p:
img = img[::-1]
bboxes[:, [1, 3]] += 2*(img_center[[1, 3]] - bboxes[:, [1, 3]])

box_h = abs(bboxes[:, 1] - bboxes[:, 3])

bboxes[:, 1] -= box_h
bboxes[:, 3] += box_h

return img, bboxes


class VerticalFlip(object):

"""Vertically flips the Image

Parameters
----------
None


Returns
-------

numpy.ndaaray
Flipped image in the numpy format of shape `HxWxC`

numpy.ndarray
Tranformed bounding box co-ordinates of the format `n x 4` where n is
number of bounding boxes and 4 represents `x1,y1,x2,y2` of the box

"""

def __init__(self):
pass

def __call__(self, img, bboxes):
img_center = np.array(img.shape[:2])[::-1]/2
img_center = np.hstack((img_center, img_center))

img = img[::-1]
bboxes[:, [1, 3]] += 2*(img_center[[1, 3]] - bboxes[:, [1, 3]])

box_h = abs(bboxes[:, 1] - bboxes[:, 3])

bboxes[:, 1] -= box_h
bboxes[:, 3] += box_h

return img, bboxes


class RandomScale(object):
"""Randomly scales an image

Expand Down
125 changes: 78 additions & 47 deletions quick-start.ipynb

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ We support a variety of data augmentations, like.
### Horizontal Flipping
![Horizontal Flip](Images/hflip.png)

### Vertical Flipping
![Vertical Flip](Images/vflip.png)

### Scaling
![Scaling](Images/scale_aug.png)

Expand Down