2016-06-24 12 views
0

私のデータが構造内に配置されている「無効なタイプのリスト」を返します。 basepathを入力として受け入れる入力パイプラインを作成しようとしており、image_oplabel_opを構築しようとしています。これらの評価は、例えば、私に(画像、ラベル)タプルを与えるだろう。:評価tf.py_funcは()

image, label = session.run([image_op, label_op]) 

を、私はその画像のパスを見なければならない与えられた画像のラベルを取得するには。一つの簡単な解決策は行うことです。

label = int("good" in path) 

は、私は(V0.9)を意識していtensorflowで、このような文字列操作のための任意のサポートはありませんので、私はのような簡単な関数の上にtf.py_funcラッパーを使用することを考えました上記。しかし、同じ方法でラベルを評価する際に、画像パスopとラベルopの両方を評価するときに、画像パスopを同じsession.run()の入力として使用すると、エラーが発生します。私はすべてが順調である

label = session.run(data_reading_graph.label) 

を実行し、予想通り、私はラベルを得れば今

def get_label(path): 
    return int("good" in str(path)) 

class DataReadingGraph: 
    """ 
    Graph for reading images into a data queue. 
    """ 

    def __init__(self, base_path): 
     """ 
     Construct the queue 
     :param base_path: path to base directory that contains good images and bad images 
     subdirectories. They in turn can contain further subdirectories. 
     :return: 
     """ 

     # tf can't handle recursive files matching (as of version 0.9), so 
     # solve that with glob and just pass globbed paths to a constant 
     pattern = os.path.join(base_path, "**/*.jpg") 
     filenames = tf.constant(glob.glob(pattern, recursive=True)) 

     filename_queue = tf.train.string_input_producer(filenames, shuffle=True) 
     reader = tf.IdentityReader() 
     self.key, self.value = reader.read(filename_queue) 

     self.label = tf.py_func(get_label, [self.key], [tf.int64]) 

は、ここに私のPythonの関数とTFグラフコードです。しかし、私が代わりに

key, label = session.run([data_reading_graph.key, data_reading_graph.label]) 

を実行する場合、私は

<class 'TypeError'> 
Fetch argument [<tf.Tensor 'PyFunc:0' shape=<unknown> dtype=int64>] of [<tf.Tensor 'PyFunc:0' shape=<unknown> dtype=int64>] has invalid type <class 'list'>, must be a string or Tensor. (Can not convert a list into a Tensor or Operation.) 

を取得し、私は本当に「それはいけないのにリストに変換してしまったものを、ここでは何が悪かったのかを理解し、なぜ私はできないんキーopとラベルopを同じsession.run()で評価する。

私は、TFのグラフを開始する前に、純粋なPythonコード内のラベル抽出を行うことを試みることができますが、問題が残っている - なぜ私はpy_funcとのリストを返すtf.py_func同じsession.run()

答えて

1

でその入力を評価することはできませんテンソル

あなただけself.label = self.label[0]を取得する必要があります:

filename_queue = tf.train.string_input_producer(filenames, shuffle=True) 
    reader = tf.IdentityReader() 
    self.key, self.value = reader.read(filename_queue) 

    self.label = tf.py_func(get_label, [self.key], [tf.int64])[0] 
+0

まあ、あなたは全く正しいです。私は今、とても愚かな気がする...ありがとう。 – Puchatek

+0

最初は、それが 'tf.py_func'の本当のバグだと思っていましたが、 –