Reputation: 1700
I'm using code from this repository - gtsrb. I have successfully trained the model with over 95% accuracy. However, I'm not sure how to get a single image tested against an already trained model. I have a trained model saved under saved_models
directory with the name egg_net_best.h5
.
Here's what I have done so far:
input = image.load(path_to_single_image, 3)
model = torch.load('saved_models/vgg_net_best.h5')
output = model:forward(input)
This gives me an output but I don't understand the format. It gives me a 9x11 tensor which looks like this
-3.1302 -2.7557 -3.6514 -5.1083 -0.4339 -3.6722 -6.7406 -4.1163 -3.2751 -3.0417 -2.4740
-4.6895 -4.5554 -1.1019 -4.1954 -1.0187 -3.2853 -4.7917 -5.1205 -2.6658 -2.3842 -2.8388
-3.2914 -2.7516 -2.1224 -4.6314 -0.7596 -3.0647 -5.5749 -3.9037 -3.4147 -2.9719 -1.9181
-3.7006 -3.0220 -2.3870 -5.2183 -0.5729 -3.1286 -6.1271 -4.4617 -3.6552 -3.1698 -1.9662
-2.4560 -2.7865 -2.0729 -2.7589 -2.4757 -2.6934 -1.3172 -2.5542 -3.0839 -3.2817 -2.4930
-3.5149 -3.2504 -2.4154 -4.9333 -0.6504 -3.3700 -5.3838 -4.4408 -2.5233 -2.3310 -2.4659
-3.5452 -4.1833 -3.3995 -2.3409 -2.9932 -2.7852 -0.5702 -3.0504 -2.8158 -3.8218 -3.9147
-3.1225 -2.7842 -2.9371 -4.6879 -0.6197 -3.1266 -5.6755 -4.0995 -3.7424 -3.1942 -1.8000
-2.9004 -2.4582 -2.5652 -4.6114 -0.6186 -3.3890 -4.1276 -3.7448 -3.1053 -3.0396 -2.6929
Questions:
Can someone point me to the right direction ?
PS: This question SO- training single image is not what Im looking for.
Edit 1:
As per suggestion I did the following but the results are just the same.
Lua 5.1 Copyright (C) 1994-2008 Lua.org, PUC-Rio
path = '/Users/pamio/Workspace/practices/torch/test_model/test/00010/IMG_2522.JPG'
input = image.load(path, 3)
model = torch.load('saved_models/vgg_net_best.h5')
model:evaluate()
output = model:forward(input):exp()
=output
Columns 1 to 10
0.0257 0.0621 0.0027 0.0169 0.0132 0.0530 0.6320 0.0798 0.0022 0.0138 0.0985
0.1033 0.0838 0.0705 0.0609 0.1639 0.0678 0.1156 0.0971 0.0653 0.1042 0.0676
0.0101 0.0262 0.0010 0.0053 0.0014 0.0312 0.8307 0.0349 0.0002 0.0050 0.0541
0.0666 0.1656 0.0054 0.0337 0.0112 0.0537 0.3638 0.1694 0.0062 0.0134 0.1111
0.0589 0.0663 0.0298 0.0148 0.1565 0.0976 0.2328 0.1159 0.0324 0.0978 0.0972
0.0511 0.0786 0.0153 0.0549 0.0341 0.1121 0.3010 0.1052 0.0314 0.0994 0.1169
0.0619 0.0777 0.0395 0.0521 0.0358 0.1125 0.3184 0.0785 0.0150 0.0588 0.1498
0.0153 0.0319 0.0009 0.0080 0.0015 0.0386 0.7779 0.0449 0.0003 0.0079 0.0726
0.0123 0.0285 0.0059 0.0062 0.0048 0.0455 0.7893 0.0269 0.0013 0.0177 0.0615
[torch.DoubleTensor of size 9x11]
Edit2:
> print(model)
nn.Sequential {
[input -> (1) -> (2) -> output]
(1): nn.Sequential {
[input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> (11) -> (12) -> (13) -> (14) -> (15) -> (16) -> (17) -> (18) -> (19) -> (20) -> (21) -> output]
(1): nn.SpatialConvolution(3 -> 32, 3x3, 1,1, 1,1)
(2): nn.SpatialBatchNormalization (4D) (32)
(3): nn.ReLU
(4): nn.SpatialConvolution(32 -> 32, 3x3, 1,1, 1,1)
(5): nn.SpatialBatchNormalization (4D) (32)
(6): nn.ReLU
(7): nn.SpatialMaxPooling(2x2, 2,2)
(8): nn.SpatialConvolution(32 -> 64, 3x3, 1,1, 1,1)
(9): nn.SpatialBatchNormalization (4D) (64)
(10): nn.ReLU
(11): nn.SpatialConvolution(64 -> 64, 3x3, 1,1, 1,1)
(12): nn.SpatialBatchNormalization (4D) (64)
(13): nn.ReLU
(14): nn.SpatialMaxPooling(2x2, 2,2)
(15): nn.SpatialConvolution(64 -> 128, 3x3, 1,1, 1,1)
(16): nn.SpatialBatchNormalization (4D) (128)
(17): nn.ReLU
(18): nn.SpatialConvolution(128 -> 128, 3x3, 1,1, 1,1)
(19): nn.SpatialBatchNormalization (4D) (128)
(20): nn.ReLU
(21): nn.SpatialMaxPooling(2x2, 2,2)
}
(2): nn.Sequential {
[input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> output]
(1): nn.View(4608)
(2): nn.Linear(4608 -> 512)
(3): nn.ReLU
(4): nn.BatchNormalization (2D) (512)
(5): nn.Dropout(0.500000)
(6): nn.Linear(512 -> 11)
(7): nn.LogSoftMax
}
}
Upvotes: 0
Views: 2351
Reputation: 2751
Seems like the image you are loading doesn't have the correct size. The network was trained with images of 3x48x48 while you are feeding them with a bigger height and width making the model internal layers mess up and treating them as batches instead of a single image.
The following code is working for me:
model:evaluate();
-- We assume that the image has a wrong size, place here your image loading code
img = torch.Tensor(3, 200, 200)
-- First we scale it to 3x48x48
img = image.scale(img, 48, 48, 'bilinear')
-- Then to avoid errors with the batchnorm layer we make it 4D
-- (batchNorm expects a 4D tensor)
image = image:reshape(1, 3, 48, 48)
-- Feed the image to the network, add the :exp() if we want to retrieve the probs.
pred = model:forward(img):exp()
-- output
Columns 1 to 10
0.01 *
9.4125 9.0546 8.9142 9.0716 9.0065 9.0865 9.4289 8.7353 9.2937 9.2811
Columns 11 to 11
0.01 *
8.7152
[torch.DoubleTensor of size 1x11]
Upvotes: 1