2016-03-26 35 views
1

我試圖運行Keras的以下網絡,由TensorFlow後端供電。這是「VGG樣」 convnet從http://keras.io/examples/適應:難以根據客戶數據調整Keras示例

import os 
import sys 
import json 
import model_control 
from numpy import loadtxt, asarray 
from pandas import read_csv 
from scipy.ndimage import imread 
from keras.models import Sequential 
from keras.layers import Dense, Dropout, Activation, Flatten 
from keras.layers import Convolution2D, MaxPooling2D 
from keras.optimizers import SGD 

Y_train = loadtxt(model_control.y_train_file, delimiter=',', dtype = int) 

train_files = os.listdir(model_control.train_img_path) 
train_files = ['%s/%s' % (model_control.train_img_path, x) for x in train_files if 'jpg' in x] 
X_train = asarray([imread(x) for x in train_files]) 

X_train.shape #..(8144, 128, 256) (a numpy array of 8144 128x256 greyscale, i.e. single-channel, images) 
Y_train.shape #..(8144,) (A 1-d numpy array of integer class labels) 

model = Sequential() 

model.add(Convolution2D(32, 5, 5, border_mode='valid', input_shape=(1, 128, 256))) 
model.add(Activation('relu')) 
model.add(Convolution2D(32, 5, 5)) 
model.add(Activation('relu')) 
model.add(MaxPooling2D(pool_size=(2, 2))) 
model.add(Dropout(0.25)) 

model.add(Convolution2D(64, 5, 5, border_mode='valid')) 
model.add(Activation('relu')) 
model.add(Convolution2D(64, 5, 5)) 
model.add(Activation('relu')) 
model.add(MaxPooling2D(pool_size=(2, 2))) 
model.add(Dropout(0.25)) 

model.add(Flatten()) 
model.add(Dense(256)) 
model.add(Activation('relu')) 
model.add(Dropout(0.5)) 

model.add(Dense(10)) 
model.add(Activation('softmax')) 

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) 
model.compile(loss='categorical_crossentropy', optimizer=sgd) 

model.fit(X_train, Y_train, batch_size=32, nb_epoch=1, verbose=1) 

這將產生錯誤:

ValueError: Cannot feed value of shape (32, 128, 256) for Tensor u'Placeholder_89:0', which has shape '(?, 1, 128, 256)' 

我已經簽出了以下職位,但一直沒能解決這個一個呢。任何幫助將不勝感激,並解釋發生了什麼問題。

    https://groups.google.com/forum/#!topic/keras-users/Vb7MhSqne0Y http://stackoverflow.com/questions/33974231/tensorflow-error-using-my-own-data

UPDATE

發佈此問題的Keras問題板(https://github.com/fchollet/keras/issues/2092)。有Gistsample data鏈接,將允許您重新創建該問題。

回答

0

解決了它。在腳本中,需要以下行來「重塑」輸入數組:

X_train = X_train.reshape(X_train.shape[0], 1, 128, 256)

真的,我們在這裏所做的是增加了幾分冗餘通道的尺寸,使陣列(8144, 1, 128, 256)的形狀,而不是的(8144, 128, 256)。如果我們使用的是RGB數組,這將不會是多餘的,因爲它將是(8144, 3, 128, 256)。底線:我的輸入數組缺少頻道維度,我認爲我可以省略greyscales。原來你仍然需要明確定義形狀。

很棒的包裝。一旦修復代碼應該按原樣執行。

相關問題