tensorflow/model-optimization

How to extract original model's weight from trained QAT model?

doomooo opened this issue · 9 comments

System information

TensorFlow version (you are using): 2.8
Are you willing to contribute it (Yes/No):
Motivation

How to extract the original model's weight from the trained QAT model?
I want to extract the original model's weight and QAT params, so I can set int8 params in TensorRT manually.

Hi, I wonder how to extract the original model's weight from the trained QAT model? @thaink

Anyone can help take a look? Thanks! @thaink @rino20

Isn't the original model weights in checkpoint files or variables/ directory?

Thanks for your reply! @thaink
No, the weight is in quant_warp, and the name is changed.

I can extract the weights with some codes or tricks, but I wonder if there exist official methods. @thaink

I don't think we have an official way for that. This use with TensorRT isn't in our set of use-cases for now.

@Xhark could you review this thread?
Thanks

    model: original model
    model_q: quantized model

    for ly in model.layers:
        name = 'quant_' + ly.name
        if len(ly.variables) > 0:
            lly = model_q.get_layer(name)
            for var in ly.variables:
                for vvar in lly.variables:
                    if var.name == vvar.name:
                        var.assign(tf.Variable(vvar.numpy()))
                        break

@WillLiGitHub Thanks! I have extracted weights in this way.