Skip to content

Commit

Permalink
network symbol in R (apache#3044)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiang Kou (KK) authored Aug 17, 2016
1 parent 08864e0 commit 83dc0b4
Show file tree
Hide file tree
Showing 9 changed files with 543 additions and 0 deletions.
36 changes: 36 additions & 0 deletions example/image-classification/symbol_alexnet.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
library(mxnet)

get_symbol <- function(num_classes = 1000) {
input_data <- mx.symbol.Variable(name = "data")
# stage 1
conv1 <- mx.symbol.Convolution(data = input_data, kernel = c(11, 11), stride = c(4, 4), num_filter = 96)
relu1 <- mx.symbol.Activation(data = conv1, act_type = "relu")
pool1 <- mx.symbol.Pooling(data = relu1, pool_type = "max", kernel = c(3, 3), stride = c(2, 2))
lrn1 <- mx.symbol.LRN(data = pool1, alpha = 0.0001, beta = 0.75, knorm = 1, nsize = 5)
# stage 2
conv2 <- mx.symbol.Convolution(data = lrn1, kernel = c(5, 5), pad = c(2, 2), num_filter = 256)
relu2 <- mx.symbol.Activation(data = conv2, act_type = "relu")
pool2 <- mx.symbol.Pooling(data = relu2, kernel = c(3, 3), stride = c(2, 2), pool_type = "max")
lrn2 <- mx.symbol.LRN(data = pool2, alpha = 0.0001, beta = 0.75, knorm = 1, nsize = 5)
# stage 3
conv3 <- mx.symbol.Convolution(data = lrn2, kernel = c(3, 3), pad = c(1, 1), num_filter = 384)
relu3 <- mx.symbol.Activation(data = conv3, act_type = "relu")
conv4 <- mx.symbol.Convolution(data = relu3, kernel = c(3, 3), pad = c(1, 1), num_filter = 384)
relu4 <- mx.symbol.Activation(data = conv4, act_type = "relu")
conv5 <- mx.symbol.Convolution(data = relu4, kernel = c(3, 3), pad = c(1, 1), num_filter = 256)
relu5 <- mx.symbol.Activation(data = conv5, act_type = "relu")
pool3 <- mx.symbol.Pooling(data = relu5, kernel = c(3, 3), stride = c(2, 2), pool_type = "max")
# stage 4
flatten <- mx.symbol.Flatten(data = pool3)
fc1 <- mx.symbol.FullyConnected(data = flatten, num_hidden = 4096)
relu6 <- mx.symbol.Activation(data = fc1, act_type = "relu")
dropout1 <- mx.symbol.Dropout(data = relu6, p = 0.5)
# stage 5
fc2 <- mx.symbol.FullyConnected(data = dropout1, num_hidden = 4096)
relu7 <- mx.symbol.Activation(data = fc2, act_type = "relu")
dropout2 <- mx.symbol.Dropout(data = relu7, p = 0.5)
# stage 6
fc3 <- mx.symbol.FullyConnected(data = dropout2, num_hidden = num_classes)
softmax <- mx.symbol.SoftmaxOutput(data = fc3, name = 'softmax')
return(softmax)
}
67 changes: 67 additions & 0 deletions example/image-classification/symbol_googlenet.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
library(mxnet)

ConvFactory <- function(data, num_filter, kernel, stride = c(1, 1), pad = c(0, 0),
name = '', suffix = '') {
conv <- mx.symbol.Convolution(data = data, num_filter = num_filter, kernel = kernel, stride = stride,
pad = pad, name = paste('conv_', name, suffix, sep = ""))
act <- mx.symbol.Activation(data = conv, act_type = 'relu', name = paste('relu_', name, suffix, sep = ''))
return(act)
}

InceptionFactory <- function(data, num_1x1, num_3x3red, num_3x3,
num_d5x5red, num_d5x5, pool, proj, name) {
# 1x1
c1x1 <- ConvFactory(data = data, num_filter = num_1x1, kernel = c(1, 1),
name = paste(name, '_1x1', sep = ''))
# 3x3 reduce + 3x3
c3x3r = ConvFactory(data = data, num_filter = num_3x3red, kernel = c(1, 1),
name = paste(name, '_3x3', sep = ''), suffix = '_reduce')
c3x3 = ConvFactory(data = c3x3r, num_filter = num_3x3, kernel = c(3, 3),
pad = c(1, 1), name = paste(name, '_3x3', sep = ''))
# double 3x3 reduce + double 3x3
cd5x5r = ConvFactory(data = data, num_filter = num_d5x5red, kernel = c(1, 1),
name = paste(name, '_5x5', sep = ''), suffix = '_reduce')
cd5x5 = ConvFactory(data = cd5x5r, num_filter = num_d5x5, kernel = c(5, 5), pad = c(2, 2),
name = paste(name, '_5x5', sep = ''))
# pool + proj
pooling = mx.symbol.Pooling(data = data, kernel = c(3, 3), stride = c(1, 1),
pad = c(1, 1), pool_type = pool,
name = paste(pool, '_pool_', name, '_pool', sep = ''))

cproj = ConvFactory(data = pooling, num_filter = proj, kernel = c(1, 1),
name = paste(name, '_proj', sep = ''))
# concat
concat_lst <- list()
concat_lst <- c(c1x1, c3x3, cd5x5, cproj)
concat_lst$num.args = 4
concat_lst$name = paste('ch_concat_', name, '_chconcat', sep = '')
concat = mxnet:::mx.varg.symbol.Concat(concat_lst)
return(concat)
}


get_symbol <- function(num_classes = 1000) {
data <- mx.symbol.Variable("data")
conv1 <- ConvFactory(data, 64, kernel = c(7, 7), stride = c(2, 2), pad = c(3, 3), name = "conv1")
pool1 <- mx.symbol.Pooling(conv1, kernel = c(3, 3), stride = c(2, 2), pool_type = "max")
conv2 <- ConvFactory(pool1, 64, kernel = c(1, 1), stride = c(1, 1), name = "conv2")
conv3 <- ConvFactory(conv2, 192, kernel = c(3, 3), stride = c(1, 1), pad = c(1, 1), name = "conv3")
pool3 <- mx.symbol.Pooling(conv3, kernel = c(3, 3), stride = c(2, 2), pool_type = "max")

in3a <- InceptionFactory(pool3, 64, 96, 128, 16, 32, "max", 32, name = "in3a")
in3b <- InceptionFactory(in3a, 128, 128, 192, 32, 96, "max", 64, name = "in3b")
pool4 <- mx.symbol.Pooling(in3b, kernel = c(3, 3), stride = c(2, 2), pool_type = "max")
in4a <- InceptionFactory(pool4, 192, 96, 208, 16, 48, "max", 64, name = "in4a")
in4b <- InceptionFactory(in4a, 160, 112, 224, 24, 64, "max", 64, name = "in4b")
in4c <- InceptionFactory(in4b, 128, 128, 256, 24, 64, "max", 64, name = "in4c")
in4d <- InceptionFactory(in4c, 112, 144, 288, 32, 64, "max", 64, name = "in4d")
in4e <- InceptionFactory(in4d, 256, 160, 320, 32, 128, "max", 128, name = "in4e")
pool5 <- mx.symbol.Pooling(in4e, kernel = c(3, 3), stride = c(2, 2), pool_type = "max")
in5a <- InceptionFactory(pool5, 256, 160, 320, 32, 128, "max", 128, name = "in5a")
in5b <- InceptionFactory(in5a, 384, 192, 384, 48, 128, "max", 128, name = "in5b")
pool6 <- mx.symbol.Pooling(in5b, kernel = c(7, 7), stride = c(1, 1), pool_type = "avg" )
flatten <- mx.symbol.Flatten(data = pool6, name = 'flatten0')
fc1 <- mx.symbol.FullyConnected(data = flatten, num_hidden = num_classes)
softmax <- mx.symbol.SoftmaxOutput(data = fc1, name = 'softmax')
return(softmax)
}
113 changes: 113 additions & 0 deletions example/image-classification/symbol_inception-bn.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
library(mxnet)

ConvFactory <- function(data, num_filter, kernel, stride = c(1, 1),
pad = c(0, 0), name = '', suffix = '') {
conv <- mx.symbol.Convolution(data = data, num_filter = num_filter,
kernel = kernel, stride = stride, pad = pad,
name = paste('conv_', name, suffix, sep = ''))

bn <- mx.symbol.BatchNorm(data = conv, name = paste('bn_', name, suffix, sep = ''))
act <- mx.symbol.Activation(data = bn, act_type = 'relu', name = paste('relu_', name, suffix, sep = ''))
return(act)
}

InceptionFactoryA <- function(data, num_1x1, num_3x3red, num_3x3, num_d3x3red,
num_d3x3, pool, proj, name) {
# 1x1
c1x1 <- ConvFactory(data = data, num_filter = num_1x1, kernel = c(1, 1), name = paste(name, '_1x1', sep = '')
)
# 3x3 reduce + 3x3
c3x3r <- ConvFactory(data = data, num_filter = num_3x3red, kernel = c(1, 1),
name = paste(name, '_3x3', sep = ''), suffix = '_reduce')

c3x3 <- ConvFactory(data = c3x3r, num_filter = num_3x3, kernel = c(3, 3),
pad = c(1, 1), name = paste(name, '_3x3', sep = ''))
# double 3x3 reduce + double 3x3
cd3x3r <- ConvFactory(data = data, num_filter = num_d3x3red, kernel = c(1, 1),
name = paste(name, '_double_3x3', sep = ''), suffix = '_reduce')

cd3x3 <- ConvFactory(data = cd3x3r, num_filter = num_d3x3, kernel = c(3, 3),
pad = c(1, 1), name = paste(name, '_double_3x3_0', sep = ''))

cd3x3 <- ConvFactory(data = cd3x3, num_filter = num_d3x3, kernel = c(3, 3),
pad = c(1, 1), name = paste(name, '_double_3x3_1', sep = ''))
# pool + proj
pooling <- mx.symbol.Pooling(data = data, kernel = c(3, 3), stride = c(1, 1),
pad = c(1, 1), pool_type = pool,
name = paste(pool, '_pool_', name, '_pool', sep = ''))
cproj <- ConvFactory(data = pooling, num_filter = proj, kernel = c(1, 1),
name = paste(name, '_proj', sep = ''))
# concat
concat_lst <- list()
concat_lst <- c(c1x1, c3x3, cd3x3, cproj)
concat_lst$num.args = 4
concat_lst$name = paste('ch_concat_', name, '_chconcat', sep = '')
concat = mxnet:::mx.varg.symbol.Concat(concat_lst)
return(concat)
}

InceptionFactoryB <- function(data, num_3x3red, num_3x3, num_d3x3red, num_d3x3, name) {
# 3x3 reduce + 3x3
c3x3r <- ConvFactory(data = data, num_filter = num_3x3red, kernel = c(1, 1),
name = paste(name, '_3x3', sep = ''), suffix = '_reduce')
c3x3 <- ConvFactory(data = c3x3r, num_filter = num_3x3, kernel = c(3, 3),
pad = c(1, 1), stride = c(2, 2), name = paste(name, '_3x3', sep = ''))
# double 3x3 reduce + double 3x3
cd3x3r <- ConvFactory(data = data, num_filter = num_d3x3red, kernel = c(1, 1),
name = paste(name, '_double_3x3', sep = ''), suffix = '_reduce')
cd3x3 <- ConvFactory(data = cd3x3r, num_filter = num_d3x3, kernel = c(3, 3),
pad = c(1, 1), stride = c(1, 1), name = paste(name, '_double_3x3_0', sep = ''))
cd3x3 = ConvFactory(data = cd3x3, num_filter = num_d3x3, kernel = c(3, 3),
pad = c(1, 1), stride = c(2, 2), name = paste(name, '_double_3x3_1', sep = ''))
# pool + proj
pooling = mx.symbol.Pooling(data = data, kernel = c(3, 3), stride = c(2, 2),
pad = c(1, 1), pool_type = "max",
name = paste('max_pool_', name, '_pool', sep = ''))
# concat
concat_lst <- list()
concat_lst <- c(c3x3, cd3x3, pooling)
concat_lst$num.args = 3
concat_lst$name = paste('ch_concat_', name, '_chconcat', sep = '')
concat = mxnet:::mx.varg.symbol.Concat(concat_lst)
return(concat)
}

get_symbol <- function(num_classes = 1000) {
# data
data = mx.symbol.Variable(name = "data")
# stage 1
conv1 = ConvFactory(data = data, num_filter = 64, kernel = c(7, 7),
stride = c(2, 2), pad = c(3, 3), name = 'conv1')
pool1 = mx.symbol.Pooling(data = conv1, kernel = c(3, 3), stride = c(2, 2),
name = 'pool1', pool_type = 'max')
# stage 2
conv2red = ConvFactory(data = pool1, num_filter = 64, kernel = c(1, 1),
stride = c(1, 1), name = 'conv2red')
conv2 = ConvFactory(data = conv2red, num_filter = 192, kernel = c(3, 3),
stride = c(1, 1), pad = c(1, 1), name = 'conv2')
pool2 = mx.symbol.Pooling(data = conv2, kernel = c(3, 3), stride = c(2, 2),
name = 'pool2', pool_type = 'max')
# stage 2
in3a = InceptionFactoryA(pool2, 64, 64, 64, 64, 96, "avg", 32, '3a')
in3b = InceptionFactoryA(in3a, 64, 64, 96, 64, 96, "avg", 64, '3b')
in3c = InceptionFactoryB(in3b, 128, 160, 64, 96, '3c')
# stage 3
in4a = InceptionFactoryA(in3c, 224, 64, 96, 96, 128, "avg", 128, '4a')
in4b = InceptionFactoryA(in4a, 192, 96, 128, 96, 128, "avg", 128, '4b')
in4c = InceptionFactoryA(in4b, 160, 128, 160, 128, 160, "avg", 128, '4c')
in4d = InceptionFactoryA(in4c, 96, 128, 192, 160, 192, "avg", 128, '4d')
in4e = InceptionFactoryB(in4d, 128, 192, 192, 256, '4e')
# stage 4
in5a = InceptionFactoryA(in4e, 352, 192, 320, 160, 224, "avg", 128, '5a')
in5b = InceptionFactoryA(in5a, 352, 192, 320, 192, 224, "max", 128, '5b')
# global avg pooling
avg = mx.symbol.Pooling(data = in5b, kernel = c(7, 7), stride = c(1, 1),
name = "global_pool", pool_type = 'avg')
# linear classifier
flatten = mx.symbol.Flatten(data = avg, name = 'flatten')
fc1 = mx.symbol.FullyConnected(data = flatten,
num_hidden = num_classes,
name = 'fc1')
softmax = mx.symbol.SoftmaxOutput(data = fc1, name = 'softmax')
return(softmax)
}
24 changes: 24 additions & 0 deletions example/image-classification/symbol_lenet.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
library(mxnet)

get_symbol <- function(num_classes = 1000) {
data <- mx.symbol.Variable('data')
# first conv
conv1 <- mx.symbol.Convolution(data = data, kernel = c(5, 5), num_filter = 20)

tanh1 <- mx.symbol.Activation(data = conv1, act_type = "tanh")
pool1 <- mx.symbol.Pooling(data = tanh1, pool_type = "max", kernel = c(2, 2), stride = c(2, 2))

# second conv
conv2 <- mx.symbol.Convolution(data = pool1, kernel = c(5, 5), num_filter = 50)
tanh2 <- mx.symbol.Activation(data = conv2, act_type = "tanh")
pool2 <- mx.symbol.Pooling(data = tanh2, pool_type = "max", kernel = c(2, 2), stride = c(2, 2))
# first fullc
flatten <- mx.symbol.Flatten(data = pool2)
fc1 <- mx.symbol.FullyConnected(data = flatten, num_hidden = 500)
tanh3 <- mx.symbol.Activation(data = fc1, act_type = "tanh")
# second fullc
fc2 <- mx.symbol.FullyConnected(data = tanh3, num_hidden = num_classes)
# loss
lenet <- mx.symbol.SoftmaxOutput(data = fc2, name = 'softmax')
return(lenet)
}
12 changes: 12 additions & 0 deletions example/image-classification/symbol_mlp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
library(mxnet)

get_symbol <- function(num_classes = 1000) {
data <- mx.symbol.Variable('data')
fc1 <- mx.symbol.FullyConnected(data = data, name = 'fc1', num_hidden = 128)
act1 <- mx.symbol.Activation(data = fc1, name = 'relu1', act_type = "relu")
fc2 <- mx.symbol.FullyConnected(data = act1, name = 'fc2', num_hidden = 64)
act2 <- mx.symbol.Activation(data = fc2, name = 'relu2', act_type = "relu")
fc3 <- mx.symbol.FullyConnected(data = act2, name = 'fc3', num_hidden = num_classes)
mlp <- mx.symbol.SoftmaxOutput(data = fc3, name = 'softmax')
return(mlp)
}
82 changes: 82 additions & 0 deletions example/image-classification/symbol_resnet-28-small.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
library(mxnet)

conv_factory <- function(data, num_filter, kernel, stride,
pad, act_type = 'relu', conv_type = 0) {
if (conv_type == 0) {
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)
} else if (conv_type == 1) {
conv = mx.symbol.Convolution(data = data, num_filter = num_filter,
kernel = kernel, stride = stride, pad = pad)
bn = mx.symbol.BatchNorm(data = conv)
return(bn)
}
}

