Skip to content

Commit

Permalink
bugfix (#43)
Browse files Browse the repository at this point in the history
* bugfix

* np.random => random
  • Loading branch information
ternaus committed Aug 9, 2018
1 parent dfdadac commit a50634a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
13 changes: 7 additions & 6 deletions albumentations/augmentations/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import cv2
import numpy as np
import random
from scipy.ndimage.filters import gaussian_filter

MAX_VALUES_BY_DTYPE = {
Expand Down Expand Up @@ -50,8 +51,8 @@ def cutout(img, num_holes, max_h_size, max_w_size):
height, width = img.shape[:2]

for n in range(num_holes):
y = np.random.randint(height)
x = np.random.randint(width)
y = random.randint(0, height)
x = random.randint(0, width)

y1 = np.clip(y - max_h_size // 2, 0, height)
y2 = np.clip(y + max_h_size // 2, 0, height)
Expand All @@ -71,7 +72,7 @@ def rotate(img, angle, interpolation=cv2.INTER_LINEAR, border_mode=cv2.BORDER_RE

def scale(img, scale, interpolation=cv2.INTER_LINEAR):
height, width = img.shape[:2]
new_height, new_width = int(height * scale), int(height * scale)
new_height, new_width = int(height * scale), int(width * scale)
img = cv2.resize(img, (new_width, new_height), interpolation=interpolation)
return img

Expand Down Expand Up @@ -250,8 +251,8 @@ def median_blur(img, ksize):

def motion_blur(img, ksize):
kernel = np.zeros((ksize, ksize))
xs, ys = np.random.randint(0, ksize), np.random.randint(0, ksize)
xe, ye = np.random.randint(0, ksize), np.random.randint(0, ksize)
xs, ys = random.randint(0, ksize), random.randint(0, ksize)
xe, ye = random.randint(0, ksize), random.randint(0, ksize)
cv2.line(kernel, (xs, ys), (xe, ye), 1, thickness=1)
return cv2.filter2D(img, -1, kernel / np.sum(kernel))

Expand Down Expand Up @@ -395,7 +396,7 @@ def invert(img):

def channel_shuffle(img):
ch_arr = [0, 1, 2]
np.random.shuffle(ch_arr)
random.shuffle(ch_arr)
img = img[..., ch_arr]
return img

Expand Down
3 changes: 2 additions & 1 deletion albumentations/augmentations/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cv2

import numpy as np
import random

from ..core.transforms_interface import to_tuple, DualTransform, ImageOnlyTransform
from . import functional as F
Expand Down Expand Up @@ -239,7 +240,7 @@ def apply(self, img, scale=0, interpolation=cv2.INTER_LINEAR, **params):
return F.scale(img, scale, interpolation)

def get_params(self):
return {'scale': np.random.uniform(1 + self.scale_limit[0], 1 + self.scale_limit[1])}
return {'scale': random.uniform(self.scale_limit[0], self.scale_limit[1])}


class ShiftScaleRotate(DualTransform):
Expand Down
26 changes: 14 additions & 12 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,21 +597,23 @@ def test_from_float_with_max_value_specified(max_value):


@pytest.mark.parametrize('target', ['image', 'mask'])
def test_scale_rotate(target):
def test_scale(target):
img = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]], dtype=np.uint8)
expected = np.array([[1, 1, 2, 3, 3, 4],
[3, 4, 4, 5, 6, 6],
[5, 6, 7, 7, 8, 8],
[8, 9, 9, 10, 11, 11],
[11, 12, 12, 13, 14, 14],
[13, 13, 14, 15, 15, 16]], dtype=np.uint8)
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]], dtype=np.uint8)
expected = np.array([[1, 1, 2, 2, 3, 3],
[2, 2, 2, 3, 3, 4],
[3, 3, 4, 4, 5, 5],
[5, 5, 5, 6, 6, 7],
[6, 6, 7, 7, 8, 8],
[8, 8, 8, 9, 9, 10],
[9, 9, 10, 10, 11, 11],
[10, 10, 11, 11, 12, 12]], dtype=np.uint8)

img, expected = convert_2d_to_target_format([img, expected], target=target)
scaled = F.scale(img, scale=1.5, interpolation=cv2.INTER_LINEAR)
scaled = F.scale(img, scale=2, interpolation=cv2.INTER_LINEAR)
assert np.array_equal(scaled, expected)


Expand Down

0 comments on commit a50634a

Please sign in to comment.