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
[Feature] Support Mixup and Cutmix for Recognizers. #681
Merged
Merged
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
55be548
add todo list
irvingzhang0512 bd0fd7e
codes of mixup/cutmix/register/recognizers
irvingzhang0512 a71aa6e
add unittest
irvingzhang0512 cab1eda
add demo config
irvingzhang0512 5952d58
fix unittest
irvingzhang0512 54f651e
remove toto list
irvingzhang0512 1b5bdf9
update changelog
irvingzhang0512 ff68dd9
Merge branch 'master' into mixup
irvingzhang0512 7a38fea
fix unittest and training bug
irvingzhang0512 bf13717
fix
irvingzhang0512 86c06b4
fix unittest
irvingzhang0512 0618906
add todo
irvingzhang0512 3a7bacf
remove useless codes
irvingzhang0512 1b2cac9
update comments
irvingzhang0512 eea8ee4
update docs
irvingzhang0512 cd3e437
fix
irvingzhang0512 1f887ca
fix a bug
irvingzhang0512 f31eaf5
update configs
irvingzhang0512 8e3e48c
update sthv1 training results
irvingzhang0512 0c8af1f
Merge branch 'master' into mixup
irvingzhang0512 95ce916
add tsn config and modify default alpha value
irvingzhang0512 0735ef7
fix lint
irvingzhang0512 60a6b15
add unittest
irvingzhang0512 5bf768b
fix tin sthv2 config
irvingzhang0512 e79c5da
update links
kennymckormick 60915ea
remove useless docs
irvingzhang0512 749de3e
Merge branch 'master' into mixup
irvingzhang0512 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
configs/recognition/tsn/tsn_r50_video_mixup_1x1x8_100e_kinetics400_rgb.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
_base_ = [ | ||
'../../_base_/schedules/sgd_100e.py', '../../_base_/default_runtime.py' | ||
] | ||
|
||
# model settings | ||
model = dict( | ||
type='Recognizer2D', | ||
backbone=dict( | ||
type='ResNet', | ||
pretrained='torchvision://resnet50', | ||
depth=50, | ||
norm_eval=False), | ||
cls_head=dict( | ||
type='TSNHead', | ||
num_classes=400, | ||
in_channels=2048, | ||
spatial_type='avg', | ||
consensus=dict(type='AvgConsensus', dim=1), | ||
dropout_ratio=0.4, | ||
init_std=0.01), | ||
# model training and testing settings | ||
# train_cfg=dict( | ||
# blending=dict(type="CutmixBlending", num_classes=400, alpha=.2)), | ||
train_cfg=dict( | ||
blending=dict(type='MixupBlending', num_classes=400, alpha=.2)), | ||
test_cfg=dict(average_clips=None)) | ||
|
||
# dataset settings | ||
dataset_type = 'VideoDataset' | ||
data_root = 'data/kinetics400/videos_train' | ||
data_root_val = 'data/kinetics400/videos_val' | ||
ann_file_train = 'data/kinetics400/kinetics400_train_list_videos.txt' | ||
ann_file_val = 'data/kinetics400/kinetics400_val_list_videos.txt' | ||
ann_file_test = 'data/kinetics400/kinetics400_val_list_videos.txt' | ||
img_norm_cfg = dict( | ||
mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_bgr=False) | ||
train_pipeline = [ | ||
dict(type='DecordInit'), | ||
dict(type='SampleFrames', clip_len=1, frame_interval=1, num_clips=8), | ||
dict(type='DecordDecode'), | ||
dict( | ||
type='MultiScaleCrop', | ||
input_size=224, | ||
scales=(1, 0.875, 0.75, 0.66), | ||
random_crop=False, | ||
max_wh_scale_gap=1), | ||
dict(type='Resize', scale=(224, 224), keep_ratio=False), | ||
dict(type='Flip', flip_ratio=0.5), | ||
dict(type='Normalize', **img_norm_cfg), | ||
dict(type='FormatShape', input_format='NCHW'), | ||
dict(type='Collect', keys=['imgs', 'label'], meta_keys=[]), | ||
dict(type='ToTensor', keys=['imgs', 'label']) | ||
] | ||
val_pipeline = [ | ||
dict(type='DecordInit'), | ||
dict( | ||
type='SampleFrames', | ||
clip_len=1, | ||
frame_interval=1, | ||
num_clips=8, | ||
test_mode=True), | ||
dict(type='DecordDecode'), | ||
dict(type='Resize', scale=(-1, 256)), | ||
dict(type='CenterCrop', crop_size=224), | ||
dict(type='Flip', flip_ratio=0), | ||
dict(type='Normalize', **img_norm_cfg), | ||
dict(type='FormatShape', input_format='NCHW'), | ||
dict(type='Collect', keys=['imgs', 'label'], meta_keys=[]), | ||
dict(type='ToTensor', keys=['imgs']) | ||
] | ||
test_pipeline = [ | ||
dict(type='DecordInit'), | ||
dict( | ||
type='SampleFrames', | ||
clip_len=1, | ||
frame_interval=1, | ||
num_clips=25, | ||
test_mode=True), | ||
dict(type='DecordDecode'), | ||
dict(type='Resize', scale=(-1, 256)), | ||
dict(type='ThreeCrop', crop_size=256), | ||
dict(type='Flip', flip_ratio=0), | ||
dict(type='Normalize', **img_norm_cfg), | ||
dict(type='FormatShape', input_format='NCHW'), | ||
dict(type='Collect', keys=['imgs', 'label'], meta_keys=[]), | ||
dict(type='ToTensor', keys=['imgs']) | ||
] | ||
data = dict( | ||
videos_per_gpu=32, | ||
workers_per_gpu=4, | ||
train=dict( | ||
type=dataset_type, | ||
ann_file=ann_file_train, | ||
data_prefix=data_root, | ||
pipeline=train_pipeline), | ||
val=dict( | ||
type=dataset_type, | ||
ann_file=ann_file_val, | ||
data_prefix=data_root_val, | ||
pipeline=val_pipeline), | ||
test=dict( | ||
type=dataset_type, | ||
ann_file=ann_file_test, | ||
data_prefix=data_root_val, | ||
pipeline=test_pipeline)) | ||
evaluation = dict( | ||
interval=5, metrics=['top_k_accuracy', 'mean_class_accuracy']) | ||
|
||
# runtime settings | ||
work_dir = './work_dirs/tsn_r50_video_mixup_1x1x8_100e_kinetics400_rgb/' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
from abc import ABCMeta, abstractmethod | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
from torch.distributions.beta import Beta | ||
|
||
from .registry import BLENDINGS | ||
|
||
__all__ = ['BaseMiniBatchBlending', 'MixupBlending', 'CutmixBlending'] | ||
|
||
|
||
class BaseMiniBatchBlending(metaclass=ABCMeta): | ||
"""Base class for Image Aliasing.""" | ||
|
||
def __init__(self, num_classes): | ||
self.num_classes = num_classes | ||
|
||
@abstractmethod | ||
def do_blending(self, imgs, label, **kwargs): | ||
pass | ||
|
||
def __call__(self, imgs, label, **kwargs): | ||
"""Blending data in a mini-batch. | ||
|
||
Images are float tensors with the shape of (B, N, C, H, W) for 2D | ||
recognizers or (B, N, C, T, H, W) for 3D recognizers. | ||
|
||
Besides, labels are converted from hard labels to soft labels. | ||
Hard labels are integer tensors with the shape of (B, 1) and all of the | ||
elements are in the range [0, num_classes - 1]. | ||
Soft labels (probablity distribution over classes) are float tensors | ||
with the shape of (B, 1, num_classes) and all of the elements are in | ||
the range [0, 1]. | ||
|
||
Args: | ||
imgs (torch.Tensor): Model input images, float tensor with the | ||
shape of (B, N, C, H, W) or (B, N, C, T, H, W). | ||
label (torch.Tensor): Hard labels, integer tensor with the shape | ||
of (B, 1) and all elements are in range [0, num_classes). | ||
kwargs (dict, optional): Other keyword argument to be used to | ||
blending imgs and labels in a mini-batch. | ||
|
||
Returns: | ||
mixed_imgs (torch.Tensor): Blending images, float tensor with the | ||
same shape of the input imgs. | ||
mixed_label (torch.Tensor): Blended soft labels, float tensor with | ||
the shape of (B, 1, num_classes) and all elements are in range | ||
[0, 1]. | ||
""" | ||
one_hot_label = F.one_hot(label, num_classes=self.num_classes) | ||
|
||
mixed_imgs, mixed_label = self.do_blending(imgs, one_hot_label, | ||
**kwargs) | ||
|
||
return mixed_imgs, mixed_label | ||
|
||
|
||
@BLENDINGS.register_module() | ||
class MixupBlending(BaseMiniBatchBlending): | ||
"""Implementing Mixup in a mini-batch. | ||
|
||
This module is proposed in `mixup: Beyond Empirical Risk Minimization | ||
<https://arxiv.org/abs/1710.09412>`_. | ||
Code Reference https://github.com/open-mmlab/mmclassification/blob/master/mmcls/models/utils/mixup.py # noqa | ||
|
||
Args: | ||
num_classes (int): The number of classes. | ||
alpha (float): Parameters for Beta distribution. | ||
""" | ||
|
||
def __init__(self, num_classes, alpha=1.): | ||
irvingzhang0512 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
super().__init__(num_classes=num_classes) | ||
self.beta = Beta(alpha, alpha) | ||
|
||
def do_blending(self, imgs, label, **kwargs): | ||
"""Blending images with mixup.""" | ||
assert len(kwargs) == 0, f'unexpected kwargs for mixup {kwargs}' | ||
|
||
lam = self.beta.sample() | ||
batch_size = imgs.size(0) | ||
rand_index = torch.randperm(batch_size) | ||
|
||
mixed_imgs = lam * imgs + (1 - lam) * imgs[rand_index, :] | ||
mixed_label = lam * label + (1 - lam) * label[rand_index, :] | ||
|
||
return mixed_imgs, mixed_label | ||
|
||
|
||
@BLENDINGS.register_module() | ||
class CutmixBlending(BaseMiniBatchBlending): | ||
"""Implementing Cutmix in a mini-batch. | ||
|
||
This module is proposed in `CutMix: Regularization Strategy to Train Strong | ||
Classifiers with Localizable Features <https://arxiv.org/abs/1905.04899>`_. | ||
Code Reference https://github.com/clovaai/CutMix-PyTorch | ||
|
||
Args: | ||
num_classes (int): The number of classes. | ||
alpha (float): Parameters for Beta distribution. | ||
""" | ||
|
||
def __init__(self, num_classes, alpha=1.): | ||
super().__init__(num_classes=num_classes) | ||
self.beta = Beta(alpha, alpha) | ||
|
||
@staticmethod | ||
def rand_bbox(img_size, lam): | ||
"""Generate a random boudning box.""" | ||
w = img_size[-1] | ||
h = img_size[-2] | ||
cut_rat = torch.sqrt(1. - lam) | ||
cut_w = torch.tensor(int(w * cut_rat)) | ||
cut_h = torch.tensor(int(h * cut_rat)) | ||
|
||
# uniform | ||
cx = torch.randint(w, (1, ))[0] | ||
cy = torch.randint(h, (1, ))[0] | ||
|
||
bbx1 = torch.clamp(cx - cut_w // 2, 0, w) | ||
bby1 = torch.clamp(cy - cut_h // 2, 0, h) | ||
bbx2 = torch.clamp(cx + cut_w // 2, 0, w) | ||
bby2 = torch.clamp(cy + cut_h // 2, 0, h) | ||
|
||
return bbx1, bby1, bbx2, bby2 | ||
|
||
def do_blending(self, imgs, label, **kwargs): | ||
"""Blending images with cutmix.""" | ||
assert len(kwargs) == 0, f'unexpected kwargs for cutmix {kwargs}' | ||
|
||
batch_size = imgs.size(0) | ||
rand_index = torch.randperm(batch_size) | ||
lam = self.beta.sample() | ||
|
||
bbx1, bby1, bbx2, bby2 = self.rand_bbox(imgs.size(), lam) | ||
imgs[:, ..., bby1:bby2, bbx1:bbx2] = imgs[rand_index, ..., bby1:bby2, | ||
bbx1:bbx2] | ||
lam = 1 - (1.0 * (bbx2 - bbx1) * (bby2 - bby1) / | ||
(imgs.size()[-1] * imgs.size()[-2])) | ||
|
||
label = lam * label + (1 - lam) * label[rand_index, :] | ||
|
||
return imgs, label |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
|
||
DATASETS = Registry('dataset') | ||
PIPELINES = Registry('pipeline') | ||
BLENDINGS = Registry('blending') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import torch | ||
|
||
from mmaction.datasets import CutmixBlending, MixupBlending | ||
|
||
|
||
def test_mixup(): | ||
alpha = 0.2 | ||
num_classes = 10 | ||
label = torch.randint(0, num_classes, (4, )) | ||
mixup = MixupBlending(num_classes, alpha) | ||
|
||
# NCHW imgs | ||
imgs = torch.randn(4, 4, 3, 32, 32) | ||
mixed_imgs, mixed_label = mixup(imgs, label) | ||
assert mixed_imgs.shape == torch.Size((4, 4, 3, 32, 32)) | ||
assert mixed_label.shape == torch.Size((4, num_classes)) | ||
|
||
# NCTHW imgs | ||
imgs = torch.randn(4, 4, 2, 3, 32, 32) | ||
mixed_imgs, mixed_label = mixup(imgs, label) | ||
assert mixed_imgs.shape == torch.Size((4, 4, 2, 3, 32, 32)) | ||
assert mixed_label.shape == torch.Size((4, num_classes)) | ||
|
||
|
||
def test_cutmix(): | ||
alpha = 0.2 | ||
num_classes = 10 | ||
label = torch.randint(0, num_classes, (4, )) | ||
mixup = CutmixBlending(num_classes, alpha) | ||
|
||
# NCHW imgs | ||
imgs = torch.randn(4, 4, 3, 32, 32) | ||
mixed_imgs, mixed_label = mixup(imgs, label) | ||
assert mixed_imgs.shape == torch.Size((4, 4, 3, 32, 32)) | ||
assert mixed_label.shape == torch.Size((4, num_classes)) | ||
|
||
# NCTHW imgs | ||
imgs = torch.randn(4, 4, 2, 3, 32, 32) | ||
mixed_imgs, mixed_label = mixup(imgs, label) | ||
assert mixed_imgs.shape == torch.Size((4, 4, 2, 3, 32, 32)) | ||
assert mixed_label.shape == torch.Size((4, num_classes)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
remove