0

I am trying to find test accuracy using the confusion matrix in UNet Model. But keep getting an error of y_true and y_pred are not defined even though they are already defined globally.
Here:

global y_true
global y_pred

def dice_coeff(y_true, y_pred):
    smooth = 1.
    # Flatten
    y_true_f = tf.reshape(y_true, [-1])
    y_pred_f = tf.reshape(y_pred, [-1])
    intersection = tf.reduce_sum(y_true_f * y_pred_f)
    score = (2. * intersection + smooth) / (tf.reduce_sum(y_true_f) + tf.reduce_sum(y_pred_f) + smooth)

def dice_loss(y_true, y_pred):
    loss = 1 - dice_coeff(y_true, y_pred)
    return loss

def bce_dice_loss(y_true, y_pred):
    loss = losses.binary_crossentropy(y_true, y_pred) + dice_loss(y_true, y_pred)
    return loss

And I am getting the error in this code as y_true and y_pred are not defined.

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_true,y_pred)

Can anyone suggest how to overcome this error or is there any other way to find the model prediction accuracy?

  • Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Please post your original code text instead of a sceenshot. – hellohawaii Jul 29 '22 at 07:44
  • @hellohawaii did the necessary changes now can you please suggest the solution or why I am getting the error? – Anushka Singhania Aug 03 '22 at 12:14
  • You may refer to [this link](https://stackoverflow.com/q/9366212/9758790). The expression `global y_true` does not define `y_true`. Did you define `y_true` and `y_pred` elsewhere? – hellohawaii Aug 03 '22 at 12:22
  • @hellohawaii No, I didn't define it elsewhere apart from the mentioned code. – Anushka Singhania Aug 03 '22 at 14:04
  • The definition of these function `dice_coeff`, `dice_loss`, `bce_dice_loss` do not define the `y_true` and `y_pred`, neither does the `global` expression. As stated in the link I provided in my last comment, `global y_true` doesn't define `y_true`. You can try `y_true = [2, 0, 2, 2, 0, 1]` and `y_pred = [0, 0, 2, 2, 0, 2]` as stated in the [doc](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html) – hellohawaii Aug 03 '22 at 15:26
  • @hellohawaii The doc example is for smaller data, I have a way bigger dataset, I can't simply put its values in an array. Though, I got your global variable point. Thanks. – Anushka Singhania Aug 03 '22 at 19:00

0 Answers0