adriansahlman / stylegan2_pytorch

A Pytorch implementation of StyleGAN2
MIT License
165 stars 36 forks source link

Some problems from exporting onnx #23

Open kasim0226 opened 3 years ago

kasim0226 commented 3 years ago

Thank you for your great work. The code is easy to read. I try to convert model to onnx format, but I am facing some errors. Could you help me?

  1. if I just comment the parts of torch.float32 in lerp function, would it causing problems?

`def lerp(a, b, beta): if isinstance(beta, numbers.Number): if beta == 1: return b elif beta == 0: return a

if torch.is_tensor(a) and a.dtype == torch.float32:

    # torch lerp only available for fp32
 #   return torch.lerp(a, b, beta)
# More numerically stable than a + beta * (b - a)
return (1 - beta) * a + beta * b`
  1. I got this error (RuntimeError: Unsupported: ONNX export of convolution for kernel of unknown shape.), but I don't have any ideas
adriansahlman commented 3 years ago

Hey, sorry for late reply.

It was a long time since I wrote this code so its difficult figuring out why the onnx export is failing.

  1. You can replace all the code in utils.lerp with return (1 - beta) * a + beta * b. It should not affect performance too much (I think torch.lerp is faster because it is implemented in C++ directly). No idea how this would affect the export to onnx.
  2. There are a lot of unconventional uses of convolutions in stylegan2, such as the blur filter and rescaling of weights before performing the convolution. Maybe these are not supported by onnx? Can you see which convolutional operation caused the error?