Skip to content

Commit 8ae70fc

Browse files
authored
Fixes #233, improve error handling (#242)
* Fixes #233 -- adds details to error handling, raises error when model is invalid * consistent onnx model checks, error messages * Removing try+exception blocks so if it fails, it will throw original exception
1 parent c49a22d commit 8ae70fc

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

onnxmltools/utils/main.py

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,8 @@ def load_model(file_path):
2727
if not path.exists(file_path):
2828
raise FileNotFoundError("{0} was not found.".format(file_path))
2929
model = onnx_proto.ModelProto()
30-
try:
31-
with open(file_path, 'rb') as f:
32-
model.ParseFromString(f.read())
33-
except IOError:
34-
print("File '{0}' could not be opened.".format(file_path))
35-
return
36-
except:
37-
print("Could not load protobuf file.")
38-
return
39-
30+
with open(file_path, 'rb') as f:
31+
model.ParseFromString(f.read())
4032
return model
4133

4234

@@ -53,18 +45,13 @@ def save_model(model, file_path):
5345
from onnxmltools.utils import save_model
5446
save_model(onnx_model, 'c:/test_model.onnx')
5547
"""
48+
if model is None or not isinstance(model, onnx_proto.ModelProto):
49+
raise ValueError("Model is not a valid ONNX model.")
5650
directory = os.path.dirname(os.path.abspath(file_path))
5751
if not path.exists(directory):
5852
raise FileNotFoundError("Directory does not exist {0}".format(directory))
59-
try:
60-
with open(file_path, 'wb') as f:
61-
f.write(model.SerializeToString())
62-
except IOError:
63-
print("Unable to write file to path '{0}', check if you have permissions.".format(file_path))
64-
return
65-
except:
66-
print("Failed trying to save file '{0}'.".format(file_path))
67-
return
53+
with open(file_path, 'wb') as f:
54+
f.write(model.SerializeToString())
6855

6956

7057
def save_text(model, file_path):
@@ -81,11 +68,10 @@ def save_text(model, file_path):
8168
from onnxmltools.utils import save_text
8269
save_text(onnx_model,"SqueezeNet.json")
8370
"""
84-
try:
85-
with open(file_path, "w") as f:
86-
f.write(str(model))
87-
except IOError:
88-
print("Could not save file")
71+
if model is None or not isinstance(model, onnx_proto.ModelProto):
72+
raise ValueError("Model is not a valid ONNX model.")
73+
with open(file_path, "w") as f:
74+
f.write(str(model))
8975

9076

9177
def set_model_domain(model, domain):
@@ -103,9 +89,9 @@ def set_model_domain(model, domain):
10389
set_model_domain(onnx_model, "com.acme")
10490
"""
10591
if model is None or not isinstance(model, onnx_proto.ModelProto):
106-
raise ValueError("model is not an onnx model")
92+
raise ValueError("Model is not a valid ONNX model.")
10793
if not convert_utils.is_string_type(domain):
108-
raise ValueError("domain must be a string type")
94+
raise ValueError("Domain must be a string type.")
10995
model.domain = domain
11096

11197

@@ -124,9 +110,9 @@ def set_model_version(model, version):
124110
set_model_version(onnx_model, 1)
125111
"""
126112
if model is None or not isinstance(model, onnx_proto.ModelProto):
127-
raise ValueError("model is not an onnx model")
113+
raise ValueError("Model is not a valid ONNX model.")
128114
if not convert_utils.is_numeric_type(version):
129-
raise ValueError("version must be a numeric type")
115+
raise ValueError("Version must be a numeric type.")
130116
model.model_version = version
131117

132118

@@ -146,9 +132,9 @@ def set_model_doc_string(model, doc, override=False):
146132
set_model_doc_string(onnx_model, "Sample doc string")
147133
"""
148134
if model is None or not isinstance(model, onnx_proto.ModelProto):
149-
raise ValueError("model is not an onnx model")
135+
raise ValueError("Model is not a valid ONNX model.")
150136
if not convert_utils.is_string_type(doc):
151-
raise ValueError("doc must be a string type")
137+
raise ValueError("Doc must be a string type.")
152138
if model.doc_string and not doc and override is False:
153-
raise ValueError("failing to overwrite the doc string with a blank string, set override to True if intentional")
139+
raise ValueError("Failing to overwrite the doc string with a blank string, set override to True if intentional.")
154140
model.doc_string = doc

0 commit comments

Comments
 (0)