mirror of
https://github.com/openai/CLIP.git
synced 2026-11-05 01:37:26 +00:00
[GH-ISSUE #113] Is there a way to only use the text encoder ? #101
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#101
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 @ranran9991 on GitHub (Jun 9, 2021).
Original GitHub issue: https://github.com/openai/CLIP/issues/113
Hey!
I'd like to use only one part of the model, specifically the text encoder in my work. I don't want to store the whole model in GPU memory just to use the text encoding part, is there a simple way to do that? or will I have to dive into the code myself
Thanks for the help ! :)
@vinson2233 commented on GitHub (Jun 9, 2021):
What I have done is to set the
model.visual = Noneto remove all the visual part. But this will raise error since dtype property is dependent on the.visualpart.github.com/openai/CLIP@cfcffb90e6/clip/model.py (L332-L334)I think if we can set all the visual part into None except for
model.visual.conv1.weightthen the encode_text will work perfectly without the need to store the visual part.@ranran9991 commented on GitHub (Jun 14, 2021):
Thank you for the help, hopefully this feature will be added soon
@lonngxiang commented on GitHub (Jun 30, 2021):
same needs
@jongwook commented on GitHub (Jun 30, 2021):
You can replace
self.visual.conv1.weight.dtypewithnext(self.parameters()).dtypeand such, which will avoid the error. I'll plan to replace it in the next round of updates.@lonngxiang commented on GitHub (Jun 30, 2021):
thumb up
@lonngxiang commented on GitHub (Jul 5, 2021):
finally, I succeed @jongwook @vinson2233 @ranran9991
model.py
save.py
@vinson2233 commented on GitHub (Jul 5, 2021):
@lonngxiang To highlight the code part, you can use ``` instead of `, put it on top and bottom part of code, all will be highlighted as code.
But I get the idea. Thanks.
@lonngxiang commented on GitHub (Jul 5, 2021):
ok,tks
@laurenspriem commented on GitHub (Feb 28, 2023):
I followed this approach and got a text encoder. However, the embeddings that the model gives are completely wrong. @lonngxiang have you verified that the resulting embeddings match those of the original model?
@jongwook is the above method still the recommended approach, or is there a better way by now?
@laurenspriem commented on GitHub (Feb 28, 2023):
Since the above solution didn't work for me, I've used another workaround that was kind of suggested earlier in this thread.
Basically, you can strip away most of the image encoder by setting
model.visual.transformer = None. Contrary tomodel.visual = None, this doesn't give an error.Downsides are that you're still left with some useless weights from the image encoder, and that you have to use
model.encode_text(text_input)instead of justmodel(text_input).@ranran9991 commented on GitHub (Feb 28, 2023):
This is the workaround that I use as well.