Skip to content

Replace deprecated np.object with object #1990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/backend_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def assert_results_equal(self, expected, actual, rtol, atol, mtol=None,
check_value=True, check_shape=True, check_dtype=True):
for expected_val, actual_val in zip(expected, actual):
if check_value:
if expected_val.dtype == np.object:
if expected_val.dtype == object:
# TFLite pads strings with nul bytes
decode = np.vectorize(lambda x: x.replace(b'\x00', b'').decode('UTF-8'))
expected_val_str = decode(expected_val)
Expand Down
2 changes: 1 addition & 1 deletion tests/run_pretrained_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ def run_tflite():
inputs[k] = np_value.astype(expected_dtype)
else:
if expected_dtype == "string":
inputs[k] = self.make_input(v).astype(np.str).astype(np.object)
inputs[k] = self.make_input(v).astype(np.str).astype(object)
else:
inputs[k] = self.make_input(v).astype(expected_dtype)

Expand Down
10 changes: 5 additions & 5 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5230,7 +5230,7 @@ def func(value, filters, output_shape):
def test_hashtable_lookup(self):
filnm = "vocab.tmp"
words = ["apple", "pear", "banana", "cherry", "grape"]
query = np.array(['cherry'], dtype=np.object)
query = np.array(['cherry'], dtype=object)
with open(filnm, "w") as f:
for word in words:
f.write(word + "\n")
Expand All @@ -5247,7 +5247,7 @@ def func(query_holder):
def test_hashtable_lookup_const(self):
filnm = "vocab.tmp"
words = ["apple", "pear", "banana", "cherry ♥", "grape"]
query_val = np.array(['cherry ♥', 'banana'], dtype=np.object).reshape((1, 2, 1))
query_val = np.array(['cherry ♥', 'banana'], dtype=object).reshape((1, 2, 1))
with open(filnm, "w", encoding='UTF-8') as f:
for word in words:
f.write(word + "\n")
Expand All @@ -5264,7 +5264,7 @@ def func():
def test_hashtable_size(self):
filnm = "vocab.tmp"
words = ["apple", "pear", "banana", "cherry", "grape"]
query = np.array(['cherry'], dtype=np.object)
query = np.array(['cherry'], dtype=object)
with open(filnm, "w") as f:
for word in words:
f.write(word + "\n")
Expand Down Expand Up @@ -5853,10 +5853,10 @@ def func(x):
return tf.identity(op_, name=_TFOUTPUT)

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

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

