- Load the .rec file as iterators. The following is the function to load the .rec data as iterators:
# Function to load data as iterators data.iterator <- function(data.shape, train.data, val.data, BATCHSIZE = 128) { # Load training data as iterator train <- mx.io.ImageRecordIter( path.imgrec = train.data, batch.size = BATCHSIZE, data.shape = data.shape, rand.crop = TRUE, rand.mirror = TRUE) # Load validation data as iterator val <- mx.io.ImageRecordIter( path.imgrec = val.data, batch.size = BATCHSIZE, data.shape = data.shape, rand.crop = FALSE, rand.mirror = FALSE ) return(list(train = train, val = val)) }
In the preceding function, mx.io.ImageRecordIter reads batches of images from the RecordIO (.rec) files.
- Load data using the data.iterator function:
# Load dataset data <- data.iterator(data.shape = c(224, 224, 3), train.data = "pks.lst_train.rec", val.data = "pks.lst_val.rec", BATCHSIZE = 8) train <- data$train val <- data$val
- Load the Inception-BN pretrained model from the Inception-BN folder:
# Load Inception-BN model inception_bn <- mx.model.load("Inception-BN", iteration = 126) symbol <- inception_bn$symbol The different layers of the model can be viewed using function symbol$arguments
- Get the layers of the Inception-BN model:
# Load model information internals <- symbol$get.internals() outputs <- internals$outputs flatten <- internals$get.output(which(outputs == "flatten_output"))
- Define a new layer to replace the flatten_output layer:
# Define new layer new_fc <- mx.symbol.FullyConnected(data = flatten, num_hidden = 2, name = "fc1") new_soft <- mx.symbol.SoftmaxOutput(data = new_fc, name = "softmax")
- Initialize weights for the newly defined layer. To retrain the last layer, weight initialization is performed using the following script:
# Re-initialize the weights for new layer arg_params_new <- mxnet:::mx.model.init.params( symbol = new_soft, input.shape = c(224, 224, 3, 8), output.shape = NULL, initializer = mxnet:::mx.init.uniform(0.2), ctx = mx.cpu(0) )$arg.params fc1_weights_new <- arg_params_new[["fc1_weight"]] fc1_bias_new <- arg_params_new[["fc1_bias"]]
In the aforementioned layer, weights are assigned using uniform distribution between [-0.2, 0.2]. The ctx define the device on which the execution is to be performed.
- Retrain the model:
# Mode re-train model <- mx.model.FeedForward.create( symbol = new_soft, X = train, eval.data = val, ctx = mx.cpu(0), eval.metric = mx.metric.accuracy, num.round = 5, learning.rate = 0.05, momentum = 0.85, wd = 0.00001, kvstore = "local", array.batch.size = 128, epoch.end.callback = mx.callback.save.checkpoint("inception_bn"), batch.end.callback = mx.callback.log.train.metric(150), initializer = mx.init.Xavier(factor_type = "in", magnitude = 2.34), optimizer = "sgd", arg.params = arg_params_new, aux.params = inception_bn$aux.params )
The preceding model is set to run on CPU with five rounds, using accuracy as the evaluation metric. The following screenshot shows the execution of the described model:
Output from Inception-BN model, trained using CIFAR-10 dataset
The trained model has produced a training accuracy of 0.97 and a validation accuracy of 0.95.