2017-02-17 89 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之前)。

希望有幫助!