Skip to content

Commit 55bbbca

Browse files
author
ayasyrev
committed
small fixes
1 parent 17ea4a8 commit 55bbbca

File tree

12 files changed

+56
-104
lines changed

12 files changed

+56
-104
lines changed

README.md

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ And by creating constructor object, then modify it and then create model.
2525

2626
First import constructor class, then create model constructor oject.
2727

28-
```python
28+
```
2929
from model_constructor.net import *
3030
```
3131

32-
```python
32+
```
3333
model = Net()
3434
```
3535

36-
```python
36+
```
3737
model
3838
```
3939

@@ -46,7 +46,7 @@ model
4646

4747
Now we have model consructor, defoult setting as xresnet18. And we can get model after call it.
4848

49-
```python
49+
```
5050
model.c_in
5151
```
5252

@@ -57,7 +57,7 @@ model.c_in
5757

5858

5959

60-
```python
60+
```
6161
model.c_out
6262
```
6363

@@ -68,7 +68,7 @@ model.c_out
6868

6969

7070

71-
```python
71+
```
7272
model.stem_sizes
7373
```
7474

@@ -79,7 +79,7 @@ model.stem_sizes
7979

8080

8181

82-
```python
82+
```
8383
model.layers
8484
```
8585

@@ -90,7 +90,7 @@ model.layers
9090

9191

9292

93-
```python
93+
```
9494
model.expansion
9595
```
9696

@@ -101,7 +101,7 @@ model.expansion
101101

102102

103103

104-
```python
104+
```
105105
model()
106106
```
107107

@@ -277,14 +277,14 @@ model()
277277
If you want to change model, just change constructor parameters.
278278
Lets create xresnet50.
279279

280-
```python
280+
```
281281
model.expansion = 4
282282
model.layers = [3,4,6,3]
283283
```
284284

285285
Now we can look at model body and if we call constructor - we have pytorch model!
286286

287-
```python
287+
```
288288
model.body
289289
```
290290

@@ -623,7 +623,7 @@ model.body
623623

624624

625625

626-
```python
626+
```
627627
model.block_szs
628628
```
629629

@@ -644,20 +644,20 @@ But now lets create model as mxresnet50 from fastai forums tread https://forums.
644644

645645
Lets create mxresnet constructor.
646646

647-
```python
647+
```
648648
mxresnet = Net()
649649
```
650650

651651
Then lets modify stem.
652652

653-
```python
653+
```
654654
mxresnet.stem_sizes = [3,32,64,64]
655655
```
656656

657657
Now lets change activation function to Mish.
658658
Here is link to forum disscussion https://forums.fast.ai/t/meet-mish-new-activation-function-possible-successor-to-relu
659659

660-
```python
660+
```
661661
class Mish(nn.Module):
662662
def __init__(self):
663663
super().__init__()
@@ -666,7 +666,7 @@ class Mish(nn.Module):
666666
return x *( torch.tanh(F.softplus(x)))
667667
```
668668

669-
```python
669+
```
670670
mxresnet.expansion = 4
671671
mxresnet.layers = [3,4,6,3]
672672
mxresnet.act_fn = Mish()
@@ -677,7 +677,7 @@ Now we have mxresnet50 constructor.
677677
We can inspect some parts of it.
678678
And after call it we got model.
679679

680-
```python
680+
```
681681
mxresnet
682682
```
683683

@@ -688,7 +688,7 @@ mxresnet
688688

689689

690690

691-
```python
691+
```
692692
mxresnet.stem.conv_1
693693
```
694694

@@ -703,7 +703,7 @@ mxresnet.stem.conv_1
703703

704704

705705

706-
```python
706+
```
707707
mxresnet.body.l_0.bl_0
708708
```
709709

@@ -738,13 +738,13 @@ mxresnet.body.l_0.bl_0
738738

739739
Now lets change Resblock. NewResBlock (stiil not own name yet) is in lib from version 0.1.0
740740

741-
```python
741+
```
742742
mxresnet.block = NewResBlock
743743
```
744744

745745
That all. Let see what we have.
746746

747-
```python
747+
```
748748
mxresnet.body.l_1.bl_0
749749
```
750750

@@ -782,46 +782,46 @@ mxresnet.body.l_1.bl_0
782782

783783
Usual way to get model - call constructor with parametrs.
784784

785-
```python
785+
```
786786
from model_constructor.constructor import *
787787
```
788788

789789
Default is resnet18.
790790

791-
```python
791+
```
792792
model = Net()
793793
```
794794

795795
You cant modify model after call constructor, so define model with parameters.
796796
For example, resnet34:
797797

798-
```python
798+
```
799799
resnet34 = Net(block=BasicBlock, blocks=[3, 4, 6, 3])
800800
```
801801

802802
## Predefined Resnet models - 18, 34, 50.
803803

804-
```python
804+
```
805805
from model_constructor.resnet import *
806806
```
807807

