Skip to content

Commit fa0b6cf

Browse files
vvolhejnhwangdeyu
andauthored
Replace deprecated np.object with object (#1990)
Replace deprecated `np.object` with `object` Signed-off-by: Václav Volhejn <[email protected]> Co-authored-by: Deyu Huang <[email protected]>
1 parent f278249 commit fa0b6cf

File tree

11 files changed

+23
-23
lines changed

11 files changed

+23
-23
lines changed

tests/backend_test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def assert_results_equal(self, expected, actual, rtol, atol, mtol=None,
112112
check_value=True, check_shape=True, check_dtype=True):
113113
for expected_val, actual_val in zip(expected, actual):
114114
if check_value:
115-
if expected_val.dtype == np.object:
115+
if expected_val.dtype == object:
116116
# TFLite pads strings with nul bytes
117117
decode = np.vectorize(lambda x: x.replace(b'\x00', b'').decode('UTF-8'))
118118
expected_val_str = decode(expected_val)

tests/run_pretrained_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def run_tflite():
525525
inputs[k] = np_value.astype(expected_dtype)
526526
else:
527527
if expected_dtype == "string":
528-
inputs[k] = self.make_input(v).astype(np.str).astype(np.object)
528+
inputs[k] = self.make_input(v).astype(np.str).astype(object)
529529
else:
530530
inputs[k] = self.make_input(v).astype(expected_dtype)
531531

tests/test_backend.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5230,7 +5230,7 @@ def func(value, filters, output_shape):
52305230
def test_hashtable_lookup(self):
52315231
filnm = "vocab.tmp"
52325232
words = ["apple", "pear", "banana", "cherry", "grape"]
5233-
query = np.array(['cherry'], dtype=np.object)
5233+
query = np.array(['cherry'], dtype=object)
52345234
with open(filnm, "w") as f:
52355235
for word in words:
52365236
f.write(word + "\n")
@@ -5247,7 +5247,7 @@ def func(query_holder):
52475247
def test_hashtable_lookup_const(self):
52485248
filnm = "vocab.tmp"
52495249
words = ["apple", "pear", "banana", "cherry ♥", "grape"]
5250-
query_val = np.array(['cherry ♥', 'banana'], dtype=np.object).reshape((1, 2, 1))
5250+
query_val = np.array(['cherry ♥', 'banana'], dtype=object).reshape((1, 2, 1))
52515251
with open(filnm, "w", encoding='UTF-8') as f:
52525252
for word in words:
52535253
f.write(word + "\n")
@@ -5264,7 +5264,7 @@ def func():
52645264
def test_hashtable_size(self):
52655265
filnm = "vocab.tmp"
52665266
words = ["apple", "pear", "banana", "cherry", "grape"]
5267-
query = np.array(['cherry'], dtype=np.object)
5267+
query = np.array(['cherry'], dtype=object)
52685268
with open(filnm, "w") as f:
52695269
for word in words:
52705270
f.write(word + "\n")
@@ -5853,10 +5853,10 @@ def func(x):
58535853
return tf.identity(op_, name=_TFOUTPUT)
58545854

58555855
# tf gets this wrong and returns fp32 instead of int
5856-
x_val = np.array("123", dtype=np.object)
5856+
x_val = np.array("123", dtype=object)
58575857
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})
58585858

5859-
x_val = np.array("123.1", dtype=np.object)
5859+
x_val = np.array("123.1", dtype=object)
58605860
# can't check the values because in onnx they are padded with 0, in tf they are not
58615861
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val}, check_value=False)
58625862

tests/test_internals.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ def test_insert_node2(self):
107107
def test_make_const_string(self):
108108
graph_proto = self.sample_net()
109109
g = GraphUtil.create_graph_from_onnx_graph(graph_proto)
110-
arr1 = np.array("test", np.object)
111-
arr2 = np.array([["A", "B"], ["C", "D"]], np.object)
112-
arr3 = np.array(b"test", np.object)
113-
arr4 = np.array([[b"A", b"B"], [b"C", b"D"]], np.object)
110+
arr1 = np.array("test", object)
111+
arr2 = np.array([["A", "B"], ["C", "D"]], object)
112+
arr3 = np.array(b"test", object)
113+
arr4 = np.array([[b"A", b"B"], [b"C", b"D"]], object)
114114
const1 = g.make_const("const1", arr1)
115115
const2 = g.make_const("const2", arr2)
116116
const3 = g.make_const("const3", arr3)

