Exemplo n.º 1
0
    def __init__(self):
        """
        create predictor manager
        """
        self.get_predictor_timeout = float(
            config.get('get.predictor.timeout', default_value=0.5))
        predictor_count = 0
        enable_mkl = False
        gpu_memory = 200
        gpu_device_ids = []

        model_dir = config.get('model.dir')
        device_type = config.get('device.type')
        if device_type == PredictorManager.CPU_DEVICE:
            cpu_predictor_count = int(
                config.getint('cpu.predictor.count', default_value=0))
            predictor_count = cpu_predictor_count
            enable_mkl = config.getboolean('cpu.enable_mkl',
                                           default_value=False)
        elif device_type == PredictorManager.GPU_DEVICE:
            gpu_predictor_count = int(
                config.getint('gpu.predictor.count', default_value=0))
            predictor_count = gpu_predictor_count
            gpu_memory = config.getint('gpu.predictor.memory',
                                       default_value=200)
            gpu_device_ids = config.get('gpu.predictor.device.id').split(',')
            gpu_device_ids = map(int, gpu_device_ids)
            if PYTHON_VERSION == 3:
                gpu_device_ids = list(gpu_device_ids)
            assert len(
                gpu_device_ids
            ) == gpu_predictor_count, "gpu predictor count doesn't match device count"
        else:
            raise Exception("no device to run predictor!")
        assert predictor_count > 0, "no device to predict"
        logger.info(
            "device type:{} predictor count:{} model dir:{} get predictor timeout:{}s"
            .format(device_type, predictor_count, model_dir,
                    self.get_predictor_timeout))
        self.predictor_queue = Queue(maxsize=predictor_count)

        for i in range(predictor_count):
            # Set config
            predictor_config = AnalysisConfig(model_dir)
            # predictor_config.specify_input_name()
            if device_type == PredictorManager.CPU_DEVICE:
                predictor_config.disable_gpu()
                if enable_mkl:
                    predictor_config.enable_mkldnn()
            else:
                device_id = gpu_device_ids[i]
                predictor_config.enable_use_gpu(gpu_memory, device_id)

            # Create PaddlePredictor
            predictor = create_paddle_predictor(predictor_config)
            self.predictor_queue.put(predictor)
Exemplo n.º 2
0
    def _load_model(self, model_path, model_name, num_label):
        """
        加载模型
        """
        gpu = config.get('gpu')
        ctx = get_context(int(gpu))
        pretrain_model = config.get('from_pretrained')
        cased = config.get('cased')
        is_cased = False
        if cased == "True":
            is_cased = True
        dropout_prob = float(config.get('dropout_prob'))
        ernie_model, vocab = get_ernie_model(pretrain_model, is_cased, ctx,
                                             dropout_prob)
        model = ERNIETagger(ernie_model, num_label + 1, dropout_prob)
        model.load_params(model_path, ctx=ctx)

        self.model_dict[model_name] = model
        self.vocab_dict["vocab_name"] = vocab
        self.ctx["gpu"] = ctx
Exemplo n.º 3
0
    def __init__(self):
        """
        create executor manager
        """
        self.get_executor_timeout = float(
            config.get('get.executor.timeout', default_value=0.5))
        model_dir = config.get('model.dir')
        # 将地址的json字符串转为字典,key为模型的名称,value为模型的地址,放便级联使用
        label_path = config.get('label_map')

        # 模型的字典
        self.model_dict = {}
        self.label_map = {}
        self.vocab_dict = {}
        self.ctx = {}
        self._load_label_map(label_path)
        #加载模型
        with open(model_dir, 'r', encoding='utf-8') as f:
            model_dir_data = json.load(f)
            for model_name in model_dir_data.keys():
                num_label = len(self.label_map[model_name])
                self._load_model(model_dir_data[model_name], model_name,
                                 num_label)
