Testing a trained network

Hi all,
I have trained a classification network using the :slight_smile:

model.fit(train,
begin_epoch = epoch,
num_epoch = num_epochs,
eval_data = val,
eval_metric = MultitaskAccuracy(num=NUM_LABELS),
optimizer = ‘nag’,
optimizer_params = optimizer_params,
#arg_params = new_args,
initializer = initializer,
allow_missing = True,
batch_end_callback = mx.callback.Speedometer(batch_size, 1000),
epoch_end_callback = checkpoint)

train and val are defined as follows:

train = mx.io.ImageRecordIter(
path_imglist= train_list
path_imgrec = train_rec,
#mean_img = train_mean,
#resize = 230,
mean_r = 127.5,
mean_g = 127.5,
mean_b = 127.5,
data_shape = data_shape,
batch_size = batch_size,
rand_crop = True,
rand_mirror = True,
shuffle = True,
label_width = NUM_LABELS
)

I have been testing it as follows:

test = mx.io.ImageRecordIter(
path_imglist= pred_file,
path_imgrec = pred_rec,
mean_r = 127.5,
mean_g = 127.5,
mean_b = 127.5,
#mean_img = mean_file,
rand_crop = False,
rand_mirror = False,
data_shape = data_shape,
batch_size = 1,
label_width = NUM_LABELS
)
predictions = mod.predict(test)

However I need to test it one image at a time, so I wrote the test in the following way:

image = cv2.cvtColor(cv2.imread(img), cv2.COLOR_BGR2RGB)
image = np.array(image, dtype=np.float32)
test_img = cv2.resize(image, (224, 224))
test_img = np.expand_dims(np.transpose(test_img, (2, 0, 1)), axis=0)
test_img = test_img - 127.5
mod.forward(Batch([mx.nd.array(test_img)]))

However the responses are different.

What is the correct way of testing the trained network, when we have to do mean-subtraction and resizing?

Hi what exactly do you mean by the responses are different? I’m assuming you mean that you get a different result using the ImageRecordIter versus using one image at a time. ImageRecordIter iterates over a .rec file which might be decoded differently than using cv2.imread on what I assume is a ‘.jpg’ file. What file type are you reading with cv2.imread. Perhaps try using mx.image.ImageIter with the raw image files to see if the responses match.

Thanks for your response.
Yes I meant the network values are different,
so can you point me to a location where we are testing a pre-trained network?
I want to know how the mean-subtraction happens in mx.image.ImageIter

mean subtraction happens when mx.image.ImageIter calls the augmenter.

This is the src for mx.image.ImageIter: https://mxnet.incubator.apache.org/_modules/mxnet/image/image.html#ImageIter and this is the src for the Augmenter https://mxnet.incubator.apache.org/_modules/mxnet/image/image.html#CastAug

1 Like