PaddleHub icon indicating copy to clipboard operation
PaddleHub copied to clipboard

使用创建在线 API 服务时出现错误

Open yuzhaoOvO opened this issue 2 years ago • 1 comments

版本:PaddlePaddle 2.1.0

错误信息: "result": "too many values to unpack (expected 3)",

自定义转换器代码:

输入函数:

import re import base64 import numpy as np import paddle.fluid as fluid

from PIL import Image from io import BytesIO

将 Base64 转成 PIL Image

def base64_to_pil(image_base64): """read image from memory""" image_base64 = re.sub('^data:image/.+;base64,', '', image_base64) # 需要去除头部格式信息 image_mem = BytesIO(base64.b64decode(image_base64)) # python3 image_pil = Image.open(image_mem).convert('RGB') return image_pil

预处理代码

def preprocess(img): # 图像缩放 if img.mode != 'RGB': img = img.convert('RGB') img = img.resize((224, 224), Image.BILINEAR) img = np.array(img).astype('float32') img = img.transpose((2, 0, 1)) / 255 # HWC to CHW 及归一化 return img

def feed(data_args = None):

def reader_infer(data_args): """ 用于输入待预测数据,返回值会直接复值给paddle.static.Executor的run方法的feed参数。 模型加载方式选择: paddle2.x:paddle.static.load_inference_model paddle1.x:paddle.fluid.io.load_inference_model

:param data_args: 客户端请求参数,以k-v形式
:return dict
"""
def reader():
# x <type 'base64'> default value:'' 
    image = data_args['imagebase']

# todo
    
    # 格式转换
    image = base64_to_pil(image)
    
    # 预处理
    image = preprocess(image)
    
    image = image[np.newaxis,:, : ,:]  #reshape(-1,3,224,224)
   
    
    # 根据输入 Tensor 的名称和 Shape 构建 DataFeeder
    c, h, w = image.shape
    print("image shape:", image.shape)
    
    img = fluid.layers.data(name='x', shape=[c, h, w], dtype='float32')
    feeder = fluid.DataFeeder([img], fluid.CPUPlace())

    return [[image]], feeder
# return image, feeder
# arg_x = None
return reader

输出函数: def output(results, data_args): """ 模型评测结果输出转换 :param results :param data_args 请求参数 :return dict """ # todo

lab = np.argmax(result.numpy())


return {'lable': lab}

不知道是那里出了问题

yuzhaoOvO avatar Feb 03 '23 08:02 yuzhaoOvO

请问你是使用哪个模型的时候报了这个错误呢,从报错信息来看,感觉像是写了 xx,xx,xx,xx .. = result, result是个3元组,但是左边的变量大于了3个报的错。你贴的这些信息目前我还看不出是哪里的问题。 可以说明一下报错的模型,程序报错展示的报错栈,以及对应报错的代码片段。

rainyfly avatar Feb 03 '23 09:02 rainyfly