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

时间:2019-08-18 10:52:28

标签: python tensorflow keras tensorflow-serving yolo

我试图在tf服务中提供keras-yolo3模型

这是我的模型摘要

y

还有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 的输出是这个

enter image description here

我使用tf服务docker托管了这个yolo模型

saved_model_cli show --dir 'serving/1' --all

运行成功

现在要测试我为此编写了客户端代码

这是我的客户端代码,用于访问托管在的tf服务器

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

我运行此代码并收到错误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

1 个答案:

答案 0 :(得分:0)

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