This is an unofficial PyTorch implementation by Ignacio Oguiza - oguiza@gmail.com based on:
class
ResBlockPlus
[source]
ResBlockPlus
(ni
,nf
,ks
=[7, 5, 3]
,coord
=False
,separable
=False
,bn_1st
=True
,zero_norm
=False
,sa
=False
,se
=None
,act
=ReLU
,act_kwargs
={}
) ::Module
Same as nn.Module
, but no need for subclasses to call super().__init__
class
ResNetPlus
[source]
ResNetPlus
(c_in
,c_out
,seq_len
=None
,nf
=64
,sa
=False
,se
=None
,fc_dropout
=0.0
,concat_pool
=False
,flatten
=False
,custom_head
=None
,y_range
=None
,ks
=[7, 5, 3]
,coord
=False
,separable
=False
,bn_1st
=True
,zero_norm
=False
,act
=ReLU
,act_kwargs
={}
) ::Sequential
A sequential container. Modules will be added to it in the order they are passed in the constructor. Alternatively, an ordered dict of modules can also be passed in.
To make it easier to understand, here is a small example::
# Example of using Sequential
model = nn.Sequential(
nn.Conv2d(1,20,5),
nn.ReLU(),
nn.Conv2d(20,64,5),
nn.ReLU()
)
# Example of using Sequential with OrderedDict
model = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(1,20,5)),
('relu1', nn.ReLU()),
('conv2', nn.Conv2d(20,64,5)),
('relu2', nn.ReLU())
]))
xb = torch.rand(2, 3, 4)
test_eq(ResNetPlus(3,2)(xb).shape, [xb.shape[0], 2])
test_eq(ResNetPlus(3,2,coord=True, separable=True, bn_1st=False, zero_norm=True, act=Swish, act_kwargs={}, fc_dropout=0.5)(xb).shape, [xb.shape[0], 2])
test_eq(total_params(ResNetPlus(3, 2))[0], 479490) # for (3,2)
from tsai.models.ResNet import *
test_eq(total_params(ResNet(3, 2))[0], total_params(ResNetPlus(3, 2))[0]) # for (3,2)
m = ResNetPlus(3, 2, zero_norm=True, coord=True, separable=True)
print('n_params:', total_params(m)[0])
print(m)
print(check_weight(m, is_bn)[0])
n_params: 114820 ResNetPlus( (backbone): Sequential( (0): ResBlockPlus( (convblock1): ConvBlock( (0): AddCoords1d() (1): SeparableConv1d( (depthwise_conv): Conv1d(4, 4, kernel_size=(7,), stride=(1,), padding=(3,), groups=4, bias=False) (pointwise_conv): Conv1d(4, 64, kernel_size=(1,), stride=(1,), bias=False) ) (2): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (3): ReLU() ) (convblock2): ConvBlock( (0): AddCoords1d() (1): SeparableConv1d( (depthwise_conv): Conv1d(65, 65, kernel_size=(5,), stride=(1,), padding=(2,), groups=65, bias=False) (pointwise_conv): Conv1d(65, 64, kernel_size=(1,), stride=(1,), bias=False) ) (2): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (3): ReLU() ) (convblock3): ConvBlock( (0): AddCoords1d() (1): SeparableConv1d( (depthwise_conv): Conv1d(65, 65, kernel_size=(3,), stride=(1,), padding=(1,), groups=65, bias=False) (pointwise_conv): Conv1d(65, 64, kernel_size=(1,), stride=(1,), bias=False) ) (2): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) ) (shortcut): ConvBlock( (0): AddCoords1d() (1): Conv1d(4, 64, kernel_size=(1,), stride=(1,), bias=False) (2): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) ) (add): Add (act): ReLU() ) (1): ResBlockPlus( (convblock1): ConvBlock( (0): AddCoords1d() (1): SeparableConv1d( (depthwise_conv): Conv1d(65, 65, kernel_size=(7,), stride=(1,), padding=(3,), groups=65, bias=False) (pointwise_conv): Conv1d(65, 128, kernel_size=(1,), stride=(1,), bias=False) ) (2): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (3): ReLU() ) (convblock2): ConvBlock( (0): AddCoords1d() (1): SeparableConv1d( (depthwise_conv): Conv1d(129, 129, kernel_size=(5,), stride=(1,), padding=(2,), groups=129, bias=False) (pointwise_conv): Conv1d(129, 128, kernel_size=(1,), stride=(1,), bias=False) ) (2): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (3): ReLU() ) (convblock3): ConvBlock( (0): AddCoords1d() (1): SeparableConv1d( (depthwise_conv): Conv1d(129, 129, kernel_size=(3,), stride=(1,), padding=(1,), groups=129, bias=False) (pointwise_conv): Conv1d(129, 128, kernel_size=(1,), stride=(1,), bias=False) ) (2): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) ) (shortcut): ConvBlock( (0): AddCoords1d() (1): Conv1d(65, 128, kernel_size=(1,), stride=(1,), bias=False) (2): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) ) (add): Add (act): ReLU() ) (2): ResBlockPlus( (convblock1): ConvBlock( (0): AddCoords1d() (1): SeparableConv1d( (depthwise_conv): Conv1d(129, 129, kernel_size=(7,), stride=(1,), padding=(3,), groups=129, bias=False) (pointwise_conv): Conv1d(129, 128, kernel_size=(1,), stride=(1,), bias=False) ) (2): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (3): ReLU() ) (convblock2): ConvBlock( (0): AddCoords1d() (1): SeparableConv1d( (depthwise_conv): Conv1d(129, 129, kernel_size=(5,), stride=(1,), padding=(2,), groups=129, bias=False) (pointwise_conv): Conv1d(129, 128, kernel_size=(1,), stride=(1,), bias=False) ) (2): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (3): ReLU() ) (convblock3): ConvBlock( (0): AddCoords1d() (1): SeparableConv1d( (depthwise_conv): Conv1d(129, 129, kernel_size=(3,), stride=(1,), padding=(1,), groups=129, bias=False) (pointwise_conv): Conv1d(129, 128, kernel_size=(1,), stride=(1,), bias=False) ) (2): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) ) (shortcut): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (add): Add (act): ReLU() ) ) (head): Sequential( (0): GAP1d( (gap): AdaptiveAvgPool1d(output_size=1) (flatten): Flatten(full=False) ) (1): Linear(in_features=128, out_features=2, bias=True) ) ) [1. 1. 0. 1. 1. 1. 0. 1. 1. 1. 0. 1.]