从现有检查点创建中心模块
Create hub module from existing checkpoint
是否可以在不链接训练代码的情况下从现有检查点创建 hub
模块?
是的,绝对是。您需要一个具有 (1) 模块和 (2) 其变量中的正确值的会话。这些是来自实际训练还是仅仅恢复检查点并不重要。给定一个 Python 模型构建库,它对 TensorFlow Hub 一无所知,您可以使用一个工具导出到 Hub 模块,如下所示:
import tensorflow_hub as hub
import your_library as build_model_body
def module_fn():
inputs = tf.placeholder(...)
logits = build_model_body(inputs)
hub.add_signature(inputs=inputs, outputs=logits)
def main(_):
spec = hub.create_module_spec(module_fn)
# Supply a checkpoint trained on a model from the same Python code.
checkpoint_path = "..."
# Output will be written here:
export_path = "..."
with tf.Graph().as_default():
module = hub.Module(spec)
init_fn = tf.contrib.framework.assign_from_checkpoint_fn(
checkpoint_path, module.variable_map)
with tf.Session() as session:
init_fn(session)
module.export(export_path, session=session)
注意事项:
build_model_body()
应将输入转换为适合 Hub 模块的输出(例如,像素到特征向量),但不包括数据读取或损失和优化器。对于迁移学习,这些最好留给模块的消费者。可能需要进行一些重构。
提供 module.variable_map
是必不可少的,将 运行 build_model_body()
本身创建的普通变量名转换为通过实例化模块创建的变量名, 居住在 module/state
.
范围内
是否可以在不链接训练代码的情况下从现有检查点创建 hub
模块?
是的,绝对是。您需要一个具有 (1) 模块和 (2) 其变量中的正确值的会话。这些是来自实际训练还是仅仅恢复检查点并不重要。给定一个 Python 模型构建库,它对 TensorFlow Hub 一无所知,您可以使用一个工具导出到 Hub 模块,如下所示:
import tensorflow_hub as hub
import your_library as build_model_body
def module_fn():
inputs = tf.placeholder(...)
logits = build_model_body(inputs)
hub.add_signature(inputs=inputs, outputs=logits)
def main(_):
spec = hub.create_module_spec(module_fn)
# Supply a checkpoint trained on a model from the same Python code.
checkpoint_path = "..."
# Output will be written here:
export_path = "..."
with tf.Graph().as_default():
module = hub.Module(spec)
init_fn = tf.contrib.framework.assign_from_checkpoint_fn(
checkpoint_path, module.variable_map)
with tf.Session() as session:
init_fn(session)
module.export(export_path, session=session)
注意事项:
build_model_body()
应将输入转换为适合 Hub 模块的输出(例如,像素到特征向量),但不包括数据读取或损失和优化器。对于迁移学习,这些最好留给模块的消费者。可能需要进行一些重构。提供
module.variable_map
是必不可少的,将 运行build_model_body()
本身创建的普通变量名转换为通过实例化模块创建的变量名, 居住在module/state
. 范围内