如何从tflite模型获取权重?

时间:2019-07-25 08:50:00

标签: tensorflow

我有一个简单的网络,并且使用张量流进行了修剪和量化。我专门按照本教程在我的网络上应用: https://www.tensorflow.org/model_optimization/guide/pruning/pruning_with_keras#convert_to_tensorflow_lite

最后,我得到了tflite文件。我想从该文件中提取权重。如何从这个量化模型中获得权重?我知道从“ h5”文件而不是从“ tflite”文件获取权重的方法。还是在对模型执行量化后还有其他方法来保存“ h5”文件?

2 个答案:

答案 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
    '''
  • Conv2D图层将具有三个与它们相关的命令:内核,偏差,conv_output,每个都有其标度,zero_points和张量。
  • 张量-是具有内核权重或偏差的np数组。对于conv_output或激活,这并不表示任何内容(不是中间输出)
  • 对于conv内核字典,张量是定形的(cout,k,k,cin)