RandomCrop 导致 INVALID_ARGUMENT:需要可广播的形状

问题描述 投票:0回答:2

我正在使用 Keras 训练神经网络,并尝试使用

RandomCrop
层。我正在使用动态大小的数据集(不同的分辨率),但我发现它目前不是此问题的原因。

当我运行

model.fit()
时,过了一会儿,我收到了上述错误
INVALID_ARGUMENT: required broadcastable shapes
。我能够得到我的模型的摘要,所以那里没有不匹配的情况。

当我删除该层时,我的模型工作正常,但我需要它来减小输入的大小(因此使用

RandomCrop
)。

完整回溯+张量流状态

2022-03-23 13:27:28.772937: W tensorflow/core/framework/op_kernel.cc:1733] INVALID_ARGUMENT: required broadcastable shapes

Traceback (most recent call last):
  File "c:\Users\samue\Desktop\rcrop\main.py", line 37, in <module>
    conv_model.fit(
  File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\eager\execute.py", line 54, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

Detected at node 'mean_squared_error/SquaredDifference' defined at (most recent call last):
    File "C:\Program Files\Python310\lib\threading.py", line 966, in _bootstrap
      self._bootstrap_inner()
    File "C:\Program Files\Python310\lib\threading.py", line 1009, in _bootstrap_inner
      self.run()
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\engine\training.py", line 1000, in run_step
      outputs = model.train_step(data)
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\engine\training.py", line 860, in train_step
      loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\engine\training.py", line 918, in compute_loss
      return self.compiled_loss(
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\engine\compile_utils.py", line 201, in __call__
      loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\losses.py", line 141, in __call__
      losses = call_fn(y_true, y_pred)
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\losses.py", line 245, in call
      return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\losses.py", line 1329, in mean_squared_error
      return backend.mean(tf.math.squared_difference(y_pred, y_true), axis=-1)
Node: 'mean_squared_error/SquaredDifference'
Detected at node 'mean_squared_error/SquaredDifference' defined at (most recent call last):
    File "C:\Program Files\Python310\lib\threading.py", line 966, in _bootstrap
      self._bootstrap_inner()
    File "C:\Program Files\Python310\lib\threading.py", line 1009, in _bootstrap_inner
      self.run()
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\engine\training.py", line 1000, in run_step
      outputs = model.train_step(data)
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\engine\training.py", line 860, in train_step
      loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\engine\training.py", line 918, in compute_loss
      return self.compiled_loss(
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\engine\compile_utils.py", line 201, in __call__
      loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\losses.py", line 141, in __call__
      losses = call_fn(y_true, y_pred)
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\losses.py", line 245, in call
      return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "C:\Users\samue\AppData\Roaming\Python\Python310\site-packages\keras\losses.py", line 1329, in mean_squared_error
      return backend.mean(tf.math.squared_difference(y_pred, y_true), axis=-1)
Node: 'mean_squared_error/SquaredDifference'
2 root error(s) found.
  (0) INVALID_ARGUMENT:  required broadcastable shapes
         [[{{node mean_squared_error/SquaredDifference}}]]
         [[div_no_nan/ReadVariableOp/_84]]
  (1) INVALID_ARGUMENT:  required broadcastable shapes
         [[{{node mean_squared_error/SquaredDifference}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_1308]

如何重现

我创建了一个最小的可重现示例,只有两个分辨率为

[10, 10]
的图像均保存为
.png
,具有 rgb 色彩空间。

运行 main.py 加载这些图像并尝试开始训练(因错误而失败)。

当我排除

RandomCrop
层时,它工作得很好。

文件夹结构

/main_folder
--main.py
--/data
   --001.png
   --002.png

主.py

import cv2, os
import keras
import tensorflow as tf
from keras import layers


strategy = tf.distribute.MirroredStrategy()

with strategy.scope():
    input_layer = keras.Input(shape=(None, None, 3))
    cropped = layers.RandomCrop(32, 32)(input_layer)
    out = layers.Conv2D(3, (3, 3), activation='sigmoid', padding='same')(cropped)

    conv_model = keras.Model(input_layer, out)
    conv_model.compile(
        optimizer='adam', 
        loss=tf.keras.losses.MeanSquaredError()
    )

conv_model.summary()

path = "data"
data = [cv2.imread(os.path.join(path, f)) / 255 for f in os.listdir(os.path.join(path))]


def data_generator():
    for i in range(len(data)):
        yield data[i], data[i]


dataset = tf.data.Dataset.from_generator(
    data_generator, 
    output_types=(tf.float32, tf.float32), 
    output_shapes=((None, None, 3), (None, None, 3))
).batch(1)

conv_model.fit(
    dataset,
    epochs=1,
    validation_data=dataset
)
python keras dataset keras-layer
2个回答
0
投票

所以,我想将其用于自动编码器(在示例中)。这意味着,我必须对输入图像和比较图像进行相同的裁剪。这听起来不像

RandomCrop
可以做到的事情,但由于我已经在使用自定义生成器,所以我可以在那里实现它:

def data_generator():
    for i in range(len(data)):
        # Custom function to determine the patch size
        x, x1, y, y1 = randomly_choose(data[i].shape)
        yield data[i][x: x1, y: y1], data[i][x: x1, y: y1]

这使我能够完全掌控生成过程,允许我进行图像翻转、旋转和其他更改。


0
投票

我也在做同样的事情。我正在实施分子微笑的 VAE 模型。 (GitHub:https://github.com/dhruv2103/chemvae/tree/tensorflow2)。我对此 git 进行了更改,并将其转换为在 TensorFlow 版本 2 上运行。现在,我正在尝试在此代码中实现

tf.distributed.MirroredStrategy()
,以便我可以充分利用我配置了 4 个 GPU 的设备的潜力。

你能帮我理解你是如何解决这个错误的吗?

这是错误: INVALID_ARGUMENT:不兼容的形状:[125,120,35] 与 [500,120,35]

© www.soinside.com 2019 - 2024. All rights reserved.