예제 #1
0
 def predict(self, data):
     try:
         shape = h5_input_shape(self.model.to_json(), logger)
         logging.info("to_json()方法可以调用,来解释模型输入shape")
     except:
         logging.info("to_json()方法无法调用,只能使用用户输入的shape数据")
         shape = self.shape
     print("该模型的输入形状是:", shape)
     if data["type"] == "b64":
         #如果b64以形如data:image/jpg;base64,开头,一定要把“data:image/jpg;base64,“这个前缀去掉,不然出错
         logging.info("处理的是b64图片")
         logger.info("处理的是b64图片")
         img_b64 = data["b64"]
         img_b64 = base64.b64decode(img_b64)
         img_b64 = universal_image_process(img_b64, shape, logger)
         result = self.model.predict(img_b64)
         # print("预测结果是:",result)
         return result
     if data["type"] == "url":
         try:
             image = requests.get(data["url"]).content  #直接得到bytes图片
             logging.info("获取网络图片成功")
         except Exception as e:
             logger.info(e)
         logger.info("处理的是网络图片")
         image = universal_image_process(image, shape, logger)
         result = self.model.predict(image)
         print("处理的结果是:", result)
         return result
예제 #2
0
 def predict(self, data):
     shape = self.session.get_inputs()[0].shape[1:]
     if data["type"] == "b64":
         #如果b64以形如data:image/jpg;base64,开头,一定要把“data:image/jpg;base64,“这个前缀去掉,不然出错
         print("处理的是b64图片")
         logger.info("处理的是b64图片")
         img_b64 = data["b64"]
         img_b64 = base64.b64decode(img_b64)
         img_b64 = universal_image_process(img_b64, shape, logger)
         input = self.session.get_inputs()[0].name
         output = self.session.get_outputs()[0].name
         result = self.session.run([output], {input: img_b64})[0]
         # print("预测结果是:",result)
         return result
     if data["type"] == "url":
         try:
             image = requests.get(data["url"]).content
             print("获取网络图片成功")
         except Exception as e:
             logger.info(e)
         logger.info("处理的是网络图片")
         image = universal_image_process(image, shape, logger)
         input = self.session.get_inputs()[0].name
         output = self.session.get_outputs()[0].name
         result = self.session.run([output], {input: image})[0]
         print("处理的结果是:", result)
         return result
예제 #3
0
    def predict(self, data):
        shape = [3, 128, 128]  #暂时写死,输入shape无法解析,需要使用者传入
        print("该模型的输入形状是:", shape)
        if data["type"] == "b64":
            # 如果b64以形如data:image/jpg;base64,开头,一定要把“data:image/jpg;base64,“这个前缀去掉,不然出错
            logging.info("处理的是b64图片")
            logger.info("处理的是b64图片")
            img_b64 = data["b64"]
            img_b64 = base64.b64decode(img_b64)
            img_b64 = universal_image_process(img_b64, shape, logger)
            result = self.model(img_b64)
            # print("预测结果是:",result)
            return result

        if data["type"] == "url":
            try:
                image = requests.get(data["url"]).content
                logging.info("获取网络图片成功")
            except Exception as e:
                logger.info(e)
            logger.info("处理的是网络图片")
            image = universal_image_process(image, shape, logger)
            result = self.model(image)
            result = self.model(image)
            print("处理的结果是:", result)
            return result
예제 #4
0
 def predict(self, data):
     shape = savemodel_input_shape(self.info, logger)
     print("该模型的输入形状是:", shape)
     if data["type"] == "b64":
         # 如果b64以形如data:image/jpg;base64,开头,一定要把“data:image/jpg;base64,“这个前缀去掉,不然出错
         print("处理的是b64图片")
         logger.info("处理的是b64图片")
         img_b64 = data["b64"]
         img_b64 = base64.b64decode(img_b64)
         img_b64 = universal_image_process(img_b64, shape, logger)
         result = self.model.predict(img_b64)
         # print("预测结果是:",result)
         return result
     if data["type"] == "url":
         try:
             image = requests.get(data["url"]).content
             print("获取网络图片成功")
         except Exception as e:
             logger.info(e)
         logger.info("处理的是网络图片")
         image = universal_image_process(image, shape, logger)
         np.set_printoptions(suppress=True)  #savedmodel返回的是科学计数法,取消科学计数法
         result = self.model.predict(image)
         print("处理的结果是:", result)
         return result
