2016-11-10 4 views
2

最後から2番目のレイヤーから事前に訓練されたネットワークで画像から特徴ベクトルを抽出します。Tensorflow:最後から2番目のレイヤーから特徴ベクトルを抽出できない

私が走ったとき:その後、

feature = sess.graph.get_tensor_by_name('global_pool:0') 

それ:私はとの最後の行を交換する場合、一方

KeyError: "The name 'fc7:0' refers to a Tensor which does not exist. The operation, 'fc7', does not exist in the graph." 

from neural_network import NET 


x = tf.placeholder(tf.float32, shape=[1, 144, 144, 3]) 

net = NET({'data': x}) 
sess = tf.InteractiveSession() 
sess.run(tf.initialize_all_variables()) 
net.load('inference.npy', sess) 

feature = sess.graph.get_tensor_by_name('fc7:0') 

を私はエラーメッセージを受信働く

私neural_network.pyファイルの最後には、次のとおりです。

(self.feed('inception_3b_pool', 
       'inception_3b_3x3', 
       'inception_3b_double_3x3_2') 
     .concat(3, name='inception_3b_output') 
     .avg_pool(9, 4, 1, 1, padding='VALID', name='global_pool') 
     .fc(256, relu=False, name='fc7')) 

とFC層の定義は次のとおりです。

def fc(self, input, num_out, name, relu=True): 
    with tf.variable_scope(name) as scope: 
     input_shape = input.get_shape() 
     if input_shape.ndims == 4: 
      # The input is spatial. Vectorize it first. 
      dim = 1 
      for d in input_shape[1:].as_list(): 
       dim *= d 
      feed_in = tf.reshape(input, [-1, dim]) 
     else: 
      feed_in, dim = (input, input_shape[-1].value) 
     weights = self.make_var('weights', shape=[dim, num_out]) 
     biases = self.make_var('biases', [num_out]) 
     op = tf.nn.relu_layer if relu else tf.nn.xw_plus_b 
     fc = op(feed_in, weights, biases, name=scope.name) 
     return fc 

答えて

0

通常FC層の出力はBiasAddノードと出力テンソル名ですfc7/BiasAdd:0のようなものです。 fcメソッドの実装によって異なりますが、

テンソルボードにグラフを読み込んで出力ノード名を調べるのは理にかなっていると思います。

+0

FC7/BiasAdd:0が動作していない、私はtensorboard – Mostafa

+0

しようとしている私は、私は – Mostafa

+1

がどのように見える特徴を抽出したいからFC層の定義を追加 –

0

も参照働くグラフのすべてのテンソル名をリストする

[n.name for n in tf.get_default_graph().as_graph_def().node] 

を行います。 sess.graph.get_tensor_by_name(<tensor_operation_name>:<output_index>")を使用して、必要なテンソルの出力を印刷します。

例:「FC7/FC7:0」

関連する問題