Issues in Programming Assignment: Art Generation with Neural Style Transfer

when I run the train step, the errors below. It looks something wrong with generated_image

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [82], in <cell line: 7>()
      1 ### you cannot edit this cell
      2 
      3 # You always must run the last cell before this one. You will get an error if not.
      5 generated_image = tf.Variable(generated_image)
----> 7 train_step_test(train_step, generated_image)

File /tf/W4A2/public_tests.py:86, in train_step_test(target, generated_image)
     82 def train_step_test(target, generated_image):
     83     generated_image = tf.Variable(generated_image)
---> 86     J1 = target(generated_image)
     87     print(J1)
     88     assert type(J1) == EagerTensor, f"Wrong type {type(J1)} != {EagerTensor}"

File /usr/local/lib/python3.8/dist-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File /tmp/__autograph_generated_filecuiry_cp.py:13, in outer_factory.<locals>.inner_factory.<locals>.tf__train_step(generated_image)
     11     a_G = ag__.converted_call(ag__.ld(vgg_model_outputs), (ag__.ld(generated_image),), None, fscope)
     12     J_style = ag__.converted_call(ag__.ld(compute_style_cost), (ag__.ld(a_S), ag__.ld(a_G)), None, fscope)
---> 13     J_content = ag__.converted_call(ag__.ld(compute_content_cost), (ag__.ld(a_C), ag__.ld(a_G)), None, fscope)
     14     J = ag__.converted_call(ag__.ld(total_cost), (ag__.ld(J_content), ag__.ld(J_style)), dict(alpha=10, beta=40), fscope)
     15 grad = ag__.converted_call(ag__.ld(tape).gradient, (ag__.ld(J), ag__.ld(generated_image)), None, fscope)

File /tmp/__autograph_generated_fileomdhzywl.py:14, in outer_factory.<locals>.inner_factory.<locals>.tf__compute_content_cost(content_output, generated_output)
     12 a_G = ag__.ld(generated_output)[(- 1)]
     13 (_, n_H, n_W, n_C) = ag__.converted_call(ag__.converted_call(ag__.ld(a_G).get_shape, (), None, fscope).as_list, (), None, fscope)
---> 14 a_C_unrolled = ag__.converted_call(ag__.ld(tf).reshape, (ag__.ld(content_output),), dict(shape=[(- 1), ag__.ld(n_C)]), fscope)
     15 a_G_unrolled = ag__.converted_call(ag__.ld(tf).reshape, (ag__.ld(generated_output),), dict(shape=[(- 1), ag__.ld(n_C)]), fscope)
     16 J_content = (ag__.converted_call(ag__.ld(tf).reduce_sum, (((ag__.ld(a_C_unrolled) - ag__.ld(a_G_unrolled)) ** 2),), None, fscope) / (((4 * ag__.ld(n_H)) * ag__.ld(n_W)) * ag__.ld(n_C)))

ValueError: in user code:

    File "<ipython-input-49-b6fcbd5a7ea2>", line 23, in train_step  *
        J_content = compute_content_cost(a_C, a_G)
    File "<ipython-input-12-7d786d95c734>", line 25, in compute_content_cost  *
        a_C_unrolled = tf.reshape(content_output, shape=[-1, n_C])

    ValueError: Tried to convert 'tensor' to a tensor and failed. Error: Dimension 1 in both shapes must be equal, but are 50 and 25. Shapes are [1,50,50,512] and [1,25,25,512].
    	From merging shape 3 with other shapes. for '{{node Reshape_10/packed}} = Pack[N=6, T=DT_FLOAT, axis=0](Reshape_10/tensor/values_0, Reshape_10/tensor/values_1, Reshape_10/tensor/values_2, Reshape_10/tensor/values_3, Reshape_10/tensor/values_4, Reshape_10/tensor/values_5)' with input shapes: [1,400,400,64], [1,200,200,128], [1,100,100,256], [1,50,50,512], [1,25,25,512], [1,25,25,512].

Hello @lijiyao,

The error happened at generated_image, but by the following last part of the message, it can trace back to the first exercise for compute_content_cost:

Let’s analyze this together! I will be brief but with sufficient understanding of the lab (you may have to re-read some parts of the lab), you should see what I am saying.

Step 1:

From my above screenshot, together with the definition line compute_content_cost(content_output, generated_output), we were feeding a_C as content_output into compute_content_cost.

What was a_C? That came from vgg_model_outputs (in section 5.5.1) and was a list of outputs of some middle layers of our VGG19 model. That list actually contained 6 outputs which could explain why, in the last line of your error message, it mentioned 6 shapes:

    	From merging shape 3 with other shapes. for '{{node Reshape_10/packed}} = Pack[N=6, T=DT_FLOAT, axis=0](Reshape_10/tensor/values_0, Reshape_10/tensor/values_1, Reshape_10/tensor/values_2, Reshape_10/tensor/values_3, Reshape_10/tensor/values_4, Reshape_10/tensor/values_5)' with input shapes: [1,400,400,64], [1,200,200,128], [1,100,100,256], [1,50,50,512], [1,25,25,512], [1,25,25,512]

Step 2:

We were trying to apply tf.reshape on a list of 6 different shapes of tensors to one shape which was [-1, n_C] as coded in your exercise for compute_content_cost and this is the reason of the error!

Step 3:

There is a problem and a suggested change:

  1. (the problem) we can’t apply tf.shape on 6 tensors like that. We are supposed to apply it on only one of the 6 tensors.

  2. (the suggested change) the targeting shape was not [-1, n_C].

Step 4:

To address them yourself, I would recommend you to go through the exercise 1’s description, especially on the following points:

  1. The instruction asked us to unroll a_C instead of content_output (as you did), and it was why the variable was named as a_C_unrolled. The definition for a_C had been provided to you in the code, which was a_C = content_output[-1], so reshape a_C instead (and if you would like to, think about why only take the last element of content_output.)

  2. read the “additional hints for unrolling” for how to specify the targeting shape. Your current [-1, n_C] might be corrected. I used “might be” because the exercise suggested a different setting, but your current setting would just be fine too, because, utimately, the purpose for “reshape” is to make sure a_C_unrolled and a_G_unrolled have the same shape so that we can use subtraction on them (we can’t subtract two tensors of different shapes).

Step 5.

Lastly, after making sure a_C_unrolled was computed according to the exercise’s description, did the same check for a_G_unrolled. Then remember to run the test code cell right below exercise 1 to make sure it passes, and then re-run the subsequent code cells one-by-one to make sure everything is alright, before trying exercise 5 again.

Good luck to you!

Cheers,
Raymond

1 Like

Good finding!

Thanks for helping me go through the error outputs, which confused me.

1 Like

You are welcome, @lijiyao! It is very important to be able to understand the error message :wink:

If there are other problems, feel free to open a new topic!

Cheers,
Raymond