简体   繁体   English

将模型另存为 H5 或 SavedModel 时出现 TensorFlow Hub 错误

[英]TensorFlow Hub error when Saving model as H5 or SavedModel

I want to use this TF Hub asset:https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/3我想使用这个 TF Hub 资产:https ://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/3

Versions:版本:

Version:  1.15.0-dev20190726
Eager mode:  False
Hub version:  0.5.0
GPU is available

Code代码

feature_extractor_url = "https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/3"
feature_extractor_layer = hub.KerasLayer(module,
                                         input_shape=(HEIGHT, WIDTH, CHANNELS))

I get:我得到:

ValueError: Importing a SavedModel with tf.saved_model.load requires a 'tags=' argument if there is more than one MetaGraph. Got 'tags=None', but there are 2 MetaGraphs in the SavedModel with tag sets [[], ['train']]. Pass a 'tags=' argument to load this SavedModel.

I tried:我试过:

module = hub.Module("https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/3",
                    tags={"train"})
feature_extractor_layer = hub.KerasLayer(module, 
                                         input_shape=(HEIGHT, WIDTH, CHANNELS))

But when I try to save the model I get:但是当我尝试保存模型时,我得到:

tf.keras.experimental.export_saved_model(model, tf_model_path)
# model.save(h5_model_path) # Same error 

NotImplementedError: Can only generate a valid config for `hub.KerasLayer(handle, ...)`that uses a string `handle`.
Got `type(handle)`: <class 'tensorflow_hub.module.Module'>

Tutorial here教程在这里

It's been a while, but assuming you have migrated to the TF2, this can easily be accomplished with the most recent model version as follows:已经有一段时间了,但假设您已迁移到 TF2,这可以使用最新的模型版本轻松完成,如下所示:

import tensorflow as tf
import tensorflow_hub as hub

num_classes=10 # For example
m = tf.keras.Sequential([
    hub.KerasLayer("https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/5", trainable=True)
    tf.keras.layers.Dense(num_classes, activation='softmax')
])
m.build([None, 224, 224, 3])  # Batch input shape.

# train as needed

m.save("/some/output/path")

Please update this question if that doesn't work for you.如果这对您不起作用,请更新此问题。 I believe your issue arose from mixing hub.Module with hub.KerasLayer .我相信您的问题是由hub.Modulehub.KerasLayer混合hub.KerasLayer The model version you were using was in TF1 Hub format, so within TF1 it is meant to be used exclusively with hub.Module , and not mixed with hub.KerasLayer .您使用的模型版本是 TF1 Hub 格式,因此在 TF1 中,它旨在专门与hub.Module一起使用,而不是与hub.KerasLayer混合使用。 Within TF2, hub.KerasLayer can load TF1 Hub format models directly from their URL for composition in larger models, but they cannot be fine-tuned.在 TF2 中, hub.KerasLayer可以直接从它们的 URL 加载 TF1 Hub 格式的模型以组合在更大的模型中,但它们不能被微调。

Please refer to this compatibility guide for more information请参阅此兼容性指南以获取更多信息

您应该使用tf.keras.models.save_model(model,'NeuralNetworkModel')您将在一个文件夹中获得保存的模型,该文件夹可以稍后在您的顺序网络中使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tensorflow keras model 保存为h5文件时出现类型错误 - Type Error when saving Tensorflow keras model as h5 file 加载 Tensorflow keras Model (.h5) 时出错 - Error loading Tensorflow keras Model (.h5) 通过相对路径保存 h5 模型 - Keras tensorflow - Saving h5 model by relative path - Keras tensorflow Convert a Tensorflow model in SavedModel format (.pb file) saved with tf.saved_model.save to a Keras model (.h5 file) - Convert a Tensorflow model in SavedModel format (.pb file) saved with tf.saved_model.save to a Keras model (.h5 file) 运行 yad2k 以生成模型 h5 文件时,“tensorflow”在 tensorflow 2.3 中没有属性“space_to_depth”错误 - 'tensorflow' has no attribute 'space_to_depth' error with tensorflow 2.3 when running yad2k to generate model h5 file 加载在线托管的 TensorFlow (.h5) 模型 - Load a TensorFlow (.h5) model hosted online tensorflow_hub 在保存 keras model 时返回 NotImplementedError - tensorflow_hub returns NotImplementedError when saving a keras model 将 Tensorflow Keras model(编码器 - 解码器)保存为 SavedModel 格式 - Saving a Tensorflow Keras model (Encoder - Decoder) to SavedModel format 将数据保存到h5 - Saving data to h5 保存 keras 模型时出错:“RuntimeError:不匹配 ReplicaContext。”,“ValueError:跟踪 SavedModel 的渐变时出错。” - Error saving keras model: "RuntimeError: Mismatching ReplicaContext.", "ValueError: Error when tracing gradients for SavedModel."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM