forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol_inception-bn-28-small.R
89 lines (83 loc) · 2.9 KB
/
symbol_inception-bn-28-small.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
library(mxnet)
# Basic Conv + BN + ReLU factory
ConvFactory <- function(data, num_filter, kernel, stride = c(1,1),
pad = c(0, 0), act_type = "relu") {
conv = mx.symbol.Convolution(
data = data, num_filter = num_filter, kernel = kernel, stride = stride, pad =
pad
)
bn = mx.symbol.BatchNorm(data = conv)
act = mx.symbol.Activation(data = bn, act_type = act_type)
return(act)
}
# A Simple Downsampling Factory
DownsampleFactory <- function(data, ch_3x3) {
# conv 3x3
conv = ConvFactory(
data = data, kernel = c(3, 3), stride = c(2, 2), num_filter = ch_3x3, pad =
c(1, 1)
)
# pool
pool = mx.symbol.Pooling(
data = data, kernel = c(3, 3), stride = c(2, 2), pad = c(1, 1), pool_type =
'max'
)
# concat
concat = mx.symbol.Concat(c(conv, pool), num.args = 2)
return(concat)
}
# A Simple module
SimpleFactory <- function(data, ch_1x1, ch_3x3) {
# 1x1
conv1x1 = ConvFactory(
data = data, kernel = c(1, 1), pad = c(0, 0), num_filter = ch_1x1
)
# 3x3
conv3x3 = ConvFactory(
data = data, kernel = c(3, 3), pad = c(1, 1), num_filter = ch_3x3
)
#concat
concat = mx.symbol.Concat(c(conv1x1, conv3x3), num.args = 2)
return(concat)
}
get_symbol <- function(num_classes = 10) {
data = mx.symbol.Variable(name = "data")
conv1 = ConvFactory(
data = data, kernel = c(3,3), pad = c(1,1), num_filter = 96,
act_type = "relu"
)
in3a = SimpleFactory(conv1, 32, 32)
in3b = SimpleFactory(in3a, 32, 48)
in3c = DownsampleFactory(in3b, 80)
in4a = SimpleFactory(in3c, 112, 48)
in4b = SimpleFactory(in4a, 96, 64)
in4c = SimpleFactory(in4b, 80, 80)
in4d = SimpleFactory(in4c, 48, 96)
in4e = DownsampleFactory(in4d, 96)
in5a = SimpleFactory(in4e, 176, 160)
in5b = SimpleFactory(in5a, 176, 160)
pool = mx.symbol.Pooling(
data = in5b, pool_type = "avg", kernel = c(7,7), name = "global_pool"
)
flatten = mx.symbol.Flatten(data = pool, name = "flatten1")
fc = mx.symbol.FullyConnected(data = flatten, num_hidden = num_classes, name =
"fc1")
softmax = mx.symbol.SoftmaxOutput(data = fc, name = "softmax")
return(softmax)
}