2017-01-11 6 views
0

이전에이 질문을했지만 문제에 대한 조사가 끝난 후에 내가 성취하려고 시도한 것의 잘못된 경로로 넘어갔습니다. Tensorflow - 이미지의 동적 슬라이스

Dynamic image cropping in Tensorflow

나는 어쩌면이 시도하는 더 나은 경로 될 줄 알았는데. 그러나 내가 알 수없는 부분은 슬라이스 작업에서 size 매개 변수로 지정해야하는 부분입니다. 근본적으로, 내가 달성하려는 것은 이미지를 잘라내어 자르는 방법을 동적으로 결정한 다음 계산 된 그래프에서 자른 이미지를 계속 사용하는 것입니다. 이것이 비효율적 인 방법이라면 자유롭게 대안을 제시 할 수 있습니다. 대신에 (당신이 배치에서 작동하지 않는) tf.slice를 사용

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.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

이것은 내가 필요한 것 같습니다. 감사! BTW, 그것은 "img = tf.placeholder ("로 시작하는 줄처럼 보입니다. 끝 부분에서 잘 렸습니다. 어떻게 끝내야하는지 알았지 만, 편집 할 수있게 해줄 것이라고 생각했습니다. 틀렸어. – Beaker

+0

D' oh. 정말로 그랬다. 나는 그것을 바로 고쳤다. 고마워! – saeta