TensorFlow多输入图像分类网络训练报‘None values not supported’错误排查
问题原因与修复方案
核心错误点
- 输入键不匹配:你的数据集里手工特征的键是
additional_features,但模型定义输入时用了pre_calc_feats,导致模型无法找到对应输入数据,触发None values not supported错误。 - Dataset返回格式错误:
fit函数要求数据集返回(输入数据字典, 标签)的结构,而不是包含标签的完整字典。如果你的Dataset直接返回{"image":..., "additional_features":..., "y":...},模型会把整个字典当作输入,无法识别标签,进而引发错误。
具体修复步骤
1. 修正模型输入的键名
把模型输入字典里的pre_calc_feats改成和数据集一致的additional_features:
model = tf.keras.Model(inputs={ "image": input_image, "additional_features" : input_features }, # 匹配数据集的键名 outputs={"y" : output_layer})
2. 调整TFRecord数据集的解析与返回格式
确保解析TFRecord时,返回(输入字典, 标签)的结构,示例代码如下:
def parse_tfrecord(example): # 定义特征解析格式 feature_description = { "image": tf.io.FixedLenFeature([], tf.string), "additional_features": tf.io.FixedLenFeature([7], tf.float32), "y": tf.io.FixedLenFeature([], tf.int64) } parsed_example = tf.io.parse_single_example(example, feature_description) # 处理图像数据(根据你的实际格式调整,比如解码、resize、归一化) image = tf.io.decode_jpeg(parsed_example["image"], channels=3) image = tf.resize(image, (image_height, image_width)) image = tf.cast(image, tf.float32) / 255.0 # 返回(输入字典, 标签)格式 return {"image": image, "additional_features": parsed_example["additional_features"]}, parsed_example["y"] # 应用解析函数到数据集 train_dataset = tf.data.TFRecordDataset("train.tfrecord") train_dataset = train_dataset.map(parse_tfrecord).batch(32) # 替换成你的batch size val_dataset = tf.data.TFRecordDataset("val.tfrecord") val_dataset = val_dataset.map(parse_tfrecord).batch(32)
3. (可选)优化模型编译的metrics配置
可以明确指定metrics对应输出,让配置更清晰:
model.compile(loss={ "y" : "sparse_categorical_crossentropy"}, metrics={"y": ['sparse_categorical_accuracy']}, optimizer=tf.keras.optimizers.Adam(learning_rate=0.001))
完成以上修改后,再运行model.fit应该就能正常训练了。
内容的提问来源于stack exchange,提问作者tobstar
相关产品推荐
相关产品推荐

