Tensorflow,渴望执行和会话:结果不一样

时间:2018-07-25 12:14:38

标签: python tensorflow machine-learning deep-learning computer-vision

我正在使用Tensorflow,并尝试将RGB图像转换为XYZ图像。 当我使用渴望执行时,我会得到良好的结果,但是当我使用Session时,我会遇到一个错误。我花了两天时间解决这个问题,无法解决。 你能帮我吗? 这是渴望执行的代码:

import tensorflow as tf
from scipy import misc
import matplotlib.pyplot as plt
import cv2

tf.enable_eager_execution()

img = misc.imread(IMAGE_PATH)
file_raw_input = 
    tf.image.convert_image_dtype(
        img, dtype=tf.float32
)
file_raw_input.set_shape([None, None, 3])
srgb_pixels = tf.reshape(file_raw_input, [-1, 3])

linear_mask = tf.cast(
    srgb_pixels <= 0.04045,
    dtype=tf.float32
)
exponential_mask = tf.cast(
    srgb_pixels > 0.04045,
    dtype=tf.float32
)
rgb_pixels = (srgb_pixels / 12.92 * linear_mask) + \
             (((srgb_pixels + 0.055) / 1.055) ** 2.4) * exponential_mask

rgb_to_xyz = tf.constant([
    #    X        Y          Z
    [0.412453, 0.212671, 0.019334], # R
    [0.357580, 0.715160, 0.119193], # G
    [0.180423, 0.072169, 0.950227], # B
], dtype=tf.float32
)

xyz_pixels = tf.matmul(rgb_pixels, rgb_to_xyz)
results = tf.reshape(xyz_pixels, tf.shape(file_raw_input))

plt.imshow(results)
plt.show()

使用此代码,它可以工作,我得到这张照片(很好):enter image description here

当我用相同的代码夹住Session时,我得到了错误的图像。这是代码:

import tensorflow as tf
from scipy import misc
import matplotlib.pyplot as plt
import cv2

img = misc.imread(IMAGE_PATH)
file_raw_input = tf.image.convert_image_dtype(img, dtype=tf.float32)
file_raw_input.set_shape([None, None, 3])
srgb_pixels = tf.reshape(file_raw_input, [-1, 3])

linear_mask = tf.cast(
    srgb_pixels <= 0.04045,
    dtype=tf.float32
)
exponential_mask = tf.cast(
    srgb_pixels > 0.04045,
    dtype=tf.float32
)
rgb_pixels = (srgb_pixels / 12.92 * linear_mask) + \
             (((srgb_pixels + 0.055) / 1.055) ** 2.4) * exponential_mask

rgb_to_xyz = tf.constant([
    #    X        Y          Z
    [0.412453, 0.212671, 0.019334], # R
    [0.357580, 0.715160, 0.119193], # G
    [0.180423, 0.072169, 0.950227], # B
], dtype=tf.float32
)

xyz_pixels = tf.matmul(rgb_pixels, rgb_to_xyz)
results = tf.reshape(xyz_pixels, tf.shape(file_raw_input))


with tf.Session() as sess:
    enhanced_crops = sess.run(results)
    plt.imshow(enhanced_crops)
    plt.show()

使用此代码,它不起作用,我得到此图像:enter image description here

任何人都有一个主意吗?我暂时处于困境之中

非常感谢:)

0 个答案:

没有答案