# MSE ouput different compared CV2 computation

Hi,

Comparing the error value between two images using CV2 and pyTorch shows different results. Here is my code. Could you let me know what is problem here?

``````T = transforms.Compose([transforms.ToTensor()])
P = transforms.Compose([transforms.ToPILImage()])

def ComputFullImgError(in_img,out_img):

inImg = T(Image.open(in_img))
outImg = T(Image.open(out_img))

inImg = inImg.unsqueeze(0)
print(inImg.size())

outImg = outImg.unsqueeze(0)
print(outImg.size())

l1_error = torch.nn.L1Loss()
print('L1 Error',l1_error(inImg,outImg))

mse_loss = torch.nn.MSELoss()
print('MSE', mse_loss(inImg,outImg) )

def CVError(in_img,out_img):

err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1] * imageA.shape[2])

print('CV',err)

ComputFullImgError('input.png','target.png')
CVError('input.png','target.png')
``````

Result:

``````torch.Size([1, 3, 1080, 1920])
torch.Size([1, 3, 1080, 1920])
L1 Error tensor(0.0103)
MSE tensor(0.0006)
CV 41.84295669367284
``````

Don’t use nn.L1loss.
It does average among batches and values in a batch.
Besides I think that transforms normalize images between 0-1 whereas they remain 0-255 in cv2.

Thanks it’ the normalization error.

Regards,
MJay