Last active
November 16, 2024 21:28
-
-
Save yuhanz/e83cf50a626ba06dc930e77a4c0cf8ba to your computer and use it in GitHub Desktop.
Get an image into feature vector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import clip | |
import torch | |
from PIL import Image | |
#image_pil = Image.open("path_to_image.jpg") | |
image_pil = Image.fromarray(image) | |
device = 'cuda' | |
image_input = preprocess(image_pil).unsqueeze(0).to(device) | |
with torch.no_grad(): | |
image_features = model.encode_image(image_input) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
from torchvision.models import resnet50 | |
from torchvision.transforms import functional as TF | |
model = resnet50(pretrained=True) | |
image = cv2.imread('shaolin-monk-test.jpg') | |
input_tensor = TF.to_tensor(image).unsqueeze(0) | |
vector = model(input_tensor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment