xinntao / Real-ESRGAN

Real-ESRGAN aims at developing Practical Algorithms for General Image/Video Restoration.
BSD 3-Clause "New" or "Revised" License
27.74k stars 3.49k forks source link

Converting PyTorch to NCNN - KeyError: 'params' #407

Open NayamAmarshe opened 2 years ago

NayamAmarshe commented 2 years ago

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'
magicse commented 2 years ago

try pytorch2onnx.py --params

NayamAmarshe commented 2 years ago

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

TechnoMasterBoy commented 2 years ago

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.

magicse commented 2 years ago

try check conversion RGB BRG link 1 link 2

rGitcy commented 1 year ago

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

NayamAmarshe commented 1 year ago

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.

haobo724 commented 1 year ago

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...

md-rifatkhan commented 10 months ago

Chainner (Convert to Onnx) convert