TensorFlow/Keras: Why do I get "ValueError: Incompatible conversion from float32 to uint8" when calling fit?

TensorFlow/Keras: Why do I get "ValueError: Incompatible conversion from float32 to uint8" when calling fit?

我使用 TensorFlow 1.12 和 eager execution。当我打电话时

model.fit(train, steps_per_epoch=int(np.ceil(num_train_samples / BATCH_SIZE)), epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, validation_data=val, validation_steps=int(np.ceil(num_val_samples / BATCH_SIZE)))

我收到以下错误:

ValueError: Incompatible type conversion requested to type 'uint8' for variable of type 'float32'

据我所知,我不会在任何地方从 uint8 转换为 float32,至少没有明确地转换。

我的数据集生成如下:

train = tf.data.Dataset.from_generator(generator=train_sample_fetcher, output_types=(tf.uint8, tf.float32))
train = train.repeat()
train = train.batch(BATCH_SIZE)
train = train.shuffle(10)
val = tf.data.Dataset.from_generator(generator=val_sample_fetcher, output_types=(tf.uint8, tf.float32))

使用以下生成器函数:

def train_sample_fetcher():
    return sample_fetcher()

def val_sample_fetcher():
    return sample_fetcher(is_validations=True)

def sample_fetcher(is_validations=False):
    sample_names = [filename[:-4] for filename in os.listdir(DIR_DATASET + "ndarrays/")]
    if not is_validations: sample_names = sample_names[:int(len(sample_names) * TRAIN_VAL_SPLIT)]
    else: sample_names = sample_names[int(len(sample_names) * TRAIN_VAL_SPLIT):]
    for sample_name in sample_names:
        rgb = tf.image.decode_jpeg(tf.read_file(DIR_DATASET + sample_name + ".jpg"))
        rgb = tf.image.resize_images(rgb, (HEIGHT, WIDTH))
        #d = tf.image.decode_jpeg(tf.read_file(DIR_DATASET + "depth/" + sample_name + ".jpg"))
        #d = tf.image.resize_images(d, (HEIGHT, WIDTH))
        #rgbd = tf.concat([rgb,d], axis=2)
        onehots = tf.convert_to_tensor(np.load(DIR_DATASET + "ndarrays/" + sample_name + ".npy"), dtype=tf.float32)
        yield rgb, onehots

---------------------------------------------------------------------------------

作为参考,完整的堆栈跟踪:

Traceback (most recent call last):
  File "tensorflow/python/ops/gen_nn_ops.py", line 976, in conv2d
    "data_format", data_format, "dilations", dilations)
tensorflow.python.eager.core._FallbackException: Expecting int64_t value for attr strides, got numpy.int32

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "dir/to/my_script.py", line 100, in <module>
    history = model.fit(train, steps_per_epoch=int(np.ceil(num_train_samples / BATCH_SIZE)), epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, validation_data=val, validation_steps=int(np.ceil(num_val_samples / BATCH_SIZE)))
  File "/tensorflow/python/keras/engine/training.py", line 1614, in fit
    validation_steps=validation_steps)
  File "/tensorflow/python/keras/engine/training_eager.py", line 705, in fit_loop
    batch_size=batch_size)
  File "/tensorflow/python/keras/engine/training_eager.py", line 251, in iterator_fit_loop
    model, x, y, sample_weights=sample_weights, training=True)
  File "/tensorflow/python/keras/engine/training_eager.py", line 511, in _process_single_batch
    training=training)
  File "/tensorflow/python/keras/engine/training_eager.py", line 90, in _model_loss
    outs, masks = model._call_and_compute_mask(inputs, **kwargs)
  File "/tensorflow/python/keras/engine/network.py", line 856, in _call_and_compute_mask
    mask=masks)
  File "/tensorflow/python/keras/engine/network.py", line 1029, in _run_internal_graph
    computed_tensor, **kwargs)
  File "/tensorflow/python/keras/engine/network.py", line 856, in _call_and_compute_mask
    mask=masks)
  File "/tensorflow/python/keras/engine/network.py", line 1031, in _run_internal_graph
    output_tensors = layer.call(computed_tensor, **kwargs)
  File "/tensorflow/python/keras/layers/convolutional.py", line 194, in call
    outputs = self._convolution_op(inputs, self.kernel)
  File "/tensorflow/python/ops/nn_ops.py", line 868, in __call__
    return self.conv_op(inp, filter)
  File "/tensorflow/python/ops/nn_ops.py", line 520, in __call__
    return self.call(inp, filter)
  File "/tensorflow/python/ops/nn_ops.py", line 204, in __call__
    name=self.name)
  File "/tensorflow/python/ops/gen_nn_ops.py", line 982, in conv2d
    name=name, ctx=_ctx)
  File "/tensorflow/python/ops/gen_nn_ops.py", line 1015, in conv2d_eager_fallback
    _attr_T, _inputs_T = _execute.args_to_matching_eager([input, filter], _ctx)
  File "/tensorflow/python/eager/execute.py", line 195, in args_to_matching_eager
    ret = [internal_convert_to_tensor(t, dtype, ctx=ctx) for t in l]
  File "/tensorflow/python/eager/execute.py", line 195, in <listcomp>
    ret = [internal_convert_to_tensor(t, dtype, ctx=ctx) for t in l]
  File "/tensorflow/python/framework/ops.py", line 1146, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/tensorflow/python/ops/variables.py", line 828, in _TensorConversionFunction
    "of type '%s'" % (dtype.name, v.dtype.name))
ValueError: Incompatible type conversion requested to type 'uint8' for variable of type 'float32'

第一个抛出的错误是关于 NumPy ndarrays,但我在导入它们后立即将它们转换为 TensorFlow 张量。非常感谢任何建议!我检查了任何 np.int32 类型,但找不到任何类型。

tf.image.resize_images 的输出是 float 类型的张量,因此从 sample_fetcher() 返回的 rgb 张量是 float 类型的张量。但是,当调用 Dataset.from_generator() 方法时,您将第一个生成的元素的输出类型指定为 tf.uint8(即 output_types=(tf.uint8, tf.float32))。因此,需要进行实际上无法完成的转​​换。对于训练和验证生成器,将其更改为 tf.float32(即 output_types=(tf.float32, tf.float32)),问题将得到解决。