hello,
i want to convert my pytorch model to tensorflow, so first i have to convert it to onnx first and then onnx to tensorflow. but when i am converting to onnx i am getting error. can someone solve this error.
this is the code
import torch.onnx
from torch.autograd import Variable
model= open(“model_weights.pth”, “w”)
dummy_input = Variable(torch.randn(1, 3, 137, 137))
// since i am giving a 137*137 image as a input i have written this not sure wheather it is right or not. also i dont know what does third parameter mean “net.onnx” is it just output file name.
torch.onnx.export(model,dummy_input,“net.onnx”)
i am getting error as
AttributeError Traceback (most recent call last)
in ()
3 model= open(“model_weights.pth”, “w”)
4 dummy_input = Variable(torch.randn(1, 3, 137, 137))
----> 5 torch.onnx.export(model,dummy_input,“net.onnx”)
/usr/local/lib/python3.6/dist-packages/torch/onnx/init.py in export(*args, **kwargs)
25 def export(*args, **kwargs):
26 from torch.onnx import utils
—> 27 return utils.export(*args, **kwargs)
28
29
/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py in export(model, args, f, export_params, verbose, training, input_names, output_names, aten, export_raw_ir, operator_export_type)
102 operator_export_type = OperatorExportTypes.ONNX
103 _export(model, args, f, export_params, verbose, training, input_names, output_names,
–> 104 operator_export_type=operator_export_type)
105
106
/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py in _export(model, args, f, export_params, verbose, training, input_names, output_names, operator_export_type, export_type, example_outputs, propagate)
279 training, input_names,
280 output_names, operator_export_type,
–> 281 example_outputs, propagate)
282
283 # TODO: Don’t allocate a in-memory string for the protobuf
/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py in _model_to_graph(model, args, f, verbose, training, input_names, output_names, operator_export_type, example_outputs, propagate)
222 raise RuntimeError(’‘forward’ method must be a script method’)
223 else:
–> 224 graph, torch_out = _trace_and_get_graph_from_model(model, args, training)
225 params = list(_unique_state_dict(model).values())
226
/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py in _trace_and_get_graph_from_model(model, args, training)
182 # A basic sanity check: make sure the state_dict keys are the same
183 # before and after running the model. Fail fast!
–> 184 orig_state_dict_keys = _unique_state_dict(model).keys()
185
186 # By default, training=False, which is good because running a model in
/usr/local/lib/python3.6/dist-packages/torch/jit/init.py in _unique_state_dict(module, keep_vars)
199
200 def _unique_state_dict(module, keep_vars=False):
–> 201 state_dict = module.state_dict(keep_vars=keep_vars)
202 filtered_dict = type(state_dict)()
203 seen_ids = set()
AttributeError: ‘_io.TextIOWrapper’ object has no attribute ‘state_dict’