-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictor.py
53 lines (37 loc) · 1.21 KB
/
predictor.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
44
45
46
47
48
49
50
51
52
53
import io
import logging
import os
import sys
import torch
import torch.nn as nn
import torchvision.models as models
from torchvision import transforms
from PIL import Image
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
NUM_CLASSES = 133
def net(device):
logger.info("Model creation started")
model = models.resnet50(pretrained=True)
for param in model.parameters():
param.requires_grad = False
model.fc = nn.Linear(model.fc.in_features, NUM_CLASSES)
model = model.to(device)
logger.info("Model creation done")
return model
def model_fn(model_dir):
model = net("cpu")
logger.info("Retrieving training model...", model_dir)
with open(os.path.join(model_dir, 'model.pth'), 'rb') as f:
model.load_state_dict(torch.load(f))
return model
def input_fn(request_body, content_type):
'''
before predicting the image do some preprocess to put the image into the correct format
'''
image = Image.open(io.BytesIO(request_body))
transformation = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor()])
return transformation(image).unsqueeze(0)