Keras“适合”输入不清楚

pay*_*yne 5 neural-network python-3.x keras tensorflow

我正在尝试将尺寸图像(350x350x3)作为输入形状提供,并且我想训练网络输出(1400x1400x3)图像(4 倍放大)。

我的训练数据集由 8 张图像组成1400x1400x3,我翻转这些图像以获得总共 32 张图像进行验证。

然后,我将这 32 张图像缩小以350x350x3获得输入图像,这些图像将与其他 32 张图像进行交叉验证。

print(type(validateData))
print(validateData.shape)
print(type(validateData[0].shape))
print(validateData[0].shape)
Run Code Online (Sandbox Code Playgroud)

返回

<class 'numpy.ndarray'>
(32,)
<class 'tuple'>
(1400, 1400, 3)
Run Code Online (Sandbox Code Playgroud)

而且,类似地:

print(type(trainingData))  # <class 'numpy.ndarray'>
print(trainingData.shape)  # (32,)
print(type(trainingData[0].shape))  # <class 'tuple'>
print(trainingData[0].shape)  # (350, 350, 3)
Run Code Online (Sandbox Code Playgroud)

所以当我做

model.fit(trainingData,
          validateData,
          epochs=5,
          verbose=2,
          batch_size=4)  # 32 images-> 8 batches of 4
Run Code Online (Sandbox Code Playgroud)

我到底应该提供什么作为.fit函数的两个第一个参数?

照原样,我收到此错误:

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

如果您想查看它,这是我的完整代码

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).
Run Code Online (Sandbox Code Playgroud)

这是我尝试使用的另一个实现的完整代码。这一次,我将参数更改为 np_arrays 的 python 列表(每个图像都是 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],
  ...
Run Code Online (Sandbox Code Playgroud)

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

den*_*ssv 4

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)
Run Code Online (Sandbox Code Playgroud)

在拟合函数中,x 是您的训练数据。也许它更适合称为 X_train 或类似的名称,而不是 trainData。在这种情况下,如果您有 32 个形状为 (350, 350, 3) 的训练图像,它们应该堆叠在一个四维 (32, 350, 350, 3) 数组中。如果您有一组图像作为对象,就像您似乎拥有的那样,您可以使用以下代码重塑

X_train = np.array([x for x in trainingData])
Run Code Online (Sandbox Code Playgroud)

那么拟合函数中的 y 是您希望网络为相应输入输出的值,通常是标签,在本例中是较大的图像。您将其称为 validateData,但更常见的名称是更接近 y_train 的名称。验证数据是在训练期间不使用的数据,仅在每个时期后评估模型时使用。因此,以与 X_train 相同的方式塑造 y_train 的形状。y_train 的预期形状为 (32, 1400, 1400, 3)。