808-
```python
808+
```
809809
model = resnet34(num_classes=10)
810810
```
811811

812-
```python
812+
```
813813
model = resnet50(num_classes=10)
814814
```
815815

816816
## Predefined Xresnet from fastai 1.
817817

818818
This ie simplified version from fastai v1. I did refactoring for better understand and experiment with models. For example, it's very simple to change activation funtions, different stems, batchnorm and activation order etc. In v2 much powerfull realisation.
819819

820-
```python
820+
```
821821
from model_constructor.xresnet import *
822822
```
823823

824-
```python
824+
```
825825
model = xresnet50()
826826
```
827827

@@ -836,11 +836,11 @@ Here is some examples:
836836

837837
Stem with 3 conv layers
838838

839-
```python
839+
```
840840
model = Net(stem=partial(Stem, stem_sizes=[32, 32]))
841841
```
842842

843-
```python
843+
```
844844
model.stem
845845
```
846846

@@ -869,11 +869,11 @@ model.stem
869869

870870

871871

872-
```python
872+
```
873873
model = Net(stem_sizes=[32, 64])
874874
```
875875

876-
```python
876+
```
877877
model.stem
878878
```
879879

@@ -904,11 +904,11 @@ model.stem
904904

905905
### Activation function before Normalization
906906

907-
```python
907+
```
908908
model = Net(bn_1st=False)
909909
```
910910

911-
```python
911+
```
912912
model.stem
913913
```
914914

@@ -930,15 +930,15 @@ model.stem
930930

931931
### Change activation function
932932

933-
```python
933+
```
934934
new_act_fn = nn.LeakyReLU(inplace=True)
935935
```
936936

937-
```python
937+
```
938938
model = Net(act_fn=new_act_fn)
939939
```
940940

941-
```python
941+
```
942942
model.stem
943943
```
944944

@@ -957,7 +957,7 @@ model.stem
957957

958958

959959

960-
```python
960+
```
961961
model.body.layer_0.block_0
962962
```
963963

docs/MXResNet.html

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -593,24 +593,6 @@ <h1 id="MXResNet-constructor.">MXResNet constructor.<a class="anchor-link" href=
593593
<div class="cell border-box-sizing code_cell rendered">
594594
<div class="input">
595595

596-
<div class="inner_cell">
597-
<div class="input_area">
598-
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">mxresnet34</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="n">Net</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;MXResnet32&#39;</span><span class="p">,</span> <span class="n">expansion</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">layers</span><span class="o">=</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="o">**</span><span class="n">mxresnet_parameters</span><span class="p">)</span>
599-
<span class="n">mxresnet50</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="n">Net</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;MXResnet50&#39;</span><span class="p">,</span> <span class="n">expansion</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">layers</span><span class="o">=</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="o">**</span><span class="n">mxresnet_parameters</span><span class="p">)</span>
600-
</pre></div>
601-
602-
</div>
603-
</div>
604-
</div>
605-
606-
</div>
607-
{% endraw %}
608-
609-
{% raw %}
610-
611-
<div class="cell border-box-sizing code_cell rendered">
612-
<div class="input">
613-
614596
<div class="inner_cell">
615597
<div class="input_area">
616598
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">model</span> <span class="o">=</span> <span class="n">mxresnet50</span><span class="p">(</span><span class="n">c_out</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>

docs/Net.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ <h1 id="ResBlock">ResBlock<a class="anchor-link" href="#ResBlock"> </a></h1>
102102

103103

