Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions Nbs/00_ModelConstructor.ipynb → Nbs/01_ModelConstructor.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,21 @@
"name": "stdout",
"output_type": "stream",
"text": [
"ModelCfg\n",
" in_chans: 3, num_classes: 1000\n",
" expansion: 1, groups: 1, dw: False, div_groups: None\n",
" act_fn: ReLU, sa: False, se: False\n",
" stem sizes: [64], stride on 0\n",
" body sizes [64, 128, 256, 512]\n",
" layers: [2, 2, 2, 2]\n"
"ModelCfg(\n",
" in_chans=3\n",
" num_classes=1000\n",
" block='BasicBlock'\n",
" conv_layer='ConvBnAct'\n",
" block_sizes=[64, 128, 256, 512]\n",
" layers=[2, 2, 2, 2]\n",
" norm='BatchNorm2d'\n",
" act_fn='ReLU'\n",
" expansion=1\n",
" groups=1\n",
" bn_1st=True\n",
" zero_bn=True\n",
" stem_sizes=[64]\n",
" stem_pool=\"MaxPool2d {'kernel_size': 3, 'stride': 2, 'padding': 1}\")\n"
]
}
],
Expand Down
1 change: 0 additions & 1 deletion Nbs/001_Blocks.ipynb → Nbs/02_1_Blocks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"outputs": [],
"source": [
"#hide\n",
"import torch\n",
"import torch.nn as nn\n",
"from functools import partial"
]
Expand Down
File renamed without changes.
9 changes: 8 additions & 1 deletion Nbs/02_XResNet.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"source": [
"#hide\n",
"import torch\n",
"from typing import List\n",
"\n",
"from functools import partial\n",
"\n",
Expand Down Expand Up @@ -98,6 +97,14 @@
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/aya/mambaforge/envs/mc/lib/python3.10/site-packages/torch/nn/modules/conv.py:137: UserWarning: Failed to initialize NumPy: No module named 'numpy' (Triggered internally at ../torch/csrc/utils/tensor_numpy.cpp:84.)\n",
" self.weight = Parameter(torch.empty(\n"
]
},
{
"data": {
"text/plain": [
Expand Down
31 changes: 30 additions & 1 deletion Nbs/03_MXResNet.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"outputs": [],
"source": [
"#hide\n",
"from typing import List\n",
"from functools import partial\n",
"\n",
"import torch\n",
Expand Down Expand Up @@ -106,6 +105,14 @@
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/aya/mambaforge/envs/mc/lib/python3.10/site-packages/torch/nn/modules/conv.py:137: UserWarning: Failed to initialize NumPy: No module named 'numpy' (Triggered internally at ../torch/csrc/utils/tensor_numpy.cpp:84.)\n",
" self.weight = Parameter(torch.empty(\n"
]
},
{
"data": {
"text/plain": [
Expand Down Expand Up @@ -664,6 +671,28 @@
"model = MxResNet34.create_model()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([16, 1000])\n"
]
}
],
"source": [
"#hide\n",
"bs_test = 16\n",
"xb = torch.randn(bs_test, 3, 128, 128)\n",
"y = model(xb)\n",
"print(y.shape)\n",
"assert y.shape == torch.Size([bs_test, 1000]), f\"size\""
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
30 changes: 16 additions & 14 deletions Nbs/04_YaResNet.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -443,39 +443,39 @@
{
"data": {
"text/plain": [
"YaResBlock(\n",
"YaBottleneckBlock(\n",
" (reduce): AvgPool2d(kernel_size=2, stride=2, padding=0)\n",
" (convs): Sequential(\n",
" (conv_0): ConvBnAct(\n",
" (conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
" (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" (conv): Conv2d(64, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
" (bn): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" (act_fn): LeakyReLU(negative_slope=0.01, inplace=True)\n",
" )\n",
" (conv_1): ConvBnAct(\n",
" (conv): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=128, bias=False)\n",
" (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" (conv): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=32, bias=False)\n",
" (bn): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" (act_fn): LeakyReLU(negative_slope=0.01, inplace=True)\n",
" )\n",
" (conv_2): ConvBnAct(\n",
" (conv): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
" (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" (conv): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
" (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" )\n",
" (se): SEModule(\n",
" (squeeze): AdaptiveAvgPool2d(output_size=1)\n",
" (excitation): Sequential(\n",
" (reduce): Linear(in_features=512, out_features=32, bias=True)\n",
" (reduce): Linear(in_features=128, out_features=8, bias=True)\n",
" (se_act): ReLU(inplace=True)\n",
" (expand): Linear(in_features=32, out_features=512, bias=True)\n",
" (expand): Linear(in_features=8, out_features=128, bias=True)\n",
" (se_gate): Sigmoid()\n",
" )\n",
" )\n",
" (sa): SimpleSelfAttention(\n",
" (conv): Conv1d(512, 512, kernel_size=(1,), stride=(1,), bias=False)\n",
" (conv): Conv1d(128, 128, kernel_size=(1,), stride=(1,), bias=False)\n",
" )\n",
" )\n",
" (id_conv): ConvBnAct(\n",
" (conv): Conv2d(256, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
" (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" (conv): Conv2d(64, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
" (bn): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
" )\n",
" (merge): LeakyReLU(negative_slope=0.01, inplace=True)\n",
")"
Expand All @@ -488,8 +488,10 @@
],
"source": [
"#collapse_output\n",
"bl = YaResBlock(\n",
" 4, 64, 128,\n",
"bl = YaBottleneckBlock(\n",
" 64,\n",
" 128,\n",
" expansion=4,\n",
" stride=2,\n",
" pool=pool,\n",
" act_fn=nn.LeakyReLU,\n",
Expand Down
8 changes: 8 additions & 0 deletions Nbs/06_ConvMixer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,14 @@
"#collapse_output\n",
"convmixer_1024_20[1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dacb4659",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
142 changes: 110 additions & 32 deletions Nbs/README.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"outputs": [],
"source": [
"#hide\n",
"import torch\n",
"from torch import nn"
]
},
Expand Down Expand Up @@ -143,13 +142,26 @@
"name": "stdout",
"output_type": "stream",
"text": [
"ModelConstructor\n",
" in_chans: 3, num_classes: 1000\n",
" expansion: 1, groups: 1, dw: False, div_groups: None\n",
" act_fn: ReLU, sa: False, se: False\n",
" stem sizes: [64], stride on 0\n",
" body sizes [64, 128, 256, 512]\n",
" layers: [2, 2, 2, 2]\n"
"ModelConstructor(\n",
" in_chans=3\n",
" num_classes=1000\n",
" block='BasicBlock'\n",
" conv_layer='ConvBnAct'\n",
" block_sizes=[64, 128, 256, 512]\n",
" layers=[2, 2, 2, 2]\n",
" norm='BatchNorm2d'\n",
" act_fn='ReLU'\n",
" expansion=1\n",
" groups=1\n",
" bn_1st=True\n",
" zero_bn=True\n",
" stem_sizes=[64]\n",
" stem_pool=\"MaxPool2d {'kernel_size': 3, 'stride': 2, 'padding': 1}\"\n",
" init_cnn='init_cnn'\n",
" make_stem='make_stem'\n",
" make_layer='make_layer'\n",
" make_body='make_body'\n",
" make_head='make_head')\n"
]
}
],
Expand Down Expand Up @@ -404,6 +416,32 @@
"mc.print_changed_fields()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can compare changed with defaults."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Changed fields:\n",
"layers: [3, 4, 6, 3] | [2, 2, 2, 2]\n",
"expansion: 4 | 1\n"
]
}
],
"source": [
"mc.print_changed_fields(show_default=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -715,7 +753,21 @@
"name": "stdout",
"output_type": "stream",
"text": [
"in_chans=3 num_classes=10 block='BasicBlock' conv_layer='ConvBnAct' block_sizes=[64, 128, 256, 512] layers=[2, 2, 2, 2] norm='BatchNorm2d' act_fn='Mish' expansion=1 groups=1 bn_1st=True zero_bn=True stem_sizes=[64] stem_pool=\"MaxPool2d {'kernel_size': 3, 'stride': 2, 'padding': 1}\"\n"
"ModelCfg(\n",
" in_chans=3\n",
" num_classes=10\n",
" block='BasicBlock'\n",
" conv_layer='ConvBnAct'\n",
" block_sizes=[64, 128, 256, 512]\n",
" layers=[2, 2, 2, 2]\n",
" norm='BatchNorm2d'\n",
" act_fn='Mish'\n",
" expansion=1\n",
" groups=1\n",
" bn_1st=True\n",
" zero_bn=True\n",
" stem_sizes=[64]\n",
" stem_pool=\"MaxPool2d {'kernel_size': 3, 'stride': 2, 'padding': 1}\")\n"
]
}
],
Expand Down Expand Up @@ -767,21 +819,13 @@
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Deprecated. Pass se_module as se argument, se_reduction as arg to se.\n",
"Deprecated. Pass se_module as se argument, se_reduction as arg to se.\n"
]
},
{
"data": {
"text/plain": [
"ModelConstructor\n",
" in_chans: 3, num_classes: 10\n",
" expansion: 1, groups: 1, dw: False, div_groups: None\n",
" act_fn: ReLU, sa: <class 'model_constructor.layers.SimpleSelfAttention'>, se: SEModule\n",
" act_fn: SELU, sa: <class 'model_constructor.layers.SimpleSelfAttention'>, se: SEModule\n",
" stem sizes: [64], stride on 0\n",
" body sizes [64, 128, 256, 512]\n",
" layers: [2, 2, 2, 2]"
Expand Down Expand Up @@ -1383,13 +1427,27 @@
"name": "stdout",
"output_type": "stream",
"text": [
"YaResNet\n",
" in_chans: 3, num_classes: 1000\n",
" expansion: 1, groups: 1, dw: False, div_groups: None\n",
" act_fn: ReLU, sa: False, se: False\n",
" stem sizes: [64], stride on 0\n",
" body sizes [64, 128, 256, 512]\n",
" layers: [2, 2, 2, 2]\n"
"ModelConstructor(\n",
" name='YaResNet'\n",
" in_chans=3\n",
" num_classes=1000\n",
" block='YaBasicBlock'\n",
" conv_layer='ConvBnAct'\n",
" block_sizes=[64, 128, 256, 512]\n",
" layers=[2, 2, 2, 2]\n",
" norm='BatchNorm2d'\n",
" act_fn='ReLU'\n",
" expansion=1\n",
" groups=1\n",
" bn_1st=True\n",
" zero_bn=True\n",
" stem_sizes=[64]\n",
" stem_pool=\"MaxPool2d {'kernel_size': 3, 'stride': 2, 'padding': 1}\"\n",
" init_cnn='init_cnn'\n",
" make_stem='make_stem'\n",
" make_layer='make_layer'\n",
" make_body='make_body'\n",
" make_head='make_head')\n"
]
}
],
Expand Down Expand Up @@ -1482,13 +1540,26 @@
"name": "stdout",
"output_type": "stream",
"text": [
"YaResnet34\n",
" in_chans: 3, num_classes: 1000\n",
" expansion: 1, groups: 1, dw: False, div_groups: None\n",
" act_fn: ReLU, sa: False, se: False\n",
" stem sizes: [64], stride on 0\n",
" body sizes [64, 128, 256, 512]\n",
" layers: [3, 4, 6, 3]\n"
"YaResnet34(\n",
" in_chans=3\n",
" num_classes=1000\n",
" block='YaBasicBlock'\n",
" conv_layer='ConvBnAct'\n",
" block_sizes=[64, 128, 256, 512]\n",
" layers=[3, 4, 6, 3]\n",
" norm='BatchNorm2d'\n",
" act_fn='ReLU'\n",
" expansion=1\n",
" groups=1\n",
" bn_1st=True\n",
" zero_bn=True\n",
" stem_sizes=[64]\n",
" stem_pool=\"MaxPool2d {'kernel_size': 3, 'stride': 2, 'padding': 1}\"\n",
" init_cnn='init_cnn'\n",
" make_stem='xresnet_stem'\n",
" make_layer='make_layer'\n",
" make_body='make_body'\n",
" make_head='make_head')\n"
]
}
],
Expand Down Expand Up @@ -1541,6 +1612,13 @@
"mc = YaResnet50()\n",
"mc"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading