|  | 
| 1 |  | -# -*- coding: utf-8 -*- | 
| 2 |  | - | 
| 3 |  | -#import cv2 | 
| 4 |  | -import numpy as np | 
| 5 |  | -import time | 
| 6 | 1 | import torch | 
| 7 |  | -import pdb | 
| 8 | 2 | from collections import OrderedDict | 
| 9 |  | - | 
| 10 |  | -import sys | 
| 11 |  | -sys.path.append('.') | 
| 12 |  | -sys.path.append('./lib') | 
| 13 |  | -import torch.nn as nn | 
| 14 |  | -from torch.autograd import Variable | 
| 15 |  | -import onnxruntime | 
| 16 |  | -import timeit | 
| 17 |  | - | 
| 18 | 3 | import argparse | 
| 19 | 4 | from GFPGANReconsitution import GFPGAN | 
| 20 | 5 | 
 | 
| 21 | 6 | parser = argparse.ArgumentParser("ONNX converter") | 
| 22 | 7 | parser.add_argument('--src_model_path', type=str, default=None, help='src model path') | 
| 23 | 8 | parser.add_argument('--dst_model_path', type=str, default=None, help='dst model path') | 
| 24 |  | -parser.add_argument('--img_size', type=int, default=None, help='img size') | 
| 25 | 9 | args = parser.parse_args() | 
| 26 |  | -     | 
| 27 |  | -#device = torch.device('cuda') | 
|  | 10 | + | 
|  | 11 | +# device = torch.device('cuda') | 
| 28 | 12 | model_path = args.src_model_path | 
| 29 | 13 | onnx_model_path = args.dst_model_path | 
| 30 |  | -img_size = args.img_size | 
| 31 | 14 | 
 | 
| 32 |  | -model = GFPGAN()#.cuda() | 
|  | 15 | +model = GFPGAN()  # .cuda() | 
| 33 | 16 | 
 | 
| 34 |  | -x = torch.rand(1, 3, 512, 512)#.cuda() | 
|  | 17 | +x = torch.rand(1, 3, 512, 512)  # .cuda() | 
| 35 | 18 | 
 | 
| 36 |  | -state_dict = torch.load(model_path)['params_ema'] | 
|  | 19 | +state_dict = torch.load(model_path, weights_only=True)['params_ema'] | 
| 37 | 20 | new_state_dict = OrderedDict() | 
| 38 | 21 | for k, v in state_dict.items(): | 
| 39 | 22 |     # stylegan_decoderdotto_rgbsdot1dotmodulated_convdotbias | 
|  | 
| 45 | 28 |         new_state_dict[k] = v | 
| 46 | 29 |     else: | 
| 47 | 30 |         new_state_dict[k] = v | 
| 48 |  | -      | 
|  | 31 | + | 
| 49 | 32 | model.load_state_dict(new_state_dict, strict=False) | 
| 50 | 33 | model.eval() | 
| 51 | 34 | 
 | 
| 52 |  | -torch.onnx.export(model, x, onnx_model_path, | 
| 53 |  | -                    export_params=True, opset_version=11, do_constant_folding=True, | 
| 54 |  | -                    input_names = ['input'],output_names = []) | 
| 55 |  | - | 
| 56 |  | - | 
| 57 |  | -#### | 
| 58 |  | -try: | 
| 59 |  | -    original_model = onnx.load(onnx_model_path) | 
| 60 |  | -    passes = ['fuse_bn_into_conv'] | 
| 61 |  | -    optimized_model = optimizer.optimize(original_model, passes) | 
| 62 |  | -    onnx.save(optimized_model, onnx_model_path) | 
| 63 |  | -except: | 
| 64 |  | -    print('skip optimize.') | 
| 65 |  | - | 
| 66 |  | -#### | 
| 67 |  | -ort_session = onnxruntime.InferenceSession(onnx_model_path) | 
| 68 |  | -for var in ort_session.get_inputs(): | 
| 69 |  | -    print(var.name) | 
| 70 |  | -for var in ort_session.get_outputs(): | 
| 71 |  | -    print(var.name) | 
| 72 |  | -_,_,input_h,input_w = ort_session.get_inputs()[0].shape | 
| 73 |  | -t = timeit.default_timer() | 
| 74 |  | - | 
| 75 |  | -img = np.zeros((input_h,input_w,3)) | 
| 76 |  | - | 
| 77 |  | -img = (np.transpose(np.float32(img[:,:,:,np.newaxis]), (3,2,0,1)) )#*self.scale | 
| 78 |  | - | 
| 79 |  | -img = np.ascontiguousarray(img) | 
| 80 |  | -#     | 
| 81 |  | -ort_inputs = {ort_session.get_inputs()[0].name: img} | 
| 82 |  | -ort_outs = ort_session.run(None, ort_inputs) | 
| 83 |  | - | 
| 84 |  | -print('onnxruntime infer time:', timeit.default_timer()-t) | 
| 85 |  | -print(ort_outs[0].shape) | 
| 86 |  | - | 
| 87 |  | -# python torch2onnx.py  --src_model_path ./experiments/pretrained_models/GFPGANCleanv1-NoCE-C2.pth --dst_model_path ./GFPGAN.onnx --img_size 512  | 
| 88 |  | - | 
| 89 |  | -# 新版本 | 
| 90 |  | - | 
| 91 |  | - | 
| 92 |  | -# wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth | 
| 93 |  | - | 
| 94 |  | -# python torch2onnx.py  --src_model_path ./GFPGANv1.4.pth --dst_model_path ./GFPGANv1.4.onnx --img_size 512  | 
| 95 |  | - | 
| 96 |  | -# python torch2onnx.py  --src_model_path ./GFPGANCleanv1-NoCE-C2.pth --dst_model_path ./GFPGANv1.2.onnx --img_size 512  | 
|  | 35 | +torch.onnx.export( | 
|  | 36 | +    model, | 
|  | 37 | +    x, | 
|  | 38 | +    onnx_model_path, | 
|  | 39 | +    export_params=True, | 
|  | 40 | +    opset_version=11, | 
|  | 41 | +    do_constant_folding=True, | 
|  | 42 | +    input_names=['input'], | 
|  | 43 | +    output_names=[] | 
|  | 44 | +) | 
0 commit comments