gpt4 book ai didi

image - Tensorflow 中的动态图像裁剪

转载 作者:行者123 更新时间:2023-12-01 10:05:16 26 4
gpt4 key购买 nike

我正在尝试弄清楚如何截取在 Tensorflow 中动态确定的图像。下面是我正在尝试完成的示例,但我似乎无法使其工作。本质上,我想在图表中提供图像和该图像的裁剪值,然后继续对这些裁剪部分进行其他计算。我目前的尝试:

import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np

sess = tf.InteractiveSession()

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]

def crop_image(img, crop):
tf.image.crop_to_bounding_box(img,
crop[0],
crop[1],
crop[2],
crop[3])


image_placeholder = tf.placeholder("float", [None, 400, 600, 3])
crop_placeholder = tf.placeholder(dtype=tf.int32)
sess.run(tf.global_variables_initializer())

cropped_image = tf.map_fn(lambda img, crop: crop_image(img, crop), elems=[image_placeholder, crop_placeholder])
result = sess.run(cropped_image, feed_dict={image_placeholder: images, crop_placeholder:crop_values})

plt.imshow(result)
plt.show()
/Users/p111/anaconda/bin/python /Users/p111/PycharmProjects/analysis_code/testing.pyTraceback (most recent call last):  File "/Users/p111/PycharmProjects/analysis_code/testing.py", line 31, in     cropped_image = tf.map_fn(lambda img, crop: crop_image(img, crop), elems=[image_placeholder, crop_placeholder])  File "/Users/p111/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/functional_ops.py", line 390, in map_fn    swap_memory=swap_memory)  File "/Users/p111/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2636, in while_loop    result = context.BuildLoop(cond, body, loop_vars, shape_invariants)  File "/Users/p111/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2469, in BuildLoop    pred, body, original_loop_vars, loop_vars, shape_invariants)  File "/Users/p111/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2419, in _BuildLoop    body_result = body(*packed_vars_for_body)  File "/Users/p111/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/functional_ops.py", line 380, in compute    packed_fn_values = fn(packed_values)TypeError: () missing 1 required positional argument: 'crop'

编辑:似乎 elems 只接受一个张量。这意味着我需要以某种方式将我的两个张量合并为一个,然后将其解压到我的函数中以获取值。我不确定我将如何执行这种张量操作。我已经找到了 glimpse 方法并且确实有效,但是我想知道是否可以使用这种特定方法来完成同样的工作。大多数情况下,我想知道如何合并然后拆分一对张量,以便在这种方法中使用它。

最佳答案

我从 here 看到这段代码.

elems = (np.array([1, 2, 3]), np.array([-1, 1, -1]))
alternate = map_fn(lambda x: x[0] * x[1], elems, dtype=tf.int64)
# alternate == [-1, 2, -3]

可以使用元组或列表将多个元素打包成一个元素,所以我尝试了这个。

import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np

sess = tf.InteractiveSession()

img1 = np.random.random([400, 600, 3])
img2 = np.random.random([400, 600, 3])
img3 = np.random.random([400, 600, 3])
images = np.array([img1, img2, img3])
# images = tf.convert_to_tensor(images) # it can be uncommented.

img1_crop = [100, 100, 100, 100]
img2_crop = [200, 150, 100, 100]
img3_crop = [150, 200, 100, 100]
crop_values = np.array([img1_crop, img2_crop, img3_crop])
# crop_values = tf.convert_to_tensor(crop_values) # it can be uncommented.

def crop_image(img, crop):
return tf.image.crop_to_bounding_box(img,
crop[0],
crop[1],
crop[2],
crop[3])

fn = lambda x: crop_image(x[0], x[1])
elems = (images, crop_values)

cropped_image = tf.map_fn(fn, elems=elems, dtype=tf.float64)
result = sess.run(cropped_image)

print result.shape

plt.imshow(result[0])
plt.show()

它可以在我的机器上使用 tf 版本 0.11 和 python2 运行。希望对您有所帮助。

关于image - Tensorflow 中的动态图像裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41576944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com