merge master
parent
cf85db0f7c
commit
8249ad7205
mmcls/models/backbones
tests/test_backbones
|
@ -4,60 +4,10 @@ import torch.utils.checkpoint as cp
|
|||
from mmcv.cnn import ConvModule, constant_init, kaiming_init
|
||||
from torch.nn.modules.batchnorm import _BatchNorm
|
||||
|
||||
from mmcls.models.utils import channel_shuffle
|
||||
from .base_backbone import BaseBackbone
|
||||
|
||||
|
||||
def channel_shuffle(x, groups):
|
||||
""" Channel Shuffle operation.
|
||||
|
||||
This function enables cross-group information flow for multiple groups
|
||||
convolution layers.
|
||||
|
||||
Args:
|
||||
x (Tensor): The input tensor.
|
||||
groups (int): The number of groups to divide the input tensor
|
||||
in the channel dimension.
|
||||
|
||||
Returns:
|
||||
Tensor: The output tensor after channel shuffle operation.
|
||||
"""
|
||||
|
||||
batchsize, num_channels, height, width = x.size()
|
||||
assert (num_channels % groups == 0), ('num_channels should be '
|
||||
'divisible by groups')
|
||||
channels_per_group = num_channels // groups
|
||||
|
||||
x = x.view(batchsize, groups, channels_per_group, height, width)
|
||||
x = torch.transpose(x, 1, 2).contiguous()
|
||||
x = x.view(batchsize, -1, height, width)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
def make_divisible(value, divisor, min_value=None):
|
||||
""" Make divisible function.
|
||||
|
||||
This function ensures that all layers have a channel number that is
|
||||
divisible by divisor.
|
||||
|
||||
Args:
|
||||
value (int): The original channel number.
|
||||
divisor (int): The divisor to fully divide the channel number.
|
||||
min_value (int, optional): The minimum value of the output channel.
|
||||
|
||||
Returns:
|
||||
int: The modified output channel number
|
||||
"""
|
||||
|
||||
if min_value is None:
|
||||
min_value = divisor
|
||||
new_value = max(min_value, int(value + divisor / 2) // divisor * divisor)
|
||||
# Make sure that round down does not go down by more than 10%.
|
||||
if new_value < 0.9 * value:
|
||||
new_value += divisor
|
||||
return new_value
|
||||
|
||||
|
||||
class InvertedResidual(nn.Module):
|
||||
"""InvertedResidual block for ShuffleNetV2 backbone.
|
||||
|
||||
|
|
|
@ -4,9 +4,7 @@ from torch.nn.modules import GroupNorm
|
|||
from torch.nn.modules.batchnorm import _BatchNorm
|
||||
|
||||
from mmcls.models.backbones import ShuffleNetv2
|
||||
from mmcls.models.backbones.shufflenet_v2 import (InvertedResidual,
|
||||
channel_shuffle,
|
||||
make_divisible)
|
||||
from mmcls.models.backbones.shufflenet_v2 import InvertedResidual
|
||||
|
||||
|
||||
def is_block(modules):
|
||||
|
@ -32,21 +30,6 @@ def check_norm_state(modules, train_state):
|
|||
return True
|
||||
|
||||
|
||||
def test_channel_shuffle():
|
||||
x = torch.randn(1, 24, 56, 56)
|
||||
with pytest.raises(AssertionError):
|
||||
# num_channels should be divisible by groups
|
||||
channel_shuffle(x, 7)
|
||||
|
||||
|
||||
def test_make_divisible():
|
||||
# test min_value is None
|
||||
make_divisible(34, 8, None)
|
||||
|
||||
# test new_value < 0.9 * value
|
||||
make_divisible(10, 8, None)
|
||||
|
||||
|
||||
def test_shufflenetv2_invertedresidual():
|
||||
|
||||
with pytest.raises(AssertionError):
|
||||
|
|
Loading…
Reference in New Issue