Пример #1
0
    def net_inference(self, msgs):
        assert isinstance(msgs, pb.ForwardMsgs)
        start = time.time()
        img_batch = mx.nd.array(
            np.zeros((self.batch_size, 3, self.width, self.height)))
        for index, msg in enumerate(msgs.msgs):
            arr = np.frombuffer(msg.network_input_buf,
                                dtype=np.float32).reshape(
                                    (3, self.width, self.height))
            img_batch[index] = arr
        start_forward = time.time()

        with self.lock:
            self.mod.forward(Batch([img_batch]))
            output_batch = self.mod.get_outputs()[0].asnumpy()
        end_forward = time.time()
        msgs_out = []
        for i in range(len(msgs.msgs)):
            msgs_out.append(
                pb.ForwardMsg(network_output_buf=cPickle.dumps(
                    output_batch[i], protocol=cPickle.HIGHEST_PROTOCOL)))
        log.info('{} use time {}, forward time {}, batch_size: {}/{}'.format(
            self.app_name,
            time.time() - start, end_forward - start_forward, len(msgs.msgs),
            self.batch_size))
        return pb.ForwardMsgs(msgs=msgs_out)
Пример #2
0
    def net_inference(self, request):
        assert isinstance(request, pb.InferenceRequest)
        img = load_imagev2(request.data.body)
        assert img.ndim == 3  # TODO
        img_height, img_width, _ = img.shape
        img_cls = cls_preProcessImage(img)
        img_det = det_preProcessImage(img)
        forward_req = {'img_cls': img_cls, 'img_det': img_det}
        msg = pb.ForwardMsg(network_input_buf=cPickle.dumps(
            forward_req, protocol=cPickle.HIGHEST_PROTOCOL),
                            reqid=request.reqid)
        msg_out = self.inference_req.inference_msg(msg)
        output = cPickle.loads(msg_out.network_output_buf)
        image_index = json.loads(
            msg_out.meta['data'].decode('utf8'))['image_index']

        cls_result = cls_post_eval(output['output_fine'],
                                   output['output_coarse'], image_index,
                                   self.cls_model)
        det_result = det_post_eval(img_height, img_width, output['output_det'],
                                   self.det_label_dict, image_index)

        cls_result = cls_merge_det(cls_result, det_result, self.cls_model,
                                   self.det_model)
        cls_result = merge_confidences(cls_result, self.cls_model)

        resp = postProcess(cls_result, det_result, self.det_model)
        return pb.InferenceResponse(code=200, result=json.dumps(resp))
Пример #3
0
 def net_inference(self, msgs):
     assert isinstance(msgs, pb.ForwardMsgs)
     start = time.time()
     start_forward = time.time()
     assert len(msgs.msgs) == 1
     arr = cPickle.loads(msgs.msgs[0].network_input_buf)
     data = [[mx.nd.array(arr['im_array']), mx.nd.array(arr['im_info'])]]
     data_batch = mx.io.DataBatch(data=data,
                                  label=[None],
                                  provide_data=arr['data_shapes'],
                                  provide_label=[None])
     with self.lock:
         # https://github.com/ataraxialab/Deformable-ConvNets/blob/master/rfcn/core/tester.py#L124
         scores, boxes, _ = im_detect(self.predictor, data_batch,
                                      ['data', 'im_info'], arr['im_scale'],
                                      config)
     end_forward = time.time()
     msgs_out = []
     for _ in range(len(msgs.msgs)):
         output = {
             'scores': scores[0],
             'boxes': boxes[0],
         }
         msgs_out.append(
             pb.ForwardMsg(network_output_buf=cPickle.dumps(
                 output, protocol=cPickle.HIGHEST_PROTOCOL)))
     log.info('{} use time {}, forward time {}, batch_size: {}/{}'.format(
         self.app_name,
         time.time() - start, end_forward - start_forward, len(msgs.msgs),
         self.batch_size))
     return pb.ForwardMsgs(msgs=msgs_out)
Пример #4
0
 def net_inference(self, msgs):  # pylint: disable=no-self-use
     assert isinstance(msgs, pb.ForwardMsgs)
     msgs_out = []
     for i in msgs:
         msg_out = pb.ForwardMsg()
         msg_out.network_output_buf = msgs[i].network_input_buf
         msgs_out.append(msg_out)
     return pb.ForwardMsgs(msgs=msgs_out)
