从本地目录将图像加载到TensorFlow2.0中:ValueError:无法将NumPy数组转换为Tensor(不支持的对象类型int)

2024-04-18 14:38:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有点像TensorFlow的新手,我正在学习神经风格转换的教程。为了表示感谢,The tutorial can be found here而且,我想玩它,并尝试使用我本地驱动器的图片,我相信这就是我与之发生冲突的地方

到目前为止,我正在编写的代码是:

import os
import tensorflow as tf
# Load compressed models from tensorflow_hub
os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
import IPython.display as display

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (12,12)
mpl.rcParams['axes.grid'] = False

import numpy as np
import PIL.Image
import time
import functools

def tensor_to_image(tensor):
  tensor = tensor*255
  tensor = np.array(tensor, dtype=np.uint8)
  if np.ndim(tensor)>3:
    assert tensor.shape[0] == 1
    tensor = tensor[0]
  return PIL.Image.fromarray(tensor)

# Load the content and style images from local directory. 
# This is where I altered the code to use my images
content_path = plt.imread('Content.jpeg')
style_path = plt.imread('Style.jpg')

def load_img(path_to_img):
  max_dim = 512
  img = tf.io.read_file(path_to_img)
  img = tf.image.decode_image(img, channels=3)
  img = tf.image.convert_image_dtype(img, tf.float32)

  shape = tf.cast(tf.shape(img)[:-1], tf.float32)
  long_dim = max(shape)
  scale = max_dim / long_dim

  new_shape = tf.cast(shape * scale, tf.int32)

  img = tf.image.resize(img, new_shape)
  img = img[tf.newaxis, :]
  return img

def imshow(image, title=None):
  if len(image.shape) > 3:
    image = tf.squeeze(image, axis=0)

  plt.imshow(image)
  if title:
    plt.title(title)

content_image = load_img(content_path)
style_image = load_img(style_path)

plt.subplot(1, 2, 1)
imshow(content_image, 'Content Image')

plt.subplot(1, 2, 2)
imshow(style_image, 'Style Image')

此时我收到一条错误消息

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-45-422fe51b5fbe> in <module>
----> 1 content_image = load_img(content_path)
      2 style_image = load_img(style_path)
      3 
      4 plt.subplot(1, 2, 1)
      5 imshow(content_image, 'Content Image')

<ipython-input-43-3717b62ca1b6> in load_img(path_to_img)
      1 def load_img(path_to_img):
      2   max_dim = 512
----> 3   img = tf.io.read_file(path_to_img)
      4   img = tf.image.decode_image(img, channels=3)
      5   img = tf.image.convert_image_dtype(img, tf.float32)

~\anaconda3\envs\newenvt\lib\site-packages\tensorflow\python\ops\gen_io_ops.py in read_file(filename, name)
    550   if tld.is_eager:
    551     try:
--> 552       _result = pywrap_tfe.TFE_Py_FastPathExecute(
    553         _ctx._context_handle, tld.device_name, "ReadFile", name,
    554         tld.op_callbacks, filename)

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).

我想我现在意识到load_img(path_to_img)函数并没有与我加载的实际jpeg同步,与教程中不同的是,它只是路径。工作正常的原件是:

content_path = tf.keras.utils.get_file('YellowLabradorLooking_new.jpg', 'https://storage.googleapis.com/download.tensorflow.org/example_images/YellowLabradorLooking_new.jpg')
style_path = tf.keras.utils.get_file('kandinsky5.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg')

检查数据类型type(content_path)str

但是,我更改了从本地计算机检索文件的步骤:

content_path = plt.imread('Content.jpeg')
style_path = plt.imread('Style.jpg')

并检查数据类型type(content_path)是否为numpy.ndarray

我猜我要么需要更改加载数据的方式,要么更改函数以使其与预处理jpeg图像兼容

如果我只想从本地目录加载和使用图像,我应该怎么做?我应该改变什么

编辑

所以,我认为以我的方式加载单个图像是很好的,但它只是需要调整功能。我不知道为什么我会收到那个错误信息

# Load the content and style images
content = plt.imread('Content.jpeg')
style = plt.imread('Style.jpg')


def transform_img(raw_img):
  max_dim = 512
  #img = tf.io.read_file(path_to_img)
  img = tf.image.decode_image(raw_img, channels=3)
  img = tf.image.convert_image_dtype(img, tf.float32)

  shape = tf.cast(tf.shape(img)[:-1], tf.float32)
  long_dim = max(shape)
  scale = max_dim / long_dim

  new_shape = tf.cast(shape * scale, tf.int32)

  img = tf.image.resize(img, new_shape)
  img = img[tf.newaxis, :]
  return img
  
def imshow(image, title=None):
  if len(image.shape) > 3:
    image = tf.squeeze(image, axis=0)

  plt.imshow(image)
  if title:
    plt.title(title)

content_image = transform_img(content)
style_image = transform_img(style)

plt.subplot(1, 2, 1)
imshow(content_image, 'Content Image')

plt.subplot(1, 2, 2)
imshow(style_image, 'Style Image')

我只是想知道如何解决在上面最后一行之后发生的bellow错误

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).


Tags: topathimageimportimgtitlestyletf
1条回答
网友
1楼 · 发布于 2024-04-18 14:38:54

plt.imread以numpy数组的形式返回图像的像素数据Documentation

而load\u img中的方法tf.io.read\u file似乎需要计算机上文件的物理路径,类似于您链接的教程。这就是ValueError产生的原因read_file Doc

我的建议是使用教程中的tf.keras.utils.get_file方法,将其设置为content_path,然后尝试load_img方法get_file Doc

content_path = tf.keras.utils.get_file('[PATH TO Content.jpg]')
style_path = tf.keras.utils.get_file('[PATH TO Style.jpg]')

必须使用单独的变量处理打印。希望这有帮助!新的回答问题的堆栈,所以让我知道,如果我可以进一步帮助

相关问题 更多 >