-
Notifications
You must be signed in to change notification settings - Fork 233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correct Resize pipeline #211
base: master
Are you sure you want to change the base?
Conversation
Some dataset configs are influenced by this change, such as lsun-car_pad_512. |
@@ -205,7 +205,7 @@ def __call__(self, results): | |||
scale = (self.scale[-1], int(self.scale[-1] / w * h)) | |||
else: | |||
# direct use the given ones | |||
scale = self.scale | |||
scale = self.scale[::-1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should consider the situation when the input scale is float or int.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the mmcv.imresize
can take argument scale
of float
or int
.
https://github.com/open-mmlab/mmcv/blob/51b40c332aff9d2927fcc252b248d295850a4d55/mmcv/image/geometric.py#L51-L95
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the original Resize
pipeline in the mmgen
does NOT support taking as input scale
of float
or int
. I tried and got an error raised.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, mmcv.imrescale
can take float
as input. In MMGen's Resize
, we compose mmcv.imrescale
and mmcv.imresize
in one function. Refers to
mmgeneration/mmgen/datasets/pipelines/augmentation.py
Lines 162 to 177 in 69333cf
if self.keep_ratio: | |
img, scale_factor = mmcv.imrescale( | |
img, | |
scale, | |
return_scale=True, | |
interpolation=self.interpolation, | |
backend=self.backend) | |
else: | |
img, w_scale, h_scale = mmcv.imresize( | |
img, | |
scale, | |
return_scale=True, | |
interpolation=self.interpolation, | |
backend=self.backend) | |
scale_factor = np.array((w_scale, h_scale), dtype=np.float32) | |
return img, scale_factor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may use the following command to run the unit test locally.
coverage run --branch --source mmgen -m pytest -s tests/test_datasets/test_pipelines/test_augmentation.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see... I think it's a little bit confusing though. Current Resize
:
Cases | Arguments | Comments |
---|---|---|
scale with a factor & keep ratio | scale=float, keep_ratio=True | lack of asserting keep_ratio=True; not support scale=int |
scale with factors (fh, fw) & not keep ratio | not supported | |
scale with size (h, w) & not keep ratio | scale=(h, w), keep_ratio=False | Misplaced h and w |
scale with size (h, -1) & keep ratio | scale=(h, -1), keep_ratio=True; | lack of asserting keep_ratio=True; variable max_long_edge could be the actual short edge |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can help resolve the problems:
- lacking asserting
keep_ratio
=True; - not support scale=int;
- Misplaced
h
andw
(solved).
But as for the " variable max_long_edge
could be the actual short edge", it requires futher discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your analysis of cases 1 and 2 are correct. And for case 3 we automatically rescale short edge and this behavior is independent of the position of -1
in scale
.
@LeoXing1996 I cannot understand the codes below when there is a mmgeneration/mmgen/datasets/pipelines/augmentation.py Lines 134 to 139 in 69333cf
mmgeneration/mmgen/datasets/pipelines/augmentation.py Lines 199 to 205 in 69333cf
Why don't just simply replace the -1 with a computed size that ensures keeping the ratio, but comparing height and width? My understanding is that you want to only resize the short edge no matter it's the height or width. But this is would be very confusing because:(1) In this case, there will be NO difference between the scale=(-1, 256) and scale=(256, -1) .(2) As the experience of using numpy and torch , -1 represents that the length at this specific dimension is automatically computed.I think it would be better to set a new argument for resizing short edges. Anyway, it should be new topic and this pull request only resolves the "misplaced |
Yes, in the current code, the output of |
I am happy that you agree with me. But changing the default operation could be dangerous, and I found that other mmlab repositories, e.g. |
You are right, openmmlab's repos treat -1 as short edges resize. Therefore I think you can open another issue and PR about how we handle |
I solved them in the latest commit, pls check if current version is ok. |
You should run |
if scale <= 0: | ||
raise ValueError(f'Invalid scale {scale}, must be positive.') | ||
elif mmcv.is_tuple_of(scale, int): | ||
max_long_edge = max(scale) | ||
max_short_edge = min(scale) | ||
if max_short_edge == -1: | ||
assert keep_ratio, ('When scale includes a -1, ' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When -1 in the given scale
, we manually calculate the size of image. Therefore we should use mmcv.imresize
and keep_ratio
should not be True. I wonder if this can pass the unit test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When -1 in the given
scale
, we manually calculate the size of image. Therefore we should usemmcv.imresize
andkeep_ratio
should not be True. I wonder if this can pass the unit test.
My bad, forget to do the test. But don't you think that here keep_ratio
as true is logically correct, since the size is computed according to the ratio?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe setting keep_ratio
as True
is logically correct because we calculate the image size manually. However, in the current code, we would call mmcv.imrescale
when keep_ratio
is True
. I suggest in this PR, we only fix the bug of size misplace and we can try to refactor the Resize
class later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should convert assert keep_ratio
to assert not keep_ratio
.
I rewrited the if-else sentence. But it still cannot pass the |
When |
Fix #209