Expand Down
8 changes: 4 additions & 4 deletions tests/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ def test_insert_node2(self):
def test_make_const_string(self):
graph_proto = self.sample_net()
g = GraphUtil.create_graph_from_onnx_graph(graph_proto)
arr1 = np.array("test", np.object)
arr2 = np.array([["A", "B"], ["C", "D"]], np.object)
arr3 = np.array(b"test", np.object)
arr4 = np.array([[b"A", b"B"], [b"C", b"D"]], np.object)
arr1 = np.array("test", object)
arr2 = np.array([["A", "B"], ["C", "D"]], object)
arr3 = np.array(b"test", object)
arr4 = np.array([[b"A", b"B"], [b"C", b"D"]], object)
const1 = g.make_const("const1", arr1)
const2 = g.make_const("const2", arr2)
const3 = g.make_const("const3", arr3)
Expand Down
6 changes: 3 additions & 3 deletions tf2onnx/custom_opsets/string_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def version_1(cls, ctx, node, **kwargs):
rewrite = node.get_attr_str("rewrite")
utils.make_sure(node.get_attr_value("replace_global") != 0,
"Can not convert StaticRegexReplace if replace_global is False")
pattern_node = ctx.make_const(utils.make_name("pattern"), np.array([pattern], np.object))
rewrite_node = ctx.make_const(utils.make_name("rewrite"), np.array([rewrite], np.object))
pattern_node = ctx.make_const(utils.make_name("pattern"), np.array([pattern], object))
rewrite_node = ctx.make_const(utils.make_name("rewrite"), np.array([rewrite], object))
del node.attr["pattern"]
del node.attr["rewrite"]
del node.attr["replace_global"]
Expand All @@ -69,7 +69,7 @@ def version_1(cls, ctx, node, **kwargs):
if separator is None:
separator = b''
separator = separator.decode('UTF-8')
separator_node = ctx.make_const(utils.make_name("separator"), np.array([separator], np.object))
separator_node = ctx.make_const(utils.make_name("separator"), np.array([separator], object))
axis_node = ctx.make_const(utils.make_name("axis"), np.array([0], np.int64))
inps_with_shapes = [i for i in node.input if ctx.get_shape(i) != []]
shape_node = None
Expand Down
2 changes: 1 addition & 1 deletion tf2onnx/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ def make_const(self, name, np_val, skip_conversion=False, raw=True):
raw: whether to store data at field of raw_data or the specific field according to its dtype
"""
np_val_flat = np_val.flatten()
is_bytes = np_val.dtype == np.object and len(np_val_flat) > 0 and isinstance(np_val_flat[0], bytes)
is_bytes = np_val.dtype == object and len(np_val_flat) > 0 and isinstance(np_val_flat[0], bytes)
if raw and not is_bytes:
onnx_tensor = numpy_helper.from_array(np_val, name)
else:
Expand Down
2 changes: 1 addition & 1 deletion tf2onnx/optimizer/reshape_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _optimize_reshape(self, node, graph):
symbolic_shape.append(SymbolicTensorElement.from_variable(i))
else:
symbolic_shape.append(SymbolicTensorElement.from_const(d))
feed_dict[n.output[0]] = np.array(symbolic_shape, np.object)
feed_dict[n.output[0]] = np.array(symbolic_shape, object)
try:
symbolic_res = SymbolicExecutor(graph).compute_outputs([node.input[1]], feed_dict)
except SymbolicExecutionException:
Expand Down
4 changes: 2 additions & 2 deletions tf2onnx/symbolic_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def compute_squeeze_unsqueeze(self, node, feed_dict):

def compute_cast(self, node, feed_dict):
inp = feed_dict[node.input[0]]
if inp.dtype == np.object:
if inp.dtype == object:
return [inp]
np_dtype = utils.ONNX_TO_NUMPY_DTYPE[node.get_attr("to").i]
return [inp.astype(np_dtype)]
Expand Down Expand Up @@ -181,7 +181,7 @@ def compute_concat(self, node, feed_dict):
def compute_gather(self, node, feed_dict):
data = feed_dict[node.input[0]]
indices = feed_dict[node.input[1]]
if indices.dtype == np.object:
if indices.dtype == object:
raise SymbolicExecutionException("Gather requires non-symbolic indices")
axis = node.get_attr_value("axis", 0)
return [np.take(data, indices, axis=axis)]
Expand Down
6 changes: 3 additions & 3 deletions tf2onnx/tf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@
def tf_to_onnx_tensor(tensor, name=""):
"""Convert tensorflow tensor to onnx tensor."""
np_data = get_tf_tensor_data(tensor)
if np_data.dtype == np.object:
if np_data.dtype == object:
# assume np_data is string, numpy_helper.from_array accepts ndarray,
# in which each item is of str while the whole dtype is of object.
try:
# Faster but fails on Unicode
np_data = np_data.astype(np.str).astype(np.object)
np_data = np_data.astype(np.str).astype(object)
except UnicodeDecodeError:
decode = np.vectorize(lambda x: x.decode('UTF-8'))
np_data = decode(np_data).astype(np.object)
np_data = decode(np_data).astype(object)
except: # pylint: disable=bare-except
raise RuntimeError("Not support type: {}".format(type(np_data.flat[0])))
return numpy_helper.from_array(np_data, name=name)
Expand Down
2 changes: 1 addition & 1 deletion tf2onnx/tflite_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def read_int(offset):
string_list = []
for i in range(count):
string_list.append(buffer_bytes[offset_list[i]:offset_list[i+1]].decode("utf-8"))
return numpy_helper.from_array(np.array(string_list, dtype=np.object).reshape(shape))
return numpy_helper.from_array(np.array(string_list, dtype=object).reshape(shape))


def op_has_scalar_output(input_shapes, optype, attr):
Expand Down
2 changes: 1 addition & 1 deletion tf2onnx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
onnx_pb.TensorProto.BOOL: np.bool,
onnx_pb.TensorProto.COMPLEX64: np.complex64,
onnx_pb.TensorProto.COMPLEX128: np.complex128,
onnx_pb.TensorProto.STRING: np.object,
onnx_pb.TensorProto.STRING: object,
}

#
Expand Down