From ede42fef5f264caa3a1117aeabba2e797bf1302e Mon Sep 17 00:00:00 2001 From: formlessli <1500424927@qq.com> Date: Thu, 14 Apr 2022 17:54:46 +0800 Subject: [PATCH] meanteacher acl --- .../cv/MEAN-TEACHER_ID0789_for_ACL/LICENSE | 35 +++++++++ .../cv/MEAN-TEACHER_ID0789_for_ACL/README.md | 57 ++++++++++++++ .../cal_inference_pref.py | 46 ++++++++++++ .../data_convert_bin.py | 75 +++++++++++++++++++ .../modelzoo_level.txt | 4 + 5 files changed, 217 insertions(+) create mode 100644 ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/LICENSE create mode 100644 ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/README.md create mode 100644 ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/cal_inference_pref.py create mode 100644 ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/data_convert_bin.py create mode 100644 ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/modelzoo_level.txt diff --git a/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/LICENSE b/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/LICENSE new file mode 100644 index 000000000..352b0c6cf --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/LICENSE @@ -0,0 +1,35 @@ +# MIT License +# +# Copyright (c) 2018 Kamran Janjua + +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. \ No newline at end of file diff --git a/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/README.md b/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/README.md new file mode 100644 index 000000000..001e2c819 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/README.md @@ -0,0 +1,57 @@ +# 概述 + mean-teacher是一种用于图像分类的半监督学习方法,能够在拥有少量有标签数据的情况下训练出分类准确率很高的网络模型。 + +- 论文链接: [Weight-averaged consistency targets improve semi-supervised deep learning results](https://arxiv.org/abs/1703.01780) + +- 官方代码仓: [链接](https://github.com/CuriousAI/mean-teacher/) + +- 精度比较 + +| 推理数据 | Accuracy Train | Accuracy Infer | +| :----:| :----: | :----: | +| 10000 | 85.4% | 86.8% | + +# 环境 +推理前请参考下文进行环境配置: [使用ATC转换OM模型](https://support.huaweicloud.com/atctool-cann502alpha3infer/atlasatc_16_0004.html) and [使用msame进行推理](https://gitee.com/ascend/tools/tree/master/msame) + +# 推理 +## 数据准备 + 使用cifar10数据集进行训练和推理,数据集路径obs://meanteacher/data/ +## 使用data_convert_to_bin.py进行数据预处理 +```commandline +#dataset_path: 数据集路径 +#bin_path: 预处理后数据和标签的路径 +#batch_size: batch大小, 需与OM模型一致,默认1 +python data_convert_bin.py --dataset_path=XXX --bin_path=XXX --batch_size=1 +``` +## OM模型转换命令 +```commandline +#参照命令和实际环境设置具体路径和参数 +atc --model=/root/mean_acl/pb_model/mean-teacher.pb \ #pb模型路径 + --framework=3 \ + --output=/root/mean_acl/pb_out/mean_teacher \ #输出的om模型路径 + --soc_version=Ascend310 \ + --input_shape="placeholders/images:1,32,32,3" \ + --log=info \ + --out_nodes="output:0" +``` + +## msame推理命令 +```commandline +#参照命令和实际环境设置路径和参数 +msame --model /home/HwHiAiUser/mean_teacher_acl/mean_teacher.om \ #用于推理的OM模型路径 + --input /home/HwHiAiUser/mean_teacher_acl/bin/image_1 \ #用于推理的bin格式数据路径 + --output /home/HwHiAiUser/mean_teacher_acl/ \ #推理结果输出路径 + --outfmt TXT +``` + +## 精度计算 +```commandline +python cal_inference_pref.py --LABEL_FLODER=XXX \ #推理结果目录 + --PREDICT_FLODER=XXX #label文件路径 +``` + +# 附录 +## 推理文件OBS路径: + - pb模型及转换后om模型: obs://meanteacher/acl/model + - 转换后的bin数据集:obs://meanteacher/acl/bin/ diff --git a/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/cal_inference_pref.py b/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/cal_inference_pref.py new file mode 100644 index 000000000..8d0fdc582 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/cal_inference_pref.py @@ -0,0 +1,46 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +import os +import numpy as np +from absl import flags,app +FLAGS = flags.FLAGS + +def main(argv): + del argv + LABEL_FLODER = FLAGS.LABEL_FLODER + PREDICT_FLODER = FLAGS.PREDICT_FLODER + + files_label = os.listdir(LABEL_FLODER) + files_predict = os.listdir(PREDICT_FLODER) + num,check_num = 0,0 + label_map = {} + for file in files_label: + if file.endswith(".txt"): + tmp = np.loadtxt(LABEL_FLODER+'/'+file, dtype='float32') + label_map[file.split("_")[0]] = tmp if tmp.ndim is not 0 else [tmp] + for file in files_predict: + if file.endswith(".txt"): + num += 1 + tmp = np.loadtxt(PREDICT_FLODER+'/'+file, dtype='int32') + tmp = tmp if tmp.ndim is not 0 else [tmp] + + if(tmp[0] == int(label_map[file.split("_")[0]][0])): + check_num += 1 + + top1_accuarcy = check_num/num + print("Totol num: %d, accuarcy: %.4f"%(num,top1_accuarcy)) +if __name__ == '__main__': + flags.DEFINE_string('LABEL_FLODER',"./bin/label_1/",'The floder of TRUE result') + flags.DEFINE_string('PREDICT_FLODER',"./inference_out/20220414_152403/",'The floder of inference result') + app.run(main) \ No newline at end of file diff --git a/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/data_convert_bin.py b/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/data_convert_bin.py new file mode 100644 index 000000000..0fe76a901 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/data_convert_bin.py @@ -0,0 +1,75 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +import numpy as np +import os +from absl import flags,app + +FLAGS = flags.FLAGS +flags.DEFINE_string( + 'dataset_path', default='./data', + help=('Directory to store dataset data')) +flags.DEFINE_string( + 'bin_path', default='./bin', + help=('Directory to store bin data')) +flags.DEFINE_integer( + 'batch_size', default=1, + help=('Batch size for inference,need to be divided by sum_samples.')) + + +def batch_generator(data, batch_size=100): + def generate(): + for idx in range(0, len(data), batch_size): + yield data[idx:(idx + batch_size)] + return generate + +def data_array(expected_n, x_data, y_data): + array = np.zeros(expected_n, dtype=[ + ('x', np.float32, (32, 32, 3)), + ('y', np.int32, ()) # We will be using -1 for unlabeled + ]) + array['x'] = x_data + array['y'] = y_data + return array + + + + +def main(argv): + if len(argv) > 3: + raise app.UsageError('Too many command-line arguments.') + image_path = os.path.join(FLAGS.bin_path,'image_' + str(FLAGS.batch_size)) + if not os.path.exists(image_path): + os.makedirs(image_path) + label_path = os.path.join(FLAGS.bin_path, 'label_' + str(FLAGS.batch_size)) + if not os.path.exists(label_path): + os.makedirs(label_path) + + data_path = os.path.join(FLAGS.dataset_path, 'cifar10_gcn_zca_v2.npz') + file_data = np.load(data_path) + test_data = data_array(10000, file_data['test_x'], file_data['test_y']) + test_batches = batch_generator(test_data,FLAGS.batch_size) + + i = 0 + for batch in test_batches(): + name_image = ''.join([image_path, '/', str(i), '_image', '.bin']) + name_label = ''.join([label_path, '/', str(i), '_label', '.txt']) + image = batch['x'].astype(np.float32) + label = batch['y'].astype(np.float32) + image.tofile(name_image) + np.savetxt(name_label, label) + i+=1 + + +if __name__ == '__main__': + app.run(main) \ No newline at end of file diff --git a/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/modelzoo_level.txt b/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/modelzoo_level.txt new file mode 100644 index 000000000..813355c67 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/MEAN-TEACHER_ID0789_for_ACL/modelzoo_level.txt @@ -0,0 +1,4 @@ +FuncStatus:OK +PerfStatus:OK +PrecisionStatus:OK +ModelConvert:OK -- Gitee