Hi community!
I am having some trouble calculating the f1-score and accuracy on a multi-label classification model, here is my code:
model:
import torch.nn as nn
from torch import optim
import torch.nn.functional as F
class MultiClassifier(nn.Module):
def __init__(self):
super(MultiClassifier, self).__init__()
#Create sequential containe. Modules will be added to it in the order they are passed in the constructor
self.ConvLayer1 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1), # 3,128,128
nn.MaxPool2d(2,2), # op: 64, 64, 64
nn.ReLU(), # op: 64, 64, 64
)
self.ConvLayer2 = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1), # 64, 64, 64
nn.MaxPool2d(2,2), #op: 128, 32, 32
nn.ReLU() # op: 128, 32, 32
)
self.ConvLayer3 = nn.Sequential(
nn.Conv2d(in_channels=128,out_channels= 256, kernel_size=3, stride=1, padding=1), # 128,32,32
nn.MaxPool2d(2,2), #op: 256, 16, 16
nn.ReLU(), #op: 256, 16, 16
nn.Dropout(0.2)
)
self.Linear1 = nn.Linear(256 * 16 * 16, 512)
self.Linear2 = nn.Linear(512, 256)
self.Linear3 = nn.Linear(256, 18)
def forward(self, x):
x = self.ConvLayer1(x)
x = self.ConvLayer2(x)
#x = self.ConvLayer3(x)
x = self.ConvLayer3(x)
x = x.view(x.size(0), -1)#view is used to change the shape of the tensor, here we flatten the convolutional layer to 1 dimension for linear layers
x = self.Linear1(x)
x = self.Linear2(x)
x = self.Linear3(x)
return torch.sigmoid(x)
train:
from tqdm import tqdm
for epoch in range(total_epoch):
train_loss = 0
loop = tqdm(enumerate(train_data), total = len(train_data), leave = False)
for i, data in loop:
inputs, target = data['image'].float().to(device), data['label'].float().to(device)
model.train().to(device)
optimizer.zero_grad()
outputs = model(inputs).to(device)
loss = criterion(outputs, target).to(device)
loss.backward()
optimizer.step()
#update progress bar
loop.set_description(f"Epoch [{epoch}/{total_epoch}]")
loop.set_postfix(loss = loss.item())
train_loss += loss.item()
y_true = target.cpu()
y_pred = outputs.cpu().int().detach().numpy()
train_f1 = f1_score(y_true, y_pred, average='micro')
train_acc = accuracy_score(y_true, y_pred)
print('Epoch: %d, loss: %.5f, train_acc: %.2f, train_f1: %.2f'%(epoch , train_loss, train_acc, train_f1))
I think it is something to do with my output as it is a multi-hot vector containing 18 classes, any advice what should I do? Thanks!