This is a modified version of fastai’s XResNet model in github
source
XResNet1dPlus
def XResNet1dPlus(
block:PrePostInitMeta= ResBlock1dPlus, expansion:int = 4 , layers:list = [3 , 4 , 6 , 3 ], fc_dropout:float = 0.0 ,
c_in:int = 3 , c_out:NoneType= None , n_out:int = 1000 , seq_len:NoneType= None , stem_szs:tuple = (32 , 32 , 64 ),
widen:float = 1.0 , sa:bool = False , act_cls:type = ReLU, ks:int = 3 , stride:int = 2 , coord:bool = False ,
custom_head:NoneType= None , block_szs_base:tuple = (64 , 128 , 256 , 512 ), groups:int = 1 , reduction:NoneType= None ,
nh1:NoneType= None , nh2:NoneType= None , dw:bool = False , g2:int = 1 , sym:bool = False , norm:str = 'Batch' ,
zero_norm:bool = True , pool:function= AvgPool, pool_first:bool = True
):
A sequential container.
Modules will be added to it in the order they are passed in the constructor. Alternatively, an OrderedDict of modules can be passed in. The forward() method of [Sequential](https://timeseriesAI.github.io/tsai/models.tabfusiontransformer.html#sequential) accepts any input and forwards it to the first module it contains. It then “chains” outputs to inputs sequentially for each subsequent module, finally returning the output of the last module.
The value a [Sequential](https://timeseriesAI.github.io/tsai/models.tabfusiontransformer.html#sequential) provides over manually calling a sequence of modules is that it allows treating the whole container as a single module, such that performing a transformation on the [Sequential](https://timeseriesAI.github.io/tsai/models.tabfusiontransformer.html#sequential) applies to each of the modules it stores (which are each a registered submodule of the [Sequential](https://timeseriesAI.github.io/tsai/models.tabfusiontransformer.html#sequential)).
What’s the difference between a [Sequential](https://timeseriesAI.github.io/tsai/models.tabfusiontransformer.html#sequential) and a :class:torch.nn.ModuleList? A ModuleList is exactly what it sounds like–a list for storing Module s! On the other hand, the layers in a [Sequential](https://timeseriesAI.github.io/tsai/models.tabfusiontransformer.html#sequential) are connected in a cascading way.
Example::
# Using Sequential to create a small model. When `model` is run,
# input will first be passed to `Conv2d(1,20,5)`. The output of
# `Conv2d(1,20,5)` will be used as the input to the first
# `ReLU`; the output of the first `ReLU` will become the input
# for `Conv2d(20,64,5)`. Finally, the output of
# `Conv2d(20,64,5)` will be used as input to the second `ReLU`
model = nn.Sequential(
nn.Conv2d(1, 20, 5), nn.ReLU(), nn.Conv2d(20, 64, 5), nn.ReLU()
)
# Using Sequential with OrderedDict. This is functionally the
# same as the above code
model = nn.Sequential(
OrderedDict(
[
("conv1", nn.Conv2d(1, 20, 5)),
("relu1", nn.ReLU()),
("conv2", nn.Conv2d(20, 64, 5)),
("relu2", nn.ReLU()),
]
)
)
source
xresnet1d50_deeperplus
def xresnet1d50_deeperplus(
c_in, c_out, seq_len:NoneType= None , act:type = ReLU, stride:int = 1 , groups:int = 1 , reduction:NoneType= None ,
nh1:NoneType= None , nh2:NoneType= None , dw:bool = False , g2:int = 1 , sa:bool = False , sym:bool = False ,
norm_type:NormType=< NormType.Batch: 1 > , act_cls:type = ReLU, ndim:int = 2 , ks:int = 3 , pool:function= AvgPool,
pool_first:bool = True , padding:NoneType= None , bias:NoneType= None , bn_1st:bool = True , transpose:bool = False ,
init:str = 'auto' , xtra:NoneType= None , bias_std:float = 0.01 , dilation:Union= 1 , padding_mode:Literal= 'zeros' ,
device:NoneType= None , dtype:NoneType= None
):
Call self as a function.
source
xresnet1d34_deeperplus
def xresnet1d34_deeperplus(
c_in, c_out, seq_len:NoneType= None , act:type = ReLU, stride:int = 1 , groups:int = 1 , reduction:NoneType= None ,
nh1:NoneType= None , nh2:NoneType= None , dw:bool = False , g2:int = 1 , sa:bool = False , sym:bool = False ,
norm_type:NormType=< NormType.Batch: 1 > , act_cls:type = ReLU, ndim:int = 2 , ks:int = 3 , pool:function= AvgPool,
pool_first:bool = True , padding:NoneType= None , bias:NoneType= None , bn_1st:bool = True , transpose:bool = False ,
init:str = 'auto' , xtra:NoneType= None , bias_std:float = 0.01 , dilation:Union= 1 , padding_mode:Literal= 'zeros' ,
device:NoneType= None , dtype:NoneType= None
):
Call self as a function.
source
xresnet1d18_deeperplus
def xresnet1d18_deeperplus(
c_in, c_out, seq_len:NoneType= None , act:type = ReLU, stride:int = 1 , groups:int = 1 , reduction:NoneType= None ,
nh1:NoneType= None , nh2:NoneType= None , dw:bool = False , g2:int = 1 , sa:bool = False , sym:bool = False ,
norm_type:NormType=< NormType.Batch: 1 > , act_cls:type = ReLU, ndim:int = 2 , ks:int = 3 , pool:function= AvgPool,
pool_first:bool = True , padding:NoneType= None , bias:NoneType= None , bn_1st:bool = True , transpose:bool = False ,
init:str = 'auto' , xtra:NoneType= None , bias_std:float = 0.01 , dilation:Union= 1 , padding_mode:Literal= 'zeros' ,
device:NoneType= None , dtype:NoneType= None
):
Call self as a function.
source
xresnet1d50_deepplus
def xresnet1d50_deepplus(
c_in, c_out, seq_len:NoneType= None , act:type = ReLU, stride:int = 1 , groups:int = 1 , reduction:NoneType= None ,
nh1:NoneType= None , nh2:NoneType= None , dw:bool = False , g2:int = 1 , sa:bool = False , sym:bool = False ,
norm_type:NormType=< NormType.Batch: 1 > , act_cls:type = ReLU, ndim:int = 2 , ks:int = 3 , pool:function= AvgPool,
pool_first:bool = True , padding:NoneType= None , bias:NoneType= None , bn_1st:bool = True , transpose:bool = False ,
init:str = 'auto' , xtra:NoneType= None , bias_std:float = 0.01 , dilation:Union= 1 , padding_mode:Literal= 'zeros' ,
device:NoneType= None , dtype:NoneType= None
):
Call self as a function.
source
xresnet1d34_deepplus
def xresnet1d34_deepplus(
c_in, c_out, seq_len:NoneType= None , act:type = ReLU, stride:int = 1 , groups:int = 1 , reduction:NoneType= None ,
nh1:NoneType= None , nh2:NoneType= None , dw:bool = False , g2:int = 1 , sa:bool = False , sym:bool = False ,
norm_type:NormType=< NormType.Batch: 1 > , act_cls:type = ReLU, ndim:int = 2 , ks:int = 3 , pool:function= AvgPool,
pool_first:bool = True , padding:NoneType= None , bias:NoneType= None , bn_1st:bool = True , transpose:bool = False ,
init:str = 'auto' , xtra:NoneType= None , bias_std:float = 0.01 , dilation:Union= 1 , padding_mode:Literal= 'zeros' ,
device:NoneType= None , dtype:NoneType= None
):
Call self as a function.
source
xresnet1d18_deepplus
def xresnet1d18_deepplus(
c_in, c_out, seq_len:NoneType= None , act:type = ReLU, stride:int = 1 , groups:int = 1 , reduction:NoneType= None ,
nh1:NoneType= None , nh2:NoneType= None , dw:bool = False , g2:int = 1 , sa:bool = False , sym:bool = False ,
norm_type:NormType=< NormType.Batch: 1 > , act_cls:type = ReLU, ndim:int = 2 , ks:int = 3 , pool:function= AvgPool,
pool_first:bool = True , padding:NoneType= None , bias:NoneType= None , bn_1st:bool = True , transpose:bool = False ,
init:str = 'auto' , xtra:NoneType= None , bias_std:float = 0.01 , dilation:Union= 1 , padding_mode:Literal= 'zeros' ,
device:NoneType= None , dtype:NoneType= None
):
Call self as a function.
source
xresnet1d152plus
def xresnet1d152plus(
c_in, c_out, seq_len:NoneType= None , act:type = ReLU, stride:int = 1 , groups:int = 1 , reduction:NoneType= None ,
nh1:NoneType= None , nh2:NoneType= None , dw:bool = False , g2:int = 1 , sa:bool = False , sym:bool = False ,
norm_type:NormType=< NormType.Batch: 1 > , act_cls:type = ReLU, ndim:int = 2 , ks:int = 3 , pool:function= AvgPool,
pool_first:bool = True , padding:NoneType= None , bias:NoneType= None , bn_1st:bool = True , transpose:bool = False ,
init:str = 'auto' , xtra:NoneType= None , bias_std:float = 0.01 , dilation:Union= 1 , padding_mode:Literal= 'zeros' ,
device:NoneType= None , dtype:NoneType= None
):
Call self as a function.
source
xresnet1d101plus
def xresnet1d101plus(
c_in, c_out, seq_len:NoneType= None , act:type = ReLU, stride:int = 1 , groups:int = 1 , reduction:NoneType= None ,
nh1:NoneType= None , nh2:NoneType= None , dw:bool = False , g2:int = 1 , sa:bool = False , sym:bool = False ,
norm_type:NormType=< NormType.Batch: 1 > , act_cls:type = ReLU, ndim:int = 2 , ks:int = 3 , pool:function= AvgPool,
pool_first:bool = True , padding:NoneType= None , bias:NoneType= None , bn_1st:bool = True , transpose:bool = False ,
init:str = 'auto' , xtra:NoneType= None , bias_std:float = 0.01 , dilation:Union= 1 , padding_mode:Literal= 'zeros' ,
device:NoneType= None , dtype:NoneType= None
):
Call self as a function.
source
xresnet1d50plus
def xresnet1d50plus(
c_in, c_out, seq_len:NoneType= None , act:type = ReLU, stride:int = 1 , groups:int = 1 , reduction:NoneType= None ,
nh1:NoneType= None , nh2:NoneType= None , dw:bool = False , g2:int = 1 , sa:bool = False , sym:bool = False ,
norm_type:NormType=< NormType.Batch: 1 > , act_cls:type = ReLU, ndim:int = 2 , ks:int = 3 , pool:function= AvgPool,
pool_first:bool = True , padding:NoneType= None , bias:NoneType= None , bn_1st:bool = True , transpose:bool = False ,
init:str = 'auto' , xtra:NoneType= None , bias_std:float = 0.01 , dilation:Union= 1 , padding_mode:Literal= 'zeros' ,
device:NoneType= None , dtype:NoneType= None
):
Call self as a function.
source
xresnet1d34plus
def xresnet1d34plus(
c_in, c_out, seq_len:NoneType= None , act:type = ReLU, stride:int = 1 , groups:int = 1 , reduction:NoneType= None ,
nh1:NoneType= None , nh2:NoneType= None , dw:bool = False , g2:int = 1 , sa:bool = False , sym:bool = False ,
norm_type:NormType=< NormType.Batch: 1 > , act_cls:type = ReLU, ndim:int = 2 , ks:int = 3 , pool:function= AvgPool,
pool_first:bool = True , padding:NoneType= None , bias:NoneType= None , bn_1st:bool = True , transpose:bool = False ,
init:str = 'auto' , xtra:NoneType= None , bias_std:float = 0.01 , dilation:Union= 1 , padding_mode:Literal= 'zeros' ,
device:NoneType= None , dtype:NoneType= None
):
Call self as a function.
source
xresnet1d18plus
def xresnet1d18plus(
c_in, c_out, seq_len:NoneType= None , act:type = ReLU, stride:int = 1 , groups:int = 1 , reduction:NoneType= None ,
nh1:NoneType= None , nh2:NoneType= None , dw:bool = False , g2:int = 1 , sa:bool = False , sym:bool = False ,
norm_type:NormType=< NormType.Batch: 1 > , act_cls:type = ReLU, ndim:int = 2 , ks:int = 3 , pool:function= AvgPool,
pool_first:bool = True , padding:NoneType= None , bias:NoneType= None , bn_1st:bool = True , transpose:bool = False ,
init:str = 'auto' , xtra:NoneType= None , bias_std:float = 0.01 , dilation:Union= 1 , padding_mode:Literal= 'zeros' ,
device:NoneType= None , dtype:NoneType= None
):
Call self as a function.
net = xresnet1d18plus(3 , 2 , coord= True )
x = torch.rand(32 , 3 , 50 )
net(x)
block <class 'tsai.models.layers.ResBlock1dPlus'> expansion 1 layers [2, 2, 2, 2]
TensorBase([[ 0.1829, 0.3597],
[ 0.0274, -0.1443],
[ 0.0240, -0.2374],
[-0.1323, -0.6574],
[ 0.1481, -0.1438],
[ 0.2410, -0.1225],
[-0.1186, -0.1978],
[-0.0640, -0.4547],
[-0.0229, -0.3214],
[ 0.2336, -0.4466],
[-0.1843, -0.0934],
[-0.0416, 0.1997],
[-0.0109, -0.0253],
[ 0.3014, -0.2193],
[ 0.0966, 0.0602],
[ 0.2364, 0.2209],
[-0.1437, -0.1476],
[ 0.0070, -0.2900],
[ 0.2807, 0.4797],
[-0.2386, -0.1563],
[ 0.1620, -0.2285],
[ 0.0479, -0.2348],
[ 0.1573, -0.4420],
[-0.5469, 0.1512],
[ 0.0243, -0.1806],
[ 0.3396, 0.1434],
[ 0.0666, -0.1644],
[ 0.3286, -0.5637],
[ 0.0993, -0.6281],
[-0.1068, -0.0763],
[-0.2713, 0.1946],
[-0.1416, -0.4043]], grad_fn=<AliasBackward0>)
bs, c_in, seq_len = 2 , 4 , 32
c_out = 2
x = torch.rand(bs, c_in, seq_len)
archs = [
xresnet1d18plus, xresnet1d34plus, xresnet1d50plus,
xresnet1d18_deepplus, xresnet1d34_deepplus, xresnet1d50_deepplus, xresnet1d18_deeperplus,
xresnet1d34_deeperplus, xresnet1d50_deeperplus
# # Long test
# xresnet1d101, xresnet1d152,
]
for i, arch in enumerate (archs):
print (i, arch.__name__ )
test_eq(arch(c_in, c_out, sa= True , act= Mish, coord= True )(x).shape, (bs, c_out))
0 xresnet1d18plus
block <class 'tsai.models.layers.ResBlock1dPlus'> expansion 1 layers [2, 2, 2, 2]
1 xresnet1d34plus
block <class 'tsai.models.layers.ResBlock1dPlus'> expansion 1 layers [3, 4, 6, 3]
2 xresnet1d50plus
block <class 'tsai.models.layers.ResBlock1dPlus'> expansion 4 layers [3, 4, 6, 3]
3 xresnet1d18_deepplus
block <class 'tsai.models.layers.ResBlock1dPlus'> expansion 1 layers [2, 2, 2, 2, 1, 1]
4 xresnet1d34_deepplus
block <class 'tsai.models.layers.ResBlock1dPlus'> expansion 1 layers [3, 4, 6, 3, 1, 1]
5 xresnet1d50_deepplus
block <class 'tsai.models.layers.ResBlock1dPlus'> expansion 4 layers [3, 4, 6, 3, 1, 1]
6 xresnet1d18_deeperplus
block <class 'tsai.models.layers.ResBlock1dPlus'> expansion 1 layers [2, 2, 1, 1, 1, 1, 1, 1]
7 xresnet1d34_deeperplus
block <class 'tsai.models.layers.ResBlock1dPlus'> expansion 1 layers [3, 4, 6, 3, 1, 1, 1, 1]
8 xresnet1d50_deeperplus
block <class 'tsai.models.layers.ResBlock1dPlus'> expansion 4 layers [3, 4, 6, 3, 1, 1, 1, 1]
m = xresnet1d34plus(4 , 2 , act= Mish)
test_eq(len (get_layers(m, is_bn)), 38 )
test_eq(check_weight(m, is_bn)[0 ].sum (), 22 )
block <class 'tsai.models.layers.ResBlock1dPlus'> expansion 1 layers [3, 4, 6, 3]