From 09f1a091d2db811357c7d11e9a690dee4253bcff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=CC=81clav=20Volhejn?= Date: Mon, 4 Jul 2022 17:17:51 +0200 Subject: [PATCH] Replace deprecated `np.object` with `object` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Václav Volhejn --- tests/backend_test_base.py | 2 +- tests/run_pretrained_models.py | 2 +- tests/test_backend.py | 10 +++++----- tests/test_internals.py | 8 ++++---- tf2onnx/custom_opsets/string_ops.py | 6 +++--- tf2onnx/graph.py | 2 +- tf2onnx/optimizer/reshape_optimizer.py | 2 +- tf2onnx/symbolic_executor.py | 4 ++-- tf2onnx/tf_utils.py | 6 +++--- tf2onnx/tflite_utils.py | 2 +- tf2onnx/utils.py | 2 +- 11 files changed, 23 insertions(+), 23 deletions(-) diff --git a/tests/backend_test_base.py b/tests/backend_test_base.py index 9d08c306a..f39c398b1 100644 --- a/tests/backend_test_base.py +++ b/tests/backend_test_base.py @@ -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) diff --git a/tests/run_pretrained_models.py b/tests/run_pretrained_models.py index 29f88af24..78f36a79c 100644 --- a/tests/run_pretrained_models.py +++ b/tests/run_pretrained_models.py @@ -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) diff --git a/tests/test_backend.py b/tests/test_backend.py index c0e822a77..b68b9228d 100755 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -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") @@ -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") @@ -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") @@ -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) diff --git a/tests/test_internals.py b/tests/test_internals.py index acd91d0cd..732616a9f 100644 --- a/tests/test_internals.py +++ b/tests/test_internals.py @@ -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) diff --git a/tf2onnx/custom_opsets/string_ops.py b/tf2onnx/custom_opsets/string_ops.py index 24b854bc3..31fb0e363 100644 --- a/tf2onnx/custom_opsets/string_ops.py +++ b/tf2onnx/custom_opsets/string_ops.py @@ -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"] @@ -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 diff --git a/tf2onnx/graph.py b/tf2onnx/graph.py index 32b8c69e3..e98eea9db 100644 --- a/tf2onnx/graph.py +++ b/tf2onnx/graph.py @@ -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: diff --git a/tf2onnx/optimizer/reshape_optimizer.py b/tf2onnx/optimizer/reshape_optimizer.py index 9eac9929c..f2ff6aa72 100644 --- a/tf2onnx/optimizer/reshape_optimizer.py +++ b/tf2onnx/optimizer/reshape_optimizer.py @@ -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: diff --git a/tf2onnx/symbolic_executor.py b/tf2onnx/symbolic_executor.py index 567147218..e8c8a5e6e 100644 --- a/tf2onnx/symbolic_executor.py +++ b/tf2onnx/symbolic_executor.py @@ -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)] @@ -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)] diff --git a/tf2onnx/tf_utils.py b/tf2onnx/tf_utils.py index 5243b3a52..54abc7071 100644 --- a/tf2onnx/tf_utils.py +++ b/tf2onnx/tf_utils.py @@ -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) diff --git a/tf2onnx/tflite_utils.py b/tf2onnx/tflite_utils.py index 111f162f4..6e3f2d024 100644 --- a/tf2onnx/tflite_utils.py +++ b/tf2onnx/tflite_utils.py @@ -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): diff --git a/tf2onnx/utils.py b/tf2onnx/utils.py index 9f3ba095d..ac6bcfcfa 100644 --- a/tf2onnx/utils.py +++ b/tf2onnx/utils.py @@ -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, } #