예제 #5
0
 def predict(self, data):
     input_tensor, output_tensor = tf.compat.v1.import_graph_def(
         self.graph_def,
         return_elements=[data["inputTensor"], data["outputTensor"]],
         name="")
     logging.info("成功还原出输入输出张量")
     # 获取输入张量的形状
     input_shape = tuple(input_tensor.shape)
     # 因为shape总是类似(None,32,32,1)这种形式,需要转为类似[32,32,1]这种形式
     print(tuple(input_shape), type(tuple(input_shape)))
     shape = []
     for element in input_shape:
         if element != None:
             shape.append(element)
     logging.info(f'输入的形状是:{shape}')
     if data["type"] == "b64":
         logger.info("处理的是b64图片")
         img_b64 = data["b64"]
         img_b64 = base64.b64decode(img_b64)  # 得到bytes形式的图片
         img_b64 = universal_image_process(
             img_b64, shape, logger)  # 最后得到的数据是在shape基础上,前面加个样本维,并且样本维的值为1
         result = PbModel.sess.run(output_tensor,
                                   feed_dict={input_tensor: img_b64})
         logging.info(f'预测的结果是:{result}')
         return result
     if data["type"] == "url":
         try:
             image = requests.get(
                 data["url"]).content  #返回的是bytes类型;text返回的是str类型
             logger.info("获取网络图片成功")
         except Exception as e:
             logger.info(e)
         logger.info("处理的是网络图片")
         image = universal_image_process(image, shape, logger)
         result = PbModel.sess.run(output_tensor,
                                   feed_dict={input_tensor: image})
         logging.info(f'预测的结果是:{result}')
         return result
