I am trying to implement the Neural style transfer example in pycharm. Link https://www.tensorflow.org/beta/tutorials/generative/style_transfer#setup. I installed all of the needed cuda software and the cudnn software needed to allow the use of the gpu. However I can't seem to figure out why I am still getting an error TypeError: Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn. When loading the image.
I have searched for fixes or explanations to why this problem occurs but can't seem to find a good answer. 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')
The code which are shown is directly used from the tutorial, I am expecting to see two subplots of the images. But I keep getting the following error
line 26, in load_img long_dim = max(shape)
TypeError: Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn.
Any ideas what I am doing wrong or what can be done to solve the problem?