Пример #5
0
    def net_inference(self, request):
        # pylint: disable=too-many-locals
        assert isinstance(request, pb.InferenceRequest)

        img = _load_image(request.data.body, self.image_width,
                          self.image_height, self.mean_value, self.std_value)
        msg = pb.ForwardMsg(network_input_buf=img.tobytes(),
                            reqid=request.reqid)
        msg_out = self.inference_req.inference_msg(msg)
        output = cPickle.loads(msg_out.network_output_buf)
        return pb.InferenceResponse(code=200,
                                    result=json.dumps(
                                        _build_result(output, self.labels)))
Пример #6
0
    def net_inference(self, request):
        classes_dict = self.labels['class']
        threshold_dict = self.labels['minMt']  # minModelThreshold
        assert isinstance(request, pb.InferenceRequest)

        img = load_imagev2(request.data.body)
        assert img.ndim == 3  # TODO

        nms = py_nms_wrapper(config.TEST.NMS)

        if img.shape[0] > img.shape[1]:
            long_side, short_side = img.shape[0], img.shape[1]
        else:
            long_side, short_side = img.shape[1], img.shape[0]

        if short_side > 0 and float(long_side) / float(short_side) > 50.0:
            raise ErrorBase(
                400,
                'aspect ration is too large, long_size:short_side should not larger than 50.0'
            )

        batch = generate_batch(img)
        msg = pb.ForwardMsg()
        msg.network_input_buf = cPickle.dumps(
            batch, protocol=cPickle.HIGHEST_PROTOCOL)

        msg_out = self.inference_req.inference_msg(msg)
        scores = []
        boxes = []

        r = cPickle.loads(msg_out.network_output_buf)
        scores.append(r['scores'])
        boxes.append(r['boxes'])

        det_ret = []
        for cls_index in sorted(classes_dict.keys()):
            cls_ind = cls_index
            cls_name = classes_dict.get(cls_ind)
            cls_boxes = boxes[0][:, 4:8] if config.CLASS_AGNOSTIC else boxes[
                0][:, 4 * cls_ind:4 * 4 * (cls_ind + 1)]
            cls_scores = scores[0][:, cls_ind, np.newaxis]
            threshold = float(threshold_dict[cls_ind])
            keep = np.where(cls_scores > threshold)[0]
            dets = np.hstack(
                (cls_boxes, cls_scores)).astype(np.float32)[keep, :]
            keep = nms(dets)
            det_ret.extend(
                _build_result(det, cls_name, cls_ind, self.labels)
                for det in dets[keep, :])
        return pb.InferenceResponse(code=200,
                                    result=json.dumps({'detections': det_ret}))
Пример #7
0
    def net_inference(self, msgs):
        assert isinstance(msgs, pb.ForwardMsgs)
        start = time.time()
        start_forward = time.time()

        for index, msg in enumerate(msgs.msgs):
            r = cPickle.loads(msg.network_input_buf)
            img_cls = r['img_cls']
            assert img_cls.shape == (3, 225, 225)
            img_det = r['img_det']
            assert img_det.shape == (3, 320, 320)

            self.net_fine.blobs['data'].data[index] = img_cls
            self.net_coarse.blobs['data'].data[index] = img_cls
            self.net_det.blobs['data'].data[index] = img_det
        with self.lock:
            output_fine = self.net_fine.forward()
            output_coarse = self.net_coarse.forward()
            output_det = self.net_det.forward()
        assert output_fine['prob'].shape[1:] == (48, 1, 1)
        # shape 第一维是 batch_size,第二维度是48类
        assert output_coarse['prob'].shape[1:] == (7, 1, 1)
        # shape 第一维是 batch_size,第二维度是7类
        assert output_det['detection_out'].shape[1] == 1
        assert output_det['detection_out'].shape[3] == 7
        # shape 第一维是 batch_size,第三维度是检测到的物体数目,第四维度是类别
        end_forward = time.time()
        buf = cPickle.dumps(
            {
                'output_fine': output_fine,
                'output_coarse': output_coarse,
                'output_det': output_det
            },
            protocol=cPickle.HIGHEST_PROTOCOL)
        msgs_out = []
        for i in range(len(msgs.msgs)):
            msgs_out.append(
                pb.ForwardMsg(network_output_buf=buf,
                              meta={
                                  "data":
                                  json.dumps({
                                      'image_index': i
                                  }).encode('utf8')
                              }))
        log.info('{} use time {}, forward time {}, batch_size: {}/{}'.format(
            self.app_name,
            time.time() - start, end_forward - start_forward, len(msgs.msgs),
            self.batch_size))
        return pb.ForwardMsgs(msgs=msgs_out)
Пример #8
0
 def process(buf):
     msg = pb.ForwardMsg()
     msg.ParseFromString(buf)
     inputs.append(msg)