python - TensorFlow/Keras:为什么在调用 fit 时会出现“ValueError:从 float32 到 uint8 的不兼容转换”?
问题描述
我使用带有急切执行的 TensorFlow 1.12。当我打电话
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
,因此从rgb
返回sample_fetcher()
的张量是类型的张量float
。但是,在调用该Dataset.from_generator()
方法时,您将第一个生成元素的输出类型指定为tf.uint8
(ie output_types=(tf.uint8, tf.float32)
)。因此,需要进行实际上无法进行的转换。将其更改为tf.float32
(ie output_types=(tf.float32, tf.float32)
) 用于训练和验证生成器,问题将得到解决。
推荐阅读
- python - ```警告:pip 配置了需要 TLS/SSL 的位置,但是 Python 中的 ssl 模块不可用```
- python - “alpha”和“start_alpha”之间的区别
- excel-formula - 在 exceI 表中,我想要每个“PC NAME”的“Total”列中前两个值(max-second max)的差异,是否有任何 excel 公式?
- python - 使用 Beautiful Soup 抓取后,IMDb 的重定向超链接不起作用
- python - discord.py bot 不能在同一个 def 中使用 message 和 ctx
- scala - 解释 Haskell 中的柯里化实现
- android - 将 Android Ionic App 更新为 Android Native 版本后的数据迁移
- python - 如何允许用户根据用户输入在程序中存储多个 CSV 文件?- Python
- python - 当 np.nan 设置为默认值时,为什么 np.select 返回 'nan' 作为字符串而不是 np.nan?
- node.js - 未能部署我的 Firebase Api FIREBASE_CONFIG 和 GCLOUD_PROJECT 环境变量丢失