Hi, I am trying to calculate the precision and recall for two images. with one image as the output of the model (shape [3,786,1024]) and the other ground truth (shape [1, 786, 1024]). But I keep getting an error. I am new to python and I am able to understand what I am doing wrong. I have tried some things like getting the input and output to the same shape, but I am getting the error now:
raise ValueError("Classification metrics can’t handle a mix of {0} "
ValueError: Classification metrics can’t handle a mix of multilabel-indicator and multiclass targets
If anybody can help me with this issue. i would be grateful.
My code is here:
def precision(outputs, labels):
op = outputs#.cpu()
la = labels#.cpu()
_, preds = torch.max(op, dim=1)
return torch.tensor(precision_score(la,preds, average='micro'))

def recall(outputs, labels):
op = outputs#.cpu()
la = labels#.cpu()
_, preds = torch.max(op, dim=1)
return torch.tensor(recall_score(la,preds, average='weighted', zero_division = 1))
def f1(outputs, labels):
op = outputs#.cpu()
la = labels#.cpu()
_, preds = torch.max(op, dim=1)
return torch.tensor(f1_score(la,preds, average='weighted'))


img50 = Image.open('0050_999.png')
numpydata_in = np.array(img50).astype('uint8')
#print("test shape",numpydata_in.shape)
numpydata_in= torch.from_numpy(numpydata_in)
print("final input shape", numpydata_in.shape)

imgT50 = Image.open('0050.jpg')
numpydata_out = np.array(imgT50).astype('uint8')
numpydata_out = transforms.ToTensor()(numpydata_out)
#print(numpydata_out.shape)
numpydata_out = torch.argmax(numpydata_out, dim=0)
#print(numpydata_out.shape)
numpydata_out = torch.squeeze(numpydata_out)
print("final shape target",numpydata_out.shape)

prec = precision(numpydata_in, numpydata_out)
print("precision is",prec)
rec = recall(numpydata_in, numpydata_out)
print("recall is",rec)
f1 = f1(numpydata_in, numpydata_out)
print("f1 score is",f1)

final input shape torch.Size([768, 1024])
final shape target torch.Size([768, 1024])
raise ValueError("Classification metrics can’t handle a mix of {0} "
ValueError: Classification metrics can’t handle a mix of multilabel-indicator and multiclass targets