我将图像转换为张量以进行神经样式转换时出现图像没有形状的值错误 [英] I got value error that image has no shape while converting image to tensor for performing neural style transfer

查看:47
本文介绍了我将图像转换为张量以进行神经样式转换时出现图像没有形状的值错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tensorflow_hub模块执行神经样式转换,并且出现错误'图像'不包含形状".我不明白我在哪里弄错了.

I am using tensorflow_hub modules to perform neural style transfer and I get the error "'images' contains no shape". I don't understand where I made a mistake.

这是我的代码:

import tensorflow_hub as hub
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

content_path = r'C:\Users\Sriram\Desktop\efil.jpg'
style_path = r'C:\Users\Sriram\Desktop\download1.jfif'
content_image = plt.imread(content_path)
style_image = plt.imread(style_path)

plt.subplot(1, 2, 1)
plt.title('Content Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.title('Style Image')
plt.axis('off')

def image_to_tensor(path_to_img):
    img = tf.io.read_file(path_to_img)
    img = tf.image.decode_image(img, channels=3, dtype=tf.float32)
    
    # Resize the image to specific dimensions
    img = tf.image.resize(img, [720, 512])
    img = img[tf.newaxis, :]
    return img

def tensor_to_image(tensor):
    tensor = tensor*255
    tensor = np.array(tensor, dtype=np.uint8)
    tensor = tensor[0]
    plt.figure(figsize=(20,10))
    plt.axis('off')
    return plt.imshow(tensor)

content_image_tensor = image_to_tensor(content_path)
style_image_tensor = image_to_tensor(style_path)
hub_module = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
combined_result = hub_module(tf.constant(content_image_tensor), tf.constant(style_image_tensor))[0]
result=tensor_to_image(combined_result)

这是错误:

runfile('C:/Users/Sriram/.spyder-py3/temp.py', wdir='C:/Users/Sriram/.spyder-py3')
Traceback (most recent call last):

  File "C:\Users\Sriram\.spyder-py3\temp.py", line 30, in <module>
    content_image_tensor = image_to_tensor(content_path)

  File "C:\Users\Sriram\.spyder-py3\temp.py", line 20, in image_to_tensor
    img = tf.image.resize(img, [720, 512])

  File "C:\Users\Sriram\anaconda3\lib\site-packages\tensorflow\python\ops\image_ops_impl.py", line 1182, in resize_images
    skip_resize_if_same=True)

  File "C:\Users\Sriram\anaconda3\lib\site-packages\tensorflow\python\ops\image_ops_impl.py", line 1029, in _resize_images_common
    raise ValueError('\'images\' contains no shape.')

ValueError: 'images' contains no shape

推荐答案

您的代码在TensorFlow 2.2上运行良好,所以我假设您遇到的是

Your code runs fine on TensorFlow 2.2, so I'm assuming you're hitting a somewhat known issue with decode_image on TensorFlow 1.x: When "generically" decoding an image, the shape is not returned - which makes the call to resize() fail. There's multiple ways to go about it.

如果您事先知道图像的大小,则可以使用

If you know the size of of your image(s) beforehand, you can force a size onto the Tensor using the set_shape(shape) method (and then resize it):

img = tf.io.read_file(path_to_img)
img = tf.image.decode_image(img, channels=3, dtype=tf.float32)
    
# Workaround
img.set_shape([width, height, 3])

img = tf.image.resize(img, [720, 512])
img = img[tf.newaxis, :]

使用已知的图像格式

如果您知道所有图像都是JPEG,则可以改用 decode_jpeg ,这会更好一些:

img = tf.io.read_file(path_to_img)

# Workaround
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.convert_image_dtype(img, np.float32)

img = tf.image.resize(img, [720, 512])
img = img[tf.newaxis, :]

通过裁剪和填充使用调整大小

或者,您可以尝试使用 tf.image.resize_with_crop_or_pad 显然可以解决该问题:

img = tf.io.read_file(path_to_img)
img = tf.image.decode_image(img, channels=3, dtype=tf.float32)
    
# Workaround
img = tf.image.resize_image_with_crop_or_pad(img, 720, 512)

img = img[tf.newaxis, :]


但有一个警告:代码假定所有操作都急切执行,这是TensorFlow 2.x上的默认设置,而不是TensorFlow 1.x上的默认设置.结果, tensor_to_image(tensor)函数将失败,因为提供的Tensor无法简单地转换为NumPy数组.要解决此问题,您可以在开始时启用急切执行通过运行


There's one caveat though: The code assumes that all operations are executed eagerly, which is the default on TensorFlow 2.x, but not on TensorFlow 1.x. As a result of that, the tensor_to_image(tensor) function will fail because the provided Tensor cannot be simply converted into an NumPy array. To fix this, you can enable eager execution at the start of your script by running

tf.compat.v1.enable_eager_execution()


附带说明-您可以使用 tf.image.convert_image_dtype() tf.squeeze() 将您的图片转换回去:


On a side note - you can make use of tf.image.convert_image_dtype() and tf.squeeze() to convert your image back:

def tensor_to_image(tensor):
    tensor = tf.image.convert_image_dtype(tensor, np.uint8)
    tensor = tf.squeeze(tensor)

    plt.figure(figsize=(20,10))
    plt.axis('off')
    return plt.imshow(tensor)

这将确保所有值正确饱和(例如,在转换为 np.float32 时,您将没有 0..1 之外的值)并获取摆脱了有点神奇的 [0] 索引.

This will make sure that all values properly saturate (you won't have values outside 0..1 when converting to np.float32, for example) and gets rid of the somewhat magic [0] indexing.

这篇关于我将图像转换为张量以进行神经样式转换时出现图像没有形状的值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