Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Fix the range of offset in random_crop #534

Merged
merged 2 commits into from
Mar 8, 2018
Merged
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
13 changes: 4 additions & 9 deletions chainercv/transforms/image/random_crop.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import random
import six


def random_crop(img, size, return_param=False, copy=False):
Expand Down Expand Up @@ -41,18 +40,14 @@ def random_crop(img, size, return_param=False, copy=False):
"""
H, W = size

if img.shape[1] == H:
y_offset = 0
elif img.shape[1] > H:
y_offset = random.choice(six.moves.range(img.shape[1] - H))
if img.shape[1] >= H:
y_offset = random.randint(0, img.shape[1] - H)
else:
raise ValueError('shape of image needs to be larger than output shape')
y_slice = slice(y_offset, y_offset + H)

if img.shape[2] == W:
x_offset = 0
elif img.shape[2] > W:
x_offset = random.choice(six.moves.range(img.shape[2] - W))
if img.shape[2] >= W:
x_offset = random.randint(0, img.shape[2] - W)
else:
raise ValueError('shape of image needs to be larger than output shape')
x_slice = slice(x_offset, x_offset + W)
Expand Down