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

[Fix Serious BUG] Using Albu will crash the trainning #918

Merged
merged 8 commits into from
Jul 28, 2022
Merged
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
15 changes: 10 additions & 5 deletions mmcls/datasets/pipelines/transforms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) OpenMMLab. All rights reserved.
import copy
import inspect
import math
import random
Expand Down Expand Up @@ -1117,19 +1118,23 @@ def mapper(d, keymap):
return updated_dict

def __call__(self, results):

# backup gt_label in case Albu modify it.
_gt_label = copy.deepcopy(results.get('gt_label', None))

# dict to albumentations format
results = self.mapper(results, self.keymap_to_albu)

# process aug
results = self.aug(**results)

if 'gt_labels' in results:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just modifying the key 'gt_labels' to 'gt_label' in the original code will solve the bug. And your comment will help people understand the code.

        if 'gt_label' in results:
            if isinstance(results['gt_label'], list):
                results['gt_label'] = np.array(results['gt_label'])
            results['gt_label'] = results['gt_label'].astype(np.int64)

Your code may cause a potential bug in multi-label tasks. If we use transforms of Albu, which would not change the 'gt_label' in multi-label tasks. The results['gt_label'] don't need to be modified. But results['gt_label'].shape != (), multi-label would be change to single label.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my case, this can't solved the problem, the shape is not equal after results['gt_label'].astype(np.int64)

In my lastest commit, I using copy.deepcopy to backup the original gt_label and update it to dict results after aug. I think this way we can make sure it is the same.

if isinstance(results['gt_labels'], list):
results['gt_labels'] = np.array(results['gt_labels'])
results['gt_labels'] = results['gt_labels'].astype(np.int64)

# back to the original format
results = self.mapper(results, self.keymap_back)

if _gt_label is not None:
# recover backup gt_label
results.update({'gt_label': _gt_label})

# update final shape
if self.update_pad_shape:
results['pad_shape'] = results['img'].shape
Expand Down
16 changes: 14 additions & 2 deletions tests/test_data/test_pipelines/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,14 +1268,25 @@ def reset_results(results, original_img):
def test_albu_transform():
results = dict(
img_prefix=osp.join(osp.dirname(__file__), '../../data'),
img_info=dict(filename='color.jpg'))
img_info=dict(filename='color.jpg'),
gt_label=np.array(1))

# Define simple pipeline
load = dict(type='LoadImageFromFile')
load = build_from_cfg(load, PIPELINES)

albu_transform = dict(
type='Albu', transforms=[dict(type='ChannelShuffle', p=1)])
type='Albu',
transforms=[
dict(type='ChannelShuffle', p=1),
dict(
type='ShiftScaleRotate',
shift_limit=0.0625,
scale_limit=0.0,
rotate_limit=0,
interpolation=1,
p=1)
])
albu_transform = build_from_cfg(albu_transform, PIPELINES)

normalize = dict(type='Normalize', mean=[0] * 3, std=[0] * 3, to_rgb=True)
Expand All @@ -1287,3 +1298,4 @@ def test_albu_transform():
results = normalize(results)

assert results['img'].dtype == np.float32
assert results['gt_label'].shape == np.array(1).shape