【问题标题】:How to pass a list of images into the keras train function如何将图像列表传递给 keras train 函数
【发布时间】:2019-05-10 04:06:32
【问题描述】:

我有一个 pgn 图片列表。从每个图像中,我提取了一个特定的对象,并将该对象仅存储到一个单独的图像中。我读了原始图像 放入xTrainnumpy数组,并将对象提取到yTrainnumpy数组中:

def getFilesList(directory):
    files = os.listdir(directory)
    return map(lambda file: directory + file, files)

def readImagesIntoNumpy(directory):
    filesList = getFilesList(directory)
    images = map(lambda file: plt.imread(file), filesList)
    return np.array(images)

xTrain = readImagesIntoNumpy("./original/")
yTrain = readImagesIntoNumpy("./objects/")

我希望模型训练如何从新图像中提取这些对象:

model = Sequential()
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
          optimizer='sgd',
          metrics=['accuracy'])

model.fit(xTrain, yTrain, epochs = 5, batch_size = 32)

问题是最后一次model.fit调用抛出错误:

ValueError: Error when checking input: expected dense_17_input
            to have 2 dimensions, but got array with shape (20, 256, 256, 4)

如何将一组图像传入 keras 模型进行训练?

【问题讨论】:

    标签: python image numpy tensorflow keras


    【解决方案1】:

    您的输入图像形状为 (256, 256, 4),输入密集层形状为 2 作为向量 将数据从 (256, 256, 1) 重塑为 (256*256*1,1)

    在第一层添加这一层

    model.add(Reshape(target_shape = (256*256*256,1), input_shape = (256, 256, 1)))
    model.add(Dense(units=64, activation='relu'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 2016-11-11
      相关资源
      最近更新 更多