Art Generation programming assignment exercise 3

Well, I was hoping to get through the rest of course 4 without needing help, but . . . .

Here is my output for this exercise. The result is wrong. Any hints? I am using a similar set of tf functions to the ones I used in exercise 1 to compute the cost, with of course the different divisor. I am assuming the double summation in the equation is equivalent to using tf.reduced_sum.

nc =  3
dims =  (3, 16) (3, 16)
gram dims =  (3, 3) (3, 3)
result =  tf.Tensor(0.0, shape=(), dtype=float32)
nc =  3
dims =  (3, 16) (3, 16)
gram dims =  (3, 3) (3, 3)
result =  tf.Tensor(2.9225328, shape=(), dtype=float32)

The error message is the following:
AssertionError                            Traceback (most recent call last)
Input In [24], in <cell line: 3>()
      1 ### you cannot edit this cell
----> 3 compute_layer_style_cost_test(compute_layer_style_cost)

File /tf/W4A2/public_tests.py:57, in compute_layer_style_cost_test(target)
     55 assert np.isclose(J_style_layer_GG, 0.0), "Wrong value. compute_layer_style_cost(A, A) must be 0"
     56 assert J_style_layer_SG > 0, "Wrong value. compute_layer_style_cost(A, B) must be greater than 0 if A != B"
---> 57 assert np.isclose(J_style_layer_SG, 14.01649), "Wrong value."
     59 print("J_style_layer = " + str(J_style_layer_SG))
     60 print("\033[92mAll tests passed")

AssertionError: Wrong value.

That 2.9xxxx cost value is what you get if you do the “reshape” operations incorrectly. The point is that you can’t just reshape directly to the output shape you want, because that scrambles the data. You must do it in a way that preserves the channel dimension. Here’s a thread which discusses that further and shows why the direct reshape makes a mess of the data. Read at least from that point forward in the thread or you might want to go back further to get the full context.

OK, got it - that thread was very illuminating. Thanks as always! My linear algebra skills (such as they ever were) are slowly reawakening.

2 Likes

That’s good news! The other conclusion from that thread I linked is that you’re far from the first person to encounter that issue. It’s a legitimate question that we saw a slightly different version of when we first encountered the “flattening” operation for images in DLS C1 W2.

Sorry to bother you again, but on exercise 6, it seems that it is only requiring us to utilize the previous functions we created with the given a_C and a_S and an a_G generated by the function vgg_model_outputs on the current generated_image (input to the function). I must be missing something, as i am getting the following error:

NotImplementedError                       Traceback (most recent call last)
Input In [36], 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_file3rptaqep.py:12, in outer_factory.<locals>.inner_factory.<locals>.tf__train_step(generated_image)
     10 with ag__.ld(tf).GradientTape() as tape:
     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)), dict(STYLE_LAYERS=ag__.ld(STYLE_LAYERS)), fscope)
     13     ag__.ld(print)('jstyle = ', ag__.ld(J_style))
     14     J_content = ag__.converted_call(ag__.ld(compute_content_cost), (ag__.ld(a_C), ag__.ld(a_G)), None, fscope)

File /tmp/__autograph_generated_file1bkxblm0.py:31, in outer_factory.<locals>.inner_factory.<locals>.tf__compute_style_cost(style_image_output, generated_image_output, STYLE_LAYERS)
     29 J_style_layer = ag__.Undefined('J_style_layer')
     30 i = ag__.Undefined('i')
---> 31 ag__.for_stmt(ag__.converted_call(ag__.ld(zip), (ag__.converted_call(ag__.ld(range), (ag__.converted_call(ag__.ld(len), (ag__.ld(a_S),), None, fscope),), None, fscope), ag__.ld(STYLE_LAYERS)), None, fscope), None, loop_body, get_state, set_state, ('J_style',), {'iterate_names': '(i, weight)'})
     32 try:
     33     do_return = True

File /tmp/__autograph_generated_file1bkxblm0.py:25, in outer_factory.<locals>.inner_factory.<locals>.tf__compute_style_cost.<locals>.loop_body(itr)
     23 nonlocal J_style
     24 (i, weight) = itr
---> 25 J_style_layer = ag__.converted_call(ag__.ld(compute_layer_style_cost), (ag__.ld(a_S)[ag__.ld(i)], ag__.ld(a_G)[ag__.ld(i)]), None, fscope)
     26 J_style = ag__.ld(J_style)
     27 J_style += (weight[1] * J_style_layer)

File /tmp/__autograph_generated_file1fmbobgq.py:13, in outer_factory.<locals>.inner_factory.<locals>.tf__compute_layer_style_cost(a_S, a_G)
     11 (m, n_H, n_W, n_C) = ag__.converted_call(ag__.converted_call(ag__.ld(a_G).get_shape, (), None, fscope).as_list, (), None, fscope)
     12 a_S = ag__.converted_call(ag__.ld(tf).reshape, (ag__.converted_call(ag__.ld(np).transpose, (ag__.ld(a_S), [0, 3, 1, 2]), None, fscope),), dict(shape=[ag__.ld(n_C), (ag__.ld(n_H) * ag__.ld(n_W))]), fscope)
---> 13 a_G = ag__.converted_call(ag__.ld(tf).reshape, (ag__.converted_call(ag__.ld(np).transpose, (ag__.ld(a_G), [0, 3, 1, 2]), None, fscope),), dict(shape=[ag__.ld(n_C), (ag__.ld(n_H) * ag__.ld(n_W))]), fscope)
     14 GS = ag__.converted_call(ag__.ld(gram_matrix), (ag__.ld(a_S),), None, fscope)
     15 GG = ag__.converted_call(ag__.ld(gram_matrix), (ag__.ld(a_G),), None, fscope)

File <__array_function__ internals>:180, in transpose(*args, **kwargs)

File /usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py:660, in transpose(a, axes)
    601 @array_function_dispatch(_transpose_dispatcher)
    602 def transpose(a, axes=None):
    603     """
    604     Reverse or permute the axes of an array; returns the modified array.
    605 
   (...)
    658 
    659     """
--> 660     return _wrapfunc(a, 'transpose', axes)

File /usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py:54, in _wrapfunc(obj, method, *args, **kwds)
     52 bound = getattr(obj, method, None)
     53 if bound is None:
---> 54     return _wrapit(obj, method, *args, **kwds)
     56 try:
     57     return bound(*args, **kwds)

File /usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py:43, in _wrapit(obj, method, *args, **kwds)
     41 except AttributeError:
     42     wrap = None
---> 43 result = getattr(asarray(obj), method)(*args, **kwds)
     44 if wrap:
     45     if not isinstance(result, mu.ndarray):

NotImplementedError: in user code:

    File "<ipython-input-35-190b42c4921e>", line 19, in train_step  *
        J_style = compute_style_cost(a_S, a_G, STYLE_LAYERS=STYLE_LAYERS)
    File "<ipython-input-22-7b56545257a6>", line 30, in compute_style_cost  *
        J_style_layer = compute_layer_style_cost(a_S[i], a_G[i])
    File "<ipython-input-17-6dce7a4a511b>", line 21, in compute_layer_style_cost  *
        a_G = tf.reshape(np.transpose(a_G, [0,3,1,2]), shape = [n_C, n_H * n_W])
    File "<__array_function__ internals>", line 180, in transpose  **
        
    File "/usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py", line 660, in transpose
        return _wrapfunc(a, 'transpose', axes)
    File "/usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py", line 54, in _wrapfunc
        return _wrapit(obj, method, *args, **kwds)
    File "/usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py", line 43, in _wrapit
        result = getattr(asarray(obj), method)(*args, **kwds)

    NotImplementedError: Cannot convert a symbolic tf.Tensor (model/block1_conv1/Relu:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported.


The first rule of debugging is “Believe the error message”. :laughing: It is telling you that you are mixing numpy and TF calls in your call graph. It could be in the train_step function or it could be in one of your previous functions that you are calling from train_step. Perhaps they passed the individual unit tests, but now you are actually running them in “graph execution” mode, as opposed to “eager” mode, so the rules can be different. Or maybe it’s because we are actually doing training now, so we need the gradients and having any numpy call in the call graph breaks the chain (you only get the autodiff gradients from TF functions). Although when I’ve seen that error before, I think the error message would actual say that it can’t compute the gradients.

One relatively simple example is that you can’t use myTensor.T to do a transpose, because that is the numpy version of transpose. You have to use tf.transpose.

Found it. I was using np.transpose in one of the functions (don’t know why I did!). Thanks again! See you in course 5 (hopefully not as often, you are thinking!)

2 Likes

That’s great that you found the problem based on that suggestion.

Course 5 is also incredibly interesting and completely new material. See you there!

And I should also say congratulations on completing ConvNets! It’s by far the most complex of the courses so far, but I think you’ll find that C5 takes it to another level. :nerd_face: At least that was my experience.

2 Likes