Skip to content

Instantly share code, notes, and snippets.

@yuhanz
Last active November 16, 2024 21:28
Show Gist options
  • Save yuhanz/e83cf50a626ba06dc930e77a4c0cf8ba to your computer and use it in GitHub Desktop.
Save yuhanz/e83cf50a626ba06dc930e77a4c0cf8ba to your computer and use it in GitHub Desktop.
Get an image into feature vector
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)
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