Train_step for target image generation

Can anyone give a hint on what is wrong in this piece of code?
It is the train_step procedure of the Neural Style transfer assignment

Compute a_G as the vgg_model_outputs for the current generated image
{Codes removed by moderator as it is part of graded assignment} -assignment code sharing is against community guidelines.

You need to give information about what is a_S, STYLE LATERS?

ALSO what is the issue you are encountering when running this code?

Is this code part of any assignment from course?

If you are encountering error kindly share screenshot of the error.

What is the generated image here? What are you training here?

Course 4 week4 programming assignment 2 about neural style transfer.
The error is Unexpected cost for epoch 0: [-3.1798282e+08] != 25629.055
It seems the train_step procedure is calculating a too small cost at the very beginning, but I do not understand what I am doing wrong

Could you share a complete screenshot of the error you are encountering as it gives glimpse into your code issue.

Regards
DP

While computing style cost, you using STYLE LAYERS is incorrect as it is part of vgg model output which is used to encode generated image and further assign to a_G

Note that you get a negative value for the cost, which should never happen. One thing to look for is the sort of problems described on this thread. Are you sure that your previous functions passed their tests?

The other thing to note here is that there is some “statefulness” in the way they wrote that training logic. You can’t just run that cell more than once without rerunning some of the previous cells. Try “Kernel → Restart and Clear Output”, “Cell → Run All” and see if that changes your results.

That is actually not a problem. That is an optional (keyword) argument to the compute_style_cost function. So whatever the problem is, that is not it.

1 Like

Hi, I took away the parameter STYLE_LAYERS, but it still does not work.
I use the previously coded function, compute_style_cost(a_S, a_G)
Is that wrong?
I think it has something to do with that function

Agree

Yes, that’s my theory. Well, did you read that thread I gave you earlier? That is exactly what that was about.

I did read your thread. Thanks!

I solved it!!
The problem was in this line of the compute_layer_style_cost procedure

the line that works is:
J_style_layer = tf.reduce_sum(tf.reduce_sum(tf.square(tf.subtract(GS,GG)),axis=-1),axis=-1)/tf.square(2.*n_H * n_W *n_C)

the line that did work for the test of that code with result 14.01649, but later not for the training was something like this, i don’t quite remember exactly:
J_style_layer = tf.reduce_sum(tf.reduce_sum(tf.square(tf.subtract(GS,GG)),axis=-1),axis=-1)/np.float(4*tf.square(n_H * n_W *n_C))
I could have made it float with the dot in 4 → 4. but for some reason I tried with np.float()

1 Like

Thanks again to both.
I did not need it to approve the course but I wanted to get it right and understand what i was doing wrong.
Rgds

Glad to hear that the information helped you get to a solution. BTW I would say the code is still more complicated than it needs to be: why do you need two nested calls to tf.reduce_sum there? If you just omit the axis argument, you get the sum over all the dimensions which is what you want in this case (a scalar output).

You are right, thanks.

Still one thing I don’t understand.
Why do you calculate this:
content_target = vgg_model_outputs(content_image) # Content encoder
style_targets = vgg_model_outputs(style_image) # Style encoder

but then do not use them.

Because you do this:
preprocessed_content = tf.Variable(tf.image.convert_image_dtype(content_image, tf.float32))
a_C = vgg_model_outputs(preprocessed_content)
preprocessed_style = tf.Variable(tf.image.convert_image_dtype(style_image, tf.float32))
a_S = vgg_model_outputs(preprocessed_style)

Why?

I don’t know why they included those first two lines you show. You’re right that those outputs are never used. I guess they just wanted to demonstrate how to call the function they had just defined.

If memory serves, I think I reported this as a bug a while back, but it was considered just “cosmetic”. There is no harm done other than just leaving us wondering a bit.

1 Like

Thank you Paul for the response but my response was the code the learner had shared and I knew it would still come back as error, and I would have guided further as the issue was the training steps of how tf needs to be used.

Thank you for adding your valuable insight further, you are any day a better mentor than me.

Regards
DP

Hello @Bob88

from what I remember the below code

does looks like not required for the assignment exercise being mentioned.
But the above steps mentions on how to use content_image and style image further when you create a get_style loss by using content_output and style output.

you will come across these steps if you are going to do tensorflow advanced technique specialisation, Course 4 week 1 assignment neural style transfer.

Regards
DP

The neural style transfer assignment is in Course 4 week 2, the 2nd assignment. Still do not understand why those lines are there. If they are not needed, why not remove them? They are misleading…in my opinion