예제 #6
0
    # 获取输入张量的形状
    input_shape = tuple(input_tensor.shape)
    # 因为shape总是类似(None,32,32,1)这种形式,需要转为类似[32,32,1]这种形式
    print(tuple(input_shape), type(tuple(input_shape)))
    shape = []
    for element in input_shape:
        if element != None:
            shape.append(element)

    print(shape)
    #测试用的图片

    # image_for_test = base64.b64decode(data["b64"])
    image_for_test = requests.get(data4["url"]).content
    image_for_test = universal_image_process(image_for_test, shape)
    print(image_for_test.shape)
    print(
        type(
            sess.run(
                [boxes_tensor, scores_tensor, classes_tensor],
                feed_dict={
                    input_tensor: image_for_test,
                    input_image_shape_tesor: [shape[0], shape[1]]
                })))
    print(
        str(
            sess.run(
                [boxes_tensor, scores_tensor, classes_tensor],
                feed_dict={
                    input_tensor: image_for_test,
예제 #7
0
    def predict(self, data=None):
        """
        对下面try...except的解释:
        try的部分写的是通过前端传来的输入输出张量名称来获取张量并进行预测。
        except部分写的是如果没有获得这个信息,那么就按照图灵引擎平台流上产生的模型所具有的固定signature来解析。
        """
        #为了处理平台上产生的YOLO,加一层异常处理专门处理YOLO
        is_yolo = False
        try:
            signature = self.model.signature_def['prediction_signature']
            logging.info("yolo模型的图定义信息加载完成")
            input_tensor_name = signature.inputs['input_img'].name
            boxes_tensor_name = signature.outputs['boxes'].name
            input_image_shape_name = signature.inputs['input_image_shape'].name
            scores = signature.outputs['scores'].name  # scores就是置信度
            classes = signature.outputs['classes'].name
            print(input_tensor_name, boxes_tensor_name, input_image_shape_name,
                  scores, classes)

            input_tensor = tf.compat.v1.get_default_graph().get_tensor_by_name(
                input_tensor_name)
            boxes_tensor = tf.compat.v1.get_default_graph().get_tensor_by_name(
                boxes_tensor_name)
            input_image_shape_tesor = tf.compat.v1.get_default_graph(
            ).get_tensor_by_name(input_image_shape_name)
            scores_tensor = tf.compat.v1.get_default_graph(
            ).get_tensor_by_name(scores)
            classes_tensor = tf.compat.v1.get_default_graph(
            ).get_tensor_by_name(classes)
            logging.info("yolo模型还原成功")
            is_yolo = True

        except:
            try:
                input_tensor = tf.compat.v1.get_default_graph(
                ).get_tensor_by_name(data["inputTensor"])
                output_tensor = tf.compat.v1.get_default_graph(
                ).get_tensor_by_name(data["outputTensor"])
                logging.info("传来的张量名称正确,已成功生成输入输出张量")
            except:
                logging.info("没有获得输入输出张量信息,只能按照固定格式解析")
                signature_def = self.model.signature_def
                signature_def = signature_def["test_signature"]
                input_tensor_name = signature_def.inputs['input_x'].name
                output_tensor_name = signature_def.outputs['outputs'].name
                print(input_tensor_name, output_tensor_name)
                input_tensor = tf.compat.v1.get_default_graph(
                ).get_tensor_by_name(input_tensor_name)
                output_tensor = tf.compat.v1.get_default_graph(
                ).get_tensor_by_name(output_tensor_name)
        # 获取输入张量的形状
        input_shape = tuple(input_tensor.shape)
        #因为shape总是类似(None,32,32,1)这种形式,需要转为类似[32,32,1]这种形式
        print(tuple(input_shape), type(tuple(input_shape)))
        shape = []
        for element in input_shape:
            if element != None:
                shape.append(element)

        print("输入形状是:", shape)
        #下面开始处理
        if data["type"] == "b64":
            # 如果b64以形如data:image/jpg;base64,开头,一定要把“data:image/jpg;base64,“这个前缀去掉,不然出错
            print("处理的是b64图片")
            logger.info("处理的是b64图片")
            img_b64 = data["b64"]
            img_b64 = base64.b64decode(img_b64)  #得到bytes形式的图片
            img_b64 = universal_image_process(
                img_b64, shape, logger)  #最后得到的数据是在shape基础上,前面加个样本维,并且样本维的值为1
            np.set_printoptions(suppress=True)  # savedmodel返回的是科学计数法形式,取消科学计数法
            if is_yolo:
                logging.info("yolo的结果")
                result = SavedModelTf1.sess.run(
                    [boxes_tensor, scores_tensor, classes_tensor],
                    feed_dict={
                        input_tensor: img_b64,
                        input_image_shape_tesor: [shape[0], shape[1]]
                    })
            else:

                result = SavedModelTf1.sess.run(
                    output_tensor, feed_dict={input_tensor: img_b64})
            print("预测结果是:", result)
            return result
        if data["type"] == "url":
            try:
                image = requests.get(
                    data["url"]).content  #返回的是bytes类型;text返回的是str类型
                print("获取网络图片成功")
            except Exception as e:
                logger.info(e)
            logger.info("处理的是网络图片")
            image = universal_image_process(image, shape, logger)
            np.set_printoptions(suppress=True)  #savedmodel返回的是科学计数法,取消科学计数法
            if is_yolo:
                logging.info("yolo的结果")
                result = SavedModelTf1.sess.run(
                    [boxes_tensor, scores_tensor, classes_tensor],
                    feed_dict={
                        input_tensor: image,
                        input_image_shape_tesor: [shape[0], shape[1]]
                    })
            else:
                result = SavedModelTf1.sess.run(
                    output_tensor, feed_dict={input_tensor: image})
            print("处理的结果是:", result)
            return result