GFPGAN-Quantization

THINK_STUDIO: <p>Hi ,<br>
I have been trying to quantize a GFPGAN model, to increase the inference speed.<br>
here is my code for same[ after i saw the official pytorch Youtube Video and read all documentations - seems like i am missing something critical as <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=12" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:" loading="lazy" width="20" height="20"><br>
repo link: <a href="https://github.com/TencentARC/GFPGAN/tree/master/gfpgan" class="inline-onebox" rel="noopener nofollow ugc">GFPGAN/gfpgan at master · TencentARC/GFPGAN · GitHub</a></p>
<p>1- My quant model size is &gt; original model size.<br>
my code here:</p>
<pre><code class="lang-auto">import cv2
import glob
import numpy as np
import os
import torch
from basicsr.utils import imwrite

from gfpgan import GFPGANer
import copy

import time
from time import perf_counter
from gfpgan.archs.gfpganv1_clean_arch import GFPGANv1Clean
from torch import nn
# Hello, I am trying to quantize a model. I have done post training static quantization following the tutorial. During the conversion, I:


#get the older model                         #ok
mymodel = GFPGANv1Clean(
                out_size=512,
                num_style_feat=512,
                channel_multiplier=2,
                decoder_load_path=None,
                fix_decoder=False,
                num_mlp=8,
                input_is_latent=True,
                different_w=True,
                narrow=1,
                sft_half=True)

#load the state_dict
mymodel.load_state_dict(torch.load('C:/Users/Risin/Desktop/GFPGAN/GFPGANv1.3.pth')['params_ema']) #k

#set to eval()

mymodel.eval() #ok

backend = "fbgemm"
"""Insert stubs"""
m = nn.Sequential(torch.quantization.QuantStub(), 
                  mymodel, 
                  torch.quantization.DeQuantStub())

"""Prepare"""
mymodel.qconfig = torch.quantization.get_default_qconfig(backend)
qmodel =torch.quantization.prepare(mymodel, inplace=False)

"""Calibrate
- This example uses random data for convenience. Use representative (validation) data instead.
"""
qmodel.eval()
with torch.inference_mode():
    

    for _ in range(10):
        x = torch.rand(1,3,512, 512)
        m(x)
    
"""Convert"""
torch.quantization.convert(qmodel)
torch.save(qmodel.state_dict(),'gulab_abhishek.pt')
</code></pre>
<p>in another approach i used deepcopy and code used is mentioned below:</p>
<pre><code class="lang-auto">gfpgan2 = copy.deepcopy(gfpgan)

#check model layer
from torchvision import models
from torchsummary import summary
print(dir(gfpgan2))
# backend = "fbgemm"
# """Insert stubs"""
# m = nn.Sequential(torch.quantization.QuantStub(), 
#                   gfpgan2, 
#                   torch.quantization.DeQuantStub())

# """Prepare"""
# gfpgan2.qconfig = torch.quantization.get_default_qconfig(backend)
# torch.quantization.prepare(gfpgan2, inplace=True)

# """Calibrate
# - This example uses random data for convenience. Use representative (validation) data instead.
# """
# with torch.inference_mode():
#   for _ in range(10):
#     x = torch.rand(1,3,512, 512)
#     m(x)
    
# """Convert"""
# torch.quantization.convert(gfpgan2, inplace=True)
torch.save(gfpgan2.state_dict(), 'quantized_modelv2.pth')

2- in second method qmodel size is nearly 1/2 of the original model size, but here i get another error in inferencing,
i get missing keys errors or i just can't load model.

 File "C:\Users\Risin\Desktop\GFPGAN\inference_TDI.py", line 19, in &lt;module&gt;
    restorer = GFPGANer(
  File "C:\Users\Risin\Desktop\GFPGAN\gfpgan\utils.py", line 102, in __init__
    self.gfpgan.load_state_dict(torch.load("C:/Users/Risin/Desktop/GFPGAN/quantized_modelv2.pth")['params_ema'])
KeyError: 'params_ema'
</code></pre>

jerryzh168: <aside class="quote no-group" data-username="THINK_STUDIO" data-post="1" data-topic="169219">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/think_studio/48/36129_2.png" class="avatar"> THINK_STUDIO:</div>
<blockquote>
<pre><code class="lang-auto">    self.gfpgan.load_state_dict(torch.load("C:/Users/Risin/Desktop/GFPGAN/quantized_modelv2.pth")['params_ema'])
</code></pre>
</blockquote>
</aside>
<p>sorry for the late reply, does saving and loading state_dict for gfpgan model it work without quantization?</p>