albumentations
albumentations copied to clipboard
RandomCropNearBBox fails when resulting bbox dimension is zero
When the max_part_shift is greater or equal to 0.5 of the element's size, the computation of the new bounding box can easily yield random dimensions which are smaller or equal to zero, resulting in a failure later on in the crop function (ValueError("Argument rows must be positive integer")
)
Issue should be fixed here: https://github.com/albumentations-team/albumentations/blob/e3b47b3a127f92541cfeb16abbb44a6f8bf79cc8/albumentations/augmentations/crops/transforms.py#L442
Possible fix: Prevent shift values which are equal or greater than 1. Alternatively: Set internal min/max for new bbox dimensions
Please provide a reproducible example. I can not reproduce this behaviour.
This is a RANDOM behavior resulting from the randint
in that function so in order to reproduce it you need some 1000 images and a large max_part_shift
param (say 0.7, but the higher you give the quicker it will happen, say 1.0).
But really, just READ the code...
If max_part_shift
(in lines 2-3 of the function below) is bigger than 0.5, then h_max_shift
and w_max_shift
are basically bigger than half the height/width of the original box.
These values then dictate the boundaries of the roundint
(lines 5-6, 8-9 below) for the shift of the new crop box and in very likely random scenarios the resulting boundaries may simply "switch" with one another and yield a non-positive box. For example, x_min may be bigger than x_max.
It's really quite clear.
def get_params_dependent_on_targets(self, params: Dict[str, Any]) -> Dict[str, int]:
bbox = params[self.cropping_bbox_key]
h_max_shift = round((bbox[3] - bbox[1]) * self.max_part_shift[0])
w_max_shift = round((bbox[2] - bbox[0]) * self.max_part_shift[1])
x_min = bbox[0] - random.randint(-w_max_shift, w_max_shift)
x_max = bbox[2] + random.randint(-w_max_shift, w_max_shift)
y_min = bbox[1] - random.randint(-h_max_shift, h_max_shift)
y_max = bbox[3] + random.randint(-h_max_shift, h_max_shift)
x_min = max(0, x_min)
y_min = max(0, y_min)
You can catch the error and print or save all the neccesary data, including random seed. For example:
try:
seed = random.randrange(sys.maxsize)
random.seed(seed)
np.random.seed(seed)
res = albu_pipe(**data)
except Exception:
with open(path_to_save, "wb) as file:
pickle.dump({"seed": seed, data: data, "albu_pipe": albu_pipe.to_dict()}, file)
raise
If everything is clear to you, please create a PR and make sure that the problem has disappeared.