2017-04-05 79 views
1

我想在keras的幫助下創建自己的圖像識別程序,但是我遇到了問題。我正在試圖帶圖片的文件夾並創建要使用的model.fit()的數據集。我知道fit_generator(),但試圖知道發電機與圖像做什麼。這就是爲什麼我試圖從圖像創建一個數組/數據集。在Keras創建可讀的圖像數據集培訓

我使用的模型是VGG16,所以這是模型的結尾和開頭:

model = Sequential() 
model.add(ZeroPadding2D((1, 1), input_shape=(256, 256, 3))) 
model.add(Convolution2D(64, 3, 3, activation='relu')) 
model.add(ZeroPadding2D((1, 1))) 
... 
model.add(Dense(4096, activation='relu')) 
model.add(Dropout(0.5)) 
model.add(Dense(3, activation='softmax')) 

編譯:

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

適合:

model.fit(test_x, 1, batch_size=32, nb_epoch=10, verbose=1, callbacks=None, validation_split=0.1) 

排列生成:

path_temp = %PATH% 

list = os.listdir(path_temp) 
array_list = [] 

for file in list: 

    img = imread(path_temp + '\\' + file, flatten=True) 
    img = np.arange(1 * 3 * 256 * 256).reshape((-1, 256, 256, 3)) 

    img = img.astype('float32') 
    array_list.append(img) 

test_x = np.stack(array_list) 
test_x /= 255.0 

錯誤:

ValueError: Error when checking model input: expected zeropadding2d_input_1 to have 4 dimensions, but got array with shape (990, 1, 256, 256, 3) 

這是我有什麼,但有從這裏某種方式來爲fit()可讀數據集/陣列?

+0

你看過我的回答嗎? –

回答

1

我會改變你的for迴路提供的代碼:

for file in list: 

    img = imread(path_temp + '\\' + file, flatten=True) 
    img = np.arange(1 * 3 * 256 * 256).reshape((256, 256, 3)) 
    # img = np.array(img).reshape((256, 256, 3)) <- consider this 

    img = img.astype('float32') 
    array_list.append(img) 

第一個問題從事實來,你是疊加圖像一起 - 所以沒有必要添加樣品維度reshape。第二件事 - 是你正在從文件中讀取img,然後通過使用np.arange函數創建一個全新的np.array來擦除它。是否有意或無意?如果不是,請檢查我提供的代碼片段。