albumentations icon indicating copy to clipboard operation
albumentations copied to clipboard

RandomCrop isn't random when cropping just 1px

Open stiansel opened this issue 4 years ago • 3 comments

🐛 Bug

Doing a RandomCrop on an image which is just 1px larger than the target crop size does not lead to a random crop. The crop coordinates are always at the top left.

To Reproduce

import albumentations
import albumentations.augmentations.functional
import numpy as np
import random

test_img = np.random.randint(0, 256, size=(17, 17, 3))
cropper = albumentations.RandomCrop(16, 16, always_apply=True)
for _ in range(100):
   # Expect this to crop to 16x16 starting at (0,0), (0,1), (1,0), or (1,1)
    crop = cropper(image=test_img)['image']
    assert crop.shape[:2] == (16, 16)
    assert np.all(crop == test_img[:-1, :-1])  # this doesn't fire, meaning we always start the crop at (0,0)

# Reason appears to be in the get_random_crop_coords function:
[albumentations.augmentations.functional.get_random_crop_coords(17, 17, 16, 16, random.random(), random.random()) for _ in range(10)]
# Whole list above contains (0, 0, 16, 16) as crop coordinates

Expected behavior

For random crop to produce crop start coordinates randomly of either 0 or 1 (in this particular case).

Environment

  • Albumentations version: 0.4.3
  • Python: Python 3.7.0
  • OS: Ubuntu 18.04
  • How you installed albumentations (conda, pip, source): pip

stiansel avatar Mar 11 '20 08:03 stiansel