Exemplo n.º 4
0
def recognize():
    """
    处理post请求,并返回处理后的结果
    """
    # print(request.headers)
    # print(request.form)
    file = request.files['imgfile']
    # print(file, file.filename)
    # with open('receiveimg.jpg', 'wb') as f:
    #     f.write(file.stream.read())
    if not request.form:
        abort(400)
    if 'log_id' in request.form:
        log_id = request.form['log_id']
        logger.set_logid(str(log_id))
    else:
        logger.set_auto_logid()
        log_id = logger.get_logid()
    log_id = int(log_id)
    result = ApiResult(log_id=log_id)
    start_time = time.time()

    try:
        image_bytes = file.stream.read()
        task_data = process_data(image_bytes)
        # print(task_data.shape)
        start_time = time.time()
        max_request_time = float(config.get('max_request_time'))
        index = pivot.set_task_queue(task_data, max_request_time)
        if index is not None:
            pred = pivot.get_result_list(index)
            pred['log_id'] = log_id
            result.success(data=pred)
            # 历史记录入库
            label, score, user_id = result.top3[0][0], result.top3[0][1], 1
            sql = "insert into record(img,label,score,user_id) values (%s,{},{},{});".format(
                label, score, user_id)
            print(result.top3[0][0], result.top3[0][1])
            cursor.execute(sql, args=(image_bytes))
            database.commit()
        else:
            result.error(message="busy, wait then retry")
    except Exception as err:
        logger.error("infer exception: {}".format(err))
        result.error(message=str(err))

    period = time.time() - start_time
    logger.info("request cost:{}".format("%2.2f sec" % period))
    return json.dumps(result, default=lambda o: o.__dict__)
def process_data(image_bytes):
    """
    根据infer_type来处理数据,并返回, 可根据业务需要自行修改
    :param inputs:    原始的输入数据
    :return:
    """
    # 两种方式 处理数据公共的部分
    input_size = config.get('input.size').split(',')
    input_size = map(int, input_size)
    if PYTHON_VERSION == 3:
        input_size = list(input_size)
    start_time = time.time()
    origin, image_data = data_util.read_image(image_bytes, input_size[1:])
    # 如果是predictor,需要进一步的把数据处理为PaddleTensor
    if PREDICTOR == infer_type:
        image_data = data_util.warp_input(image_data, input_size)
    period = time.time() - start_time
    logger.info("prepare input cost time: {}".format("%2.2f sec" % period))
    return image_data
Exemplo n.º 6
0
def recognize():
    """
    处理post请求,并返回处理后的结果
    """
    if not request.json:
        abort(400)
    if 'orderNo' in request.json:
        orderNo = request.json['orderNo']
        logger.set_logid(str(orderNo))
    else:
        logger.set_auto_logid()
        orderNo = logger.get_logid()
    orderNo = int(orderNo)
    result = ApiResult(orderNo=orderNo)
    start_time = time.time()

    try:
        origin_data = request.json
        log.debug('origin_data: %s' % repr(origin_data))
        task_data = process_data(origin_data)
        log.debug('task_data_orderNo: %s' % repr(task_data))
        start_time = time.time()
        log.debug("prepare start_time cost time: {}".format("%2.2f sec" %
                                                            start_time))
        max_request_time = float(config.get('max_request_time'))
        index = pivot.set_task_queue(task_data, max_request_time)
        if index is not None:
            text = pivot.get_result_list(index)
            log.debug('text: %s' % repr(text))
            pred = dict()
            pred['result'] = text
            pred['orderNo'] = orderNo
            result.success(data=pred)
        else:
            result.error(message="busy, wait then retry")
    except Exception as err:
        logger.error("infer exception: {}".format(err))
        result.error(message=str(err))

    period = time.time() - start_time
    logger.info("request cost:{}".format("%2.2f sec" % period))
    return json.dumps(result, default=lambda o: o.__dict__)
# -*- coding: utf-8 -*-
"""
业务相关的写在这里
"""
import numpy as np
import time
import sys
EXECUTOR = 'executor'
PREDICTOR = 'predictor'

from conf.server_conf import config
from utils.logger_util import logger
import utils.data_util as data_util
infer_type = config.get('predictor_or_executor')

if PREDICTOR == infer_type:
    from infer_service import infer_with_predictor
elif EXECUTOR == infer_type:
    from infer_service import infer_with_executor
PYTHON_VERSION = int(sys.version.split('.')[0])


def business_process(inputs):
    """
    不同业务,数据处理的方式可能不一样,统一在这里修改和控制
    为了充分利用多线程并行的方式来处理数据,所以首先由生产者调用process_data来处理数据,并提交至任务队列
    此处直接从任务队列中获取处理好的数据即可,因此该部分应该和process_data一起修改
    :param inputs:      从任务队列中获取的预处理后的数据
    :return:
    """
