The error is telling you your predictions and labels are not of the same size, so when calculating the loss that relevant fuction is not working. Check the shapes of these two implemented in your code.
Ok Sir
I am using below statement to calculate expected output, is it correct.
expected_output = tf.one_hot([class_index] * image.shape[0], num_classes)
for loss
loss = tf.keras.losses.binary_crossentropy(expected_output, predictions)
No they are both not right, understand that you have 2 dinstict classes and also read carefully instructions and comments. Labs of that week can be helpful too.
Can any one correct me, as my model architecture is same as given in assignment. I am trying to solve this 4th assignment since from many days not able to resolve it, I have followed all instruction and read all comments in week 4 and refer previous lab but didnt work for me.
I am not able to overcome the problem.
when i am using categorical_crossentropy, I am getting below error
UFuncTypeError: ufunc āaddā did not contain a loop with signature matching types (dtype(ā<U32ā), dtype(ā<U32ā)) ā dtype(ā<U32ā)
when i am using sparse_categorical_crossentropy,I am getting below error
ValueError: Shape mismatch: The shape of labels (received (2,)) should equal the shape of logits except for the last dimension (received (1, 2)).
I did tell you that both; your expected_output and loss function are not right. In the first one you are missing a variable in the second one you are not using the right loss function. Concentrate on those 2 points, think and try a few choices.
Dear Sir, as per your suggestion in expected loss, I have added second variable
whether is it correct? I executed but got error. I am confused about second variable i.e image.shape[0], as in ungraded lab we are using inception net and here our own model.
as per my knowledge in this assignment 2 classes are their, so binary_crossentropy should work, but it didnt work for me.
Initially I experienced the same error as the OP, logits and labels not the same shape.
The guess by @guidini.ian is a good one, but in defense of us poor learners, I call your attention to the guidelines provided for the augment_images() preprocessing function:
Define a function that takes in an image and label. /begin_rant it is a pet peeve of mine that functions should have informative names. This one is named augment_images plural, but as far as I can tell it takes in and operates on a single image, not multiple /end_rant. Hereās what it says:
Create preprocessing function
Define a function that takes in an image and label. This will:
cast the image to float32
normalize the pixel values to [0, 1]
resize the image to 300 x 300
If you implement a function that does that, exactly that, and only that, the label parameter that is passed into the function remains unchanged. If Iām not mistaken, there needs to be one extra step there to mutate the parameter label before it is returned. A bit naughty that we are given detailed boilerplate for 3 out of the 4 steps, no? Did I miss something?
No there is no need to change the label, the instructions are correct as given. Later on at do_salience function you are defining the expected_output which will convert the labels to one_hot encoding to be used further down on to calculate the loss.
Interesting. I guess I have to go back and play with the code some more, because I added a one_hot conversion inside augment_image() and my logits/labels size mismatch went away. To the best of my recollection, thatās the only change I made.
Raises the question for me, if label isnāt mutated within that function, why is it passed in and returned?
Hi
Iām having similar difficulties as @ nisarggandhewar
With a different loss.
My function of do_salience on cat1.jpg generates the exact result as in the given output so I assume my predictions and labels have the same size. But when I try to fit the model I get an error of size miss-match. Any help would be appreciated.
This is the error log:
This is most probably happening because your output and loss function are not appropriate for each other. Probably from what I see in the assignment the predictions are not right!