|
a |
|
b/tools/torchserve/mmseg_handler.py |
|
|
1 |
# Copyright (c) OpenMMLab. All rights reserved. |
|
|
2 |
import base64 |
|
|
3 |
import os |
|
|
4 |
|
|
|
5 |
import cv2 |
|
|
6 |
import mmcv |
|
|
7 |
import torch |
|
|
8 |
from mmcv.cnn.utils.sync_bn import revert_sync_batchnorm |
|
|
9 |
from ts.torch_handler.base_handler import BaseHandler |
|
|
10 |
|
|
|
11 |
from mmseg.apis import inference_segmentor, init_segmentor |
|
|
12 |
|
|
|
13 |
|
|
|
14 |
class MMsegHandler(BaseHandler): |
|
|
15 |
|
|
|
16 |
def initialize(self, context): |
|
|
17 |
properties = context.system_properties |
|
|
18 |
self.map_location = 'cuda' if torch.cuda.is_available() else 'cpu' |
|
|
19 |
self.device = torch.device(self.map_location + ':' + |
|
|
20 |
str(properties.get('gpu_id')) if torch.cuda. |
|
|
21 |
is_available() else self.map_location) |
|
|
22 |
self.manifest = context.manifest |
|
|
23 |
|
|
|
24 |
model_dir = properties.get('model_dir') |
|
|
25 |
serialized_file = self.manifest['model']['serializedFile'] |
|
|
26 |
checkpoint = os.path.join(model_dir, serialized_file) |
|
|
27 |
self.config_file = os.path.join(model_dir, 'config.py') |
|
|
28 |
|
|
|
29 |
self.model = init_segmentor(self.config_file, checkpoint, self.device) |
|
|
30 |
self.model = revert_sync_batchnorm(self.model) |
|
|
31 |
self.initialized = True |
|
|
32 |
|
|
|
33 |
def preprocess(self, data): |
|
|
34 |
images = [] |
|
|
35 |
|
|
|
36 |
for row in data: |
|
|
37 |
image = row.get('data') or row.get('body') |
|
|
38 |
if isinstance(image, str): |
|
|
39 |
image = base64.b64decode(image) |
|
|
40 |
image = mmcv.imfrombytes(image) |
|
|
41 |
images.append(image) |
|
|
42 |
|
|
|
43 |
return images |
|
|
44 |
|
|
|
45 |
def inference(self, data, *args, **kwargs): |
|
|
46 |
results = [inference_segmentor(self.model, img) for img in data] |
|
|
47 |
return results |
|
|
48 |
|
|
|
49 |
def postprocess(self, data): |
|
|
50 |
output = [] |
|
|
51 |
|
|
|
52 |
for image_result in data: |
|
|
53 |
_, buffer = cv2.imencode('.png', image_result[0].astype('uint8')) |
|
|
54 |
content = buffer.tobytes() |
|
|
55 |
output.append(content) |
|
|
56 |
return output |