Exemplo n.º 8
0
        logger.critical(
            "too less argv, need a start profile, run as: python app_server.py dev"
        )
        exit(1)
    else:
        # conf.server_conf.ServerConfig.init_evn(sys.argv[1])
        conf.server_conf.ServerConfig.init_evn("dev")

        from twisted.internet import reactor
        from twisted.web.resource import Resource
        from twisted.web.server import Site
        from twisted.web.wsgi import WSGIResource
        from util.consumer import InferConsumer
        from business_service import process_data

        pivot = Pivot(int(config.get("max_task_num")))
        infer_consumer = InferConsumer(pivot,
                                       float(config.get("max_get_time")))
        port = int(config.get("flask.server.port"))
        work_thread_count = int(config.get('work.thread.count'))
        site_root = str(config.get('server.root.site'))

        reactor.suggestThreadPoolSize(int(work_thread_count))
        flask_site = WSGIResource(reactor, reactor.getThreadPool(), app)
        root = Resource()
        root.putChild(
            site_root if PYTHON_VERSION == 2 else bytes(site_root,
                                                        encoding='utf-8'),
            flask_site)
        logger.info(
            "start app listen port:{} thread pool size:{} site root:{}".format(
    def __init__(self):
        """
        create executor manager
        """
        self.get_executor_timeout = float(
            config.get('get.executor.timeout', default_value=0.5))
        model_dir = config.get('model.dir')
        # 将地址的json字符串转为字典,key为模型的名称,value为模型的地址,放便级联使用
        model_dir = json.loads(model_dir)
        executor_count = 0
        enable_mkl = False
        gpu_memory = 200
        gpu_device_ids = []
        self.places_list = []

        # 模型的字典
        self.model_dict = {}
        for model_name in model_dir.keys():
            self._load_model(model_dir[model_name], model_name)

        device_type = config.get('device.type')
        if device_type == ExecutorManager.CPU_DEVICE:
            cpu_executor_count = int(
                config.getint('cpu.executor.count', default_value=0))
            executor_count = cpu_executor_count
        elif device_type == ExecutorManager.GPU_DEVICE:
            gpu_executor_count = int(
                config.getint('gpu.executor.count', default_value=0))
            executor_count = gpu_executor_count
            gpu_device_ids = config.get('gpu.executor.device.id').split(',')
            gpu_device_ids = map(int, gpu_device_ids)
            if PYTHON_VERSION == 3:
                gpu_device_ids = list(gpu_device_ids)
            assert len(
                gpu_device_ids
            ) == gpu_executor_count, "gpu executor count doesn't match device count"
        else:
            raise Exception("no device to run executor!")
        assert executor_count > 0, "no device to predict"
        logger.info(
            "device type:{} executor count:{} model dir:{} get executor timeout:{}s"
            .format(device_type, executor_count, model_dir,
                    self.get_executor_timeout))
        self.executor_queue = Queue(maxsize=executor_count)

        for i in range(executor_count):
            # executor is thread safe,
            # supports single/multiple-GPU running,
            # and single/multiple-CPU running,
            # if CPU device, only create one for all thread
            if device_type == ExecutorManager.CPU_DEVICE:
                if self.executor_queue.empty():
                    place = fluid.CPUPlace()
                    executor = fluid.Executor(place)
                    self._temp_executor = executor
                else:
                    executor = self._temp_executor
            else:
                device_id = gpu_device_ids[i]
                place = fluid.CUDAPlace(device_id)
                executor = fluid.Executor(place)

            self.executor_queue.put(executor)
Exemplo n.º 10
0

if __name__ == '__main__':
    """
    web server start
    """

    if len(sys.argv) < 2:
        logger.critical(
            "too less argv, need a start profile, run as: python app_server.py dev"
        )
        exit(1)
    else:
        conf.server_conf.ServerConfig.init_evn(sys.argv[1])

        infer_type = config.get('predictor_or_executor')

        from twisted.internet import reactor
        from twisted.web.resource import Resource
        from twisted.web.server import Site
        from twisted.web.wsgi import WSGIResource
        from utils.consumer import InferConsumer
        from business_service import process_data

        pivot = Pivot(int(config.get("max_task_num")))
        infer_consumer = InferConsumer(pivot,
                                       float(config.get("max_get_time")))
        port = int(config.get("flask.server.port"))
        work_thread_count = int(config.get('work.thread.count'))
        site_root = str(config.get('server.root.site'))