residual_factory <- function(data, num_filter, dim_match) {
if (dim_match) {
identity_data = data
conv1 = conv_factory(data = data, num_filter = num_filter, kernel = c(3, 3),
stride = c(1, 1), pad = c(1, 1), act_type = 'relu', conv_type = 0)

conv2 = conv_factory(data = conv1, num_filter = num_filter, kernel = c(3, 3),
stride = c(1, 1), pad = c(1, 1), conv_type = 1)
new_data = identity_data + conv2
act = mx.symbol.Activation(data = new_data, act_type = 'relu')
return(act)
} else {
conv1 = conv_factory(data = data, num_filter = num_filter, kernel = c(3, 3),
stride = c(2, 2), pad = c(1, 1), act_type = 'relu', conv_type = 0)
conv2 = conv_factory(data = conv1, num_filter = num_filter, kernel = c(3, 3),
stride = c(1, 1), pad = c(1, 1), conv_type = 1)

# adopt project method in the paper when dimension increased
project_data = conv_factory(data = data, num_filter = num_filter, kernel = c(1, 1),
stride = c(2, 2), pad = c(0, 0), conv_type = 1)
new_data = project_data + conv2
act = mx.symbol.Activation(data = new_data, act_type = 'relu')
return(act)
}
}

residual_net <- function(data, n) {
#fisrt 2n layers
for (i in 1:n) {
data = residual_factory(data = data, num_filter = 16, dim_match = TRUE)
}


#second 2n layers
for (i in 1:n) {
if (i == 1) {
data = residual_factory(data = data, num_filter = 32, dim_match = FALSE)
} else {
data = residual_factory(data = data, num_filter = 32, dim_match = TRUE)
}
}
#third 2n layers
for (i in 1:n) {
if (i == 1) {
data = residual_factory(data = data, num_filter = 64, dim_match = FALSE)
} else {
data = residual_factory(data = data, num_filter = 64, dim_match = TRUE)
}
}
return(data)
}

get_symbol <- function(num_classes = 10) {
conv <- conv_factory(data = mx.symbol.Variable(name = 'data'), num_filter = 16,
kernel = c(3, 3), stride = c(1, 1), pad = c(1, 1),
act_type = 'relu', conv_type = 0)
n <- 3 # set n = 3 means get a model with 3*6+2=20 layers, set n = 9 means 9*6+2=56 layers
resnet <- residual_net(conv, n) #
pool <- mx.symbol.Pooling(data = resnet, kernel = c(7, 7), pool_type = 'avg')
flatten <- mx.symbol.Flatten(data = pool, name = 'flatten')
fc <- mx.symbol.FullyConnected(data = flatten, num_hidden = num_classes, name = 'fc1')
softmax <- mx.symbol.SoftmaxOutput(data = fc, name = 'softmax')
return(softmax)
}
Loading

0 comments on commit 83dc0b4

Please sign in to comment.