首页 > 解决方案 > ValueError:无法设置张量:得到 NOTYPE 类型的值,但输入 0 的预期类型为 FLOAT64,名称:serving_default_z_raw_min:0

问题描述

我正在尝试测试转换后的 tflite 模型,但出现以下错误

ValueError: Cannot set tensor: Got value of type NOTYPE but expected type FLOAT64 for input 0, name: serving_default_z_raw_min:0

我的代码如下所示

def run_tflite_accel_model(tflite_file, accel_input):
     interpreter = tf.lite.Interpreter(model_path=str(tflite_file))
     interpreter.allocate_tensors()
     input_details = interpreter.get_input_details()
     output_details = interpreter.get_output_details()

     for feature in FeatureSet: # FeatureSet here is an enum
          input_data = np.array(accel_input[feature.name], dtype=np.float64)
          interpreter.set_tensor(input_details[feature.value]["index"], input_data)

     interpreter.invoke()
     prediction = interpreter.get_tensor(output_details[0]["index"])
     print(prediction)
     return 1 if prediction >= 0.5 else 0



converted_model = "models/converted/model.tflite"
test_df = pd.read_csv('datasets/test.csv', delimiter=',')
positive_example = test_df[test_df['label'] == 1].drop(test_df.columns[[0, 1]], axis=1)
negative_example = test_df[test_df['label'] == 0].drop(test_df.columns[[0, 1]], axis=1)

prediction = run_tflite_accel_model(converted_model, positive_example)

测试.csv

group_timestamp,label,x_mean,x_median,x_stdev,x_raw_min,x_raw_max,x_abs_min,x_abs_max,y_mean,y_median,y_stdev,y_raw_min,y_raw_max,y_abs_min,y_abs_max,z_mean,z_median,z_stdev,z_raw_min,z_raw_max,z_abs_min,z_abs_max
2017-05-02 17:20:00,0,0.3764845679999999,0.3743770899999999,0.0326161594656692,0.28987303,0.45394787,0.28987303,0.45394787,1.25197066,1.24568915,0.0771853360531384,1.0819329,1.3764594,1.0819329,1.3764594,0.20113363268,0.20198536,0.0416523722047812,0.094861984,0.3127308,0.094861984,0.3127308
2017-05-02 20:15:20,1,0.18945148568,0.177686765,0.0693435673249932,0.0403703,0.32699412,0.0403703,0.32699412,-0.0355906866083799,-0.0298280714999999,0.0712611092133111,-0.22528648,0.14023209,8.583069000000001e-06,0.22528648,-0.086219737276,-0.0860004425,0.0690978288158458,-0.24878252,0.04982233,0.0023088455,0.24878252

口译员输入细节

[{'name': 'serving_default_z_raw_min:0', 'index': 0, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_y_raw_min:0', 'index': 1, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_y_median:0', 'index': 2, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_z_abs_max:0', 'index': 3, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_y_stdev:0', 'index': 4, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_z_abs_min:0', 'index': 5, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_y_mean:0', 'index': 6, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_z_stdev:0', 'index': 7, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_z_median:0', 'index': 8, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_z_raw_max:0', 'index': 9, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_z_mean:0', 'index': 10, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_x_mean:0', 'index': 11, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_y_raw_max:0', 'index': 12, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_y_abs_min:0', 'index': 13, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_y_abs_max:0', 'index': 14, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_x_stdev:0', 'index': 15, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_x_median:0', 'index': 16, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_x_raw_min:0', 'index': 17, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_x_raw_max:0', 'index': 18, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_x_abs_min:0', 'index': 19, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'serving_default_x_abs_max:0', 'index': 20, 'shape': array([1, 1], dtype=int32), 'shape_signature': array([-1,  1], dtype=int32), 'dtype': <class 'numpy.float64'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]

这是21个输入

标签: pythontensorflowtensorflow-lite

解决方案


由于输入详细信息的转储信息显示有 21 个输入,因此应该有 21input.set_tensor次调用才能将输入数据正确地提供给 TFLite 解释器。

interpreter.set_tensor(input_details[0]["index"], ...)
interpreter.set_tensor(input_details[1]["index"], ...)
...

您可以依靠 numpy 库来生成输入数据。例如,

input_ids = np.array(input_ids, dtype=np.int32)
input_mask = np.array(input_mask, dtype=np.int32)
segment_ids = np.array(segment_ids, dtype=np.int32)

interpreter.set_tensor(input_details[0]["index"], input_ids)
interpreter.set_tensor(input_details[1]["index"], input_mask)
interpreter.set_tensor(input_details[2]["index"], segment_ids)

请参考此链接


推荐阅读