2017-02-17 94 views
11

我已经训练了一个DCGAN模型,现在想将它加载到一个库中,通过图像空间优化将神经元激活的驱动程序可视化。从metagraph文件导入张量流模型时配置input_map

下面的代码有效,但是在进行后续图像分析时,强制我使用(1,宽度,高度,通道)图像进行处理,这是一种痛苦(图书馆对网络输入形状的假设)。

# creating TensorFlow session and loading the model 
graph = tf.Graph() 
sess = tf.InteractiveSession(graph=graph) 

new_saver = tf.train.import_meta_graph(model_fn) 
new_saver.restore(sess, './') 

我想改变input_map,读取源后,我预计这个代码工作:

graph = tf.Graph() 
sess = tf.InteractiveSession(graph=graph) 

t_input = tf.placeholder(np.float32, name='images') # define the input tensor 
t_preprocessed = tf.expand_dims(t_input, 0) 

new_saver = tf.train.import_meta_graph(model_fn, input_map={'images': t_input}) 
new_saver.restore(sess, './') 

但得到了一个错误:

ValueError: tf.import_graph_def() requires a non-empty name if input_map is used.

当堆栈得到下来tf.import_graph_def()名称字段设置为import_scope,所以我尝试了以下内容:

graph = tf.Graph() 
sess = tf.InteractiveSession(graph=graph) 

t_input = tf.placeholder(np.float32, name='images') # define the input tensor 
t_preprocessed = tf.expand_dims(t_input, 0) 

new_saver = tf.train.import_meta_graph(model_fn, input_map={'images': t_input}, import_scope='import') 
new_saver.restore(sess, './') 

这使我获得以下KeyError

KeyError: "The name 'gradients/discriminator/minibatch/map/while/TensorArrayWrite/TensorArrayWriteV3_grad/TensorArrayReadV3/RefEnter:0' refers to a Tensor which does not exist. The operation, 'gradients/discriminator/minibatch/map/while/TensorArrayWrite/TensorArrayWriteV3_grad/TensorArrayReadV3/RefEnter', does not exist in the graph."

如果我设置“import_scope”,我得到了同样的错误是否我设置“input_map”。

我不确定该从哪里出发。

+0

如果你有一个独立的例子,我很高兴看到它。你可以在[test](https://www.github.com/tensorflow/tensorflow/blob/master/tensorflow/python/framework/meta_graph_test.py#L262)中看到如何使用'input_map'。我会尽量接近测试,看看它在哪里分歧。 – drpng

+0

@drpng在我打开的github问题中有一个完整的示例(https://github.com/tensorflow/tensorflow/issues/7634)。感谢您推荐查看测试。我会尝试为低层功能设计的方法。 – Sevenless

回答

1

在更新版本的tensorflow> = 1.2.0中,以下步骤正常工作。

t_input = tf.placeholder(np.float32, shape=[None, width, height, channels], name='new_input') # define the input tensor 

# here you need to give the name of the original model input placeholder name 
# For example if the model has input as; input_original= tf.placeholder(tf.float32, shape=(1, width, height, channels, name='original_placeholder_name')) 
new_saver = tf.train.import_meta_graph(/path/to/checkpoint_file.meta, input_map={'original_placeholder_name:0': t_input}) 
new_saver.restore(sess, '/path/to/checkpointfile') 
0

所以,主要问题是你没有使用正确的语法。请查阅tf.import_graph_def的文档以了解input_maplink)的使用。

让我们来细数这行:

new_saver = tf.train.import_meta_graph(model_fn, input_map={'images': t_input}, import_scope='import') 

你没有什么大纲是model_fn,但它需要的文件的路径。 在接下来的部分,在input_map,你说:更换原图(DCGAN),其nameimages我的变量(在当前图形)的输入称为t_input。问题在于,t_inputimages被引用以不同的方式相同的对象按这条线:

t_input = tf.placeholder(np.float32, name='images') 

换句话说,imagesinput_map实际上应该是任何变量名,你试图在DCGAN更换图形。您必须以其基本格式(即没有0​​行)导入图形,并找出要链接到的变量的名称。导入图表后,它将位于由tf.get_collection('variables')返回的列表中。查找尺寸(1,宽度,高度,通道),但用值代替变量名称。如果它是占位符,它将看起来像scope/Placeholder:0,其中scope被替换为变量的作用域。

字谨慎:

Tensorflow是关于什么的,预计图表的样子非常挑剔。因此,如果在原始图形规范中明确指定了宽度,高度和通道,那么当您尝试使用不同的维度集连接placeholder时,Tensorflow会发出抱怨(抛出错误)。而且,这是有道理的。如果系统是用一些维度进行训练的,那么它只知道如何生成这些维度的图像。

从理论上讲,你仍然可以在网络的前端粘贴各种奇怪的东西。但是,您需要将其缩小以便首先满足这些尺寸(并且Tensorflow文档表示最好使用CPU之外的CPU执行此操作;即在输入feed_dict之前)。

希望有帮助!