forked from bonn0062/basic_model_deployment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommons.py
More file actions
25 lines (22 loc) · 800 Bytes
/
commons.py
File metadata and controls
25 lines (22 loc) · 800 Bytes
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
import io
import torch
import torch.nn as nn
from torchvision import models
from PIL import Image
import torchvision.transforms as transforms
def get_model():
checkpoint_path = 'checkpoint_final.pth'
model = models. densenet161(pretrained=True)
model.classifier = nn.Linear(2208, 102)
model.load_state_dict(torch.load(
checkpoint_path, map_location='cpu'), strict=False)
model.eval()
return model
def get_tensor(image_bytes):
my_transforms = 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])])
image = Image.open(io.BytesIO(image_bytes))
return my_transforms(image).unsqueeze(0)