【问题标题】:Compute a “mirror”, where the first half of the image is copied, flipped (l-r) and then copied into the second half using tensorflow计算一个“镜像”,其中图像的前半部分被复制、翻转(l-r),然后使用 tensorflow 复制到后半部分
【发布时间】:2016-10-17 14:31:17
【问题描述】:

我想使用 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

我该用什么??

【问题讨论】:

  • 您是想在 Tensorflow 图中创建一个节点来生成镜像,还是只想将结果保存在一个 numpy 数组中? (例如绘制它)
  • 一个节点会很棒

标签: python numpy indexing tensorflow


【解决方案1】:

您必须使用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])

代码也适用于变量。

【讨论】:

    【解决方案2】:

    我在 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.最后一个练习的解法

    【讨论】:

    • 如果这是一个新问题,请点击 按钮提问。如果有助于提供上下文,请包含指向此问题的链接。如果这是一个答案,请说得更清楚。
    • 来自审核队列:我可以请求您在您的答案周围添加更多上下文。仅代码的答案很难理解。如果您可以在帖子中添加更多信息,它将帮助提问者和未来的读者。另见Explaining entirely code-based answers
    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2016-09-17
    相关资源
    最近更新 更多