Hi,
I am trying to build a binary classifier to classify pneumonia in radiology images. here is my model architecture
image901×434 48 KB
here is code to train the model
NUM_EPOCHS = 5
BEST_MODEL_PATH = 'best_model.pth'
best_accuracy = 0.0

optimizer = optim.SGD(model.parameters(), lr=0.001)

for epoch in range(NUM_EPOCHS):
    train_error_count=0.0
    for images, labels in iter(train_loader):
        labels = labels.type(torch.FloatTensor)
        images = images.to(device)
        labels = labels.to(device)
        optimizer.zero_grad()
        outputs = model(images)
        train_error_count += float(torch.sum(torch.abs(labels - outputs.argmax(1))))
        outputs = torch.squeeze(outputs)
        loss = nn.BCELoss()(outputs, labels)
        loss.backward()
        optimizer.step()
        
    train_accuracy = 1.0 - float(train_error_count) / float(len(train_dataset))
    
    test_error_count = 0.0
    for images, labels in iter(test_loader):
        images = images.to(device)
        labels = labels.to(device)
        outputs = model(images)
        test_error_count += float(torch.sum(torch.abs(labels - outputs.argmax(1))))
    
    test_accuracy = 1.0 - float(test_error_count) / float(len(test_dataset))
    print('Epoch %d: train_acc =%f  test_acc =%f' % (epoch, train_accuracy, test_accuracy))
    if test_accuracy > best_accuracy:
        torch.save(model.state_dict(), BEST_MODEL_PATH)
        best_accuracy = test_accuracy
        print('Best Model Saved!!!')

I am unable to train it successfully because it is showing the following accuracy.

It would be great if anyone can identify a bug. I am a beginner in PyTorch so it would be helpful.
Thank you,