Skip to content

Commit 17ea4a8

Browse files
author
ayasyrev
committed
YaResnet, MXResNet, Mish
1 parent d1543b0 commit 17ea4a8

17 files changed

+3492
-1699
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-
```
28+
```python
2929
from model_constructor.net import *
3030
```
3131

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

36-
```
36+
```python
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-
```
49+
```python
5050
model.c_in
5151
```
5252

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

5858

5959

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

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

6969

7070

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

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

8080

8181

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

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

9191

9292

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

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

102102

103103

104-
```
104+
```python
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-
```
280+
```python
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-
```
287+
```python
288288
model.body
289289
```
290290

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

624624

625625

626-
```
626+
```python
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-
```
647+
```python
648648
mxresnet = Net()
649649
```
650650

651651
Then lets modify stem.
652652

653-
```
653+
```python
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-
```
660+
```python
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-
```
669+
```python
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-
```
680+
```python
681681
mxresnet
682682
```
683683

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

689689

690690

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

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

704704

705705

706-
```
706+
```python
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-
```
741+
```python
742742
mxresnet.block = NewResBlock
743743
```
744744

745745
That all. Let see what we have.
746746

747-
```
747+
```python
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-
```
785+
```python
786786
from model_constructor.constructor import *
787787
```
788788

789789
Default is resnet18.
790790

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

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

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

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

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

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

812-
```
812+
```python
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-
```
820+
```python
821821
from model_constructor.xresnet import *
822822
```
823823

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

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

837837
Stem with 3 conv layers
838838

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

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

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

870870

871871

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

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

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

905905
### Activation function before Normalization
906906

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

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

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

931931
### Change activation function
932932

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

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

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

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

958958

959959

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

0 commit comments

Comments
 (0)