我有一个简单的网络,并且使用张量流完成了修剪和量化。我特别按照本教程应用于我的网络: https://www.tensorflow.org/model_optimization/guide/pruning/pruning_with_keras#convert_to_tensorflow_lite
最后,我得到了 tflite 文件。我想从这个文件中提取权重。我如何从这个量化模型中获得权重?我知道从“h5”文件获取权重的方法,但不知道从“tflite”文件获取权重的方法。或者在模型上执行量化后是否有其他方法保存“h5”文件?
创建 tflite 解释器并(可选)执行推理。tflite_interpreter.get_tensor_details() 将给出具有权重、偏差、尺度、零点等的字典列表。
'''
Create interpreter, allocate tensors
'''
tflite_interpreter = tf.lite.Interpreter(model_path='model_file.tflite')
tflite_interpreter.allocate_tensors()
'''
Check input/output details
'''
input_details = tflite_interpreter.get_input_details()
output_details = tflite_interpreter.get_output_details()
print("== Input details ==")
print("name:", input_details[0]['name'])
print("shape:", input_details[0]['shape'])
print("type:", input_details[0]['dtype'])
print("\n== Output details ==")
print("name:", output_details[0]['name'])
print("shape:", output_details[0]['shape'])
print("type:", output_details[0]['dtype'])
'''
Run prediction (optional), input_array has input's shape and dtype
'''
tflite_interpreter.set_tensor(input_details[0]['index'], input_array)
tflite_interpreter.invoke()
output_array = tflite_interpreter.get_tensor(output_details[0]['index'])
'''
This gives a list of dictionaries.
'''
tensor_details = tflite_interpreter.get_tensor_details()
for dict in tensor_details:
i = dict['index']
tensor_name = dict['name']
scales = dict['quantization_parameters']['scales']
zero_points = dict['quantization_parameters']['zero_points']
tensor = tflite_interpreter.tensor(i)()
print(i, type, name, scales.shape, zero_points.shape, tensor.shape)
'''
See note below
'''
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6929 次 |
| 最近记录: |