DNN module: switch Torch model between train() and evaluation() modes
Torch models can run in two different modes: train() and evaluation(). Results of some layers such as batch normalization will be affected by the modes.
Currently it seems (i.e., when I tested) that OpenCV's DNN module loads and runs torch models in train() mode. Is there a way to use torch models in evaluation() mode using OpenCV?
(ADDED) Below is the lua code for running the model with two different modes.
require 'torch'
require 'image'
require 'nn'
local net = torch.load('model.t7')
net:evaluate() -- you can turn this on or off
local image_input = image.load('input_image.jpg', 3, 'float')
image_input:resize(1, image_input:size()[1], image_input:size()[2], image_input:size()[3])
local result = net:forward(image_input)
image.save('result.jpg', result[1])
You need to call
evaluation()
before Torch's net serialization.@dkurt Would you explain to me a bit in detail? I tried calling model:evaluation() and then using torch.save() to save the model file. But OpenCV DNN is still using the train mode.
@Jaewoo, what do you mean by
OpenCV DNN is still using the train mode
? How do you test it?@dkurt I have a deconvolution network. I ran it on my desktop with same input, once with model:train() and the other time with model:evaluation(). Then I got two different results. After that I ran the model with same input on Android using OpenCV for Android. Then the result from the phone was same with the result from the desktop with model:train() mode.
@Jaewoo, At first check that both inputs for train and test phase are similar. Please also check that you use correct method to switch the phase (in OpenCV's scripts there is
net:evaluate()
but notnet:evaluation()
).Please also provide a standalone lua script which runs your model imported from
.t7
(or `.net).BTW, deconvolution's layer forward pass doesn't depend on learning phase.
@dkurt Oh, I used
net:evaluate()
, notnet:evaluation()
, in torch. Sorry for the typo. I am sure the inputs were exactly same because I used one image file for every run. May I ask what do you mean by OpenCV hasnet:evaluate()
? Is thereevaluate()
function which is not listed in https://docs.opencv.org/3.4.4/db/d30/... ? (Added) The difference of the result comes from batch normalization which are inserted between deconvolution layers.@Jaewoo, I meant just script to generate test data for OpenCV's Torch importer: https://github.com/opencv/opencv_extr....
@Jaewoo, if you're able to share the model - it'd be the simplest way to reproduce your problem.
@dkurt The model is on https://github.com/chuanli11/MGANs/bl... . (ADDED) I added the my lua code for running
train()
andevaluate()
. The purpose of the code was to confirm the difference between two modes.