albumentations icon indicating copy to clipboard operation
albumentations copied to clipboard

ReplayCompose incorrect behavior with PadIfNeeded

Open Dipet opened this issue 4 years ago • 7 comments

PadIfNeeded update params dependent on target size using function update_params, but ReplayCompose does not process it. Also some problems with LongestMaxSize, because resize params determined in apply call.

Dipet avatar Oct 04 '20 08:10 Dipet

Hi, I found the params of LongestMaxSize and PadIfNeeded is always empty when print data['replay'].

Do you know how to fix it? and are there any alternatives?

My code is

transform = A.ReplayCompose([
    A.LongestMaxSize(224),
    A.PadIfNeeded(224, 224, border_mode=cv2.BORDER_CONSTANT),
    A.RandomCrop(224, 224, always_apply=True),
    A.Rotate(limit=(-20,20), border_mode=cv2.BORDER_CONSTANT, p=1.0),
    A.MotionBlur(blur_limit=(3,7), p=1.0),
], keypoint_params=A.KeypointParams(format='xy'))

Thanks!

VVingerfly avatar Jan 11 '21 04:01 VVingerfly

Hello. That is a some kind of bug, because all transforms must generate all parameters in specific methods, but LongestMaxSize does not do this and calculates this parameters for each call. Bug with PadIfNeeded happens because this transform update his parameters using function update_params and it is called inside apply_with_params that called after copying all parameters to replay dict. https://github.com/albumentations-team/albumentations/blob/1f4fa59389f07a692a3934ba5c2c7d3c4e89e9f6/albumentations/core/transforms_interface.py#L89

Simple fix for PadIfNeeded is move apply_with_params call before copying parameters to replay dict and rewrite code like this:

res = self.apply_with_params(params, **kwargs)
if self.deterministic:
    if self.targets_as_params:
        warn(
            self.get_class_fullname() + " could work incorrectly in ReplayMode for other input data"
            " because its' params depend on targets."
        )
    kwargs[self.save_key][id(self)] = deepcopy(params)
return res

Dipet avatar Jan 15 '21 08:01 Dipet

Thanks for your advice. And is there any plan to fix this bug in the near release?

Thanks B. R.

VVingerfly avatar Jan 29 '21 09:01 VVingerfly

Also ran into this issue

samedii avatar Feb 02 '23 07:02 samedii

Same here but with additional_targets instead of ReplayCompose which should be very similar

mikel-brostrom avatar Feb 22 '23 14:02 mikel-brostrom

This: https://github.com/albumentations-team/albumentations/issues/716#issuecomment-760723917 didn't fix it for me

mikel-brostrom avatar Feb 22 '23 14:02 mikel-brostrom

Also ran into this issue

windygooo avatar Sep 21 '23 15:09 windygooo