예제 #1
0
class Sample(object):
    """
    样例入口
    """
    def __init__(self,
                 device_id,
                 model_path,
                 model_input_width,
                 model_input_height):
        self.device_id = device_id      # int
        self.model_path = model_path    # string
        self.model_id = None            # pointer
        self.context = None             # pointer

        self.input_data = None
        self.output_data = None
        self.model_desc = None          # pointer when using
        self.load_input_dataset = None
        self.load_output_dataset = None
        self.init_resource()

        self._model_input_width = model_input_width
        self._model_input_height = model_input_height

        self.model_process = Model(self.context,
                                   self.stream,
                                   self.model_path)

        self.dvpp_process = Dvpp(self.stream,
                                 model_input_width,
                                 model_input_height)

        self.sing_op = SingleOp(self.stream)

    def release_resource(self):
        if self.model_process:
            del self.model_process

        if self.dvpp_process:
            del self.dvpp_process

        if self.sing_op:
            del self.sing_op

        if self.stream:
            acl.rt.destroy_stream(self.stream)

        if self.context:
            acl.rt.destroy_context(self.context)
        acl.rt.reset_device(self.device_id)
        acl.finalize()
        print("[Sample] class Samle release source success")

    def init_resource(self):
        print("[Sample] init resource stage:")
        acl.init()
        ret = acl.rt.set_device(self.device_id)
        check_ret("acl.rt.set_device", ret)

        self.context, ret = acl.rt.create_context(self.device_id)
        check_ret("acl.rt.create_context", ret)

        self.stream, ret = acl.rt.create_stream()
        check_ret("acl.rt.create_stream", ret)
        print("[Sample] init resource stage success")

    def _transfer_to_device(self, img_path, dtype=np.uint8):
        img = np.fromfile(img_path, dtype=dtype)
        if "bytes_to_ptr" in dir(acl.util):
            bytes_data = img.tobytes()
            img_ptr = acl.util.bytes_to_ptr(bytes_data)
        else:
            img_ptr = acl.util.numpy_to_ptr(img)
        img_buffer_size = img.itemsize * img.size
        img_device, ret = acl.media.dvpp_malloc(img_buffer_size)
        check_ret("acl.media.dvpp_malloc", ret)
        ret = acl.rt.memcpy(img_device,
                            img_buffer_size,
                            img_ptr,
                            img_buffer_size,
                            ACL_MEMCPY_HOST_TO_DEVICE)
        check_ret("acl.rt.memcpy", ret)

        return img_device, img_buffer_size

    def forward(self, img_dict):
        img_path, _ = img_dict["path"], img_dict["dtype"]
        # copy images to device
        with Image.open(img_path) as image_file:
            width, height = image_file.size
            print("[Sample] width:{} height:{}".format(width, height))
            print("[Sample] image:{}".format(img_path))
        img_device, img_buffer_size = \
            self._transfer_to_device(img_path, img_dict["dtype"])

        # decode and resize
        dvpp_output_buffer, dvpp_output_size = \
            self.dvpp_process.run(img_device,
                                  img_buffer_size,
                                  width,
                                  height)
        self.model_process.run(
            dvpp_output_buffer,
            dvpp_output_size)
        self.sing_op.run(self.model_process.get_result())
        if img_device:
            acl.media.dvpp_free(img_device)
예제 #2
0
class Sample(object):
    def __init__(self, device_id, model_path, vdec_out_path, model_input_width,
                 model_input_height):
        self.device_id = device_id  # int
        self.model_path = model_path  # string
        self.context = None  # pointer
        self.stream = None
        self.model_input_width = model_input_width
        self.model_input_height = model_input_height,
        self.init_resource()
        self.model_process = Model(self.context, self.stream, model_path)
        self.vdec_process = Vdec(self.context, self.stream, vdec_out_path)
        self.dvpp_process = Dvpp(self.stream, model_input_width,
                                 model_input_height)
        self.model_input_width = model_input_width
        self.model_input_height = model_input_height
        self.vdec_out_path = vdec_out_path

    def init_resource(self):
        print("init resource stage:")
        acl.init()
        ret = acl.rt.set_device(self.device_id)
        check_ret("acl.rt.set_device", ret)

        self.context, ret = acl.rt.create_context(self.device_id)
        check_ret("acl.rt.create_context", ret)

        self.stream, ret = acl.rt.create_stream()
        check_ret("acl.rt.create_stream", ret)
        print("init resource stage success")

    def release_resource(self):
        print('[Sample] release source stage:')
        if self.dvpp_process:
            del self.dvpp_process

        if self.model_process:
            del self.model_process

        if self.vdec_process:
            del self.vdec_process

        if self.stream:
            ret = acl.rt.destroy_stream(self.stream)
            check_ret("acl.rt.destroy_stream", ret)

        if self.context:
            ret = acl.rt.destroy_context(self.context)
            check_ret("acl.rt.destroy_context", ret)

        ret = acl.rt.reset_device(self.device_id)
        check_ret("acl.rt.reset_device", ret)
        ret = acl.finalize()
        check_ret("acl.finalize", ret)
        print('[Sample] release source stage success')

    def _transfer_to_device(self, img):
        img_device = img["buffer"]
        img_buffer_size = img["size"]
        '''
        if the buffer is not in device, need to copy to device, but here, the data is from vdec, no need to copy.
        '''
        return img_device, img_buffer_size

    def forward(self, temp):
        _, input_width, input_height, _ = temp

        # vdec process,note:the input is h264 file,vdec output datasize need to be computed by strided width and height by 16*2
        self.vdec_process.run(temp)

        images_buffer = self.vdec_process.get_image_buffer()
        if images_buffer:
            for img_buffer in images_buffer:
                img_device, img_buffer_size = \
                    self._transfer_to_device(img_buffer)

                print("vdec output, img_buffer_size = ", img_buffer_size)
                # vpc process, parameters:vdec output buffer and size, original picture width and height.
                dvpp_output_buffer, dvpp_output_size = \
                    self.dvpp_process.run(img_device,
                                          img_buffer_size,
                                          input_width,
                                          input_height)

                ret = acl.media.dvpp_free(img_device)
                check_ret("acl.media.dvpp_free", ret)

                self.model_process.run(dvpp_output_buffer, dvpp_output_size)