【问题标题】:Neural style transfer using python iterable error?使用python可迭代错误的神经风格转移?
【发布时间】:2019-08-11 10:34:15
【问题描述】:

我正在尝试在 pycharm 中实现神经风格迁移示例。链接https://www.tensorflow.org/beta/tutorials/generative/style_transfer#setup。我安装了所有需要的 cuda 软件和允许使用 gpu 所需的 cudnn 软件。但是,我似乎无法弄清楚为什么我仍然收到错误 TypeError: Tensor objects are only iterable when eager execution is enabled。要迭代此张量,请使用 tf.map_fn。加载图像时。

我已经搜索了解决此问题的原因或解释,但似乎找不到一个好的答案。 Tensor` objects are not iterable when eager execution is not enabled. To iterate over this tensor use `tf.map_fn` https://intellipaat.com/community/6771/tensor-objects-are-not-iterable-when-eager-execution-is-not-enabled-to-iterate-over-this-tensor-use-tf-mapfn

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
import IPython.display as display
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import time
import functools


mpl.rcParams['figure.figsize'] = (12,12)
mpl.rcParams['axes.grid'] = False

content_path = tf.keras.utils.get_file('turtle.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/Green_Sea_Turtle_grazing_seagrass.jpg')
style_path = tf.keras.utils.get_file('kandinsky.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/Vassily_Kandinsky%2C_1913_-_Composition_7.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')

显示的代码直接来自教程,我希望看到图像的两个子图。但我不断收到以下错误

第 26 行,在 load_img 中 long_dim = max(shape)

TypeError:张量对象仅在启用急切执行时才可迭代。要迭代此张量,请使用 tf.map_fn。

任何想法我做错了什么或可以做些什么来解决问题?

【问题讨论】:

  • 你安装了哪个版本的TensorFlow?该教程适用于 TensorFlow 2.0 beta。
  • 好像我使用的是 1.14.0 版本 :( 。我现在正在安装 tensorflow-gpu==2.0.0-beta1... 我将发布结果,感谢您的快速响应!

标签: python tensorflow pycharm


【解决方案1】:

感谢AKX,问题可以解决,只是版本的问题。请按照教程的建议使用 TensorFlow 2.0 Beta!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 2014-03-25
    • 2023-01-27
    • 1970-01-01
    • 2020-02-28
    • 2020-02-10
    相关资源
    最近更新 更多