Real-ESRGAN icon indicating copy to clipboard operation
Real-ESRGAN copied to clipboard

Converting PyTorch to NCNN - KeyError: 'params'

Open NayamAmarshe opened this issue 1 year ago • 4 comments

Hi, I'm trying to convert BSRGAN model : https://drive.google.com/drive/folders/13kfr3qny7S2xwG9h7v95F5mkWs0OmU0D?usp=sharing and I also tried 4x_RRDB_ESRGAN.pth: https://drive.google.com/drive/folders/1O9qLq2CjywbS4FJXvJMggX8DjX4e-GAV

It's a pth file but I'm unable to convert it to ncnn. When I run the pytorch2onnx.py file and input the model it throws this error:

python3 pytorch2onnx.py --input '/home/user/Downloads/realesrgan-gui/Real-ESRGAN-master/experiments/pretrained_models/RealESRGAN_x4plus.pth' 
<frozen importlib._bootstrap>:228: RuntimeWarning: scipy._lib.messagestream.MessageStream size changed, may indicate binary incompatibility. Expected 56 from C header, got 64 from PyObject
Traceback (most recent call last):
  File "/home/user/Downloads/realesrgan-gui/Real-ESRGAN-master/scripts/pytorch2onnx.py", line 36, in <module>
    main(args)
  File "/home/user/Downloads/realesrgan-gui/Real-ESRGAN-master/scripts/pytorch2onnx.py", line 14, in main
    model.load_state_dict(torch.load(args.input)[keyname])
KeyError: 'params'

NayamAmarshe avatar Aug 11 '22 16:08 NayamAmarshe

try pytorch2onnx.py --params

magicse avatar Aug 14 '22 01:08 magicse

try pytorch2onnx.py --params

Doesn't work either.

Here's what I did, I changed the pytorch2onnx.py's line 14 from
model.load_state_dict(torch.load(args.input)[keyname]) to
model.load_state_dict(torch.load(args.input), Strict=False) and it worked and then I used https://convertmodel.com to get ncnn file from onnx.

BUT there's an issue, I do not know how to fix the param file. Would you happen to know about it?

I changed the first 2 lines where it said input.1 and I replaced input.1 with data. At the end, I replaced 1895 with output and then I used the executable to try the image upscaling, it works but the output is totally weird, image

If you have any idea how to fix it, please let me know: bsrgan.param.txt

NayamAmarshe avatar Aug 14 '22 02:08 NayamAmarshe

Use chaiNNer (https://github.com/JoeyBallentine/chaiNNer) to convert the pth file to onnx, then use https://convertmodel.com to convert the onnx to ncnn.

TechnoMasterBoy avatar Aug 21 '22 09:08 TechnoMasterBoy

try check conversion RGB BRG link 1 link 2

magicse avatar Aug 21 '22 11:08 magicse

I also get the same error "KeyError: 'params'".I just want to convert RealESRGAN_x4plus.pth to onnx.

rGitcy avatar May 29 '23 06:05 rGitcy

I also get the same error "KeyError: 'params'".I just want to convert RealESRGAN_x4plus.pth to onnx.

I used Chainner and it works great! Just import the model and convert it to onnx directly.

NayamAmarshe avatar May 29 '23 07:05 NayamAmarshe

I also get the same error "KeyError: 'params'".I just want to convert RealESRGAN_x4plus.pth to onnx.

I used Chainner and it works great! Just import the model and convert it to onnx directly.

sorry to bother you , May I ask the speed you infer on ONNX model is better or worse than native pytorch model? Because in my case the inference speed of onnx model is even worse...

haobo724 avatar Jul 12 '23 14:07 haobo724

Chainner (Convert to Onnx) convert

md-rifatkhan avatar Nov 08 '23 17:11 md-rifatkhan