了解 tensorflow 中 tf.slice 的参数以及为啥我不能更改它

Posted

技术标签:

【中文标题】了解 tensorflow 中 tf.slice 的参数以及为啥我不能更改它【英文标题】:Understand parameters of tf.slice in tensorflow and why I cannot change it了解 tensorflow 中 tf.slice 的参数以及为什么我不能更改它 【发布时间】:2019-03-19 06:36:30 【问题描述】:

下面是我的代码:

import matplotlib.pyplot as plt
import matplotlib.image as mp_image
filename = "abc.jpeg"
input_image = mp_image.imread(filename)
my_image=tf.placeholder("uint8",[None, None, 3])
myimage=tf.placeholder("uint8",[None, None, 3])
slice1=tf.slice(my_image,[0,100,0],[300,400,-1]) #[x,y,?],[x,y,?]
with tf.Session() as sess:
    result = sess.run(slice1,feed_dict=my_image: input_image)
print(result.shape)
plt.imshow(result)
plt.show()

在切片 1 中,作为列表传递的参数表示 [x,y,?],[x,y,?]。

在 tf.slice(image_tensor,[0,0,0],[100,200,-1]) 中。这里的 0 和 -1 代表什么,为什么我不能更改它们?

【问题讨论】:

【参考方案1】:

查看tf.slice的文档字符串,参数分别为input_beginsize。你的代码在做

slice1=tf.slice(my_image,[x_begin,y_begin,channel_begin],[x_size,y_size,channel_size])

请注意,第三个参数描述的是size,而不是绝对的to 索引(@XMANX 是错误的)。 size 参数接受标记值 -1,这意味着维度中的所有剩余元素都包含在切片中。

例如,如果您有一个张量 t,形状为 [X, Y, Z]

tf.slice(t, [x_begin, y_begin, z_begin], [x_size, y_size, z_size])

相当于做

t[x_begin : x_begin+x_size, y_begin : y_begin+y_size, z_begin : z_begin+z_size]

为了仅从图像中提取 R 通道,您可以执行以下操作:

import matplotlib.pyplot as plt
import matplotlib.image as mp_image

filename = "abc.jpeg"
input_image = mp_image.imread(filename)
my_image=tf.placeholder("uint8",[None, None, 3])
# Doesn't slice along the x and y dimensions, but takes only one channel
sliced = tf.slice(my_image,[0, 0, 0], [-1, -1, 1])
squeezed = tf.squeeze(slice)  # Removes last dimension
with tf.Session() as sess:
    result = sess.run(squeezed,feed_dict=my_image: input_image)
plt.imshow(result)
plt.show()

【讨论】:

我在 'plt.imshow(result)'||**TypeError: Invalid dimensions for image data**||... 上给出了一个错误,它真的可以只打印 R 通道吗?请向我提供仅打印一个频道的完整代码 sn-p。 @rachelim 当你遇到这样的错误时,你应该通过阅读它的文档字符串来检查plt.imshow 期望的形状输入......如果你这样做,你会看到它期望输入具有形状 (n, m) 或 (n, m, 3) 或 (n, m, 4)。在这种情况下,您试图给它一个 (n, m, 1) 图像。您可以通过执行 tf.squeeze 删除最后一个维度(以创建 (n,m) 图像)。我已经编辑了上面的代码 sn-p 以反映这一点。【参考方案2】:

[x,y,?],[x,y,?] 在你的情况下,形状的第三个参数是图像通道数

对于第二个问题的答案,让我们看看tf.slice 是如何工作的,如果图像具有 RGB 通道,它看起来像 tf.slice([from_x, from_y, from_channel], [to_x, to_y, to_channel]) 在形状定义中你可以使用 -1 这样你告诉传感器流量切片到最大可用值。从您的代码示例中,您尝试切片输入图像[0,100, 0],[300,400, 3],您可以更改第三个参数,它是一个有效的代码,但应该记住matplotlib 只允许您显示图片(M, N), (M, N, 3), (M, N, 4)

代码说明:

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import matplotlib.image as mp_image

filename = "abc.png"
input_image = mp_image.imread(filename)
my_image = tf.placeholder("float32",[None, None, 3])
myimage = tf.placeholder("float32",[None, None, 3])

# this way you slicing RGB image
slice1=tf.slice(my_image,[0,100,0],[300, 400, -1])

with tf.Session() as sess:
    result = sess.run(slice1, feed_dict=my_image: input_image)

plt.subplot(1, 2, 1)
plt.imshow(result)

# this way you slicing image and keep just R channel
slice1=tf.slice(my_image,[0,100,0],[300, 400, 1]) #[x,y,?],[x,y,?]

with tf.Session() as sess:
    result = sess.run(slice1, feed_dict=my_image: input_image)

plt.subplot(1, 2, 2)
# matplotlib imshow show need extra options to render image with single chanel
plt.imshow(np.reshape(result, (result.shape[0], result.shape[1])), cmap='gray')
plt.show()

result image

【讨论】:

可以更改from_channel和to_channel的值吗?如果是,它的可能值是多少?它会在输出中产生什么差异? 你可以改变这个值,例如tf.slice([0, 0, 0], [-1, -1, 1])切片输入图像,只保留Rchanel。 感谢您的努力,但我仍然无法使用您的代码获取图像的 R 通道。我应该使用像散景这样的不同库吗?我是张量流的新手。请向我提供完整的代码 sn-p,它将显示任何 1 个频道。

以上是关于了解 tensorflow 中 tf.slice 的参数以及为啥我不能更改它的主要内容,如果未能解决你的问题,请参考以下文章

Tensorflow API 学习-tf.slice()

在 Tensorflow 中删除张量的维度

像在 numpy 中一样使用 tf.slice 检测越界切片

tf.slice()函数详解(极详细)

tensorrt不支持:tf.unpack,tf.slice,tf.tile,tf.expand_dims,tf.fill,tf.cast,tf.floor_div,tf.range

利用Tensorflow读取二进制CIFAR-10数据集