如何解决tensorflow中的'ValueError: Too many elements provided. Needed at most'错误?

2024-05-13 08:13:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在tf上提供keras-yolo3模型

这是我的模型摘要

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

还有saved_model_cli show --dir 'serving/1' --all输出是

enter image description here

我用tf服务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

正在成功运行

为了测试,我编写了以下客户端代码

这是我的客户端代码,用于访问托管在的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])

我运行这段代码并得到错误ValueError: Too many elements provided. Needed at most 173056, but received 1367280

它的指向线

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


Tags: renoneaddrequesttfbatchnormalizationzero