README.md 7.14 KB
Newer Older
Bernhard Rumpe's avatar
BR-sy    
Bernhard Rumpe committed
1
<!-- (c) https://github.com/MontiCore/monticore -->
2
3
4
![pipeline](https://git.rwth-aachen.de/monticore/EmbeddedMontiArc/languages/EmbeddedMontiArcDL/badges/master/build.svg)
![coverage](https://git.rwth-aachen.de/monticore/EmbeddedMontiArc/languages/EmbeddedMontiArcDL/badges/master/coverage.svg)

5
# EmbeddedMontiArcDL
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
6
Embeds [CNNArch](https://github.com/EmbeddedMontiArc/CNNArchLang), [CNNTrain](https://github.com/EmbeddedMontiArc/CNNTrainLang) and MontiMath into EmbeddedMontiArc.
7

8
9
10
11
## Examples
In the following, we list common CNN architectures that are modeled inside an EMA component.
```
component LeNet{
12
13
14
15
16
17
18
19
    ports in Z(0:255)^{1,28,28} data,
          out Q(0:1)^{10} predictions;

    implementation CNN {
        data ->
        Convolution(kernel=(5,5), channels=20) ->
        Tanh() ->
        Pooling(pool_type="max", kernel=(2,2), stride=(2,2)) ->
20
        Convolution(kernel=(5,5), channels=50) ->
21
22
        Tanh() ->
        Pooling(pool_type="max", kernel=(2,2), stride=(2,2)) ->
23
        FullyConnected(units=500) ->
24
25
26
27
28
29
30
31
32
        Tanh() ->
        Dropout() ->
        FullyConnected(units=10) ->
        Softmax() ->
        predictions
    }
}
```
```
33
34
component VGG16<Z(1:oo) channels=3, Z(1:oo) height=224, Z(1:oo) width=224, Z(2:oo) classes=1000>{
    ports in Z(0:255)^{channels, height, width} image,
35
         out Q(0:1)^{classes} predictions;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

    implementation CNN {
        def conv(filter, channels){
            Convolution(kernel=(filter,filter), channels=channels) ->
            Relu()
        }
        def fc(){
            FullyConnected(units=4096) ->
            Relu() ->
            Dropout(p=0.5)
        }
        image ->
        conv(filter=3, channels=64, ->=2) ->
        Pooling(pool_type="max", kernel=(2,2), stride=(2,2)) ->
        conv(filter=3, channels=128, ->=2) ->
        Pooling(pool_type="max", kernel=(2,2), stride=(2,2)) ->
        conv(filter=3, channels=256, ->=3) ->
        Pooling(pool_type="max", kernel=(2,2), stride=(2,2)) ->
        conv(filter=3, channels=512, ->=3) ->
        Pooling(pool_type="max", kernel=(2,2), stride=(2,2)) ->
        conv(filter=3, channels=512, ->=3) ->
        Pooling(pool_type="max", kernel=(2,2), stride=(2,2)) ->
        fc() ->
        fc() ->
60
        FullyConnected(units=classes) ->
61
62
63
64
65
66
        Softmax() ->
        predictions
    }
}
```
```
67
68
component ResNet34<Z(1:oo) channels=3, Z(1:oo) height=224, Z(1:oo) width=224, Z(2:oo) classes=1000>{
    ports in Z(0:255)^{channels, height, width} image,
69
         out Q(0:1)^{classes} predictions;
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

    implementation CNN {
        def conv(filter, channels, stride=1, act=true){
            Convolution(kernel=(filter,filter), channels=channels, stride=(stride,stride)) ->
            BatchNorm() ->
            Relu(?=act)
        }
        def skip(channels, stride){
            Convolution(kernel=(1,1), channels=channels, stride=(stride,stride)) ->
            BatchNorm()
        }
        def resLayer(channels, stride=1){
            (
                conv(filter=3, channels=channels, stride=stride) ->
                conv(filter=3, channels=channels, act=false)
            |
                skip(channels=channels, stride=stride, ? = (stride != 1))
            ) ->
            Add() ->
            Relu()
        }
    
        image ->
        conv(filter=7, channels=64, stride=2) ->
        Pooling(pool_type="max", kernel=(3,3), stride=(2,2)) ->
        resLayer(channels=64, ->=3) ->
        resLayer(channels=128, stride=2) ->
        resLayer(channels=128, ->=3) ->
        resLayer(channels=256, stride=2) ->
        resLayer(channels=256, ->=5) ->
        resLayer(channels=512, stride=2) ->
        resLayer(channels=512, ->=2) ->
        GlobalPooling(pool_type="avg") ->
103
        FullyConnected(units=classes) ->
104
105
106
107
108
109
        Softmax() ->
        predictions
    }
}
```
```
110
111
component Alexnet<Z(1:oo) channels=3, Z(1:oo) height=224, Z(1:oo) width=224, Z(2:oo) classes=1000>{
    ports in Z(0:255)^{channels, height, width} image,
112
          out Q(0:1)^{classes} predictions;
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

    implementation CNN {
        def split1(i){
            [i] ->
            Convolution(kernel=(5,5), channels=128) ->
            Lrn(nsize=5, alpha=0.0001, beta=0.75) ->
            Pooling(pool_type="max", kernel=(3,3), stride=(2,2), padding="no_loss") ->
            Relu()
        }
        def split2(i){
            [i] ->
            Convolution(kernel=(3,3), channels=192) ->
            Relu() ->
            Convolution(kernel=(3,3), channels=128) ->
            Pooling(pool_type="max", kernel=(3,3), stride=(2,2), padding="no_loss") ->
            Relu()
        }
        def fc(){
            FullyConnected(units=4096) ->
            Relu() ->
            Dropout()
        }
        image ->
        Convolution(kernel=(11,11), channels=96, stride=(4,4), padding="no_loss") ->
        Lrn(nsize=5, alpha=0.0001, beta=0.75) ->
        Pooling(pool_type="max", kernel=(3,3), stride=(2,2), padding="no_loss") ->
        Relu() ->
        Split(n=2) ->
        split1(i=[0|1]) ->
        Concatenate() ->
        Convolution(kernel=(3,3), channels=384) ->
        Relu() ->
        Split(n=2) ->
        split2(i=[0|1]) ->
        Concatenate() ->
        fc(->=2) ->
149
        FullyConnected(units=classes) ->
150
151
152
153
154
155
        Softmax() ->
        predictions
    }
}
```
```
156
157
component ResNeXt50<Z(1:oo) channels=3, Z(1:oo) height=224, Z(1:oo) width=224, Z(2:oo) classes=1000>{
    ports in Z(0:255)^{channels, height, width} image,
158
         out Q(0:1)^{classes} predictions;
159
160

    implementation CNN {
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
161
162
        def conv(kernel, channels, stride=1, act=true){
            Convolution(kernel=(kernel,kernel), channels=channels, stride=(stride,stride)) ->
163
164
165
166
            BatchNorm() ->
            Relu(?=act)
        }
        def resGroup(innerChannels, outChannels, stride=1){
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
167
168
169
            conv(kernel=1, channels=innerChannels) ->
            conv(kernel=3, channels=innerChannels, stride=stride) ->
            conv(kernel=1, channels=outChannels, act=false)
170
        }
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
171
        def resLayer(innerChannels, outChannels, stride=1, addSkipConv=false){
172
173
174
175
176
177
178
            (
                resGroup(innerChannels=innerChannels,
                         outChannels=outChannels,
                         stride=stride,
                         | = 32) ->
                Add()
            |
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
179
                conv(kernel=1, channels=outChannels, stride=stride, act=false, ? = addSkipConv)
180
181
182
183
            ) ->
            Add() ->
            Relu()
        }
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
184
185
186

        data ->
        conv(kernel=7, channels=64, stride=2) ->
187
        Pooling(pool_type="max", kernel=(3,3), stride=(2,2)) ->
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
188
189
190
        resLayer(innerChannels=4, outChannels=256, addSkipConv=true) ->
        resLayer(innerChannels=4, outChannels=256, -> = 2) ->
        resLayer(innerChannels=8, outChannels=512, stride=2, addSkipConv=true) ->
191
        resLayer(innerChannels=8, outChannels=512, -> = 3) ->
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
192
        resLayer(innerChannels=16, outChannels=1024, stride=2, addSkipConv=true) ->
193
        resLayer(innerChannels=16, outChannels=1024, -> = 5) ->
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
194
        resLayer(innerChannels=32, outChannels=2048, stride=2, addSkipConv=true) ->
195
196
        resLayer(innerChannels=32, outChannels=2048, -> = 2) ->
        GlobalPooling(pool_type="avg") ->
197
        FullyConnected(units=classes) ->
198
199
200
201
        Softmax() ->
        predictions
    }
}
Thomas Michael Timmermanns's avatar
Thomas Michael Timmermanns committed
202
```