Tensorflow - 图像的动态切片

Tensorflow - Dynamic Slicing of Images

我之前问过这个问题,但在对该问题进行一些调查之后,我似乎只是走错了我想要实现的目标的道路。

我认为这可能是一个更好的尝试途径。但我无法弄清楚的部分是我应该为切片操作的大小参数添加什么。从根本上说,我想要实现的是能够动态决定如何裁剪图像,然后裁剪它,然后在我的计算图中继续处理那些裁剪后的图像。如果这似乎是解决此问题的低效方法,请随时提供替代方案。

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)

我建议使用 tf.image.extract_glimpse,而不是使用 tf.slice(它不允许您对批处理进行操作)。下面是一个批量运行的玩具示例程序:

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