TensorFlow:如何从 TFHub 模块制作推理图?

TensorFlow: how to make an inference graph from a TFHub module?

我在目录 path 中有一个保存为 TFHub 模块的 VAE 模型。该模型可以从以下网址下载 https://storage.googleapis.com/disentanglement_lib/unsupervised_study_v1/0.zip。我可以使用 hub.Module:

加载它
import tensorflow_hub as hub
module = hub.Module(path)

机型相关信息:

module.variables
[<tf.Variable 'module_1/decoder/conv2d_transpose/bias:0' shape=(64,) dtype=float32>,
<tf.Variable 'module_1/decoder/conv2d_transpose/kernel:0' shape=(4, 4, 64, 64) dtype=float32>,
<tf.Variable 'module_1/decoder/conv2d_transpose_1/bias:0' shape=(32,) dtype=float32>,
<tf.Variable 'module_1/decoder/conv2d_transpose_1/kernel:0' shape=(4, 4, 32, 64) dtype=float32>,
<tf.Variable 'module_1/decoder/conv2d_transpose_2/bias:0' shape=(32,) dtype=float32>,
<tf.Variable 'module_1/decoder/conv2d_transpose_2/kernel:0' shape=(4, 4, 32, 32) dtype=float32>,
<tf.Variable 'module_1/decoder/conv2d_transpose_3/bias:0' shape=(1,) dtype=float32>,
<tf.Variable 'module_1/decoder/conv2d_transpose_3/kernel:0' shape=(4, 4, 1, 32) dtype=float32>,
<tf.Variable 'module_1/decoder/dense/bias:0' shape=(256,) dtype=float32>,
<tf.Variable 'module_1/decoder/dense/kernel:0' shape=(10, 256) dtype=float32>,
<tf.Variable 'module_1/decoder/dense_1/bias:0' shape=(1024,) dtype=float32>,
<tf.Variable 'module_1/decoder/dense_1/kernel:0' shape=(256, 1024) dtype=float32>,
<tf.Variable 'module_1/encoder/e1/bias:0' shape=(32,) dtype=float32>,
<tf.Variable 'module_1/encoder/e1/kernel:0' shape=(4, 4, 1, 32) dtype=float32>,
<tf.Variable 'module_1/encoder/e2/bias:0' shape=(32,) dtype=float32>,
<tf.Variable 'module_1/encoder/e2/kernel:0' shape=(4, 4, 32, 32) dtype=float32>,
<tf.Variable 'module_1/encoder/e3/bias:0' shape=(64,) dtype=float32>,
<tf.Variable 'module_1/encoder/e3/kernel:0' shape=(2, 2, 32, 64) dtype=float32>,
<tf.Variable 'module_1/encoder/e4/bias:0' shape=(64,) dtype=float32>,
<tf.Variable 'module_1/encoder/e4/kernel:0' shape=(2, 2, 64, 64) dtype=float32>,
<tf.Variable 'module_1/encoder/e5/bias:0' shape=(256,) dtype=float32>,
<tf.Variable 'module_1/encoder/e5/kernel:0' shape=(1024, 256) dtype=float32>,
<tf.Variable 'module_1/encoder/log_var/bias:0' shape=(10,) dtype=float32>,
<tf.Variable 'module_1/encoder/log_var/kernel:0' shape=(256, 10) dtype=float32>,
<tf.Variable 'module_1/encoder/means/bias:0' shape=(10,) dtype=float32>,
<tf.Variable 'module_1/encoder/means/kernel:0' shape=(256, 10) dtype=float32>]
module.get_input_info_dict(signature='gaussian_encoder') 
module.get_input_info_dict(signature='decoder') 
module.get_input_info_dict(signature='reconstructions')
{'images': <hub.ParsedTensorInfo shape=(?, 64, 64, 1) dtype=float32 is_sparse=False>}
{'latent_vectors': <hub.ParsedTensorInfo shape=(?, 10) dtype=float32 is_sparse=False>}
{'images': <hub.ParsedTensorInfo shape=(?, 64, 64, 1) dtype=float32 is_sparse=False>}
module.get_output_info_dict(signature='gaussian_encoder') 
module.get_output_info_dict(signature='decoder') 
module.get_output_info_dict(signature='reconstructions')
{'mean': <hub.ParsedTensorInfo shape=(?, 10) dtype=float32 is_sparse=False>, 'logvar': <hub.ParsedTensorInfo shape=(?, 10) dtype=float32 is_sparse=False>}
{'images': <hub.ParsedTensorInfo shape=(?, 64, 64, 1) dtype=float32 is_sparse=False>}
{'images': <hub.ParsedTensorInfo shape=(?, 64, 64, 1) dtype=float32 is_sparse=False>}

构建推理图:

此 VAE 是在 dsprites 上训练的。假设我得到了大小为 2 的 dsprites 的子集,因此我有一个维度数据集:

dataset.shape
(2, 64, 64, 1)

其中数据集是一个 numpy 数组。

  1. 如何使用模块将数据集中的输入图像编码为 meanlogvar
  2. 如何使用模块从数据集中重建输入图像,即编码和解码?

相关信息来源:

How to use TensorFlow Hub with code examples

此 link 提供必要的文档 www.tensorflow.org/hub/common_signatures/images。以下代码进行编码:

import tensorflow as tf
import tensorflow_hub as hub 
path = '../0/model/tfhub'

module = hub.Module(path)
outputs = module(dict(images=dataset), signature='gaussian_encoder',as_dict=True)
logvar = outputs["logvar"]
mean = outputs["mean"]

sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run([logvar, mean])

重构可以类推