我有一个简单的网络,并且使用张量流进行了修剪和量化。我专门按照本教程在我的网络上应用: https://www.tensorflow.org/model_optimization/guide/pruning/pruning_with_keras#convert_to_tensorflow_lite
最后,我得到了tflite文件。我想从该文件中提取权重。如何从这个量化模型中获得权重?我知道从“ h5”文件而不是从“ tflite”文件获取权重的方法。还是在对模型执行量化后还有其他方法来保存“ h5”文件?
答案 0 :(得分:0)
我已经使用Netron解决了这个问题。在Netron中,权重可以另存为numpy数组。 https://github.com/lutzroeder/netron
答案 1 :(得分:0)
创建一个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
'''