<jupyter_start><jupyter_text>YaResNet.> Yet Another ResNet model.<jupyter_code>#hide
import torch
import torch.nn as nn

from nbdev.showdoc import show_doc
from IPython.display import Markdown, display
# hide
def print_doc(func_name):
    doc = show_doc(func_name, title_level=4, disp=False)
    display(Markdown(doc))
#hide
# from nbdev.showdoc import *
# from fastcore.test import *
# import torch
# import torch.nn as nn
# from functools import partial
# from collections import OrderedDict
# from model_constructor.layers import SEBlock, ConvLayer, act_fn, noop, SimpleSelfAttention
# from model_constructor.net import Net
# from model_constructor.activations import Mish<jupyter_output><empty_output><jupyter_text>YaResBlock<jupyter_code>#hide
from model_constructor.yaresnet import YaResBlock
#hide_input
print_doc(YaResBlock)
#collapse_output
bl = YaResBlock(1,64,64,sa=True)
bl
#hide
bs_test = 16
xb = torch.randn(bs_test, 64, 32, 32)
y = bl(xb)
print(y.shape)
assert y.shape == torch.Size([bs_test, 64, 32, 32]), f"size"
#collapse_output
bl = YaResBlock(1,64,64,se=True)
bl
#hide
bs_test = 16
xb = torch.randn(bs_test, 64, 32, 32)
y = bl(xb)
print(y.shape)
assert y.shape == torch.Size([bs_test, 64, 32, 32]), f"size"
#collapse_output
bl = YaResBlock(4,64,128,stride=2,act_fn=nn.LeakyReLU(), bn_1st=False)
bl
#hide
bs_test = 16
xb = torch.randn(bs_test, 256, 32, 32)
y = bl(xb)
print(y.shape)
assert y.shape == torch.Size([bs_test, 512, 16, 16]), f"size"
#collapse_output
bl = YaResBlock(4,64,128,stride=2,act_fn=nn.LeakyReLU(), bn_1st=False, groups=4)
bl
#hide
bs_test = 16
xb = torch.randn(bs_test, 256, 32, 32)
y = bl(xb)
print(y.shape)
assert y.shape == torch.Size([bs_test, 512, 16, 16]), f"size"
#collapse_output
bl = YaResBlock(4,64,128,stride=2,act_fn=nn.LeakyReLU(), bn_1st=False, div_groups=4)
bl
#hide
bs_test = 16
xb = torch.randn(bs_test, 256, 32, 32)
y = bl(xb)
print(y.shape)
assert y.shape == torch.Size([bs_test, 512, 16, 16]), f"size"
#collapse_output
bl = YaResBlock(4,64,128,stride=2,act_fn=nn.LeakyReLU(), bn_1st=False, dw=True)
bl
#hide
bs_test = 16
xb = torch.randn(bs_test, 256, 32, 32)
y = bl(xb)
print(y.shape)
assert y.shape == torch.Size([bs_test, 512, 16, 16]), f"size"<jupyter_output>torch.Size([16, 512, 16, 16])<jupyter_text>YaResNet constructor.<jupyter_code>from model_constructor import ModelConstructor
yaresnet  = ModelConstructor(block=YaResBlock, stem_sizes = [3, 32, 64, 64], name='YaResNet')
yaresnet
yaresnet.block_sizes, yaresnet.layers
#collapse_output
yaresnet.stem
#hide
bs_test = 16
xb = torch.randn(bs_test, 3, 128, 128)
y = yaresnet.stem(xb)
print(y.shape)
assert y.shape == torch.Size([bs_test, 64, 32, 32]), f"size"
#collapse_output
yaresnet.body
#collapse_output
yaresnet.head<jupyter_output><empty_output><jupyter_text>Lots of experiments showed that it worth trying Mish activation function.<jupyter_code>#collapse_output
yaresnet.act_fn = torch.nn.Mish()
yaresnet()
#hide
bs_test = 16
xb = torch.randn(bs_test, 3, 128, 128)
y = yaresnet()(xb)
print(y.shape)
assert y.shape == torch.Size([bs_test, 1000]), f"size"<jupyter_output>torch.Size([16, 1000])
{'fraction_non_alphanumeric': 0.14258188824662812, 'fraction_numerical': 0.060051380860629416, 'mean_word_length': 6.653562653562654, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '663893', 'license': 'Apache-2.0', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1246, 'n_words': 282, 'path': 'Nbs/04_YaResNet.ipynb', 'repo_name': 'ayasyrev/model_constructor', 'size': '3114'}