Hey, I am computing the accuracy in a same way. As
 for epoch in range(epochs):
        net.train()

        epoch_loss = 0
        total_train = 0
        correct_train = 0
     
        with tqdm(total=n_train, desc=f'Epoch {epoch + 1}/{epochs}', unit='img') as pbar:
            for batch in train_loader:
                #continue
                imgs = batch['image']
                true_masks = batch['mask']
                n_batch = np.ceil(n_train/batch_size)
                assert imgs.shape[1] == net.n_channels, \
                    f'Network has been defined with {net.n_channels} input channels, ' \
                    f'but loaded images have {imgs.shape[1]} channels. Please check that ' \
                    'the images are loaded correctly.'

                imgs = imgs.to(device=device, dtype=torch.float32)
                mask_type = torch.float32 if net.n_classes == 1 else torch.long
                true_masks = true_masks.to(device=device, dtype=mask_type)

                logits,probs,masks_pred = net(imgs) #logits, probas, preds
                loss = criterion(logits, true_masks)
                epoch_loss += loss.item()
                #writer.add_scalar('Loss/train', loss.item(), global_step)

                pbar.set_postfix(**{'loss (batch)': loss.item()})

                optimizer.zero_grad()
                loss.backward()
                nn.utils.clip_grad_value_(net.parameters(), 0.1)
                #! nvidia-smi
                optimizer.step()
                torch.cuda.empty_cache()
                #! nvidia-smi
                total_train += true_masks.nelement()
                
                print('total_train',total_train)
                #print('masks_pred.eq(true_masks).sum()',masks_pred.eq(true_masks).sum())
                correct_train += masks_pred.eq(true_masks).sum().item()
                print('correct_train',correct_train)
                train_accuracy = 100*correct_train/total_train
                print('train_accuracy',train_accuracy)

The weird thing is that, when my batch_size=1, the accuracy is a value between 0-100%. But, when the batch_size => 2, the accuracy isn’t in a range of 100% any more. I tried to compute the
 train_accuracy = 
100*correct_train/total_train

outside of the
for batch in train_loader:

but, still didn’t get a reasonable value. I didn’t get why it doesn’t work properly, when the batch_size = > 2. Could you please explain it? Thank u.
@ptrblck  @Neda