【发布时间】: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