2017-04-05 82 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来擦除它。是否有意或无意?如果不是,请检查我提供的代码片段。