From d6359d007cb0d31f87b8a0252dcad323d498fb4c Mon Sep 17 00:00:00 2001 From: wu-lanchabu <1098900762@qq.com> Date: Fri, 25 Mar 2022 08:54:58 +0000 Subject: [PATCH 1/7] add TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow. --- .../contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py diff --git a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py new file mode 100644 index 000000000..e69de29bb -- Gitee From 3ce8f78109aff24215ab0e51579e011dbc71ed62 Mon Sep 17 00:00:00 2001 From: wu-lanchabu <1098900762@qq.com> Date: Fri, 25 Mar 2022 09:10:03 +0000 Subject: [PATCH 2/7] update TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py. --- .../predict_from_ckpt.py | 182 ++++++++++++++++++ 1 file changed, 182 insertions(+) diff --git a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py index e69de29bb..24d65b963 100644 --- a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py +++ b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py @@ -0,0 +1,182 @@ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# 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. +# ============================================================================ +# 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 glob import glob +import scipy.io as sio +from skimage.io import imread, imsave +from skimage.transform import rescale, resize +from time import time +import argparse +import ast +# from api import PRN +import cv2 +import tensorflow as tf +from skimage.io import imread, imsave +from skimage.transform import estimate_transform, warp +from tensorflow.core.protobuf.rewriter_config_pb2 import RewriterConfig +import npu_bridge + + +def NME(S_bbox, infer, label): + if label.shape[1] == 2: + error = np.mean(np.sqrt((label[:, 0] - infer[:, 0]) ** 2 + (label[:, 1] - infer[:, 1]) ** 2)) + elif label.shape[1] == 3: + error = np.mean(np.sqrt((label[:, 0] - infer[:, 0]) ** 2 + (label[:, 1] - infer[:, 1]) ** 2 + (label[:, 2] - infer[:, 2]) ** 2)) + nme = error / S_bbox + # print(nme*1000) + return nme + + +def to255(arr): + tmp = np.zeros(arr.shape, dtype=np.uint8) + index_less_equal_one = arr <= 1 + index_greater_one = arr > 1 + tmp[index_less_equal_one] = arr[index_less_equal_one] * 255 + tmp[index_greater_one] = arr[index_greater_one] + + return tmp + + +class Infer: + + def __init__(self, args): + # -------------------------------------------------------------------------------- + config = tf.ConfigProto() + custom_op = config.graph_options.rewrite_options.custom_optimizers.add() + if "autotune" in os.environ: + print ("autotune value is " + os.environ["autotune"]) + if os.environ["autotune"] == "True": + print ("autotune is set !") + custom_op.parameter_map["autotune_tune_mode"].s = tf.compat.as_bytes("RL,GA") + print ("autotune set success") + else: + print ("autotune is not set!") + + custom_op.name = "NpuOptimizer" + # 1)run on Ascend NPU + custom_op.parameter_map["use_off_line"].b = True + + # 2)recommended use fp16 datatype to obtain better performance + custom_op.parameter_map["precision_mode"].s = tf.compat.as_bytes("force_fp16") + + # 3)disable remapping + config.graph_options.rewrite_options.remapping = RewriterConfig.OFF + + # 4)set graph_run_mode=0,obtain better performance + custom_op.parameter_map["graph_run_mode"].i = 0 + # -------------------------------------------------------------------------------- + + # load model, set graph input nodes and output nodes + self.uv_kpt_ind = np.loadtxt('Data/uv-data/uv_kpt_ind.txt').astype(np.int32) # 2 x 68 get kpt + self.inputs = tf.placeholder(tf.float32, shape=[1, None, None, 3], name='input') + self.net = resfcn256(256, 256) + self.logits = self.net(self.inputs, is_training=False) + self.sess = tf.Session(config=config) + self.__resotre(args.model_path) + + def __resotre(self, ckpt_path): + saver = tf.train.Saver() + saver.resotre(self.sess, ckpt_path) + + def do_infer(self, data): + out = self.sess.run(self.output_tensor, feed_dict={self.input_tensor: data[np.newaxis, :, :, :]}) + out = np.squeeze(out) + return out * 256 * 1.1 + + def get_landmarks(self, pos): + kpt = pos[self.uv_kpt_ind[1, :], self.uv_kpt_ind[0, :], :] + return kpt + + + +def main(args): + tf.reset_default_graph() + infer = Infer(args) + + # ---- init PRN + # prn = PRN(is_dlib=args.isDlib) + + # ------------- load data + image_folder = args.inputDir + + types = ('*.jpg', '*.png') + image_path_list = [] + for files in types: + image_path_list.extend(glob(os.path.join(image_folder, files))) + total_num = len(image_path_list) + + nme = 0.0 # normalized mean error + + for i, image_path in enumerate(image_path_list): + + name = image_path.strip().split('/')[-1][:-4] + # read image + image = cv2.imread(image_path) + [h, w, c] = image.shape + if c > 3: + image = image[:, :, :3] + + pos = infer.do_infer(image / 255.) # input image has been cropped to 256x256 + S = image.shape[0] * image.shape[1] + image = image / 255. + + if pos is None: + continue + + if args.isKpt: + # get predict landmarks + predict_kpt = infer.get_landmarks(pos) + label_kpt = np.loadtxt(os.path.join(image_folder, name + '.txt')) + error = NME(S, predict_kpt, label_kpt) + nme += error + print(name + '\t:\t' + str(error*1000)) + + nme /= total_num + print("========================NME(%)========================") + print('NME : ',nme*1000) + print("========================NME(%)========================") + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Joint 3D Face Reconstruction and Dense Alignment with Position Map Regression Network') + + parser.add_argument('-i', '--inputDir', default='Dataset/TestData/LFPA/', type=str, + help='path to the input directory, where input images are stored.') + parser.add_argument('--isDlib', default=False, type=ast.literal_eval, + help='whether to use dlib for detecting face, default is True, if False, the input image ' + 'should be cropped in advance') + parser.add_argument('--isKpt', default=True, type=ast.literal_eval,help='whether to output key points(.txt)') + + parser.add_argument('--batchsize', default=1,help="""batchsize""") + parser.add_argument('--model_path', default='model/prnet.pb',help="""ckpt path""") + + + main(parser.parse_args()) -- Gitee From 26502d4013ac6c969375d9b9d48ea9fa2a3b5c37 Mon Sep 17 00:00:00 2001 From: wu-lanchabu <1098900762@qq.com> Date: Fri, 25 Mar 2022 09:18:42 +0000 Subject: [PATCH 3/7] update TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh. --- .../cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh index 094dd665b..72d67e67d 100644 --- a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh +++ b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh @@ -130,7 +130,11 @@ do --root_train_data_dir=${data_path}/TrainData \ --batch_size=64 \ --end_data_num=800 \ - --epochs=1 > $cur_path/test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 + --epochs=10 > $cur_path/test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 + + python3 predict_from_ckpt.py \ + -i ${data_path}/TestData/LFPA \ + --model_path=./checkpoint/model.ckpt >> $cur_path/test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 done @@ -159,7 +163,7 @@ CaseName=${Network}_bs${BatchSize}_${RANK_SIZE}'p'_'perf' ##获取性能数据,不需要修改 #吞吐量 ActualFPS=`awk 'BEGIN{printf "%.3f\n", 64/'${TrainingTime}'}'` - +train_acc=`grep "NME :" $cur_path/test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log |awk '{print $3}'||awk -F'%' '{print $1}'` #最后一个迭代loss值,不需要修改 grep 'loss:' $cur_path/test/output/$ASCEND_DEVICE_ID/train_$ASCEND_DEVICE_ID.log |awk '{print $7}' |awk -F':' '{print $2}' |tail -n +2 > $cur_path/test/output/$ASCEND_DEVICE_ID/train_${CaseName}_loss.txt @@ -178,4 +182,5 @@ echo "CaseName = ${CaseName}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseN echo "ActualFPS = ${ActualFPS}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log echo "TrainingTime = ${TrainingTime}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log echo "ActualLoss = ${ActualLoss}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "TrainAccuracy = ${train_acc}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log echo "E2ETrainingTime = ${e2e_time}" >> $cur_path/test/output/$ASCEND_DEVICE_ID/${CaseName}.log -- Gitee From bfee4e487ef54a97780f02dc8289486c00cd8565 Mon Sep 17 00:00:00 2001 From: wu-lanchabu <1098900762@qq.com> Date: Fri, 25 Mar 2022 09:32:04 +0000 Subject: [PATCH 4/7] update TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh. --- .../cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh index 72d67e67d..de6d32bb8 100644 --- a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh +++ b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh @@ -24,7 +24,7 @@ Network="PRNET_ID0727_for_TensorFlow" #训练epoch train_epochs=1 #训练batch_size -batch_size=64 +batch_size=32 #训练step train_steps=`expr 1281167 / ${batch_size}` #学习率 @@ -128,12 +128,11 @@ do python3 train.py \ --root_train_data_dir=${data_path}/TrainData \ - --batch_size=64 \ - --end_data_num=800 \ + --batch_size=32 \ --epochs=10 > $cur_path/test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 python3 predict_from_ckpt.py \ - -i ${data_path}/TestData/LFPA \ + -i=${data_path}/TestData/LFPA \ --model_path=./checkpoint/model.ckpt >> $cur_path/test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 -- Gitee From c91f21b2881872ae3dd60d78ab64121b50e8b33d Mon Sep 17 00:00:00 2001 From: wu-lanchabu <1098900762@qq.com> Date: Mon, 28 Mar 2022 01:17:23 +0000 Subject: [PATCH 5/7] update TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py. --- .../contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py | 1 + 1 file changed, 1 insertion(+) diff --git a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py index 24d65b963..8d7bdca04 100644 --- a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py +++ b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py @@ -42,6 +42,7 @@ from skimage.io import imread, imsave from skimage.transform import estimate_transform, warp from tensorflow.core.protobuf.rewriter_config_pb2 import RewriterConfig import npu_bridge +from predictor import resfcn256 def NME(S_bbox, infer, label): -- Gitee From 5bd8ae53d17809b088d2bb520c070609c6e9e485 Mon Sep 17 00:00:00 2001 From: wu-lanchabu <1098900762@qq.com> Date: Mon, 28 Mar 2022 01:25:22 +0000 Subject: [PATCH 6/7] update TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py. --- .../cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py index 8d7bdca04..4a5abfd31 100644 --- a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py +++ b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/predict_from_ckpt.py @@ -100,14 +100,14 @@ class Infer: self.net = resfcn256(256, 256) self.logits = self.net(self.inputs, is_training=False) self.sess = tf.Session(config=config) - self.__resotre(args.model_path) + self.__restore(args.model_path) - def __resotre(self, ckpt_path): + def __restore(self, ckpt_path): saver = tf.train.Saver() - saver.resotre(self.sess, ckpt_path) + saver.restore(self.sess, ckpt_path) def do_infer(self, data): - out = self.sess.run(self.output_tensor, feed_dict={self.input_tensor: data[np.newaxis, :, :, :]}) + out = self.sess.run(self.logits, feed_dict={self.inputs: data[np.newaxis, :, :, :]}) out = np.squeeze(out) return out * 256 * 1.1 -- Gitee From 574e519609db77ab39dd39b3ff80101e3a270993 Mon Sep 17 00:00:00 2001 From: wu-lanchabu <1098900762@qq.com> Date: Mon, 28 Mar 2022 01:40:03 +0000 Subject: [PATCH 7/7] update TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh. --- .../cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh index de6d32bb8..de3ce0eb4 100644 --- a/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh +++ b/TensorFlow/contrib/cv/PRNET_ID0727_for_TensorFlow/test/train_full_1p.sh @@ -157,7 +157,7 @@ echo "E2E Training Duration sec : $e2e_time" #训练用例信息,不需要修改 BatchSize=${batch_size} DeviceType=`uname -m` -CaseName=${Network}_bs${BatchSize}_${RANK_SIZE}'p'_'perf' +CaseName=${Network}_bs${BatchSize}_${RANK_SIZE}'p'_'acc' ##获取性能数据,不需要修改 #吞吐量 -- Gitee