【问题标题】:Create readable image dataset for training in Keras在 Keras 中创建用于训练的可读图像数据集
【发布时间】:2017-04-06 03:40:14
【问题描述】:

我正在尝试借助 keras 创建自己的图像识别程序,但遇到了问题。我正在尝试获取带有图片的文件夹并创建一个数据集供model.fit() 使用。我知道fit_generator(),但想知道生成器对图像做了什么。这就是为什么我要手动尝试从图像中创建一个数字数组/数据集。

我使用的模型是 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() 创建一个可读的数据集/数组吗?

【问题讨论】:

  • 你看过我的回答了吗?

标签: dataset deep-learning keras


【解决方案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 中添加 sample 维度。第二件事是您正在从文件中读取img,然后您通过使用np.arange 函数创建一个全新的np.array 来擦除它。这是有意的还是无意的?如果没有,请检查我提供的代码 sn-p。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 2017-03-19
    • 2018-02-04
    • 2018-07-17
    • 1970-01-01
    • 2021-03-12
    相关资源
    最近更新 更多