【问题标题】:Error in fitting model and input_shape with keras ConvNet使用 keras ConvNet 拟合模型和 input_shape 时出错
【发布时间】:2017-03-27 22:54:40
【问题描述】:

如果您有 keras 方面的经验,我们将不胜感激。这是我的第一次牛仔竞技表演,野马的表现非常强劲!

我对肺部图像进行了预处理,并提取了 48x48x48mm 立方阵列,表示感兴趣区域中的像素 (dtype=uint8)。我已将这 8504 个立方体存储在 *.npy 文件中。

当我将立方体列表作为训练数据传递给模型时,遇到以下错误:

“检查模型输入时出错:您传递给模型的 Numpy 数组列表不是模型预期的大小。预计会看到 1 个数组,但得到了以下 8504 个数组列表”。

我可能做错了什么?

相关代码:

# input layer of model
c3d_model.add(Convolution3D(64, 8,8,8, activation='relu', border_mode='same',
              name='conv1', input_shape=(48, 48, 48, 1)))
# other layers ....

# get_data()
cubes = [np.load(os.path.join(CUBES_DIR, cubefile)) for cubefile in cubefiles] # cubefiles is a list of 8504 filenames

# shuffle data and labels to avoid skewing the training
ix = [i for i in range(len(labels))]
shuffle(ix)
X_cubes = [cubes[i] for i in ix]
Y_labels = [labels[i] for i in ix]

# and here's where I run aground
model.fit(cubes, Y_labels, validation_split=0.30, nb_epoch=1, batch_size=32, callbacks=[save_weights], verbose=2)

谢谢!

【问题讨论】:

    标签: keras conv-neural-network


    【解决方案1】:

    错误说 Keras 需要一个数组,但你给它一个数组列表。

    洗牌后,试试:

    X_cubes = np.array(X_cubes)
    Y_labels = np.array(X_cubes)
    

    还请注意,您正在改组 XY,但只将改组后的 Y 提供给 Keras,未改组 X。我想这不是你想要的。

    【讨论】:

    • 嗨@vega,你帮我解决了这个错误,谢谢!是的,通过未洗牌的 X 是一个错误。但是现在我在将 X_cubes 作为 numpy 数组传递后看到了一个不同的错误:ValueError: Error when checks model input: expected convolution3d_input_14 to have 5 dimensions, but got array with shape (8504, 1).对此有什么想法吗?
    • 是的,您输入的形状错误。如果你输入cubes[0].shape,它会说什么?您从中加载的文件中的数据是如何格式化的?您需要了解这一点,才能知道如何为网络正确地塑造数据。
    • cubes[0].shape == (48, 48, 48) - 感谢您的帮助,@vega!
    • 您已将卷积输入声明为 input_shape=(48, 48, 48, 1),而您的输入是 (48, 48, 48),少一维。这些应该匹配。如果你做cubes.shape,你会得到(8504,48,48,48)?
    • 我发现,有 10 个样本,np.array(cubes).shape == (10,48,48,48)。但是有 11 个样本(或更多),np.array(cubes).shape = (11,)。因此,我正在编写一个一次生成 10 个样本的生成器;如果我能让它工作,我会发表评论。至于额外维度,我的理解是 3D 卷积需要 5 个维度(样本、行、列、深度、通道),这就是为什么我在末尾提供 1(指定 1 个通道)。 IIRC 我看到了为灰度图像指定最终维度为 1 的示例代码,这就是我正在使用的。再次感谢@vega!
    猜你喜欢
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多