pytorch2onnx.py 661 B

1234567891011121314151617
  1. import torch
  2. import torch.onnx
  3. from basicsr.archs.rrdbnet_arch import RRDBNet
  4. # An instance of your model
  5. model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32)
  6. model.load_state_dict(torch.load('experiments/pretrained_models/RealESRGAN_x4plus.pth')['params_ema'])
  7. # set the train mode to false since we will only run the forward pass.
  8. model.train(False)
  9. model.cpu().eval()
  10. # An example input you would normally provide to your model's forward() method
  11. x = torch.rand(1, 3, 64, 64)
  12. # Export the model
  13. with torch.no_grad():
  14. torch_out = torch.onnx._export(model, x, 'realesrgan-x4.onnx', opset_version=11, export_params=True)