forked from aws/amazon-sagemaker-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet18.py
37 lines (28 loc) · 1.05 KB
/
resnet18.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
def neo_preprocess(payload, content_type):
import PIL.Image # Training container doesn't have this package
import logging
import numpy as np
import io
logging.info('Invoking user-defined pre-processing function')
if content_type != 'application/x-image':
raise RuntimeError('Content type must be application/x-image')
f = io.BytesIO(payload)
# Load image and convert to RGB space
image = PIL.Image.open(f).convert('RGB')
# Resize
image = np.asarray(image.resize((224, 224)))
# Transpose
image = np.rollaxis(image, axis=2, start=0)[np.newaxis, :]
return image
def neo_postprocess(result):
import logging
import numpy as np
import json
logging.info('Invoking user-defined post-processing function')
# Softmax (assumes batch size 1)
result = np.squeeze(result)
result_exp = np.exp(result - np.max(result))
result = result_exp / np.sum(result_exp)
response_body = json.dumps(result.tolist())
content_type = 'application/json'
return response_body, content_type