22> Constructor to create pytorch model.
33
44
5- _
5+ # News
6+
7+ 2020-05-10 add Twist module, tutorial will be soon.
68
79## Install
810
@@ -23,15 +25,15 @@ And by creating constructor object, then modify it and then create model.
2325
2426First import constructor class, then create model constructor oject.
2527
26- ```
28+ ``` python
2729from model_constructor.net import *
2830```
2931
30- ```
32+ ``` python
3133model = Net()
3234```
3335
34- ```
36+ ``` python
3537model
3638```
3739
4446
4547Now we have model consructor, defoult setting as xresnet18. And we can get model after call it.
4648
47- ```
49+ ``` python
4850model.c_in
4951```
5052
@@ -55,7 +57,7 @@ model.c_in
5557
5658
5759
58- ```
60+ ``` python
5961model.c_out
6062```
6163
@@ -66,7 +68,7 @@ model.c_out
6668
6769
6870
69- ```
71+ ``` python
7072model.stem_sizes
7173```
7274
@@ -77,7 +79,7 @@ model.stem_sizes
7779
7880
7981
80- ```
82+ ``` python
8183model.layers
8284```
8385
@@ -88,7 +90,7 @@ model.layers
8890
8991
9092
91- ```
93+ ``` python
9294model.expansion
9395```
9496
@@ -99,7 +101,7 @@ model.expansion
99101
100102
101103
102- ```
104+ ``` python
103105model()
104106```
105107
@@ -275,14 +277,14 @@ model()
275277If you want to change model, just change constructor parameters.
276278Lets create xresnet50.
277279
278- ```
280+ ``` python
279281model.expansion = 4
280282model.layers = [3 ,4 ,6 ,3 ]
281283```
282284
283285Now we can look at model body and if we call constructor - we have pytorch model!
284286
285- ```
287+ ``` python
286288model.body
287289```
288290
@@ -621,7 +623,7 @@ model.body
621623
622624
623625
624- ```
626+ ``` python
625627model.block_szs
626628```
627629
@@ -642,20 +644,20 @@ But now lets create model as mxresnet50 from fastai forums tread https://forums.
642644
643645Lets create mxresnet constructor.
644646
645- ```
647+ ``` python
646648mxresnet = Net()
647649```
648650
649651Then lets modify stem.
650652
651- ```
653+ ``` python
652654mxresnet.stem_sizes = [3 ,32 ,64 ,64 ]
653655```
654656
655657Now lets change activation function to Mish.
656658Here is link to forum disscussion https://forums.fast.ai/t/meet-mish-new-activation-function-possible-successor-to-relu
657659
658- ```
660+ ``` python
659661class Mish (nn .Module ):
660662 def __init__ (self ):
661663 super ().__init__ ()
@@ -664,7 +666,7 @@ class Mish(nn.Module):
664666 return x * ( torch.tanh(F.softplus(x)))
665667```
666668
667- ```
669+ ``` python
668670mxresnet.expansion = 4
669671mxresnet.layers = [3 ,4 ,6 ,3 ]
670672mxresnet.act_fn = Mish()
@@ -675,7 +677,7 @@ Now we have mxresnet50 constructor.
675677We can inspect some parts of it.
676678And after call it we got model.
677679
678- ```
680+ ``` python
679681mxresnet
680682```
681683
@@ -686,7 +688,7 @@ mxresnet
686688
687689
688690
689- ```
691+ ``` python
690692mxresnet.stem.conv_1
691693```
692694
@@ -701,7 +703,7 @@ mxresnet.stem.conv_1
701703
702704
703705
704- ```
706+ ``` python
705707mxresnet.body.l_0.bl_0
706708```
707709
@@ -736,13 +738,13 @@ mxresnet.body.l_0.bl_0
736738
737739Now lets change Resblock. NewResBlock (stiil not own name yet) is in lib from version 0.1.0
738740
739- ```
741+ ``` python
740742mxresnet.block = NewResBlock
741743```
742744
743745That all. Let see what we have.
744746
745- ```
747+ ``` python
746748mxresnet.body.l_1.bl_0
747749```
748750
@@ -780,46 +782,46 @@ mxresnet.body.l_1.bl_0
780782
781783Usual way to get model - call constructor with parametrs.
782784
783- ```
785+ ``` python
784786from model_constructor.constructor import *
785787```
786788
787789Default is resnet18.
788790
789- ```
791+ ``` python
790792model = Net()
791793```
792794
793795You cant modify model after call constructor, so define model with parameters.
794796For example, resnet34:
795797
796- ```
798+ ``` python
797799resnet34 = Net(block = BasicBlock, blocks = [3 , 4 , 6 , 3 ])
798800```
799801
800802## Predefined Resnet models - 18, 34, 50.
801803
802- ```
804+ ``` python
803805from model_constructor.resnet import *
804806```
805807
806- ```
808+ ``` python
807809model = resnet34(num_classes = 10 )
808810```
809811
810- ```
812+ ``` python
811813model = resnet50(num_classes = 10 )
812814```
813815
814816## Predefined Xresnet from fastai 1.
815817
816818This 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.
817819
818- ```
820+ ``` python
819821from model_constructor.xresnet import *
820822```
821823
822- ```
824+ ``` python
823825model = xresnet50()
824826```
825827
@@ -834,11 +836,11 @@ Here is some examples:
834836
835837Stem with 3 conv layers
836838
837- ```
839+ ``` python
838840model = Net(stem = partial(Stem, stem_sizes = [32 , 32 ]))
839841```
840842
841- ```
843+ ``` python
842844model.stem
843845```
844846
@@ -867,11 +869,11 @@ model.stem
867869
868870
869871
870- ```
872+ ``` python
871873model = Net(stem_sizes = [32 , 64 ])
872874```
873875
874- ```
876+ ``` python
875877model.stem
876878```
877879
@@ -902,11 +904,11 @@ model.stem
902904
903905### Activation function before Normalization
904906
905- ```
907+ ``` python
906908model = Net(bn_1st = False )
907909```
908910
909- ```
911+ ``` python
910912model.stem
911913```
912914
@@ -928,15 +930,15 @@ model.stem
928930
929931### Change activation function
930932
931- ```
933+ ``` python
932934new_act_fn = nn.LeakyReLU(inplace = True )
933935```
934936
935- ```
937+ ``` python
936938model = Net(act_fn = new_act_fn)
937939```
938940
939- ```
941+ ``` python
940942model.stem
941943```
942944
@@ -955,7 +957,7 @@ model.stem
955957
956958
957959
958- ```
960+ ``` python
959961model.body.layer_0.block_0
960962```
961963
0 commit comments