gpt4 book ai didi

python - TensorFlow 中图像分类的输入/输出形状错误

转载 作者:太空宇宙 更新时间:2023-11-03 19:45:50 26 4
gpt4 key购买 nike

我收到以下错误:

ValueError:检查输入时出错:预期 flatten_input 有 4 个维度,但得到的数组形状为 (80, 80, 3)

Traceback (most recent call last):
File "/home/ubuntu/capstone/TFcaps.py", line 163, in <module>
validation_steps=total_val // batch_size
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py", line 819, in fit
use_multiprocessing=use_multiprocessing)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_v2.py", line 235, in fit
use_multiprocessing=use_multiprocessing)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_v2.py", line 593, in _process_training_inputs
use_multiprocessing=use_multiprocessing)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_v2.py", line 706, in _process_inputs
use_multiprocessing=use_multiprocessing)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/data_adapter.py", line 702, in __init__
x = standardize_function(x)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_v2.py", line 684, in standardize_function
return dataset.map(map_fn, num_parallel_calls=dataset_ops.AUTOTUNE)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 1591, in map
self, map_func, num_parallel_calls, preserve_cardinality=True)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3926, in __init__
use_legacy_function=use_legacy_function)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3147, in __init__
self._function = wrapper_fn._get_concrete_function_internal()
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 2395, in _get_concrete_function_internal
*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 2389, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 2703, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 2593, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/func_graph.py", line 978, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3140, in wrapper_fn
ret = _wrapper_helper(*args)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3082, in _wrapper_helper
ret = autograph.tf_convert(func, ag_ctx)(*nested_args)
File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/autograph/impl/api.py", line 237, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in converted code:

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_v2.py:677 map_fn
batch_size=None)
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py:2410 _standardize_tensors
exception_prefix='input')
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_utils.py:573 standardize_input_data
'with shape ' + str(data_shape))

ValueError: Error when checking input: expected flatten_input to have 4 dimensions, but got array with shape (80, 80, 3)

我的模型如下所示:

model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(80, 80, 3)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(4)])

这些是我的图像形状:

Image shape:  (80, 80, 3)
Label: [False False True False]
<TakeDataset shapes: ((80, 80, 3), (4,)), types: (tf.float32, tf.bool)>

这就是我编译模型的方式:

model.compile(optimizer=tf.keras.optimizers.Adam(lr=LR), loss=tf.keras.losses.CategoricalCrossentropy(), metrics=["accuracy"])

这就是我的搭配:

history = model.fit(
train_data,
steps_per_epoch=total_train // batch_size,
epochs=epochs,
validation_data=val_data,
validation_steps=total_val // batch_size
)

如何解决这个问题?

最佳答案

在执行model.fit()之前,将3维训练输入图像train_data[0]转换为4维尺寸如下:

train_data[0] = np.reshape(train_data[0], ((-1, 80, 80, 3)))

同样,将训练输入标签 train_data[1] 也转换为二维,如下所示:

train_data[1] = np.reshape(train_data[1], ((-1, 4)))

还需要对验证数据val_data的输入图像和标签执行类似的操作。

PS:上述解决方案假设 train_data[0]train_data[1] 可用于索引输入训练图像和训练标签分别。如果不是,请修改索引/切片以访问这些元素,并根据需要将它们 reshape 为 4 维(或 2 维)。另外,我注意到 bool 值被用来表示 True/False 目标值。然而,为了训练模型,需要这些标签的数字表示。因此,您可能需要在训练之前使用 Keras 的 to_categorical() ( link here ) 将 bool 标签转换为 one-hot 编码表示。

希望这有帮助! :)

关于python - TensorFlow 中图像分类的输入/输出形状错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60143510/

26 4 0
文章推荐: python - 为什么我的 A*A Transpose 和 A Transpose*A 的特征值不匹配?
文章推荐: c# - 以编程方式运行 T4
文章推荐: python - fileinput.input 的单元测试
文章推荐: css - 为什么 CSS 类声明不适用于标记的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com