2017-10-15 362 views
0

我想要預測單張圖片的結果,但它會給出不相關的結果。我已經訓練模型上CIFAR 10集 我已經使用keras和tensorflow訓練這個模型keras:預測單張圖片

這裏是訓練碼要點:https://github.com/09rohanchopra/cifar10/blob/master/cifar10-simple-cnn.ipynb 代碼用於預測simgle圖像

from keras.preprocessing import image 
from keras.models import load_model 
from scipy.misc import imread,imresize 
import numpy as np 
model=load_model('augmented_best_model.h5') 
im=imread('1.jpg') 
im=im/255 
im=im.resize(im,(32,32)) 
pr=model.predict(im.reshape(-1,3,32,32)) 
+2

爲什麼你將圖像重新塑造成通道?在你的訓練代碼中,圖像輸入形狀是'input_shape =(IMAGE_SIZE,IMAGE_SIZE,CHANNELS)' – Matin

+0

另外,如果im有一個整數的dtype,'im/255'可能會在Python 2中將其歸零 –

回答

0

我答案

x=imread('test/2.jpg',mode='RGB') 
x=imresize(x,(32,32)) 
x=np.invert(x) 
x=x.reshape(-1,32,32,3) 
1

imread現已被棄用。更好的選擇是使用keras函數。

x = image.load_img('test/2.jpg', target_size=(32,32)) 
x = image.img_to_array(x) 
x = x.reshape((1,) + x.shape) 
x = x/255.