Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HalfTensor (float16) support? tried to implement but failed :( #97

Open
arpitest opened this issue Oct 23, 2022 · 2 comments
Open

HalfTensor (float16) support? tried to implement but failed :( #97

arpitest opened this issue Oct 23, 2022 · 2 comments

Comments

@arpitest
Copy link

arpitest commented Oct 23, 2022

Is it possible to run this with half precision, to be able to use higher image resolution with limited VRAM?
I've tried to do it (similar way to how stable diffusion does):

  • added "cnn=cnn.half()" after caling loadCaffemodel
  • replaced all FloatTensor by HalfTensor in neural_style.py
    It is running, but loss calculation is not working:

Running optimization with L-BFGS
Iteration 10 / 1000
Content 1 loss: nan
Style 1 loss: nan
Style 2 loss: nan
Style 3 loss: nan
Style 4 loss: nan
Style 5 loss: nan
Total loss: nan

any idea how to fix that?

@ziye3001
Copy link

well, I guess something exceeds the maximal value of float16, which is a fairly small number 65500. For example, the Gram Matrix is approximately the sum of the feature^2, which easily gets very large. The MSE loss of two Gram Matrices could have even larger magnitude. A possible solution could be some alternative losses, e.g. BN loss, in MMD paper. But you have to adjust the style weight to get similar results with the ones with Gram Matrices.

@JCBrouwer
Copy link

Another option would be to find out what the max feature values are on average and then add a fixed scaling before calculating the gram matrix.

This way the magnitude of the squaring operation is essentially limited to 1. You'd have to make sure that the loss weights are also adjusted accordingly though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants