Repository navigation
Expand file tree
/
Copy pathpredictor.py
More file actions
198 lines (159 loc) · 7.09 KB
/
Copy pathpredictor.py
File metadata and controls
198 lines (159 loc) · 7.09 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -*- coding: utf-8 -*-
import sys
import json
import os
import warnings
import flask
import boto3
import io
from PIL import Image
import sys
sys.path.append('/opt/yolov5')
import argparse
import time
from pathlib import Path
import cv2
import torch
import torch.backends.cudnn as cudnn
from models.experimental import attempt_load
from utils.datasets import LoadStreams, LoadImages
from utils.general import check_img_size, check_requirements, check_imshow, non_max_suppression, apply_classifier, \
scale_coords, xyxy2xywh, strip_optimizer, set_logging, increment_path
from utils.plots import colors
from utils.torch_utils import select_device, load_classifier, time_sync
# The flask app for serving predictions
app = flask.Flask(__name__)
s3_client = boto3.client('s3')
name = 'tutorial'
weights = '/opt/ml/model/{}/weights/best.pt'.format(name)
imgsz = 640
conf_thres = 0.02
iou_thres = 0.45
max_det = 1000
device = 'cpu'
classes = None
agnostic_nms = False
augment = False
half = False
def init(weights='yolov5s.pt', # model.pt path(s)
source='data/images', # file/dir/URL/glob, 0 for webcam
imgsz=640, # inference size (pixels)
conf_thres=0.25, # confidence threshold
iou_thres=0.45, # NMS IOU threshold
max_det=1000, # maximum detections per image
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
view_img=False, # show results
save_txt=False, # save results to *.txt
save_conf=False, # save confidences in --save-txt labels
save_crop=False, # save cropped prediction boxes
nosave=False, # do not save images/videos
classes=None, # filter by class: --class 0, or --class 0 2 3
agnostic_nms=False, # class-agnostic NMS
augment=False, # augmented inference
update=False, # update all models
project='runs/detect', # save results to project/name
name='exp', # save results to project/name
exist_ok=False, # existing project/name ok, do not increment
line_thickness=3, # bounding box thickness (pixels)
hide_labels=False, # hide labels
hide_conf=False, # hide confidences
half=False, # use FP16 half-precision inference
):
# Initialize
set_logging()
device = select_device(device)
half &= device.type != 'cpu' # half precision only supported on CUDA
# Load model
model = attempt_load(weights, map_location=device) # load FP32 model
stride = int(model.stride.max()) # model stride
imgsz = check_img_size(imgsz, s=stride) # check image size
names = model.module.names if hasattr(model, 'module') else model.names # get class names
if half:
model.half() # to FP16
# Run inference
if device.type != 'cpu':
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once
return model, names
model, names = init(weights=weights, imgsz=imgsz, conf_thres=conf_thres, iou_thres=iou_thres, max_det=max_det, device=device, classes=classes, agnostic_nms=agnostic_nms, augment=augment, half=half)
def detect(source):
stride = int(model.stride.max()) # model stride
dataset = LoadImages(source, img_size=imgsz, stride=stride)
t0 = time.time()
for path, img, im0s, vid_cap, s in dataset:
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Inference
t1 = time_sync()
pred = model(img, augment=augment)[0]
# Apply NMS
pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
t2 = time_sync()
# Process detections
result = []
for i, det in enumerate(pred): # detections per image
p, s, im0, frame = path, '', im0s.copy(), getattr(dataset, 'frame', 0)
p = Path(p) # to Path
s += '%gx%g ' % img.shape[2:] # print string
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
imc = im0 # for save_crop
if len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()
# Print results
for c in det[:, -1].unique():
n = (det[:, -1] == c).sum() # detections per class
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
# Write results
for *xyxy, conf, cls in reversed(det):
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
line = (cls, *xywh) # label format
line = ('%g ' * len(line)).rstrip() % line
result.append(line)
# Print time (inference + NMS)
print(f'{s}Done. ({t2 - t1:.3f}s)')
print('result:', result)
return result
@app.route('/ping', methods=['GET'])
def ping():
"""Determine if the container is working and healthy. In this sample container, we declare
it healthy if we can load the model successfully."""
# health = ScoringService.get_model() is not None # You can insert a health check here
health = 1
status = 200 if health else 404
# print("===================== PING ===================")
return flask.Response(response="{'status': 'Healthy'}\n", status=status, mimetype='application/json')
@app.route('/invocations', methods=['POST'])
def invocations():
"""Do an inference on a single batch of data. In this sample server, we take data as CSV, convert
it to a pandas data frame for internal use and then convert the predictions back to CSV (which really
just means one prediction per line, since there's a single column.
"""
data = None
print("================ INVOCATIONS =================")
#parse json in request
print ("<<<< flask.request.content_type", flask.request.content_type)
if flask.request.content_type == 'application/x-image':
image_as_bytes = io.BytesIO(flask.request.data)
img = Image.open(image_as_bytes)
download_file_name = '/tmp/tmp.jpg'
img.save(download_file_name)
print ("<<<<download_file_name ", download_file_name)
else:
data = flask.request.data.decode('utf-8')
data = json.loads(data)
bucket = data['bucket']
image_uri = data['image_uri']
download_file_name = '/tmp/'+image_uri.split('/')[-1]
print ("<<<<download_file_name ", download_file_name)
try:
s3_client.download_file(bucket, image_uri, download_file_name)
except:
#local test
download_file_name = './bus.jpg'
print('Download finished!')
inference_result = detect(download_file_name)
_payload = json.dumps(inference_result,ensure_ascii=False)
return flask.Response(response=_payload, status=200, mimetype='application/json')