2017-03-15 23 views
0

我使用代碼從這個倉庫 - 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張量,而應該1x11?

能有人指出正確的方向?

PS:這個問題SO- training single image是不是進出口尋找。

編輯1:

按照建議我做了以下但結果是一樣的。

的Lua 5.1版權(C)1994年至2008年Lua.org,PUC-力

路徑= '/Users/pamio/Workspace/practices/torch/test_model/test/00010/IMG_2522.JPG'

輸入= image.load(路徑,3)

模型= torch.load( 'saved_models/vgg_net_best.h5')

模型:評估()

輸出=模型:前向(輸入):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..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

好像圖像你正在加載的尺寸不正確。該網絡,而你是一個更大的高度和寬度使得模型內部層弄亂和治療它們作爲批次而不是單個圖像餵養它們與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] 
+0

真但那並非如此。順便說一句,我的測試單一圖像對訓練模型的方法是否正確? –

+0

我也在gitter上問過這個問題,但還沒有收到任何迴應。 –

+0

@PamioSolanky是的,這種方法看起來是正確的。也許你還應該在':forward()'之前添加'model:evaluate()',對像'dropout()'或'batchNormalization()'這樣的層在訓練和評估過程中有不同的行爲。另外,正如我在答案中提到的那樣,由於存儲庫中的模型最後有一個'LogSoftMax()'層,因此如果您只想獲得概率,則需要將':exp()'添加到輸出中最好的預測':sort()'數組。如果您希望我幫助您解決問題,請粘貼'print model'輸出!希望能幫助到你! –