Tensorflow 中 model.fit 的 InvalidArgumentError
InvalidArgumentError with model.fit in Tensorflow
使用 CNN 进行图像分类。当 model.fit()
被调用时,它开始训练模型一段时间,并在执行过程中被中断并且 returns 一条错误消息。
错误信息如下
InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: Input size should match (header_size + row_size * abs_height) but they differ by 2
[[{{node decode_image/DecodeImage}}]]
[[IteratorGetNext]]
[[IteratorGetNext/_4]]
(1) Invalid argument: Input size should match (header_size + row_size * abs_height) but they differ by 2
[[{{node decode_image/DecodeImage}}]]
[[IteratorGetNext]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_8873]
Function call stack:
train_function -> train_function
更新:我的建议是检查数据集的元数据。它帮助解决了我的问题。
您没有指定参数 label_mode
。为了使用 SparseCategoricalCrossentropy
作为损失函数,您需要将其设置为 int
。
如果您不指定它,那么它将设置为 None
as per the documentation.
您还需要根据您从中读取图像的目录结构将参数labels
指定为inferred
。
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
labels="inferred",
label_mode="int",
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
labels="inferred",
label_mode="int",
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
使用 CNN 进行图像分类。当 model.fit()
被调用时,它开始训练模型一段时间,并在执行过程中被中断并且 returns 一条错误消息。
错误信息如下
InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: Input size should match (header_size + row_size * abs_height) but they differ by 2
[[{{node decode_image/DecodeImage}}]]
[[IteratorGetNext]]
[[IteratorGetNext/_4]]
(1) Invalid argument: Input size should match (header_size + row_size * abs_height) but they differ by 2
[[{{node decode_image/DecodeImage}}]]
[[IteratorGetNext]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_8873]
Function call stack:
train_function -> train_function
更新:我的建议是检查数据集的元数据。它帮助解决了我的问题。
您没有指定参数 label_mode
。为了使用 SparseCategoricalCrossentropy
作为损失函数,您需要将其设置为 int
。
如果您不指定它,那么它将设置为 None
as per the documentation.
您还需要根据您从中读取图像的目录结构将参数labels
指定为inferred
。
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
labels="inferred",
label_mode="int",
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
labels="inferred",
label_mode="int",
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)