choyingw / SynergyNet

3DV 2021: Synergy between 3DMM and 3D Landmarks for Accurate 3D Facial Geometry
MIT License
382 stars 57 forks source link

Normalization, ToTensor, PILToTensor #19

Closed awarebayes closed 2 years ago

awarebayes commented 2 years ago

Hello, I'd been reading and executing your code and noticed that you are using ToTensor transform from torchvision. I think you need to be aware of it's scaling effects. Lets break down transform = transforms.Compose([ToTensor(), Normalize(mean=127.5, std=128)]) Assume the intensity of a pixel is 128. ToTensor converts it to float, BUT ALSO divides it by 255. So now your intensity is 0.5 Then in Normalize: (0.5 - 127.5) / 128 = -127 / 128 ~ -0.97. Applying it to other values from range withing [0, 255], we get a range for your intensity in [-0.998, 0.996], which is quiet opposite for what I would consider normalization

image

The fix is pretty simple, that is to use PILToTensor from the same torchvision, that does not scale your input by 255. Doint that you will get way normal normalization as shown below, in (-1, 1) :)

image

I will try retraining and benchmarking with better normalization and will submit a pr

awarebayes commented 2 years ago

Ah I am sorry to bother, your code is correct, you wrote your own ToTensor, closing it for now....