[GH-ISSUE #418] Regarding the fact that the clip library can only be used to read data using the PIL library, we hope that the official can modify the source code #356

Open
opened 2026-10-31 21:14:19 +00:00 by bsakaguchi · 0 comments
Owner

Originally created by @qsd-github on GitHub (Jan 8, 2024).
Original GitHub issue: https://github.com/openai/CLIP/issues/418

My code is as follows:

model, preprocess = clip.load("./weights/ViT-B-32.pt", device=device)
image = preprocess(Image.open("./xxx.jpg")).unsqueeze(0).to(device) # only PIL library can be used here, using cv2 library will result in an error.

I took a look at the source code:
def load(name: str,..):
....
return return model, _transform(model.visual.input_resolution) # The problem is that _transform()

def _transform(n_px):
return Compose([
ToPILImage(), # Adding ToPILImage() here and importing the corresponding library can solve the problem of not being able to read cv files
Resize(n_px, interpolation=BICUBIC),
CenterCrop(n_px),
_convert_image_to_rgb,
ToTensor(),
Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
])
This is my usage source code and solution. If you use it differently from my source code and support the cv2 library, please share your opinion. I suggest the official modify this location!

Originally created by @qsd-github on GitHub (Jan 8, 2024). Original GitHub issue: https://github.com/openai/CLIP/issues/418 My code is as follows: model, preprocess = clip.load("./weights/ViT-B-32.pt", device=device) image = preprocess(Image.open("./xxx.jpg")).unsqueeze(0).to(device) # only PIL library can be used here, using cv2 library will result in an error. I took a look at the source code: def load(name: str,..): .... return return model, _transform(model.visual.input_resolution) # The problem is that _transform() def _transform(n_px): return Compose([ ToPILImage(), # Adding ToPILImage() here and importing the corresponding library can solve the problem of not being able to read cv files Resize(n_px, interpolation=BICUBIC), CenterCrop(n_px), _convert_image_to_rgb, ToTensor(), Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)), ]) This is my usage source code and solution. If you use it differently from my source code and support the cv2 library, please share your opinion. I suggest the official modify this location!
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openai/CLIP#356
No description provided.