Keras的“fit”输入不清楚

6

我试图将尺寸为(350x350x3)的图片作为输入形状,训练网络输出一个(1400x1400x3)的图像(4倍放大)。

我的训练数据集包含8张1400x1400x3的图片,我会对它们进行翻转等处理,以获得32张用于验证的图片。

然后,我会将这32张图像缩小到350x350x3的大小,以获取输入图像,这些图像将与其另外32个对应项进行交叉验证。

print(type(validateData))
print(validateData.shape)
print(type(validateData[0].shape))
print(validateData[0].shape)

返回值

<class 'numpy.ndarray'>
(32,)
<class 'tuple'>
(1400, 1400, 3)

而且,类似地:
print(type(trainingData))  # <class 'numpy.ndarray'>
print(trainingData.shape)  # (32,)
print(type(trainingData[0].shape))  # <class 'tuple'>
print(trainingData[0].shape)  # (350, 350, 3)

所以当我执行以下操作时:
model.fit(trainingData,
          validateData,
          epochs=5,
          verbose=2,
          batch_size=4)  # 32 images-> 8 batches of 4

我应该将.fit函数的前两个参数设置为什么?

目前情况是,我遇到了以下错误:

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (32, 1)

这是我完整代码,如果您想深入了解,请查看。 Keras API对应输入数据格式并不十分明确。
fit
fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None)
Trains the model for a given number of epochs (iterations on a dataset).

Arguments

x: Numpy array of training data (if the model has a single input), or list of Numpy arrays (if the model has multiple inputs). If input layers in the model are named, you can also pass a dictionary mapping input names to Numpy arrays.  x can be None (default) if feeding from framework-native tensors (e.g. TensorFlow data tensors).
y: Numpy array of target (label) data (if the model has a single output), or list of Numpy arrays (if the model has multiple outputs). If output layers in the model are named, you can also pass a dictionary mapping output names to Numpy arrays.  y can be None (default) if feeding from framework-native tensors (e.g. TensorFlow data tensors).

这里是我尝试使用的另一种实现的完整代码(链接)。这次,我将参数更改为Python列表np_array(每个图像都是3D np_array)。现在我遇到了这个错误:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 32 arrays: [array([[[0.6774938 , 0.64219969, 0.60690557],
        [0.67257049, 0.63743775, 0.60206295],
        [0.67203473, 0.6418085 , 0.60398018],
        ...,
        [0.55292714, 0.5253832 , 0.46217287],
  ...

很难知道我是更接近还是更遥远。

1个回答

3
fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None)

在fit函数中,x代表你的训练数据。也许更恰当的称呼应该是X_train或类似名称,而不是trainData。在这种情况下,如果您有32个形状为(350,350,3)的训练图像,则应将它们堆叠在四维(32,350,350,3)数组中。如果您的图像以对象的形式存在于数组中,您可以使用以下代码进行重塑。
X_train = np.array([x for x in trainingData])

在fit函数中,y代表你想让你的神经网络对应输入输出的值,通常是标签,而在这种情况下是更大的图像。你将其称为validateData,但更常用的名称是y_train。验证数据是不参与训练,只在每个epoch后评估模型的数据。因此,以与X_train相同的方式形状化y_train。y_train的预期形状应为(32, 1400, 1400, 3)。


应该将它们堆叠在一个四维(32,350,350,3)数组中。这正是我迷失的地方。我不明白我应该使用什么Python数据类型作为输入(np_array,或每个图像作为np_array的列表,或每个图像作为数组的列表,或图像数组作为np_array等)。 - payne
1
它应该是一个形状为(nr_of_images, image_height, image_width, color_channels)的单一四维numpy数组。你似乎有一个32个对象的numpy数组。如果reshape命令不起作用(你试过了吗?),我已经编辑了另一段代码。 - dennissv
我也意识到代码示例中有一些错别字,对此感到抱歉。现在应该已经修复了。 - dennissv
无法工作。您介意在此加入聊天吗?这样我们就不会在评论区垃圾信息了。 :) - payne
为了澄清,我想让社区知道sds解决了我的问题。我的numpy数组是由两个不同大小的图像创建的,然后我会将其分离以分别处理这两类图像。但是,由于数组的创建方式,分离会留下一个“包含numpy数组的numpy数组”的痕迹,而不是单个的“numpy数组”。在拆分后应用的代码行“X_train = np.array([x for x in trainingData])”起到了作用!他还解决了关于输入格式的问题:一个大小为“(num_imgs x W x H x D)”的numpy数组。 - payne

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接