tf2onnx/custom_opsets/string_ops.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def version_1(cls, ctx, node, **kwargs):
5353
rewrite = node.get_attr_str("rewrite")
5454
utils.make_sure(node.get_attr_value("replace_global") != 0,
5555
"Can not convert StaticRegexReplace if replace_global is False")
56-
pattern_node = ctx.make_const(utils.make_name("pattern"), np.array([pattern], np.object))
57-
rewrite_node = ctx.make_const(utils.make_name("rewrite"), np.array([rewrite], np.object))
56+
pattern_node = ctx.make_const(utils.make_name("pattern"), np.array([pattern], object))
57+
rewrite_node = ctx.make_const(utils.make_name("rewrite"), np.array([rewrite], object))
5858
del node.attr["pattern"]
5959
del node.attr["rewrite"]
6060
del node.attr["replace_global"]
@@ -69,7 +69,7 @@ def version_1(cls, ctx, node, **kwargs):
6969
if separator is None:
7070
separator = b''
7171
separator = separator.decode('UTF-8')
72-
separator_node = ctx.make_const(utils.make_name("separator"), np.array([separator], np.object))
72+
separator_node = ctx.make_const(utils.make_name("separator"), np.array([separator], object))
7373
axis_node = ctx.make_const(utils.make_name("axis"), np.array([0], np.int64))
7474
inps_with_shapes = [i for i in node.input if ctx.get_shape(i) != []]
7575
shape_node = None

tf2onnx/graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ def make_const(self, name, np_val, skip_conversion=False, raw=True):
582582
raw: whether to store data at field of raw_data or the specific field according to its dtype
583583
"""
584584
np_val_flat = np_val.flatten()
585-
is_bytes = np_val.dtype == np.object and len(np_val_flat) > 0 and isinstance(np_val_flat[0], bytes)
585+
is_bytes = np_val.dtype == object and len(np_val_flat) > 0 and isinstance(np_val_flat[0], bytes)
586586
if raw and not is_bytes:
587587
onnx_tensor = numpy_helper.from_array(np_val, name)
588588
else:

tf2onnx/optimizer/reshape_optimizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _optimize_reshape(self, node, graph):
5454
symbolic_shape.append(SymbolicTensorElement.from_variable(i))
5555
else:
5656
symbolic_shape.append(SymbolicTensorElement.from_const(d))
57-
feed_dict[n.output[0]] = np.array(symbolic_shape, np.object)
57+
feed_dict[n.output[0]] = np.array(symbolic_shape, object)
5858
try:
5959
symbolic_res = SymbolicExecutor(graph).compute_outputs([node.input[1]], feed_dict)
6060
except SymbolicExecutionException:

tf2onnx/symbolic_executor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def compute_squeeze_unsqueeze(self, node, feed_dict):
136136

137137
def compute_cast(self, node, feed_dict):
138138
inp = feed_dict[node.input[0]]
139-
if inp.dtype == np.object:
139+
if inp.dtype == object:
140140
return [inp]
141141
np_dtype = utils.ONNX_TO_NUMPY_DTYPE[node.get_attr("to").i]
142142
return [inp.astype(np_dtype)]
@@ -181,7 +181,7 @@ def compute_concat(self, node, feed_dict):
181181
def compute_gather(self, node, feed_dict):
182182
data = feed_dict[node.input[0]]
183183
indices = feed_dict[node.input[1]]
184-
if indices.dtype == np.object:
184+
if indices.dtype == object:
185185
raise SymbolicExecutionException("Gather requires non-symbolic indices")
186186
axis = node.get_attr_value("axis", 0)
187187
return [np.take(data, indices, axis=axis)]

tf2onnx/tf_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@
5050
def tf_to_onnx_tensor(tensor, name=""):
5151
"""Convert tensorflow tensor to onnx tensor."""
5252
np_data = get_tf_tensor_data(tensor)
53-
if np_data.dtype == np.object:
53+
if np_data.dtype == object:
5454
# assume np_data is string, numpy_helper.from_array accepts ndarray,
5555
# in which each item is of str while the whole dtype is of object.
5656
try:
5757
# Faster but fails on Unicode
58-
np_data = np_data.astype(np.str).astype(np.object)
58+
np_data = np_data.astype(np.str).astype(object)
5959
except UnicodeDecodeError:
6060
decode = np.vectorize(lambda x: x.decode('UTF-8'))
61-
np_data = decode(np_data).astype(np.object)
61+
np_data = decode(np_data).astype(object)
6262
except: # pylint: disable=bare-except
6363
raise RuntimeError("Not support type: {}".format(type(np_data.flat[0])))
6464
return numpy_helper.from_array(np_data, name=name)

tf2onnx/tflite_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def read_int(offset):
271271
string_list = []
272272
for i in range(count):
273273
string_list.append(buffer_bytes[offset_list[i]:offset_list[i+1]].decode("utf-8"))
274-
return numpy_helper.from_array(np.array(string_list, dtype=np.object).reshape(shape))
274+
return numpy_helper.from_array(np.array(string_list, dtype=object).reshape(shape))
275275

276276

277277
def op_has_scalar_output(input_shapes, optype, attr):

0 commit comments

Comments
 (0)