割炬 - 加载已保存的模型并测试单个图像以进行分类

时间:2017-03-15 14:18:32

标签: lua torch

我正在使用此存储库中的代码 - gtsrb。我已经成功地训练了模型,准确率超过95%。但是,我不确定如何针对已经训练过的模型测试单个图像。我有一个训练有素的模型保存在saved_models目录下,名称为egg_net_best.h5

这是我到目前为止所做的:

input = image.load(path_to_single_image, 3)
model = torch.load('saved_models/vgg_net_best.h5')
output = model:forward(input)

这给了我一个输出,但我不明白格式。它给了我一个9x11张量,看起来像这样

-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

问题:

  1. 是针对受过训练的模型测试单个图像的方法吗?
  2. 为什么我看到9x11 Tensor应该是1x11?
  3. 有人能指出我正确的方向吗?

    PS:这个问题SO- training single image不是我想要的。

    编辑1:

    根据建议我做了以下但结果是一样的。

      

    Lua 5.1版权所有(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')

         

    模型:评估()

         

    output = model:forward(input):exp()

         

    =输出

    第1至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
      }
     }
    

1 个答案:

答案 0 :(得分:1)

似乎您正在加载的图片没有正确的尺寸。网络使用3x48x48的图像进行训练,同时为它们提供更大的高度和宽度,使模型内部层变得混乱并将它们视为批次而不是单个图像。

以下代码对我有用:

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]