2017-01-11 10 views
0

私はこれまでにこの質問をしていましたが、問題の調査の結果、私が達成しようとしているものに対して間違った道を辿った可能性があります。Tensorflow - イメージの動的スライシング

Dynamic image cropping in Tensorflow

私は多分これがしようとするより良いパスかもしれないと思いました。しかし、私が把握できない部分は、スライス操作のsizeパラメータのために置くべきものです。基本的に、私が達成しようとしているのは、画像をトリミングしてトリミングする方法を動的に決定し、そのトリミングされた画像を計算グラフで続ける能力を持つことです。これが非効率的な方法であると思われる場合は、気軽に代替案を提供してください。

import numpy as np 
import tensorflow as tf 

img1 = np.random.random([400, 600, 3]) 
img2 = np.random.random([400, 600, 3]) 
img3 = np.random.random([400, 600, 3]) 

images = [img1, img2, img3] 

img1_crop = [100, 100, 100, 100] 
img2_crop = [200, 150, 100, 100] 
img3_crop = [150, 200, 100, 100] 

crop_values = [img1_crop, img2_crop, img3_crop] 

x = tf.placeholder(tf.float32, shape=[None, 400, 600, 3]) 
i = tf.placeholder(tf.int32, shape=[None, 4]) 
y = tf.slice(x, i, size="Not sure what to put here") 

# initialize 
init = tf.global_variables_initializer() 
sess = tf.Session() 
sess.run(init) 

# run 
result = sess.run(y, feed_dict={x: images, i: crop_values}) 
print(result) 

答えて

1

の代わりに(あなたがバッチを操作することはできません)tf.sliceを使用して、私はtf.image.extract_glimpseを使用することをお勧めします。

import tensorflow as tf 
import numpy as np 

NUM_IMAGES = 2 
NUM_CHANNELS = 1 
CROP_SIZE = [3, 4] 
IMG_HEIGHT=10 
IMG_WIDTH=10 

# Fake input data, but ordered so we can look at the printed values and 
# map them back. The values of the first image are: 
# array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
#  [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],  
#  [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],  
#  [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],  
#  [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],  
#  [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],  
#  [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],  
#  [70, 71, 72, 73, 74, 75, 76, 77, 78, 79],  
#  [80, 81, 82, 83, 84, 85, 86, 87, 88, 89],  
#  [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])  
image1 = np.reshape(
    np.array(xrange(NUM_IMAGES * IMG_HEIGHT * IMG_WIDTH * NUM_CHANNELS)), 
    [NUM_IMAGES, IMG_HEIGHT, IMG_WIDTH, NUM_CHANNELS]) 

# We use normalized=False to use pixel indexing. 
# normalized=True means centers are specified between [0,1). 
image1_center = [0, 0] # The center of the crop is ~ the center of the image. 
image2_center = [3, 5] # Offset down & right in the image. 

img = tf.placeholder(tf.float32, shape=[NUM_IMAGES, IMG_HEIGHT, IMG_WIDTH, NUM_CHANNELS], name="img") 
size = tf.placeholder(tf.int32, shape=[2], name="crop_size") 
centers = tf.placeholder(tf.float32, shape=[NUM_IMAGES, 2], name="centers") 
output = tf.image.extract_glimpse(img, size, centers, normalized=False) 

sess = tf.Session() 
feed_dict = { 
    img: image1, 
    size: CROP_SIZE, 
    centers: [image1_center, image2_center], 
} 
print sess.run(output, feed_dict=feed_dict) 

あなたは複数のサイズ(画像あたりの偶数倍見え隠れ)を抽出したい場合は、tf.image.crop_and_resizeをチェックアウト:ここではバッチで動作おもちゃのサンプルプログラムです。

文書:https://www.tensorflow.org/api_docs/python/image/cropping#extract_glimpse

+0

これはまさに私が必要としているようです。ありがとう!ところで、 "img = tf.placeholder("で始まる行のように見えますが、最後に切り捨てられていましたが、どうすれば終了するのか分かりましたが、 – Beaker

+0

D'oh。確かにそれだった。私は今修正した。ありがとう! – saeta

関連する問題