Normalising images using mean and std of a dataset
I used the following snippet to compute the mean and std of the images in the cityscapes dataset to normalise them:
def compute_mean_std(dataloader):
    
    pop_mean = []
    
    pop_std = []
    
    for i, (img,mask, rgb_mask) in enumerate(dataloader):
        
        numpy_image = img.cpu().numpy()
        
        batch_mean = np.mean(numpy_image,axis=(0,2,3))
        pop_mean.append(batch_mean)
        
        #print(batch_mean.shape)
        
        batch_std = np.mean(numpy_image, axis=(0,2,3))
        pop_std.append(batch_std)
        
        #print(batch_std.shape)
        
        
    pop_mean = np.array(pop_mean).mean(axis=0)
    pop_std = np.array(pop_std).std(axis=0)
    
    print(pop_mean.shape)
    print(pop_std.shape)
    
    return(pop_mean, pop_std)

This code gave me the following mean and std:

MEAN = [0.28660315, 0.32426634, 0.28302112]
STD = [0.00310452, 0.00292714, 0.00296411]

but when I computed the mean and std of images after normalisation using these mean and std, they are not close to 0 and 1.
Is this approach correct to compute mean and std over the whole dataset and normalising images?

Your formulas are not correct. You can't take the mean of the values of a batch and then the standard deviation of these means and expect it to be the standard deviation over the entire dataset. Try something like:
total = 0.0
totalsq = 0.0
count = 0

for data, *_ in dataloader:
    count += np.prod(data.shape)
    total += data.sum()
    totalsq += (data**2).sum()

mean = total/count
var = (totalsq/count) - (mean**2)
std = torch.sqrt(var)

