简体   繁体   English

如何修复'ValueError:提供的元素过多。 张量流中最多是否需要错误?

[英]How to fix 'ValueError: Too many elements provided. Needed at most' error in tensorflow?

I have trying to serve keras-yolo3 model on tf-serving 我试图在TF服务上提供keras-yolo3模型

this is my model summary 这是我的模型摘要

Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, None, None, 3 0                                            
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, None, None, 3 864         input_1[0][0]                    
__________________________________________________________________________________________________
batch_normalization_1 (BatchNor (None, None, None, 3 128         conv2d_1[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_1 (LeakyReLU)       (None, None, None, 3 0           batch_normalization_1[0][0]      
__________________________________________________________________________________________________
zero_padding2d_1 (ZeroPadding2D (None, None, None, 3 0           leaky_re_lu_1[0][0]              
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, None, None, 6 18432       zero_padding2d_1[0][0]           
__________________________________________________________________________________________________
batch_normalization_2 (BatchNor (None, None, None, 6 256         conv2d_2[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_2 (LeakyReLU)       (None, None, None, 6 0           batch_normalization_2[0][0]      
__________________________________________________________________________________________________
conv2d_3 (Conv2D)               (None, None, None, 3 2048        leaky_re_lu_2[0][0]              
__________________________________________________________________________________________________
batch_normalization_3 (BatchNor (None, None, None, 3 128         conv2d_3[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_3 (LeakyReLU)       (None, None, None, 3 0           batch_normalization_3[0][0]      
__________________________________________________________________________________________________
conv2d_4 (Conv2D)               (None, None, None, 6 18432       leaky_re_lu_3[0][0]              
__________________________________________________________________________________________________
batch_normalization_4 (BatchNor (None, None, None, 6 256         conv2d_4[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_4 (LeakyReLU)       (None, None, None, 6 0           batch_normalization_4[0][0]      
__________________________________________________________________________________________________
add_1 (Add)                     (None, None, None, 6 0           leaky_re_lu_2[0][0]              
                                                                 leaky_re_lu_4[0][0]              
__________________________________________________________________________________________________
zero_padding2d_2 (ZeroPadding2D (None, None, None, 6 0           add_1[0][0]                      
__________________________________________________________________________________________________
conv2d_5 (Conv2D)               (None, None, None, 1 73728       zero_padding2d_2[0][0]           
__________________________________________________________________________________________________
batch_normalization_5 (BatchNor (None, None, None, 1 512         conv2d_5[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_5 (LeakyReLU)       (None, None, None, 1 0           batch_normalization_5[0][0]      
__________________________________________________________________________________________________
conv2d_6 (Conv2D)               (None, None, None, 6 8192        leaky_re_lu_5[0][0]              
__________________________________________________________________________________________________
batch_normalization_6 (BatchNor (None, None, None, 6 256         conv2d_6[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_6 (LeakyReLU)       (None, None, None, 6 0           batch_normalization_6[0][0]      
__________________________________________________________________________________________________
conv2d_7 (Conv2D)               (None, None, None, 1 73728       leaky_re_lu_6[0][0]              
__________________________________________________________________________________________________
batch_normalization_7 (BatchNor (None, None, None, 1 512         conv2d_7[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_7 (LeakyReLU)       (None, None, None, 1 0           batch_normalization_7[0][0]      
__________________________________________________________________________________________________
add_2 (Add)                     (None, None, None, 1 0           leaky_re_lu_5[0][0]              
                                                                 leaky_re_lu_7[0][0]              
__________________________________________________________________________________________________
conv2d_8 (Conv2D)               (None, None, None, 6 8192        add_2[0][0]                      
__________________________________________________________________________________________________
batch_normalization_8 (BatchNor (None, None, None, 6 256         conv2d_8[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_8 (LeakyReLU)       (None, None, None, 6 0           batch_normalization_8[0][0]      
__________________________________________________________________________________________________
conv2d_9 (Conv2D)               (None, None, None, 1 73728       leaky_re_lu_8[0][0]              
__________________________________________________________________________________________________
batch_normalization_9 (BatchNor (None, None, None, 1 512         conv2d_9[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_9 (LeakyReLU)       (None, None, None, 1 0           batch_normalization_9[0][0]      
__________________________________________________________________________________________________
add_3 (Add)                     (None, None, None, 1 0           add_2[0][0]                      
                                                                 leaky_re_lu_9[0][0]              
__________________________________________________________________________________________________
zero_padding2d_3 (ZeroPadding2D (None, None, None, 1 0           add_3[0][0]                      
__________________________________________________________________________________________________
conv2d_10 (Conv2D)              (None, None, None, 2 294912      zero_padding2d_3[0][0]           
__________________________________________________________________________________________________
batch_normalization_10 (BatchNo (None, None, None, 2 1024        conv2d_10[0][0]                  
__________________________________________________________________________________________________
leaky_re_lu_10 (LeakyReLU)      (None, None, None, 2 0           batch_normalization_10[0][0]     
__________________________________________________________________________________________________
conv2d_11 (Conv2D)              (None, None, None, 1 32768       leaky_re_lu_10[0][0]             
__________________________________________________________________________________________________
batch_normalization_11 (BatchNo (None, None, None, 1 512         conv2d_11[0][0]                  
__________________________________________________________________________________________________
leaky_re_lu_11 (LeakyReLU)      (None, None, None, 1 0           batch_normalization_11[0][0]     
__________________________________________________________________________________________________
conv2d_12 (Conv2D)              (None, None, None, 2 294912      leaky_re_lu_11[0][0]             
__________________________________________________________________________________________________
batch_normalization_12 (BatchNo (None, None, None, 2 1024        conv2d_12[0][0]                  
__________________________________________________________________________________________________
leaky_re_lu_12 (LeakyReLU)      (None, None, None, 2 0           batch_normalization_12[0][0]     
__________________________________________________________________________________________________
add_4 (Add)                     (None, None, None, 2 0           leaky_re_lu_10[0][0]             
                                                                 leaky_re_lu_12[0][0]             
__________________________________________________________________________________________________
conv2d_13 (Conv2D)              (None, None, None, 1 32768       add_4[0][0]                      
__________________________________________________________________________________________________
    batch_normalization_13 (BatchNo (None, None, None, 1 512         conv2d_13[0][0]                  
    __________________________________________________________________________________________________
    LAST LAYERS

__________________________________________________________________________________________________
    leaky_re_lu_72 (LeakyReLU)      (None, None, None, 2 0           batch_normalization_72[0][0]     
    __________________________________________________________________________________________________
    conv2d_59 (Conv2D)              (None, None, None, 2 261375      leaky_re_lu_58[0][0]             
    __________________________________________________________________________________________________
    conv2d_67 (Conv2D)              (None, None, None, 2 130815      leaky_re_lu_65[0][0]             
    __________________________________________________________________________________________________
    conv2d_75 (Conv2D)              (None, None, None, 2 65535       leaky_re_lu_72[0][0]             
    ==================================================================================================
    Total params: 62,001,757
    Trainable params: 61,949,149
    Non-trainable params: 52,608

and also saved_model_cli show --dir 'serving/1' --all output is this, 以及saved_model_cli show --dir 'serving/1' --all输出是这个,

在此处输入图片说明

I hosted this yolo model using tf-serving docker 我使用tf-serving docker托管了这个yolo模型

docker run  --runtime= -t --rm -p 8900:8500 -p 8901:8501 -v /home/sp/Desktop/coco/serving:/models/yolo -e MODEL_NAME=yolo tensorflow/serving:latest

which is running succesfully 运行成功

now to test I wrote client code this 现在测试我写了客户端代码

this my client code to access the tf-server which is hosted at 这是我的客户端代码,用于访问托管在的tf服务器

def do_inference(hostport, work_dir, concurrency):
    """Tests PredictionService with concurrent requests.
    Args:
        hostport: Host:port address of the PredictionService.
        work_dir: The full path of working directory for test data set.
        concurrency: Maximum number of concurrent requests.
        num_tests: Number of test images to use.
    Returns:
        The classification error rate.
    Raises:
        IOError: An error occurred processing test data set.
    """
    channel = grpc.insecure_channel(hostport)
    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'yolo'
    request.model_spec.signature_name = 'serving_default'






        x= cv.imread("test.jpg")
        request.inputs['input_image'].CopyFrom(tf.contrib.util.make_tensor_proto(x, shape=[1,1,416, 416]))
        #result_counter.throttle()
        result_future = stub.Predict.future(request, 10.25)  # 5 seconds  

        # if things are wrong the callback will not come, so uncomment below to force the result
        # or get to see what is the bug
        res= result_future.result()
        response = numpy.array(res.outputs['conv2d_75/BiasAdd:0'].float_val)
        prediction = numpy.argmax(response)
        print("Predicted Result is ", prediction,"Detection Porbability= ",response[prediction])

I run this code and getting error ValueError: Too many elements provided. Needed at most 173056, but received 1367280 我运行此代码并收到错误ValueError: Too many elements provided. Needed at most 173056, but received 1367280 ValueError: Too many elements provided. Needed at most 173056, but received 1367280

And its pointing on line 并指向在线

request.inputs['input_image'].CopyFrom(tf.contrib.util.make_tensor_proto(x, shape=[1,1,416, 416]))

似乎input_image的大小比指定形状大得多(416、416),可能是您想预先调整input_image的大小或更改形状以匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM