ENOT-AutoDL / onnx2torch

Convert ONNX models to PyTorch.
Apache License 2.0
610 stars 71 forks source link

Optimizer got an empty parameter list. #194

Open lk1983823 opened 10 months ago

lk1983823 commented 10 months ago

I build a keras model and save it as onnx form. I use tf2onnx to convert the model.

model_input_signature = [
    tf.TensorSpec(np.array((None, 3)), name='input'), 
]
output_path = "./save_for_mpc/" + model_name + ".onnx"
onnx_model, _ = tf2onnx.convert.from_keras(model,
    output_path=output_path,
    input_signature=model_input_signature
)

When I convert the onnx one to a torch model, it works successfully and can make inference. However, when I set the model to a trainable one, it shows ValueError: optimizer got an empty parameter list. Here is the code :

torch_model_1 = convert(onnx_model_path)

GraphModule( (initializers): Module() (sequential/mono_dense/MatMul): OnnxMatMul() (sequential/mono_dense/PartitionedCall/split): OnnxSplit13() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/sub_1): OnnxBinaryMathOperation() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/PartitionedCall_1/Neg): OnnxNeg() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/PartitionedCall_1/Elu): ELU(alpha=1.0) (sequential/mono_dense/PartitionedCall/PartitionedCall_1/PartitionedCall_1/Neg_1): OnnxNeg() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/ones_like/Shape): OnnxShape() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/ones_like/Shape7): OnnxCast() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/ones_like__8): OnnxCast() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/ones_like): OnnxExpand() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/mul): OnnxBinaryMathOperation() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/Elu): ELU(alpha=1.0) (sequential/mono_dense/PartitionedCall/PartitionedCall_1/add_1): OnnxBinaryMathOperation() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/add): OnnxBinaryMathOperation() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/Elu_1): ELU(alpha=1.0) (sequential/mono_dense/PartitionedCall/PartitionedCall_1/sub): OnnxBinaryMathOperation() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/LessEqual): OnnxCompare() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/SelectV29): OnnxCast() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/SelectV215): OnnxBinaryMathOperation() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/SelectV211): OnnxNot() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/SelectV213): OnnxCast() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/SelectV218): OnnxBinaryMathOperation() (sequential/mono_dense/PartitionedCall/PartitionedCall_1/SelectV2): OnnxBinaryMathOperation() (sequential/mono_dense/PartitionedCall/PartitionedCall/Neg): OnnxNeg() (sequential/mono_dense/PartitionedCall/PartitionedCall/Elu): ELU(alpha=1.0) (sequential/mono_dense/PartitionedCall/PartitionedCall/Neg_1): OnnxNeg() (sequential/mono_dense/PartitionedCall/Elu): ELU(alpha=1.0) (sequential/mono_dense/PartitionedCall/concat): OnnxConcat() (sequential/mono_dense_1/MatMul): OnnxMatMul() (sequential/mono_dense_1/PartitionedCall/split): OnnxSplit13() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/sub_1): OnnxBinaryMathOperation() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/PartitionedCall_1/Neg): OnnxNeg() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/PartitionedCall_1/Elu): ELU(alpha=1.0) (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/PartitionedCall_1/Neg_1): OnnxNeg() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/ones_like/Shape): OnnxShape() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/ones_like/Shape22): OnnxCast() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/ones_like23): OnnxCast() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/ones_like): OnnxExpand() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/mul): OnnxBinaryMathOperation() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/Elu): ELU(alpha=1.0) (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/add_1): OnnxBinaryMathOperation() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/add): OnnxBinaryMathOperation() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/Elu_1): ELU(alpha=1.0) (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/sub): OnnxBinaryMathOperation() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/LessEqual): OnnxCompare() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/SelectV226): OnnxNot() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/SelectV2__28): OnnxCast() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/SelectV233): OnnxBinaryMathOperation() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/SelectV224): OnnxCast() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/SelectV2__30): OnnxBinaryMathOperation() (sequential/mono_dense_1/PartitionedCall/PartitionedCall_1/SelectV2): OnnxBinaryMathOperation() (sequential/mono_dense_1/PartitionedCall/PartitionedCall/Neg): OnnxNeg() (sequential/mono_dense_1/PartitionedCall/PartitionedCall/Elu): ELU(alpha=1.0) (sequential/mono_dense_1/PartitionedCall/PartitionedCall/Neg_1): OnnxNeg() (sequential/mono_dense_1/PartitionedCall/Elu): ELU(alpha=1.0) (sequential/mono_dense_1/PartitionedCall/concat): OnnxConcat() (sequential/mono_dense_2/MatMul): OnnxMatMul() (sequential/mono_dense_2/PartitionedCall/split): OnnxSplit13() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/sub_1): OnnxBinaryMathOperation() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/PartitionedCall_1/Neg): OnnxNeg() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/PartitionedCall_1/Neg_1): OnnxNeg() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/ones_like/Shape): OnnxShape() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/ones_like/Shape37): OnnxCast() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/ones_like38): OnnxCast() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/ones_like): OnnxExpand() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/mul): OnnxBinaryMathOperation() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/add_1): OnnxBinaryMathOperation() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/add): OnnxBinaryMathOperation() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/sub): OnnxBinaryMathOperation() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/LessEqual): OnnxCompare() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/SelectV2__41): OnnxNot() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/SelectV243): OnnxCast() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/SelectV248): OnnxBinaryMathOperation() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/SelectV2__39): OnnxCast() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/SelectV245): OnnxBinaryMathOperation() (sequential/mono_dense_2/PartitionedCall/PartitionedCall_1/SelectV2): OnnxBinaryMathOperation() (sequential/mono_dense_2/PartitionedCall/PartitionedCall/Neg): OnnxNeg() (sequential/mono_dense_2/PartitionedCall/PartitionedCall/Neg_1): OnnxNeg() (sequential/mono_dense_2/PartitionedCall/concat): OnnxConcat() )

loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam(torch_model_1.parameters())

this shows:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[/tmp/ipykernel_32945/3047762149.py](https://file+.vscode-resource.vscode-cdn.net/tmp/ipykernel_32945/3047762149.py) in 
      1 loss_fn = torch.nn.MSELoss()
----> 2 optimizer = torch.optim.Adam(torch_model_1.parameters())

[~/anaconda3/envs/dpc/lib/python3.10/site-packages/torch/optim/adam.py](https://file+.vscode-resource.vscode-cdn.net/media/lk/lksgcc/DL_lk/202205_RZ_GAN%E6%A8%A1%E5%9E%8B/model/EvoOpt/~/anaconda3/envs/dpc/lib/python3.10/site-packages/torch/optim/adam.py) in __init__(self, params, lr, betas, eps, weight_decay, amsgrad, foreach, maximize, capturable, differentiable, fused)
     31                         maximize=maximize, foreach=foreach, capturable=capturable,
     32                         differentiable=differentiable, fused=fused)
---> 33         super().__init__(params, defaults)
     34 
     35         if fused:

[~/anaconda3/envs/dpc/lib/python3.10/site-packages/torch/optim/optimizer.py](https://file+.vscode-resource.vscode-cdn.net/media/lk/lksgcc/DL_lk/202205_RZ_GAN%E6%A8%A1%E5%9E%8B/model/EvoOpt/~/anaconda3/envs/dpc/lib/python3.10/site-packages/torch/optim/optimizer.py) in __init__(self, params, defaults)
    185         param_groups = list(params)
    186         if len(param_groups) == 0:
--> 187             raise ValueError("optimizer got an empty parameter list")
    188         if not isinstance(param_groups[0], dict):
    189             param_groups = [{'params': param_groups}]

ValueError: optimizer got an empty parameter list

Python 3.10.0 onnx2torch 1.5.13 onnx 1.15.0 torch 2.0.1+cu117 tf2onnx 1.16.0 toymodel.zip I have uploaded my onnx model. Can anyone give me some help? Thanks!