title: Tensorflow error. TypeError: Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn
body: <p>I am trying to run this on Amazon Sagemaker but I am getting this error while when I try to run it on my local machine, it works very fine.</p>
  <p>this is my code:</p>
  <pre><code>import tensorflow as tf
  
  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)&gt;3:
      assert tensor.shape[0] == 1
      tensor = tensor[0]
    return PIL.Image.fromarray(tensor)
  
  content_path = tf.keras.utils.get_file('YellowLabradorLooking_nw4.jpg', 'https://example.com/IMG_20200216_163015.jpg')
  
  
  style_path = tf.keras.utils.get_file('kandinsky3.jpg','https://example.com/download+(2).png')
  
  
  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) &gt; 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')
  
  import tensorflow_hub as hub
  hub_module = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/1')
  stylized_image = hub_module(tf.constant(content_image), tf.constant(style_image))[0]
  tensor_to_image(stylized_image)
  
  
  file_name = 'stylized-image5.png'
  tensor_to_image(stylized_image).save(file_name)
  </code></pre>
  <p>This is the exact error I get:</p>
  <pre><code>---------------------------------------------------------------------------
  </code></pre>
  <p>TypeError                                 Traceback (most recent call last)</p>
  <pre><code>&lt;ipython-input-24-c47a4db4880c&gt; in &lt;module&gt;()
       53 
       54 
  ---&gt; 55 content_image = load_img(content_path)
       56 style_image = load_img(style_path)
       57 
  </code></pre>
  <p> in load_img(path_to_img)</p>
  <pre><code>     34 
       35     shape = tf.cast(tf.shape(img)[:-1], tf.float32)
  ---&gt; 36     long_dim = max(shape)
       37     scale = max_dim / long_dim
       38 
  </code></pre>
  <p>~/anaconda3/envs/amazonei_tensorflow_p36/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in <strong>iter</strong>(self)</p>
  <pre><code>    475     if not context.executing_eagerly():
      476       raise TypeError(
  --&gt; 477           &quot;Tensor objects are only iterable when eager execution is &quot;
      478           &quot;enabled. To iterate over this tensor use tf.map_fn.&quot;)
      479     shape = self._shape_tuple()
  </code></pre>
  <p>TypeError: Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn.</p>