@@ -25,15 +25,15 @@ And by creating constructor object, then modify it and then create model.
2525
2626First import constructor class, then create model constructor oject.
2727
28- ```
28+ ``` python
2929from model_constructor.net import *
3030```
3131
32- ```
32+ ``` python
3333model = Net()
3434```
3535
36- ```
36+ ``` python
3737model
3838```
3939
4646
4747Now we have model consructor, defoult setting as xresnet18. And we can get model after call it.
4848
49- ```
49+ ``` python
5050model.c_in
5151```
5252
@@ -57,7 +57,7 @@ model.c_in
5757
5858
5959
60- ```
60+ ``` python
6161model.c_out
6262```
6363
@@ -68,7 +68,7 @@ model.c_out
6868
6969
7070
71- ```
71+ ``` python
7272model.stem_sizes
7373```
7474
@@ -79,7 +79,7 @@ model.stem_sizes
7979
8080
8181
82- ```
82+ ``` python
8383model.layers
8484```
8585
@@ -90,7 +90,7 @@ model.layers
9090
9191
9292
93- ```
93+ ``` python
9494model.expansion
9595```
9696
@@ -101,7 +101,7 @@ model.expansion
101101
102102
103103
104- ```
104+ ``` python
105105model()
106106```
107107
@@ -277,14 +277,14 @@ model()
277277If you want to change model, just change constructor parameters.
278278Lets create xresnet50.
279279
280- ```
280+ ``` python
281281model.expansion = 4
282282model.layers = [3 ,4 ,6 ,3 ]
283283```
284284
285285Now we can look at model body and if we call constructor - we have pytorch model!
286286
287- ```
287+ ``` python
288288model.body
289289```
290290
@@ -623,7 +623,7 @@ model.body
623623
624624
625625
626- ```
626+ ``` python
627627model.block_szs
628628```
629629
@@ -644,20 +644,20 @@ But now lets create model as mxresnet50 from fastai forums tread https://forums.
644644
645645Lets create mxresnet constructor.
646646
647- ```
647+ ``` python
648648mxresnet = Net()
649649```
650650
651651Then lets modify stem.
652652
653- ```
653+ ``` python
654654mxresnet.stem_sizes = [3 ,32 ,64 ,64 ]
655655```
656656
657657Now lets change activation function to Mish.
658658Here is link to forum disscussion https://forums.fast.ai/t/meet-mish-new-activation-function-possible-successor-to-relu
659659
660- ```
660+ ``` python
661661class 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- ```
669+ ``` python
670670mxresnet.expansion = 4
671671mxresnet.layers = [3 ,4 ,6 ,3 ]
672672mxresnet.act_fn = Mish()
@@ -677,7 +677,7 @@ Now we have mxresnet50 constructor.
677677We can inspect some parts of it.
678678And after call it we got model.
679679
680- ```
680+ ``` python
681681mxresnet
682682```
683683
@@ -688,7 +688,7 @@ mxresnet
688688
689689
690690
691- ```
691+ ``` python
692692mxresnet.stem.conv_1
693693```
694694
@@ -703,7 +703,7 @@ mxresnet.stem.conv_1
703703
704704
705705
706- ```
706+ ``` python
707707mxresnet.body.l_0.bl_0
708708```
709709
@@ -738,13 +738,13 @@ mxresnet.body.l_0.bl_0
738738
739739Now lets change Resblock. NewResBlock (stiil not own name yet) is in lib from version 0.1.0
740740
741- ```
741+ ``` python
742742mxresnet.block = NewResBlock
743743```
744744
745745That all. Let see what we have.
746746
747- ```
747+ ``` python
748748mxresnet.body.l_1.bl_0
749749```
750750
@@ -782,46 +782,46 @@ mxresnet.body.l_1.bl_0
782782
783783Usual way to get model - call constructor with parametrs.
784784
785- ```
785+ ``` python
786786from model_constructor.constructor import *
787787```
788788
789789Default is resnet18.
790790
791- ```
791+ ``` python
792792model = Net()
793793```
794794
795795You cant modify model after call constructor, so define model with parameters.
796796For example, resnet34:
797797
798- ```
798+ ``` python
799799resnet34 = Net(block = BasicBlock, blocks = [3 , 4 , 6 , 3 ])
800800```
801801
802802## Predefined Resnet models - 18, 34, 50.
803803
804- ```
804+ ``` python
805805from model_constructor.resnet import *
806806```
807807
808- ```
808+ ``` python
809809model = resnet34(num_classes = 10 )
810810```
811811
812- ```
812+ ``` python
813813model = resnet50(num_classes = 10 )
814814```
815815
816816## Predefined Xresnet from fastai 1.
817817
818818This 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- ```
820+ ``` python
821821from model_constructor.xresnet import *
822822```
823823
824- ```
824+ ``` python
825825model = xresnet50()
826826```
827827
@@ -836,11 +836,11 @@ Here is some examples:
836836
837837Stem with 3 conv layers
838838
839- ```
839+ ``` python
840840model = Net(stem = partial(Stem, stem_sizes = [32 , 32 ]))
841841```
842842
843- ```
843+ ``` python
844844model.stem
845845```
846846
@@ -869,11 +869,11 @@ model.stem
869869
870870
871871
872- ```
872+ ``` python
873873model = Net(stem_sizes = [32 , 64 ])
874874```
875875
876- ```
876+ ``` python
877877model.stem
878878```
879879
@@ -904,11 +904,11 @@ model.stem
904904
905905### Activation function before Normalization
906906
907- ```
907+ ``` python
908908model = Net(bn_1st = False )
909909```
910910
911- ```
911+ ``` python
912912model.stem
913913```
914914
@@ -930,15 +930,15 @@ model.stem
930930
931931### Change activation function
932932
933- ```
933+ ``` python
934934new_act_fn = nn.LeakyReLU(inplace = True )
935935```
936936
937- ```
937+ ``` python
938938model = Net(act_fn = new_act_fn)
939939```
940940
941- ```
941+ ``` python
942942model.stem
943943```
944944
@@ -957,7 +957,7 @@ model.stem
957957
958958
959959
960- ```
960+ ``` python
961961model.body.layer_0.block_0
962962```
963963
0 commit comments