104104
<div class="output_markdown rendered_html output_subarea ">
105-
<h2 id="ResBlock" class="doc_header"><code>class</code> <code>ResBlock</code><a href="https://github.com/ayasyrev/model_constructor/tree/master/model_constructor/layers.py#L211" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>ResBlock</code>(<strong><code>ni</code></strong>, <strong><code>nh</code></strong>, <strong><code>expansion</code></strong>=<em><code>1</code></em>, <strong><code>stride</code></strong>=<em><code>1</code></em>, <strong><code>conv_layer</code></strong>=<em><code>'ConvLayer'</code></em>, <strong><code>conv_block</code></strong>=<em><code>'ConvBlockBasic'</code></em>, <strong><code>downsample</code></strong>=<em><code>'DownsampleLayer'</code></em>, <strong><code>act_fn</code></strong>=<em><code>ReLU(inplace=True)</code></em>, <strong><code>act_id</code></strong>=<em><code>False</code></em>, <strong><code>zero_bn</code></strong>=<em><code>True</code></em>, <strong><code>sa</code></strong>=<em><code>False</code></em>, <strong><code>sym</code></strong>=<em><code>False</code></em>, <strong>**<code>kwargs</code></strong>) :: <code>Module</code></p>
105+
<h2 id="ResBlock" class="doc_header"><code>class</code> <code>ResBlock</code><a href="https://github.com/ayasyrev/model_constructor/tree/master/model_constructor/layers.py#L209" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>ResBlock</code>(<strong><code>ni</code></strong>, <strong><code>nh</code></strong>, <strong><code>expansion</code></strong>=<em><code>1</code></em>, <strong><code>stride</code></strong>=<em><code>1</code></em>, <strong><code>conv_layer</code></strong>=<em><code>'ConvLayer'</code></em>, <strong><code>conv_block</code></strong>=<em><code>'ConvBlockBasic'</code></em>, <strong><code>downsample</code></strong>=<em><code>'DownsampleLayer'</code></em>, <strong><code>act_fn</code></strong>=<em><code>ReLU(inplace=True)</code></em>, <strong><code>act_id</code></strong>=<em><code>False</code></em>, <strong><code>zero_bn</code></strong>=<em><code>True</code></em>, <strong><code>sa</code></strong>=<em><code>False</code></em>, <strong><code>sym</code></strong>=<em><code>False</code></em>, <strong>**<code>kwargs</code></strong>) :: <code>Module</code></p>
106106
</blockquote>
107107
<p>Base class for all neural network modules.</p>
108108
<p>Your models should also subclass this class.</p>

docs/YaResNet.html

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -699,24 +699,6 @@ <h2 id="YResNet-constructor">YResNet constructor<a class="anchor-link" href="#YR
699699
<div class="cell border-box-sizing code_cell rendered">
700700
<div class="input">
701701

702-
<div class="inner_cell">
703-
<div class="input_area">
704-
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">yaresnet34</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="n">Net</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;YaResnet34&#39;</span><span class="p">,</span> <span class="n">expansion</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">layers</span><span class="o">=</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="o">**</span><span class="n">yaresnet_parameters</span><span class="p">)</span>
705-
<span class="n">yaresnet50</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="n">Net</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;YaResnet50&#39;</span><span class="p">,</span> <span class="n">expansion</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">layers</span><span class="o">=</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="o">**</span><span class="n">yaresnet_parameters</span><span class="p">)</span>
706-
</pre></div>
707-
708-
</div>
709-
</div>
710-
</div>
711-
712-
</div>
713-
{% endraw %}
714-
715-
{% raw %}
716-
717-
<div class="cell border-box-sizing code_cell rendered">
718-
<div class="input">
719-
720702
<div class="inner_cell">
721703
<div class="input_area">
722704
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">model</span> <span class="o">=</span> <span class="n">yaresnet50</span><span class="p">(</span><span class="n">c_out</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>

model_constructor/_nbdev.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@
4040
"ResBlockTwist": "05_Twist.ipynb",
4141
"YaResBlock": "06_YaResNet.ipynb",
4242
"yaresnet_parameters": "06_YaResNet.ipynb",
43-
"mxresnet_parameters": "07_MXResNet.ipynb"}
43+
"yaresnet34": "06_YaResNet.ipynb",
44+
"yaresnet50": "06_YaResNet.ipynb",
45+
"mxresnet_parameters": "07_MXResNet.ipynb",
46+
"mxresnet34": "07_MXResNet.ipynb",
47+
"mxresnet50": "07_MXResNet.ipynb"}
4448

4549
modules = ["constructor.py",
4650
"layers.py",

model_constructor/mxresnet.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/07_MXResNet.ipynb (unless otherwise specified).
22

3-
__all__ = ['mxresnet_parameters']
3+
__all__ = ['mxresnet_parameters', 'mxresnet34', 'mxresnet50']
44

55
# Cell
66
import torch.nn as nn
@@ -11,4 +11,6 @@
1111
from .net import Net
1212

1313
# Cell
14-
mxresnet_parameters = {'stem_sizes': [3, 32, 64, 64], 'act_fn': Mish()}
14+
mxresnet_parameters = {'stem_sizes': [3, 32, 64, 64], 'act_fn': Mish()}
15+
mxresnet34 = partial(Net, name='MXResnet32', expansion=1, layers=[3, 4, 6, 3], **mxresnet_parameters)
16+
mxresnet50 = partial(Net, name='MXResnet50', expansion=4, layers=[3, 4, 6, 3], **mxresnet_parameters)

model_constructor/net.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def __init__(self, name='Net', expansion=1, c_in=3, c_out=1000,
118118
self.stem_sizes = stem_sizes if stem_sizes else [c_in,32,32,64]
119119
self.stem_pool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
120120
self.stem_bn_end = False
121-
# self.norm = nn.BatchNorm2d
121+
self.norm = nn.BatchNorm2d
122122
self.bn_1st = True
123123
self.zero_bn=True
124124
self.conv_layer = ConvLayer

0 commit comments

Comments
 (0)