-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
43 lines (30 loc) · 969 Bytes
/
helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import torch
from PIL import Image
from torchvision import transforms
def get_feature_vector(path):
torch.manual_seed(0)
# Load the pretrained model from pytorch
my_model = torch.load('model.pt')
for param in my_model.parameters():
param.requires_grad = False
# Load the image
img = Image.open(path)
# Define the transforms
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
# Apply the transforms to the image
img_tensor = transform(img)
img_tensor = img_tensor.unsqueeze(0)
output = my_model(img_tensor)
output = output.squeeze()
return output
if __name__ == "__main__":
path = 'media/cat.2636.jpg'
output = get_feature_vector(path)
print(output.shape)
print(output)