Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
[MXNET-690] Add tests for initializers in R (#12360)
Browse files Browse the repository at this point in the history
* Add tests for intializer

* lint and indent
  • Loading branch information
anirudhacharya authored and hetong007 committed Aug 28, 2018
1 parent d9ea96a commit d234b32
Show file tree
Hide file tree
Showing 9 changed files with 642 additions and 621 deletions.
91 changes: 41 additions & 50 deletions R-package/tests/testthat/get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,105 +3,96 @@ GetMNIST_ubyte <- function() {
if (!dir.exists("data")) {
dir.create("data/")
}
if (!file.exists('data/train-images-idx3-ubyte') |
!file.exists('data/train-labels-idx1-ubyte') |
!file.exists('data/t10k-images-idx3-ubyte') |
!file.exists('data/t10k-labels-idx1-ubyte')) {
download.file('http://data.mxnet.io/mxnet/data/mnist.zip', destfile = 'data/mnist.zip')
unzip('data/mnist.zip', exdir = 'data/')
file.remove('data/mnist.zip')
if (!file.exists("data/train-images-idx3-ubyte") | !file.exists("data/train-labels-idx1-ubyte") |
!file.exists("data/t10k-images-idx3-ubyte") | !file.exists("data/t10k-labels-idx1-ubyte")) {
download.file("http://data.mxnet.io/mxnet/data/mnist.zip", destfile = "data/mnist.zip")
unzip("data/mnist.zip", exdir = "data/")
file.remove("data/mnist.zip")
}
}

GetMNIST_csv <- function() {
if (!dir.exists("data")) {
dir.create("data/")
}
if (!file.exists('data/train.csv') |
!file.exists('data/test.csv')) {
download.file('https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/R/data/mnist_csv.zip',
destfile = 'data/mnist_csv.zip')
unzip('data/mnist_csv.zip', exdir = 'data/')
file.remove('data/mnist_csv.zip')
if (!file.exists("data/train.csv") | !file.exists("data/test.csv")) {
download.file("https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/R/data/mnist_csv.zip",
destfile = "data/mnist_csv.zip")
unzip("data/mnist_csv.zip", exdir = "data/")
file.remove("data/mnist_csv.zip")
}
}

GetCifar10 <- function() {
if (!dir.exists("data")) {
dir.create("data/")
}
if (!file.exists('data/cifar/train.rec') |
!file.exists('data/cifar/test.rec') |
!file.exists('data/cifar/train.lst') |
!file.exists('data/cifar/test.lst')) {
download.file('http://data.mxnet.io/mxnet/data/cifar10.zip',
destfile = 'data/cifar10.zip')
unzip('data/cifar10.zip', exdir = 'data/')
file.remove('data/cifar10.zip')
if (!file.exists("data/cifar/train.rec") | !file.exists("data/cifar/test.rec") |
!file.exists("data/cifar/train.lst") | !file.exists("data/cifar/test.lst")) {
download.file("http://data.mxnet.io/mxnet/data/cifar10.zip", destfile = "data/cifar10.zip")
unzip("data/cifar10.zip", exdir = "data/")
file.remove("data/cifar10.zip")
}
}

GetInception <- function() {
if (!dir.exists("model")) {
dir.create("model/")
}
if (!file.exists('model/Inception-BN-0126.params')) {
download.file('http://data.dmlc.ml/models/imagenet/inception-bn/Inception-BN-0126.params',
destfile = 'model/Inception-BN-0126.params')
if (!file.exists("model/Inception-BN-0126.params")) {
download.file("http://data.dmlc.ml/models/imagenet/inception-bn/Inception-BN-0126.params",
destfile = "model/Inception-BN-0126.params")
}
if (!file.exists('model/Inception-BN-symbol.json')) {
download.file('http://data.dmlc.ml/models/imagenet/inception-bn/Inception-BN-symbol.json',
destfile = 'model/Inception-BN-symbol.json')
if (!file.exists("model/Inception-BN-symbol.json")) {
download.file("http://data.dmlc.ml/models/imagenet/inception-bn/Inception-BN-symbol.json",
destfile = "model/Inception-BN-symbol.json")
}
}

GetCatDog <- function() {
if (!dir.exists("data")) {
dir.create("data/")
}
if (!file.exists('data/cats_dogs/cats_dogs_train.rec') |
!file.exists('data/cats_dogs/cats_dogs_val.rec')) {
download.file('https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/R/data/cats_dogs.zip',
destfile = 'data/cats_dogs.zip')
unzip('data/cats_dogs.zip', exdir = 'data/')
file.remove('data/cats_dogs.zip')
if (!file.exists("data/cats_dogs/cats_dogs_train.rec") | !file.exists("data/cats_dogs/cats_dogs_val.rec")) {
download.file("https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/R/data/cats_dogs.zip",
destfile = "data/cats_dogs.zip")
unzip("data/cats_dogs.zip", exdir = "data/")
file.remove("data/cats_dogs.zip")
}
}

GetMovieLens <- function() {
if (!dir.exists("data")) {
dir.create("data/")
}
if (!file.exists('data/ml-100k/u.data')) {
download.file('http://files.grouplens.org/datasets/movielens/ml-100k.zip',
destfile = 'data/ml-100k.zip')
unzip('data/ml-100k.zip', exdir = 'data/')
file.remove('data/ml-100k.zip')
if (!file.exists("data/ml-100k/u.data")) {
download.file("http://files.grouplens.org/datasets/movielens/ml-100k.zip",
destfile = "data/ml-100k.zip")
unzip("data/ml-100k.zip", exdir = "data/")
file.remove("data/ml-100k.zip")
}
}

GetISBI_data <- function() {
if (!dir.exists("data")) {
dir.create("data/")
}
if (!file.exists('data/ISBI/train-volume.tif') |
!file.exists('data/ISBI/train-labels.tif')) {
download.file('https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/R/data/ISBI.zip',
destfile = 'data/ISBI.zip')
unzip('data/ISBI.zip', exdir = 'data/')
file.remove('data/ISBI.zip')
if (!file.exists("data/ISBI/train-volume.tif") | !file.exists("data/ISBI/train-labels.tif")) {
download.file("https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/R/data/ISBI.zip",
destfile = "data/ISBI.zip")
unzip("data/ISBI.zip", exdir = "data/")
file.remove("data/ISBI.zip")
}
}

GetCaptcha_data <- function() {
if (!dir.exists("data")) {
dir.create("data/")
}
if (!file.exists('data/captcha_example/captcha_train.rec') |
!file.exists('data/captcha_example/captcha_test.rec')) {
download.file('https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/R/data/captcha_example.zip',
destfile = 'data/captcha_example.zip')
unzip('data/captcha_example.zip', exdir = 'data/')
file.remove('data/captcha_example.zip')
if (!file.exists("data/captcha_example/captcha_train.rec") | !file.exists("data/captcha_example/captcha_test.rec")) {
download.file("https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/R/data/captcha_example.zip",
destfile = "data/captcha_example.zip")
unzip("data/captcha_example.zip", exdir = "data/")
file.remove("data/captcha_example.zip")
}
}
155 changes: 84 additions & 71 deletions R-package/tests/testthat/test_img_seg.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ require(mxnet)

source("get_data.R")

if (Sys.getenv("R_GPU_ENABLE") != "" & as.integer(Sys.getenv("R_GPU_ENABLE")) == 1) {
if (Sys.getenv("R_GPU_ENABLE") != "" & as.integer(Sys.getenv("R_GPU_ENABLE")) ==
1) {
mx.ctx.default(new = mx.gpu())
message("Using GPU for testing.")
}
Expand All @@ -12,124 +13,136 @@ print_inferred_shape <- function(net) {
print(slist$out.shapes)
}

convolution_module <- function(net, kernel_size, pad_size, filter_count,
stride = c(1, 1), work_space = 2048, batch_norm = TRUE,
down_pool = FALSE, up_pool = FALSE, act_type = "relu",
convolution = TRUE) {
convolution_module <- function(net, kernel_size, pad_size, filter_count, stride = c(1,
1), work_space = 2048, batch_norm = TRUE, down_pool = FALSE, up_pool = FALSE,
act_type = "relu", convolution = TRUE) {
if (up_pool) {
net = mx.symbol.Deconvolution(net, kernel = c(2, 2), pad = c(0, 0),
stride = c(2, 2), num_filter = filter_count,
workspace = work_space)
net = mx.symbol.BatchNorm(net)
net <- mx.symbol.Deconvolution(net, kernel = c(2, 2), pad = c(0, 0), stride = c(2,
2), num_filter = filter_count, workspace = work_space)
net <- mx.symbol.BatchNorm(net)
if (act_type != "") {
net = mx.symbol.Activation(net, act_type = act_type)
net <- mx.symbol.Activation(net, act_type = act_type)
}
}
if (convolution) {
conv = mx.symbol.Convolution(data = net, kernel = kernel_size, stride = stride,
pad = pad_size, num_filter = filter_count,
workspace = work_space)
net = conv
conv <- mx.symbol.Convolution(data = net, kernel = kernel_size, stride = stride,
pad = pad_size, num_filter = filter_count, workspace = work_space)
net <- conv
}
if (batch_norm) {
net = mx.symbol.BatchNorm(net)
net <- mx.symbol.BatchNorm(net)
}

if (act_type != "") {
net = mx.symbol.Activation(net, act_type = act_type)
net <- mx.symbol.Activation(net, act_type = act_type)
}

if (down_pool) {
pool = mx.symbol.Pooling(net, pool_type = "max", kernel = c(2, 2), stride = c(2, 2))
net = pool
pool <- mx.symbol.Pooling(net, pool_type = "max", kernel = c(2, 2), stride = c(2,
2))
net <- pool
}
print_inferred_shape(net)
return(net)
}

get_unet <- function() {
data = mx.symbol.Variable('data')
kernel_size = c(3, 3)
pad_size = c(1, 1)
filter_count = 32
pool1 = convolution_module(data, kernel_size, pad_size, filter_count = filter_count, down_pool = TRUE)
net = pool1
pool2 = convolution_module(net, kernel_size, pad_size, filter_count = filter_count * 2, down_pool = TRUE)
net = pool2
pool3 = convolution_module(net, kernel_size, pad_size, filter_count = filter_count * 4, down_pool = TRUE)
net = pool3
pool4 = convolution_module(net, kernel_size, pad_size, filter_count = filter_count * 4, down_pool = TRUE)
net = pool4
net = mx.symbol.Dropout(net)
pool5 = convolution_module(net, kernel_size, pad_size, filter_count = filter_count * 8, down_pool = TRUE)
net = pool5
net = convolution_module(net, kernel_size, pad_size, filter_count = filter_count * 4, up_pool = TRUE)
net = convolution_module(net, kernel_size, pad_size = c(2, 2), filter_count = filter_count * 4, up_pool = TRUE)
net = mx.symbol.Crop(net, pool3, num.args = 2)
net = mx.symbol.concat(c(pool3, net), num.args = 2)
net = mx.symbol.Dropout(net)
net = convolution_module(net, kernel_size, pad_size, filter_count = filter_count * 4)
net = convolution_module(net, kernel_size, pad_size, filter_count = filter_count * 4, up_pool = TRUE)
data <- mx.symbol.Variable("data")
kernel_size <- c(3, 3)
pad_size <- c(1, 1)
filter_count <- 32
pool1 <- convolution_module(data, kernel_size, pad_size, filter_count = filter_count,
down_pool = TRUE)
net <- pool1
pool2 <- convolution_module(net, kernel_size, pad_size, filter_count = filter_count *
2, down_pool = TRUE)
net <- pool2
pool3 <- convolution_module(net, kernel_size, pad_size, filter_count = filter_count *
4, down_pool = TRUE)
net <- pool3
pool4 <- convolution_module(net, kernel_size, pad_size, filter_count = filter_count *
4, down_pool = TRUE)
net <- pool4
net <- mx.symbol.Dropout(net)
pool5 <- convolution_module(net, kernel_size, pad_size, filter_count = filter_count *
8, down_pool = TRUE)
net <- pool5
net <- convolution_module(net, kernel_size, pad_size, filter_count = filter_count *
4, up_pool = TRUE)
net <- convolution_module(net, kernel_size, pad_size = c(2, 2), filter_count = filter_count *
4, up_pool = TRUE)
net <- mx.symbol.Crop(net, pool3, num.args = 2)
net <- mx.symbol.concat(c(pool3, net), num.args = 2)
net <- mx.symbol.Dropout(net)
net <- convolution_module(net, kernel_size, pad_size, filter_count = filter_count *
4)
net <- convolution_module(net, kernel_size, pad_size, filter_count = filter_count *
4, up_pool = TRUE)

net = mx.symbol.Concat(c(pool2, net), num.args = 2)
net = mx.symbol.Dropout(net)
net = convolution_module(net, kernel_size, pad_size, filter_count = filter_count * 4)
net = convolution_module(net, kernel_size, pad_size, filter_count = filter_count * 4, up_pool = TRUE)
convolution_module(net, kernel_size, pad_size, filter_count = filter_count * 4)
net = mx.symbol.Concat(c(pool1, net), num.args = 2)
net = mx.symbol.Dropout(net)
net = convolution_module(net, kernel_size, pad_size, filter_count = filter_count * 2)
net = convolution_module(net, kernel_size, pad_size, filter_count = filter_count * 2, up_pool = TRUE)
net = convolution_module(net, kernel_size, pad_size, filter_count = 1, batch_norm = FALSE, act_type = "")
net = mx.symbol.SoftmaxOutput(data = net, name = 'sm')
net <- mx.symbol.Concat(c(pool2, net), num.args = 2)
net <- mx.symbol.Dropout(net)
net <- convolution_module(net, kernel_size, pad_size, filter_count = filter_count *
4)
net <- convolution_module(net, kernel_size, pad_size, filter_count = filter_count *
4, up_pool = TRUE)
convolution_module(net, kernel_size, pad_size, filter_count = filter_count *
4)
net <- mx.symbol.Concat(c(pool1, net), num.args = 2)
net <- mx.symbol.Dropout(net)
net <- convolution_module(net, kernel_size, pad_size, filter_count = filter_count *
2)
net <- convolution_module(net, kernel_size, pad_size, filter_count = filter_count *
2, up_pool = TRUE)
net <- convolution_module(net, kernel_size, pad_size, filter_count = 1, batch_norm = FALSE,
act_type = "")
net <- mx.symbol.SoftmaxOutput(data = net, name = "sm")
return(net)
}

context("Image segmentation")

test_that("UNET", {
list.of.packages <- c("imager")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages, repos = "https://cloud.r-project.org/")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,
"Package"])]
if (length(new.packages))
install.packages(new.packages, repos = "https://cloud.r-project.org/")
GetISBI_data()
library(imager)
IMG_SIZE <- 168
files <- list.files(path = "data/ISBI/train-volume/")
a = 'data/ISBI/train-volume/'
filess = paste(a, files, sep = '')
list_of_images = lapply(filess, function(x) {
a <- "data/ISBI/train-volume/"
filess <- paste(a, files, sep = "")
list_of_images <- lapply(filess, function(x) {
x <- load.image(x)
y <- resize(x, size_x = IMG_SIZE, size_y = IMG_SIZE)
})

train.x = do.call('cbind', lapply(list_of_images, as.vector))
train.x <- do.call("cbind", lapply(list_of_images, as.vector))
train.array <- train.x
dim(train.array) <- c(IMG_SIZE, IMG_SIZE, 1, 30)

files <- list.files(path = "data/ISBI/train-labels")
b = 'data/ISBI/train-labels/'
filess = paste(b, files, sep = '')
list_of_images = lapply(filess, function(x) {
b <- "data/ISBI/train-labels/"
filess <- paste(b, files, sep = "")
list_of_images <- lapply(filess, function(x) {
x <- load.image(x)
y <- resize(x, size_x = IMG_SIZE, size_y = IMG_SIZE)
})

train.y = do.call('cbind', lapply(list_of_images, as.vector))
train.y <- do.call("cbind", lapply(list_of_images, as.vector))

train.y[which(train.y < 0.5)] = 0
train.y[which(train.y > 0.5)] = 1
train.y.array = train.y
dim(train.y.array) = c(IMG_SIZE, IMG_SIZE, 1, 30)
train.y[which(train.y < 0.5)] <- 0
train.y[which(train.y > 0.5)] <- 1
train.y.array <- train.y
dim(train.y.array) <- c(IMG_SIZE, IMG_SIZE, 1, 30)

devices <- mx.ctx.default()
mx.set.seed(0)

net <- get_unet()

model <- mx.model.FeedForward.create(net, X = train.array, y = train.y.array,
ctx = devices, num.round = 2,
initializer = mx.init.normal(sqrt(2 / 576)),
learning.rate = 0.05,
momentum = 0.99,
array.batch.size = 2)
model <- mx.model.FeedForward.create(net, X = train.array, y = train.y.array,
ctx = devices, num.round = 2, initializer = mx.init.normal(sqrt(2/576)),
learning.rate = 0.05, momentum = 0.99, array.batch.size = 2)
})
Loading

0 comments on commit d234b32

Please sign in to comment.