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

Random Crop #11

Merged
merged 4 commits into from
Aug 11, 2020
Merged
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,32 @@ Horizontally flips the given image.

<tr>

<td style="width: 20%">
<b><a href="doc/discolight.md#RandomCrop">RandomCrop</a></b>
<br/>
Randomly crops the given image.
</td>

<td style="width: 20%; vertical-align: bottom">
<img src="doc/images/RandomCrop-input.jpg" width="100px" height="75px" style="display: block; width: 100%"/>
</td>

<td style="width: 20%; vertical-align: bottom">
<img src="doc/images/RandomCrop.jpg" width="100px" height="75px" style="display: block; width: 100%"/>
</td>

<td style="width: 20%; vertical-align: bottom">
<img src="doc/images/RandomCrop-input-bboxes.jpg" width="100px" height="75px" style="display: block; width: 100%"/>
</td>

<td style="width: 20%; vertical-align: bottom">
<img src="doc/images/RandomCrop-bboxes.jpg" width="100px" height="75px" style="display: block; width: 100%"/>
</td>

</tr>

<tr>

<td style="width: 20%">
<b><a href="doc/discolight.md#RandomEraser">RandomEraser</a></b>
<br/>
Expand Down
62 changes: 62 additions & 0 deletions doc/discolight.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,68 @@ The probability that this augmentation will be applied



## RandomCrop

Randomly crops the given image\.

### Example
<table style="width: 100%">
<tr>
<td><b>Input Image</b></td>
<td><b>Augmented Image</b></td>
<td><b>Input Image<br/>(with Bounding Boxes)</b></td>
<td><b>Augmented Image<br/>(with Bounding Boxes)</b></td>
</tr>
<tr>
<td style="vertical-align: bottom">
<img src="images/RandomCrop-input.jpg" width="235px" height="176px" style="display: block; width: 100%"/>
</td>

<td style="vertical-align: bottom">
<img src="images/RandomCrop.jpg" width="235px" height="176px" style="display: block; width: 100%"/>
</td>

<td style="vertical-align: bottom">
<img src="images/RandomCrop-input-bboxes.jpg" width="235px" height="176px" style="display: block; width: 100%"/>
</td>

<td style="vertical-align: bottom">
<img src="images/RandomCrop-bboxes.jpg" width="235px" height="176px" style="display: block; width: 100%"/>
</td>

</tr>
</table>

### Parameters


**max\_height** *(float in range \[0, 1\])* = 0\.7<br/>
Maximum height of cropped area \(normalized\)



**max\_width** *(float in range \[0, 1\])* = 0\.7<br/>
Maximum width of cropped area \(normalized\)



**probs** *(float in range \[0\.0, 1\.0\])* = 1\.0<br/>
The probability that this augmentation will be applied





Sample image augmented with options:
```
max_height: 0.9
max_width: 0.9
```





## RandomEraser

Randomly erase a rectangular area in the given image\.
Expand Down
Binary file added doc/images/RandomCrop-bboxes.jpg
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 doc/images/RandomCrop-input-bboxes.jpg
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 doc/images/RandomCrop-input.jpg
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 doc/images/RandomCrop.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ output:
annotations_file: ./output_folder/aug_annotations.csv
normalized: true
augmentations:
- name: RandomCrop
- name: Sequence
options:
augmentations:
Expand Down
Binary file added snapshots/augmentations/RandomCrop-bboxes.npy
Binary file not shown.
Binary file added snapshots/augmentations/RandomCrop-image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions src/discolight/augmentations/randomcrop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""An augmentation that randomly crops an image."""
import random
import cv2
import numpy as np
from discolight.params.params import Params
from .augmentation.types import Augmentation, BoundedNumber
from .decorators.accepts_probs import accepts_probs


def get_bboxes_in_cropped_area(x, y, w, h, bboxlist):
"""Filter out bounding boxes within the given cropped area."""
bboxes = []

for item in bboxlist:
# if xmin is greater or equal to x, if bbox is inside the crop
if ((item[0] >= x) and (item[1] >= y) and (item[2] <= (x + w))
and (item[3] <= (y + h))):
bboxes.append(item)
else:
continue

return bboxes


@accepts_probs
class RandomCrop(Augmentation):

"""Randomly crops the given image."""

def __init__(self, max_width, max_height):
"""Construct a RandomCrop augmenation.

You should probably use the augmentation factory or Discolight
library interface to construct augmentations. Only invoke
this constructor directly if you know what you are doing.
"""
self.max_width = max_width
self.max_height = max_height

@staticmethod
def params():
"""Return a Params object describing constructor parameters."""
return Params().add("max_width",
"Maximum width of cropped area (normalized)",
BoundedNumber(float, 0, 1), 0.7).add(
"max_height",
"Maximum height of cropped area (normalized)",
BoundedNumber(float, 0, 1), 0.7)

def augment(self, img, bboxes, iteration=100):
"""Augment an image."""
if iteration <= 0:
raise RuntimeError(
"Couldn't find crop that did not keep some bounding boxes.")

height, width, _ = img.shape

crop_width = random.randint(0, int(width * self.max_width))
crop_height = random.randint(0, int(height * self.max_height))

x = random.randint(0, crop_width)
y = random.randint(0, crop_height)
reduced_bboxes = np.array(
get_bboxes_in_cropped_area(x, y, crop_width, crop_height, bboxes))

# if no bbox, get_aug return false; and recall .get_aug
if len(reduced_bboxes) == 0 and len(bboxes) > 0:
return self.augment(img, bboxes, iteration - 1)

cropped_img = img[y:y + crop_height, x:x + crop_width]

# u need the ratio for bounding boxes and images.
width_ratio_resize = width / cropped_img.shape[1]
height_ratio_resize = img.shape[0] / cropped_img.shape[0]
resized_cropped_img = cv2.resize(cropped_img, (width, height))

reduced_bboxes[:,
[0]] = (reduced_bboxes[:, [0]] - x) * width_ratio_resize
reduced_bboxes[:,
[2]] = (reduced_bboxes[:, [2]] - x) * width_ratio_resize
reduced_bboxes[:, [1]] = (reduced_bboxes[:, [1]] -
y) * height_ratio_resize
reduced_bboxes[:, [3]] = (reduced_bboxes[:, [3]] -
y) * height_ratio_resize

return resized_cropped_img, reduced_bboxes
2 changes: 2 additions & 0 deletions src/discolight/doc_templates/augmentations/RandomCrop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_height: 0.9
max_width: 0.9