Tensorflow - 图像的动态切片
Posted
技术标签:
【中文标题】Tensorflow - 图像的动态切片【英文标题】:Tensorflow - Dynamic Slicing of Images 【发布时间】:2017-05-26 16:39:43 【问题描述】:我之前曾问过这个问题,但在对该问题进行了一些调查后,我似乎只是走错了路,以实现我想要实现的目标。
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.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
【讨论】:
这看起来正是我需要的!谢谢!顺便说一句,看起来以“img = tf.placeholder(”开头的行最后被切断了。我很确定我知道它应该如何结束,但我想我会让你编辑它以防万一我'我错了。 哦。确实如此。我现在已经更正了。谢谢!以上是关于Tensorflow - 图像的动态切片的主要内容,如果未能解决你的问题,请参考以下文章