From 2548b6037c3469d27dc512ce1417ea8de1111a59 Mon Sep 17 00:00:00 2001 From: hejunchao Date: Wed, 13 Sep 2023 13:44:14 +0800 Subject: [PATCH 01/19] fix pytest's cosine --- tests/caffe_test_runner.py | 15 +++++++++++++++ tests/compare_util.py | 28 +++++++++++++++++++++++++++- tests/evaluator.py | 15 +++++++++++++++ tests/generator.py | 15 +++++++++++++++ tests/inference.py | 15 +++++++++++++++ tests/json2md.py | 15 +++++++++++++++ tests/nuc_proxy.py | 15 +++++++++++++++ tests/onnx_test_runner.py | 15 +++++++++++++++ tests/preprocess_utils.py | 15 +++++++++++++++ tests/test_runner.py | 15 +++++++++++++++ tests/test_utils.py | 15 +++++++++++++++ tests/tflite_test_runner.py | 15 +++++++++++++++ 12 files changed, 192 insertions(+), 1 deletion(-) diff --git a/tests/caffe_test_runner.py b/tests/caffe_test_runner.py index 817253849f..a0fdfb4300 100644 --- a/tests/caffe_test_runner.py +++ b/tests/caffe_test_runner.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import caffe from test_runner import * import os diff --git a/tests/compare_util.py b/tests/compare_util.py index 93f19d8827..050a9e6e89 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import enum import math import os @@ -11,13 +26,24 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): + if np.isnan(gt).any(): + gt = remove_nan(gt) + if np.isnan(pred).any(): + pred = remove_nan(gt) + if compare_arrays(gt, pred): + return 1 return (gt @ pred) / (np.linalg.norm(gt, 2) * np.linalg.norm(pred, 2)) +def remove_nan(array): + mask = np.isnan(array) + return array[~mask] + +def compare_arrays(arr1, arr2): + return np.array_equal(arr1, arr2) def euclidean(gt: np.ndarray, pred: np.ndarray, *args): return np.linalg.norm(gt - pred, 2)**2 - def allclose(gt: np.ndarray, pred: np.ndarray, thresh: float): return np.allclose(gt, pred, atol=thresh) diff --git a/tests/evaluator.py b/tests/evaluator.py index 149f098ab4..3b36b8abe8 100644 --- a/tests/evaluator.py +++ b/tests/evaluator.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + from typing import List, Dict, Union, Tuple import os import nncase diff --git a/tests/generator.py b/tests/generator.py index bcccb4ad4d..20d7bcd0a2 100644 --- a/tests/generator.py +++ b/tests/generator.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + from typing import Any, Dict, List, Tuple, Union import numpy as np import os diff --git a/tests/inference.py b/tests/inference.py index 52e9deecd2..8388a8e816 100644 --- a/tests/inference.py +++ b/tests/inference.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + from typing import List, Dict, Union, Tuple import os import nncase diff --git a/tests/json2md.py b/tests/json2md.py index 983d859166..d8a78cfe04 100644 --- a/tests/json2md.py +++ b/tests/json2md.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import argparse import json import pandas as pd diff --git a/tests/nuc_proxy.py b/tests/nuc_proxy.py index 408bdc6141..7ff0204095 100644 --- a/tests/nuc_proxy.py +++ b/tests/nuc_proxy.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import os import argparse import stat diff --git a/tests/onnx_test_runner.py b/tests/onnx_test_runner.py index acbd16ed9a..d1299bb234 100644 --- a/tests/onnx_test_runner.py +++ b/tests/onnx_test_runner.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + from onnx import version_converter, helper import onnxsim import onnxruntime as ort diff --git a/tests/preprocess_utils.py b/tests/preprocess_utils.py index 174e09132c..0ece47e4cd 100644 --- a/tests/preprocess_utils.py +++ b/tests/preprocess_utils.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + def get_source_transpose_index(perm): """ transpose model output with postprocess to framework output diff --git a/tests/test_runner.py b/tests/test_runner.py index 7ac0613ffa..16da8d4d83 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import copy import os import re diff --git a/tests/test_utils.py b/tests/test_utils.py index c2d3894868..f5adebab3b 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import os import json import numpy as np diff --git a/tests/tflite_test_runner.py b/tests/tflite_test_runner.py index f68236d287..772449e462 100644 --- a/tests/tflite_test_runner.py +++ b/tests/tflite_test_runner.py @@ -1,3 +1,18 @@ +# Copyright 2019-2021 Canaan Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# pylint: disable=invalid-name, unused-argument, import-outside-toplevel + import tensorflow as tf from test_runner import * import os From 647e3ee69eb54e7b190f0a58059f89322a3ccd44 Mon Sep 17 00:00:00 2001 From: hejunchao Date: Wed, 13 Sep 2023 13:44:20 +0800 Subject: [PATCH 02/19] fix --- tests/kernels/test_cum_sum.cpp | 4 ++-- tests/kernels/test_range.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/kernels/test_cum_sum.cpp b/tests/kernels/test_cum_sum.cpp index e6a287e32c..18cf05837f 100644 --- a/tests/kernels/test_cum_sum.cpp +++ b/tests/kernels/test_cum_sum.cpp @@ -73,13 +73,13 @@ TEST_P(CumSumTest, cum_sum) { .expect("create tensor failed"); // actual - float_t exclusive[] = {0}; + float exclusive[] = {0}; auto exclusive_ptr = hrt::create(nncase::dt_float32, {1}, {reinterpret_cast(exclusive), sizeof(exclusive)}, true, host_runtime_tensor::pool_cpu_only) .expect("create tensor failed"); - float_t reverse[] = {0}; + float reverse[] = {0}; auto reverse_ptr = hrt::create(nncase::dt_float32, {1}, {reinterpret_cast(reverse), sizeof(reverse)}, diff --git a/tests/kernels/test_range.cpp b/tests/kernels/test_range.cpp index b9574a0c05..bc9ba8216e 100644 --- a/tests/kernels/test_range.cpp +++ b/tests/kernels/test_range.cpp @@ -40,21 +40,21 @@ class RangeTest : public KernelTest, auto step_value = GetFloatNumber("step"); auto typecode = GetDataType("lhs_type"); - float_t begin_array[] = {begin_value}; + float begin_array[] = {begin_value}; begin = hrt::create(typecode, shape, {reinterpret_cast(begin_array), sizeof(begin_array)}, true, host_runtime_tensor::pool_cpu_only) .expect("create tensor failed"); - float_t end_array[] = {end_value}; + float end_array[] = {end_value}; end = hrt::create( typecode, shape, {reinterpret_cast(end_array), sizeof(end_array)}, true, host_runtime_tensor::pool_cpu_only) .expect("create tensor failed"); - float_t step_array[] = {step_value}; + float step_array[] = {step_value}; step = hrt::create(typecode, shape, {reinterpret_cast(step_array), sizeof(step_array)}, From 8361f70d153b8ce8479f0bc4fc6b814ec66aa960 Mon Sep 17 00:00:00 2001 From: HeJunchao100813 Date: Wed, 13 Sep 2023 05:47:18 +0000 Subject: [PATCH 03/19] Apply code-format changes --- tests/compare_util.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/compare_util.py b/tests/compare_util.py index 050a9e6e89..727f5122a0 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -34,16 +34,20 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): return 1 return (gt @ pred) / (np.linalg.norm(gt, 2) * np.linalg.norm(pred, 2)) + def remove_nan(array): mask = np.isnan(array) return array[~mask] + def compare_arrays(arr1, arr2): return np.array_equal(arr1, arr2) + def euclidean(gt: np.ndarray, pred: np.ndarray, *args): return np.linalg.norm(gt - pred, 2)**2 + def allclose(gt: np.ndarray, pred: np.ndarray, thresh: float): return np.allclose(gt, pred, atol=thresh) From 342e7ab26a2253be50060b8879e97df983e9e8fe Mon Sep 17 00:00:00 2001 From: hejunchao Date: Wed, 13 Sep 2023 14:24:59 +0800 Subject: [PATCH 04/19] fix --- tests/compare_util.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/compare_util.py b/tests/compare_util.py index 727f5122a0..1c29ebeebf 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -29,7 +29,7 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): if np.isnan(gt).any(): gt = remove_nan(gt) if np.isnan(pred).any(): - pred = remove_nan(gt) + pred = remove_nan(pred) if compare_arrays(gt, pred): return 1 return (gt @ pred) / (np.linalg.norm(gt, 2) * np.linalg.norm(pred, 2)) @@ -40,8 +40,8 @@ def remove_nan(array): return array[~mask] -def compare_arrays(arr1, arr2): - return np.array_equal(arr1, arr2) +def compare_arrays(gt: np.ndarray, pred: np.ndarray): + return np.array_equal(gt, pred) def euclidean(gt: np.ndarray, pred: np.ndarray, *args): From 0e045c45f0c630b87b4acebfd098aea06d5f2c7e Mon Sep 17 00:00:00 2001 From: hejunchao Date: Wed, 13 Sep 2023 14:28:06 +0800 Subject: [PATCH 05/19] fix --- tests/compare_util.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/compare_util.py b/tests/compare_util.py index 1c29ebeebf..ef055fc412 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -26,12 +26,16 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): + # remove the nan number. if np.isnan(gt).any(): gt = remove_nan(gt) if np.isnan(pred).any(): pred = remove_nan(pred) + + # exclude situation of all zeros in ndarray if compare_arrays(gt, pred): return 1 + return (gt @ pred) / (np.linalg.norm(gt, 2) * np.linalg.norm(pred, 2)) @@ -45,7 +49,7 @@ def compare_arrays(gt: np.ndarray, pred: np.ndarray): def euclidean(gt: np.ndarray, pred: np.ndarray, *args): - return np.linalg.norm(gt - pred, 2)**2 + return np.linalg.norm(gt - pred, 2) ** 2 def allclose(gt: np.ndarray, pred: np.ndarray, thresh: float): @@ -116,7 +120,6 @@ def compare_ndarray(expected: np.ndarray, threshold: float = 0.99, dump_hist: bool = True, dump_file: str = 'hist.csv') -> bool: - if expected.size == actual.size: similarity = similarity_func[similarity_name](expected.flatten(), actual.flatten()) else: From 7572707124a60ef6ae1b42c3846f95333c76cfe1 Mon Sep 17 00:00:00 2001 From: hejunchao Date: Wed, 13 Sep 2023 14:53:03 +0800 Subject: [PATCH 06/19] fix --- tests/compare_util.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/compare_util.py b/tests/compare_util.py index ef055fc412..bf8205c305 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -32,7 +32,11 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): if np.isnan(pred).any(): pred = remove_nan(pred) - # exclude situation of all zeros in ndarray + # return -1 if gt.size !=pred.size after remove the nan number. + if gt.size != pred.size: + return -1 + + # exclude the situation of all zeros in ndarray. if compare_arrays(gt, pred): return 1 From bfe7e57eac003b612faecda277de88aeff9f4704 Mon Sep 17 00:00:00 2001 From: hejunchao Date: Wed, 13 Sep 2023 15:39:08 +0800 Subject: [PATCH 07/19] fix --- tests/compare_util.py | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/tests/compare_util.py b/tests/compare_util.py index bf8205c305..3c14aeff66 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -26,26 +26,39 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): - # remove the nan number. - if np.isnan(gt).any(): - gt = remove_nan(gt) - if np.isnan(pred).any(): - pred = remove_nan(pred) - - # return -1 if gt.size !=pred.size after remove the nan number. - if gt.size != pred.size: + # remove the NaN values in the same location. + if np.isnan(gt).any() and np.isnan(pred).any(): + gt = remove_nan(gt, pred, gt) + pred = remove_nan(gt, pred, pred) + + # remove the INF values in the same location. + if np.isinf(gt).any() and np.isinf(pred).any(): + gt = remove_inf(gt, pred, gt) + pred = remove_inf(gt, pred, pred) + + # return -1 if the nan/inf value is still in the array. + if np.isnan(gt).any() or np.isnan(pred).any() or np.isinf(gt).any() or np.isinf(pred).any(): return -1 # exclude the situation of all zeros in ndarray. if compare_arrays(gt, pred): return 1 - return (gt @ pred) / (np.linalg.norm(gt, 2) * np.linalg.norm(pred, 2)) + result = (gt @ pred) / (np.linalg.norm(gt, 2) * np.linalg.norm(pred, 2)) + return -1 if math.isnan(result) else result -def remove_nan(array): - mask = np.isnan(array) - return array[~mask] + +def remove_nan(gt: np.ndarray, pred: np.ndarray, target: np.ndarray): + gt_mask = np.isnan(gt) + pred_mask = np.isnan(pred) + return target[~(gt_mask & pred_mask)] + + +def remove_inf(gt: np.ndarray, pred: np.ndarray, target: np.ndarray): + gt_mask = np.isinf(gt) + pred_mask = np.isinf(pred) + return target[~(gt_mask & pred_mask)] def compare_arrays(gt: np.ndarray, pred: np.ndarray): From ea35753b5e9c492f0f76ee578608c6a040ad9414 Mon Sep 17 00:00:00 2001 From: hejunchao Date: Thu, 14 Sep 2023 10:50:28 +0800 Subject: [PATCH 08/19] fix --- tests/compare_util.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/compare_util.py b/tests/compare_util.py index 3c14aeff66..08d8373af0 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -28,13 +28,15 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): # remove the NaN values in the same location. if np.isnan(gt).any() and np.isnan(pred).any(): + gt_temp = gt gt = remove_nan(gt, pred, gt) - pred = remove_nan(gt, pred, pred) + pred = remove_nan(gt_temp, pred, pred) # remove the INF values in the same location. if np.isinf(gt).any() and np.isinf(pred).any(): + gt_temp = gt gt = remove_inf(gt, pred, gt) - pred = remove_inf(gt, pred, pred) + pred = remove_inf(gt_temp, pred, pred) # return -1 if the nan/inf value is still in the array. if np.isnan(gt).any() or np.isnan(pred).any() or np.isinf(gt).any() or np.isinf(pred).any(): From db534c7a170e608c6bd5c1cdf0a33dd47db60331 Mon Sep 17 00:00:00 2001 From: hejunchao Date: Thu, 14 Sep 2023 11:18:40 +0800 Subject: [PATCH 09/19] fix --- tests/compare_util.py | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/tests/compare_util.py b/tests/compare_util.py index 08d8373af0..fc63a90e6b 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -28,15 +28,19 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): # remove the NaN values in the same location. if np.isnan(gt).any() and np.isnan(pred).any(): - gt_temp = gt - gt = remove_nan(gt, pred, gt) - pred = remove_nan(gt_temp, pred, pred) + gt_mask = np.isnan(gt) + pred_mask = np.isnan(pred) + mask = gt_mask & pred_mask + gt = gt[~mask] + pred = pred[~mask] # remove the INF values in the same location. if np.isinf(gt).any() and np.isinf(pred).any(): - gt_temp = gt - gt = remove_inf(gt, pred, gt) - pred = remove_inf(gt_temp, pred, pred) + gt_mask = np.isinf(gt) + pred_mask = np.isinf(pred) + mask = gt_mask & pred_mask + gt = gt[~mask] + pred = pred[~mask] # return -1 if the nan/inf value is still in the array. if np.isnan(gt).any() or np.isnan(pred).any() or np.isinf(gt).any() or np.isinf(pred).any(): @@ -51,18 +55,6 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): return -1 if math.isnan(result) else result -def remove_nan(gt: np.ndarray, pred: np.ndarray, target: np.ndarray): - gt_mask = np.isnan(gt) - pred_mask = np.isnan(pred) - return target[~(gt_mask & pred_mask)] - - -def remove_inf(gt: np.ndarray, pred: np.ndarray, target: np.ndarray): - gt_mask = np.isinf(gt) - pred_mask = np.isinf(pred) - return target[~(gt_mask & pred_mask)] - - def compare_arrays(gt: np.ndarray, pred: np.ndarray): return np.array_equal(gt, pred) From 079faaa76a72721d7b3688cda15a8fcc1eb33458 Mon Sep 17 00:00:00 2001 From: hejunchao Date: Thu, 14 Sep 2023 16:28:18 +0800 Subject: [PATCH 10/19] fix --- tests/compare_util.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/compare_util.py b/tests/compare_util.py index fc63a90e6b..583bc5567d 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -13,7 +13,6 @@ # limitations under the License. # pylint: disable=invalid-name, unused-argument, import-outside-toplevel -import enum import math import os import re From 38737461890ba632fcb1dc7dcef39f4d05984d9e Mon Sep 17 00:00:00 2001 From: hejunchao Date: Thu, 14 Sep 2023 17:21:32 +0800 Subject: [PATCH 11/19] fix --- tests/compare_util.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/compare_util.py b/tests/compare_util.py index 583bc5567d..3076b9843c 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -25,6 +25,14 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): + + gt = np.reshape(gt,-1) + pred = np.reshape(pred,-1) + for i in range(gt.size): + print(gt[i]) + print() + for i in range(pred.size): + print(pred[i]) # remove the NaN values in the same location. if np.isnan(gt).any() and np.isnan(pred).any(): gt_mask = np.isnan(gt) From 9f0e0789ca9ab776348290c98f82523bfa3806b9 Mon Sep 17 00:00:00 2001 From: HeJunchao100813 Date: Thu, 14 Sep 2023 09:24:12 +0000 Subject: [PATCH 12/19] Apply code-format changes --- tests/compare_util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/compare_util.py b/tests/compare_util.py index 3076b9843c..f6ee6c5d01 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -26,8 +26,8 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): - gt = np.reshape(gt,-1) - pred = np.reshape(pred,-1) + gt = np.reshape(gt, -1) + pred = np.reshape(pred, -1) for i in range(gt.size): print(gt[i]) print() From 72d81cf54e2ddfda82ba383c1ad03041591e8c2e Mon Sep 17 00:00:00 2001 From: hejunchao Date: Thu, 14 Sep 2023 17:55:09 +0800 Subject: [PATCH 13/19] fix --- tests/compare_util.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/compare_util.py b/tests/compare_util.py index 3076b9843c..99b29524fc 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -25,14 +25,14 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): + # gt = np.reshape(gt,-1) + # pred = np.reshape(pred,-1) + # for i in range(gt.size): + # print(gt[i]) + # print() + # for i in range(pred.size): + # print(pred[i]) - gt = np.reshape(gt,-1) - pred = np.reshape(pred,-1) - for i in range(gt.size): - print(gt[i]) - print() - for i in range(pred.size): - print(pred[i]) # remove the NaN values in the same location. if np.isnan(gt).any() and np.isnan(pred).any(): gt_mask = np.isnan(gt) From 8cad12955af0a412d1bfa0aad28e07276c29f3d2 Mon Sep 17 00:00:00 2001 From: hejunchao Date: Mon, 25 Sep 2023 19:46:14 +0800 Subject: [PATCH 14/19] fix --- tests/compare_util.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/compare_util.py b/tests/compare_util.py index 99b29524fc..f67e575db3 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -67,8 +67,7 @@ def compare_arrays(gt: np.ndarray, pred: np.ndarray): def euclidean(gt: np.ndarray, pred: np.ndarray, *args): - return np.linalg.norm(gt - pred, 2) ** 2 - + return np.linalg.norm(gt.reshape(-1) - pred.reshape(-1)) def allclose(gt: np.ndarray, pred: np.ndarray, thresh: float): return np.allclose(gt, pred, atol=thresh) @@ -123,7 +122,7 @@ def compare_binfile(result_path: Tuple[str, str], np.savetxt(str(p.parent / (p.stem + '_hist.csv')), np.stack((x[:-1], y)).T, fmt='%f', delimiter=',') similarity_info = f"\n{similarity_name} similarity = {similarity}, threshold = {threshold}\n" - if similarity_name in ['cosine', 'euclidean', 'segment']: + if similarity_name in ['cosine', 'segment']: compare_op = lt else: compare_op = gt @@ -141,14 +140,14 @@ def compare_ndarray(expected: np.ndarray, if expected.size == actual.size: similarity = similarity_func[similarity_name](expected.flatten(), actual.flatten()) else: - raise ValueError("The number of elements in gt and result not match\n") + return False, f"The numbers of elements in gt({expected.size}) and result({actual.size}) are not match.\n" if dump_hist: y, x = np.histogram(expected - actual, 100) np.savetxt(dump_file, np.stack((x[:-1], y)).T, fmt='%f', delimiter=',') similarity_info = f"{similarity_name} similarity = {similarity}, threshold = {threshold}\n" - if similarity_name in ['cosine', 'euclidean', 'segment']: + if similarity_name in ['cosine', 'segment']: compare_op = lt else: compare_op = gt From 41845c7dba12c92c81085132bea9040ed8812dc9 Mon Sep 17 00:00:00 2001 From: HeJunchao100813 Date: Mon, 25 Sep 2023 11:49:14 +0000 Subject: [PATCH 15/19] Apply code-format changes --- tests/compare_util.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/compare_util.py b/tests/compare_util.py index f67e575db3..4db9da9a48 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -69,6 +69,7 @@ def compare_arrays(gt: np.ndarray, pred: np.ndarray): def euclidean(gt: np.ndarray, pred: np.ndarray, *args): return np.linalg.norm(gt.reshape(-1) - pred.reshape(-1)) + def allclose(gt: np.ndarray, pred: np.ndarray, thresh: float): return np.allclose(gt, pred, atol=thresh) From f42398786b1037a6d5340f626deab7cb0317d07c Mon Sep 17 00:00:00 2001 From: hejunchao Date: Mon, 25 Sep 2023 20:24:42 +0800 Subject: [PATCH 16/19] fix --- tests/importer/tflite_/basic/test_depthwise_conv2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/importer/tflite_/basic/test_depthwise_conv2d.py b/tests/importer/tflite_/basic/test_depthwise_conv2d.py index 1b416c5236..a9d5b0c1a8 100644 --- a/tests/importer/tflite_/basic/test_depthwise_conv2d.py +++ b/tests/importer/tflite_/basic/test_depthwise_conv2d.py @@ -59,7 +59,7 @@ def __call__(self, x): strides = [ [1, 1], [1, 3], - [5, 5] + # [5, 5] ] paddings = [ From 6a69ffef0b89c154d562f44c5b8954a3ee594a51 Mon Sep 17 00:00:00 2001 From: hejunchao Date: Mon, 25 Sep 2023 20:41:14 +0800 Subject: [PATCH 17/19] fix --- tests/importer/tflite_/basic/test_depthwise_conv2d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/importer/tflite_/basic/test_depthwise_conv2d.py b/tests/importer/tflite_/basic/test_depthwise_conv2d.py index a9d5b0c1a8..7521b6a380 100644 --- a/tests/importer/tflite_/basic/test_depthwise_conv2d.py +++ b/tests/importer/tflite_/basic/test_depthwise_conv2d.py @@ -69,7 +69,7 @@ def __call__(self, x): dilations = [ [1, 1], - # [2, 2] there is a bug in tf.nn.depthwise_conv2d that produces incorrect output shape + # [2, 2] there is a bug in tf.nn.depthwise_conv2d that produces incorrect output shape. ] From 5d20abba4b8e61c3edb1139bd91e6a9f28627310 Mon Sep 17 00:00:00 2001 From: hejunchao Date: Tue, 26 Sep 2023 14:11:19 +0800 Subject: [PATCH 18/19] fix --- tests/compare_util.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/compare_util.py b/tests/compare_util.py index 4db9da9a48..46b1273b61 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -25,13 +25,6 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): - # gt = np.reshape(gt,-1) - # pred = np.reshape(pred,-1) - # for i in range(gt.size): - # print(gt[i]) - # print() - # for i in range(pred.size): - # print(pred[i]) # remove the NaN values in the same location. if np.isnan(gt).any() and np.isnan(pred).any(): @@ -53,7 +46,7 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): if np.isnan(gt).any() or np.isnan(pred).any() or np.isinf(gt).any() or np.isinf(pred).any(): return -1 - # exclude the situation of all zeros in ndarray. + # exclude the situation of all zeros in array. if compare_arrays(gt, pred): return 1 From 3182ee35ee7f7aa562d037b7687a5c6856f089ea Mon Sep 17 00:00:00 2001 From: hejunchao Date: Thu, 12 Oct 2023 17:12:44 +0800 Subject: [PATCH 19/19] fix similarity --- tests/compare_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/compare_util.py b/tests/compare_util.py index 46b1273b61..5e1eb56ba0 100644 --- a/tests/compare_util.py +++ b/tests/compare_util.py @@ -25,7 +25,6 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): - # remove the NaN values in the same location. if np.isnan(gt).any() and np.isnan(pred).any(): gt_mask = np.isnan(gt) @@ -52,6 +51,7 @@ def cosine(gt: np.ndarray, pred: np.ndarray, *args): result = (gt @ pred) / (np.linalg.norm(gt, 2) * np.linalg.norm(pred, 2)) + # When tensor gt is a multiple of tensor pred, their similarity is also 1. return -1 if math.isnan(result) else result