Skip to content

Commit b696ded

Browse files
author
ayasyrev
committed
add Twist module
1 parent 84d7673 commit b696ded

File tree

13 files changed

+2378
-614
lines changed

13 files changed

+2378
-614
lines changed

README.md

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
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

2426
First import constructor class, then create model constructor oject.
2527

26-
```
28+
```python
2729
from model_constructor.net import *
2830
```
2931

30-
```
32+
```python
3133
model = Net()
3234
```
3335

34-
```
36+
```python
3537
model
3638
```
3739

@@ -44,7 +46,7 @@ model
4446

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

47-
```
49+
```python
4850
model.c_in
4951
```
5052

@@ -55,7 +57,7 @@ model.c_in
5557

5658

5759

58-
```
60+
```python
5961
model.c_out
6062
```
6163

@@ -66,7 +68,7 @@ model.c_out
6668

6769

6870

69-
```
71+
```python
7072
model.stem_sizes
7173
```
7274

@@ -77,7 +79,7 @@ model.stem_sizes
7779

7880

7981

80-
```
82+
```python
8183
model.layers
8284
```
8385

@@ -88,7 +90,7 @@ model.layers
8890

8991

9092

91-
```
93+
```python
9294
model.expansion
9395
```
9496

@@ -99,7 +101,7 @@ model.expansion
99101

100102

101103

102-
```
104+
```python
103105
model()
104106
```
105107

@@ -275,14 +277,14 @@ model()
275277
If you want to change model, just change constructor parameters.
276278
Lets create xresnet50.
277279

278-
```
280+
```python
279281
model.expansion = 4
280282
model.layers = [3,4,6,3]
281283
```
282284

283285
Now we can look at model body and if we call constructor - we have pytorch model!
284286

285-
```
287+
```python
286288
model.body
287289
```
288290

@@ -621,7 +623,7 @@ model.body
621623

622624

623625

624-
```
626+
```python
625627
model.block_szs
626628
```
627629

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

643645
Lets create mxresnet constructor.
644646

645-
```
647+
```python
646648
mxresnet = Net()
647649
```
648650

649651
Then lets modify stem.
650652

651-
```
653+
```python
652654
mxresnet.stem_sizes = [3,32,64,64]
653655
```
654656

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

658-
```
660+
```python
659661
class 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
668670
mxresnet.expansion = 4
669671
mxresnet.layers = [3,4,6,3]
670672
mxresnet.act_fn = Mish()
@@ -675,7 +677,7 @@ Now we have mxresnet50 constructor.
675677
We can inspect some parts of it.
676678
And after call it we got model.
677679

678-
```
680+
```python
679681
mxresnet
680682
```
681683

@@ -686,7 +688,7 @@ mxresnet
686688

687689

688690

689-
```
691+
```python
690692
mxresnet.stem.conv_1
691693
```
692694

@@ -701,7 +703,7 @@ mxresnet.stem.conv_1
701703

702704

703705

704-
```
706+
```python
705707
mxresnet.body.l_0.bl_0
706708
```
707709

@@ -736,13 +738,13 @@ mxresnet.body.l_0.bl_0
736738

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

739-
```
741+
```python
740742
mxresnet.block = NewResBlock
741743
```
742744

743745
That all. Let see what we have.
744746

745-
```
747+
```python
746748
mxresnet.body.l_1.bl_0
747749
```
748750

@@ -780,46 +782,46 @@ mxresnet.body.l_1.bl_0
780782

781783
Usual way to get model - call constructor with parametrs.
782784

783-
```
785+
```python
784786
from model_constructor.constructor import *
785787
```
786788

787789
Default is resnet18.
788790

789-
```
791+
```python
790792
model = Net()
791793
```
792794

793795
You cant modify model after call constructor, so define model with parameters.
794796
For example, resnet34:
795797

796-
```
798+
```python
797799
resnet34 = Net(block=BasicBlock, blocks=[3, 4, 6, 3])
798800
```
799801

800802
## Predefined Resnet models - 18, 34, 50.
801803

802-
```
804+
```python
803805
from model_constructor.resnet import *
804806
```
805807

806-
```
808+
```python
807809
model = resnet34(num_classes=10)
808810
```
809811

810-
```
812+
```python
811813
model = resnet50(num_classes=10)
812814
```
813815

814816
## Predefined Xresnet from fastai 1.
815817

816818
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.
817819

818-
```
820+
```python
819821
from model_constructor.xresnet import *
820822
```
821823

822-
```
824+
```python
823825
model = xresnet50()
824826
```
825827

@@ -834,11 +836,11 @@ Here is some examples:
834836

835837
Stem with 3 conv layers
836838

837-
```
839+
```python
838840
model = Net(stem=partial(Stem, stem_sizes=[32, 32]))
839841
```
840842

841-
```
843+
```python
842844
model.stem
843845
```
844846

@@ -867,11 +869,11 @@ model.stem
867869

868870

869871

870-
```
872+
```python
871873
model = Net(stem_sizes=[32, 64])
872874
```
873875

874-
```
876+
```python
875877
model.stem
876878
```
877879

@@ -902,11 +904,11 @@ model.stem
902904

903905
### Activation function before Normalization
904906

905-
```
907+
```python
906908
model = Net(bn_1st=False)
907909
```
908910

909-
```
911+
```python
910912
model.stem
911913
```
912914

@@ -928,15 +930,15 @@ model.stem
928930

929931
### Change activation function
930932

931-
```
933+
```python
932934
new_act_fn = nn.LeakyReLU(inplace=True)
933935
```
934936

935-
```
937+
```python
936938
model = Net(act_fn=new_act_fn)
937939
```
938940

939-
```
941+
```python
940942
model.stem
941943
```
942944

@@ -955,7 +957,7 @@ model.stem
955957

956958

957959

958-
```
960+
```python
959961
model.body.layer_0.block_0
960962
```
961963

0 commit comments

Comments
 (0)