-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcommons.py
27 lines (21 loc) · 890 Bytes
/
commons.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
import io
import torch
import torch.nn as nn
from torchvision import models,transforms
def get_model():
checkpoint_path='food_classifier.pt'
model=models.densenet201(pretrained=True)
model.classifier=model.classifier = nn.Sequential(nn.Linear(1920,1024),nn.LeakyReLU(),nn.Linear(1024,101))
model.load_state_dict(torch.load(checkpoint_path,map_location='cpu'),strict=False)
model.eval()
#from PIL import Image
return model
def get_tensor(image_bytes):
my_transforms=transforms.Compose([transforms.Resize(255),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
[0.485,0.456,0.406],
[0.229,0.224,0.225])])
image=Image.open(io.BytesIO(image_bytes))
return my_transforms(image).unsqueeze(0)