mirror of
https://github.com/openai/CLIP.git
synced 2026-11-05 01:37:26 +00:00
[GH-ISSUE #40] NaN values after a single gradient step #38
Labels
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
openai/CLIP#38
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @MartinPernus on GitHub (Feb 10, 2021).
Original GitHub issue: https://github.com/openai/CLIP/issues/40
Hi!
Using PyTorch 1.7.1, I get NaN values after a single parameter update:
Output:
@jongwook commented on GitHub (Feb 12, 2021):
Hi, the model checkpoint contains fp16 parameters for speed, but gradients for these weights are very prone to overflow/underflow without careful loss scaling, causing
nanoutputs after a gradient step.You can avoid this by casting all weights to fp32 with
model.float(). Also, passjit=Falsetoclip.loadwhen doing this, because the JIT-compiled code contains hard-coded dtype values and won't work.@MartinPernus commented on GitHub (Feb 12, 2021):
Thank you! Did not realize a single step already breaks the fp16 parameters. The code works now.
@NotNANtoN commented on GitHub (Oct 1, 2021):
@MartinPernus Hey, I'm curious about fine-tuning CLIP.
Could you share some details on how many image-text pairs you used and what batch size? Did it work to fine-tune it? I'd appreciate any input!
@MartinPernus commented on GitHub (Oct 4, 2021):
@NotNANtoN
I tried to fine-tune it on face-text pairs of images. I tried different batch sizes. Unfortunately it did not work - while the training and validation loss of my dataset decreased, it did not generalize on the test set. The problem might be the text part - I generated it from manually from a set of binary attributes. I have also tried to include some domain adaptation techniques to no avail.
@NotNANtoN commented on GitHub (Oct 5, 2021):
Interesting use-case, thanks!
Weird, that the validation loss decreases while the test loss doesn't, I think I've never seen that during my trainings.
But it seems like using text might be a bit overkill when you can just fine-tune the vision-encoder to predict your binary attributes.
@MartinPernus commented on GitHub (Oct 5, 2021):
I should have elaborated further. The training and validation splits were constructed manually from a set of binary attributes, while the test set belonged to an actual dataset of face descriptions + face images. The motivation for that was that we could in theory leverage datasets that contain binary attributes to improve CLIP performance for text-based face search. This could be useful in criminal investigations, where the victim describes the suspect and we would not have to constrain ourself to prefixed set of binary attributes.
Hope I have made myself clearer, I am happy to discuss this further.
@NotNANtoN commented on GitHub (Oct 11, 2021):
Hi Martin, thanks that clears it up! Interesting use-case, but it makes sense it this is very hard for the model to extrapolate to fuller sentences.
Unfortunately, I can't think of a way to leverage datasets with binary attributes in a better way than you tried there.
@cheng321284 commented on GitHub (Mar 24, 2022):
Thanks, it works for me~
@lailanqing commented on GitHub (Sep 5, 2023):
Freezing the loaded model may also solve the problem. For example:
model, _ = clip.load('VIT_32')
for name, param in model.named_parameters():
param.requires_grad = False
@Dinosaurcubs commented on GitHub (May 12, 2024):
I met the same promblom recently, nan after random epochs. Now I changed the model to fp32 and train again, but the training time is nearly double....Is there any way to use fp16 and avoid nan? Why the nan happen during training with fp16 model? Thanks for ur reply.