Evaluation of a CNN?

Hey guys !

I am currently working on a CNN using a public dataset in order to make classification between two type of images : images containing brain hemorraghies, and images that doesn’t.

I am trying to evaluate my model, but was wondering how to implement various things, like accuracy, precision, recall, specificity, etc.
So two things : are these methods the best way to evaluate CNN ? How to implement it correctly ? I was wondering if I could use sklearn metrics or if it would be easier to write functions in PyTorch.

My way to calculate accuracy at the moment seems like not working, as it doesn’t update at all.
Here’s my code (definition of model and training).

class Net(Module):   
    def __init__(self):
        super(Net, self).__init__()

        self.cnn_layers = Sequential(
            # Defining a 2D convolution layer
            Conv2d(3, 4, kernel_size=3, stride=1, padding=1),
            # Defining another 2D convolution layer
            Conv2d(4, 4, kernel_size=4, stride=1, padding=1),

        self.linear_layers = Sequential(
            Linear(4 * 49 * 49, 1)

    # Defining the forward pass    
    def forward(self, x):
        x = self.cnn_layers(x)
        x = x.view(x.size(0), -1)
        x = self.linear_layers(x)
        return x
# defining the model
model = Net()
model = model.to('cuda')
# defining the optimizer
optimizer = Adam(model.parameters(), lr=0.07)
# defining the loss function
criterion = BCEWithLogitsLoss()
# checking if GPU is available
if torch.cuda.is_available():
    model = model.cuda()
    criterion = criterion.cuda()
from torchsummary import summary

#vgg = models.vgg16()
#summary(model.cuda(), (3, 200, 200))

#Fonction d'entraînement actuelle.
def train2(epoch):
    tr_loss = 0
    total = 0
    correct = 0
    target_true = 0
    predicted_true = 0
    correct_true = 0
    X_train = X
    y_train = y
    # converting the data into GPU format
    if torch.cuda.is_available():
        X_train = X_train.cuda()
        y_train = y_train.cuda()
    # clearing the Gradients of the model parameters
    # prediction for training and validation set
    output_train = model(X_train)
    # computing the training and validation loss
    loss_train = criterion(output_train, y_train.unsqueeze(1).type_as(output_train))
    total += y_train.size(0)
    correct += (output_train.argmax(-1) == y_train).sum().item()
    # computing the updated weights of all the model parameters
    tr_loss = loss_train.item()
    if epoch%2 == 0:
        # printing the validation loss
        print('Epoch : ',epoch+1, '\t', 'loss : ', tr_loss, 'accuracy : ', (correct / total)*100)
        #print('Accuracy : ', accuracy_score(output_train.cpu().detach().numpy(), y_train.cpu().detach().numpy()))

hi @SoulHokib

You can draw the confusion matrix as given here:

You can get the tn,tp,fn,fp as follows:

1 Like

Thanks for your help, I’ll have an eye on it !