I have a portion of my code as:
def DepthError(depth_pred, depth_gt):
    L_d, Mcard = 0.0, 0
    B, c, H, W = depth_pred.shape #batch, channels, height, width
    for b in range(B): #B is batch size, iterating over the images in a batch
        depth_pred2D = depth_pred[b][0] # bth depth image, depth images have 1 channel thus 0
        depth_gt2D = depth_gt[b][0]
        for h in range(H): # iterate over pixels along height
            for w in range(W):
                if depth_gt2D[h][w] <= 0:
                    continue
                Mcard += 1
                diff = torch.sub(depth_pred2D[h][w], depth_gt2D[h][w])
                L_d += torch.abs(diff)
    L_d = torch.div(L_d, Mcard)
    return L_d #return depth loss

train_data = next(iter(train_dataloader))
image = train_data['image'].cuda()
depth_pred = depth_net(image) #estimate depth by passing image through network
L_d = DepthError(depth_pred, depth_gt) #depth loss

My code seems to be terribly slow, also I am trying to overfit to one batch by setting batch size to be 10. But my loss seems to be stuck at the same value and is not decreasing.
Is the above method the right way to process images in a batch. Also, am I doing things correctly?
Thanks!