计算一个“镜像”,其中图像的前半部分被复制、翻转 (l-r),然后使用 tensorflow 复制到后半部分

Compute a “mirror”, where the first half of the image is copied, flipped (l-r) and then copied into the second half using tensorflow

我想使用 tensorflow 解决这个问题,但我在网上搜索并发现 git issue#206 指出从 numpy 数组初始化的张量变量仍然不支持索引和切片。

不然我早就...

image = mpimg.imread(filename)
height, width, depth = image.shape
x = tf.Variable(image, name='x')
model = tf.initialize_all_variables()

with tf.Session() as session:
    session.run(model)
    result = session.run(x[::1,:width*0.5,::1]) #this step is not possible

我该用什么??

您必须使用 tf.slicetf.reverse,然后连接结果。

image = tf.placeholder(tf.float32, [height, width, depth])

half_left = tf.slice(image, [0, 0, 0], [height, width/2, depth])
half_right = tf.reverse(half_left, [False, True, False])

res = tf.concat(1, [half_left, half_right])

该代码也适用于变量。

我也在 Tensor Flow 教程中遇到过那个问题,这里是将实现您想要的内容的完整代码:

import numpy as np
import tensorflow as tf
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import os
# First, load the image again
dir_path = os.path.dirname(os.path.realpath(__file__))
filename = dir_path + "/MarshOrchid.jpg"
image = mpimg.imread(filename)
height, width, depth = image.shape

# Create a TensorFlow Variable
x = tf.Variable(image, name='x')


model = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(model)
    left_part = tf.slice(x, [0, 0, 0], [height, width/2, depth]) #Extract Left part
    rigth_part = tf.slice(x, [0, width/2, 0], [height, width/2, depth]) #Extract Right part
    left_part = tf.reverse_sequence(left_part, np.ones((height,)) * width/2, 1, batch_dim=0) #Reverse Left Part
    rigth_part = tf.reverse_sequence(left_part, np.ones((height,)) * width/2, 1, batch_dim=0) #Reverse Right Part
    x = tf.concat([left_part, rigth_part],1) #Concat them together along the second edge (the width)

    result = session.run(x)


print(result.shape)
plt.imshow(result)
plt.show()

这些代码是this tutorial.

最后一个练习的答案