MCPcopy Create free account
hub / github.com/MegEngine/MegFlow / Classify

Class Classify

flow-python/examples/application/simple_det_classify/classify.py:21–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19
20@register(inputs=['inp'], outputs=['out'])
21class Classify:
22 def __init__(self, name, arg):
23 logger.info("loading Resnet18 Classification...")
24 self.name = name
25 self.batch_size = arg['max_batch']
26 self.timeout = arg['wait_time']
27
28 # load ReID model and warmup
29 self._model = PredictorLite(path=arg['path'],
30 device=arg['device'],
31 device_id=arg['device_id'])
32 warmup_data = np.zeros((224, 224, 3), dtype=np.uint8)
33 self._model.inference(warmup_data)
34 logger.info("Resnet18 loaded.")
35
36 def expand(self, box, max_w, max_h, ratio):
37 l = box[0]
38 r = box[2]
39 t = box[1]
40 b = box[3]
41 center_x = (l + r) / 2
42 center_y = (t + b) / 2
43 w_side = (r - l) * ratio / 2
44 h_side = (b - t) * ratio / 2
45
46 l = center_x - w_side
47 r = center_x + w_side
48 t = center_y - h_side
49 b = center_y + h_side
50 l = max(0, l)
51 t = max(0, t)
52 r = min(max_w, r)
53 b = min(max_h, b)
54 return int(l), int(t), int(r), int(b)
55
56 def exec(self):
57 # batching
58 (envelopes, _) = self.inp.batch_recv(self.batch_size, self.timeout)
59
60 if len(envelopes) == 0:
61 return
62
63 crops = []
64 for env in envelopes:
65 data = env.msg['data']
66 items = env.msg['items']
67 for item in items:
68 assert 'bbox' in item
69 bbox = item['bbox']
70 l, t, r, b = self.expand(bbox, data.shape[1], data.shape[0],
71 1.1)
72 crop = cv2.resize(data[t:b, l:r], (224, 224))
73 crops.append(crop[np.newaxis, :])
74 if len(crops) > 0:
75 data = np.concatenate(crops)
76 types = self._model.inference_batch(data)
77 for _type in types:
78 self.out.send(envelopes[0].repack(json.dumps(str(_type))))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected