Skip to content
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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion configs/_base_/datasets/lsun-car_pad_512.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
key='real_img',
io_backend='disk',
),
dict(type='Resize', keys=['real_img'], scale=(512, 384)),
dict(type='Resize', keys=['real_img'], scale=(384, 512)),
dict(
type='NumpyPad',
keys=['real_img'],
Expand Down
24 changes: 15 additions & 9 deletions mmgen/datasets/pipelines/augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,17 @@ def __init__(self,
assert size_factor is not None, (
'When max_size is used, '
f'size_factor should also be set. But received {size_factor}.')
if isinstance(scale, float):
if isinstance(scale, float, int):
assert keep_ratio, ('When scale is a single float/int,'
' keep_ratio must be ture')
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, '
Copy link
Collaborator

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.

Copy link
Author

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.

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?

Copy link
Collaborator

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.

Copy link
Collaborator

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.

'keep_ratio must be ture')
# assign np.inf to long edge for rescaling short edge later.
scale = (np.inf, max_long_edge)
elif scale is not None:
Expand All @@ -159,7 +163,7 @@ def _resize(self, img, scale):
tuple: Tuple contains resized image and scale factor in resize
process.
"""
if self.keep_ratio:
if isinstance(scale, (float, int)):
img, scale_factor = mmcv.imrescale(
img,
scale,
Expand Down Expand Up @@ -196,16 +200,18 @@ def __call__(self, results):
new_w = min(self.max_size - (self.max_size % self.size_factor),
new_w)
scale = (new_w, new_h)
elif isinstance(self.scale, tuple) and (np.inf in self.scale):
elif isinstance(self.scale, tuple):
# find inf in self.scale, calculate ``scale`` manually
h, w = results[self.keys[0]].shape[:2]
if h < w:
scale = (int(self.scale[-1] / h * w), self.scale[-1])
if np.inf in self.scale:
h, w = results[self.keys[0]].shape[:2]
if h < w:
scale = (int(self.scale[-1] / h * w), self.scale[-1])
else:
scale = (self.scale[-1], int(self.scale[-1] / w * h))
else:
scale = (self.scale[-1], int(self.scale[-1] / w * h))
scale = self.scale[::-1]
else:
# direct use the given ones
scale = self.scale
scale = float(self.scale)

# here we assume all images in self.keys have the same input size
for key in self.keys:
Expand Down