TensorFlow:如何在不包括零的张量中找到minimum/maximum个分段坐标?
TensorFlow: How to find minimum/maximum coordinates of segmentations in a tensor excluding zeros?
为了计算 Intersection over Union (IoU),我想在由 float32 3D 张量表示的分割图像 image_pred
中找到最小值和最大值(边界像素)的坐标。特别是,我的目标是找到图像中对象的左上角和右下角坐标。图像完全由黑色像素(值 0.0)组成,除了对象所在的位置,我有彩色像素(0.0 < 值 < 1.0)。这是这样一个边界框的示例(在我的例子中,对象是交通标志,环境被涂黑):
到目前为止,我的方法是 tf.boolean_mask
将每个像素设置为 False,除了颜色像素:
zeros = tf.zeros_like(image_pred)
mask = tf.greater(image_pred, zeros)
boolean_mask_pred = tf.boolean_mask(image_pred, mask)
然后使用tf.where
求蒙版图像的坐标。为了确定矩形的左上角和右下角的水平和垂直坐标值,我想到了使用tf.recude_max
和tf.reduce_min
,但由于这些确实如果我提供 axis
,则不是 return 单个值,我不确定这是否是要使用的正确函数。根据文档,如果我不指定 axis
,该函数将减少所有维度,这也不是我想要的。哪个是执行此操作的正确功能?最后的IoU是一个单一的一维浮点值。
coordinates_pred = tf.where(boolean_mask_pred)
x21 = tf.reduce_min(coordinates_pred, axis=1)
y21 = tf.reduce_min(coordinates_pred, axis=0)
x22 = tf.reduce_max(coordinates_pred, axis=1)
y22 = tf.reduce_max(coordinates_pred, axis=0)
您只需不使用 tf.boolean_mask
。首先,我定制了一张类似的图片。
import numpy as np
from matplotlib import pyplot as plt
image = np.zeros(shape=(256,256))
np.random.seed(0)
image[12:76,78:142] = np.random.random_sample(size=(64,64))
plt.imshow(image)
plt.show()
然后通过tensorflow获取其最大值和最小值的坐标
import tensorflow as tf
image_pred = tf.placeholder(shape=(256,256),dtype=tf.float32)
zeros = tf.zeros_like(image_pred)
mask = tf.greater(image_pred, zeros)
coordinates_pred = tf.where(mask)
xy_min = tf.reduce_min(coordinates_pred, axis=0)
xy_max = tf.reduce_max(coordinates_pred, axis=0)
with tf.Session() as sess:
print(sess.run(xy_min,feed_dict={image_pred:image}))
print(sess.run(xy_max,feed_dict={image_pred:image}))
[12 78]
[ 75 141]
为了计算 Intersection over Union (IoU),我想在由 float32 3D 张量表示的分割图像 image_pred
中找到最小值和最大值(边界像素)的坐标。特别是,我的目标是找到图像中对象的左上角和右下角坐标。图像完全由黑色像素(值 0.0)组成,除了对象所在的位置,我有彩色像素(0.0 < 值 < 1.0)。这是这样一个边界框的示例(在我的例子中,对象是交通标志,环境被涂黑):
到目前为止,我的方法是 tf.boolean_mask
将每个像素设置为 False,除了颜色像素:
zeros = tf.zeros_like(image_pred)
mask = tf.greater(image_pred, zeros)
boolean_mask_pred = tf.boolean_mask(image_pred, mask)
然后使用tf.where
求蒙版图像的坐标。为了确定矩形的左上角和右下角的水平和垂直坐标值,我想到了使用tf.recude_max
和tf.reduce_min
,但由于这些确实如果我提供 axis
,则不是 return 单个值,我不确定这是否是要使用的正确函数。根据文档,如果我不指定 axis
,该函数将减少所有维度,这也不是我想要的。哪个是执行此操作的正确功能?最后的IoU是一个单一的一维浮点值。
coordinates_pred = tf.where(boolean_mask_pred)
x21 = tf.reduce_min(coordinates_pred, axis=1)
y21 = tf.reduce_min(coordinates_pred, axis=0)
x22 = tf.reduce_max(coordinates_pred, axis=1)
y22 = tf.reduce_max(coordinates_pred, axis=0)
您只需不使用 tf.boolean_mask
。首先,我定制了一张类似的图片。
import numpy as np
from matplotlib import pyplot as plt
image = np.zeros(shape=(256,256))
np.random.seed(0)
image[12:76,78:142] = np.random.random_sample(size=(64,64))
plt.imshow(image)
plt.show()
然后通过tensorflow获取其最大值和最小值的坐标
import tensorflow as tf
image_pred = tf.placeholder(shape=(256,256),dtype=tf.float32)
zeros = tf.zeros_like(image_pred)
mask = tf.greater(image_pred, zeros)
coordinates_pred = tf.where(mask)
xy_min = tf.reduce_min(coordinates_pred, axis=0)
xy_max = tf.reduce_max(coordinates_pred, axis=0)
with tf.Session() as sess:
print(sess.run(xy_min,feed_dict={image_pred:image}))
print(sess.run(xy_max,feed_dict={image_pred:image}))
[12 78]
[ 75 141]