2015-11-26 14 views
13

私はcifar10 convolution exampleを私の問題に適応させています。私は、ファイルからイメージを読み込んだデザインから、既にメモリ内にあるイメージセットで動作するデザインにデータ入力を変更したいと思います。オリジナル版ではテンソルのTensorflow「マップ操作」?

read_input = cifar10_input.read_cifar10(filename_queue) 
reshaped_image = tf.cast(read_input.uint8image, tf.float32) 
# Crop the central [height, width] of the image. 
resized_image = tf.image.resize_image_with_crop_or_pad(reshaped_image, 
                width, height) 

read_input 1枚の画像を含むテンソルである:元inputs()関数は次のようになります。

私はRAMにすべての私のイメージを保持するので、代わりにfilename_queueを使用して、私はimages_tensor.shapeは(何か、32、32、3)ある一つの巨大なimages_tensor = tf.constant(images)を、持っています。

私の質問は非常に基本的です:images_tensorのすべての要素にいくつかの機能(私の場合はtf.image.resize_image_with_crop_or_pad)を適用する最良の方法は何ですか?

反復は、スライスが限定されたテンソルフロー(TensorFlow - numpy-like tensor indexing)では問題があります。 1つのコマンドでこれを達成するソリューションはありますか?

答えて

8

いくつかの回答があります。マップ機能ほどエレガントではありません。どちらが最適なのかは、メモリ効率に対するあなたの望みに少し依存します。

(a)は一度にtf.FIFOQueueとデキューとtf.image.resize_image_with_crop_or_pad画像にそれらを投げるためにenqueue_manyを使用し、バック一つの大きなsmooshにそれをすべてCONCATすることができます。これはおそらく遅いです。 N個の画像に対してN個の呼び出しが必要です。

(b)1つのプレースホルダーフィードを使用して、元のデータソースから途中でサイズを変更してトリミングすることができます。これは、おそらくメモリに格納されていないデータを保存する必要がないため、メモリの観点からは最適なオプションです。

(c)tf.control_flow_ops.Whileオペレーションを使用してフルバッチを繰り返し、結果をtf.Variableにすることができます。特にwhileで許可されている並列実行を利用すると、これが最も速いアプローチになりそうです。

メモリの使用量を最小限に抑えたい場合を除き、(オプションb)の方法でフィルタリングするほうがよいでしょう。

8

バージョン0.8では、があります。 documentationから:

map_fn(fn, elems, dtype=None, parallel_iterations=10, back_prop=True, swap_memory=False, name=None)

map on the list of tensors unpacked from elems on dimension 0.

This map operator repeatedly applies the callable fn to a sequence of elements from first to last. The elements are made of the tensors unpacked from elems . dtype is the data type of the return value of fn . Users must provide dtype if it is different from the data type of elems .

Suppose that elems is unpacked into values , a list of tensors. The shape of the result tensor is [len(values)] + fn(values[0]).shape .

Args:

fn: The callable to be performed.

elems: A tensor to be unpacked to apply fn .

dtype: (optional) The output type of fn .

parallel_iterations: (optional) The number of iterations allowed to run in parallel. back_prop: (optional) True enables back propagation. swap_memory: (optional) True enables GPU-CPU memory swapping. name: (optional) Name prefix for the returned tensors.

Returns:

A tensor that packs the results of applying fn to the list of tensors unpacked from elems , from first to last.

Raises:

TypeError: if fn is not callable.

Example:

elems = [1, 2, 3, 4, 5, 6] 
    squares = map_fn(lambda x: x * x, elems) 
    # squares == [1, 4, 9, 16, 25, 36] 
    ``` 
+0

どのバージョンのドキュメントでも 'map_fn'が見つかりません。リンク?ちょうどあなたがバージョンが0.8以降であるなら必ず ヘルプ(tf.map_fn) TFとして 輸入tensorflowからカム上記の(と私はそれを完了した) - –

+2

私はどちらかのオンライン何かを見つけることができません。 – DomJack

1

Tensorflowはhigher-order functionsのカップルを提供し、そのうちの一つはtf.map_fnです。使い方は非常に簡単です:マッパーを定義してテンソルに適用します:

X = tf.Variable(...) 
mapping = lambda x: f(x) 
res = tf.map_fn(mapping, X) 
関連する問題