From 53bc3b1c3535831f4643685f40dbb496846d8071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D=E5=BF=83=E6=82=A6?= <1090110089@qq.com> Date: Fri, 2 Dec 2022 03:35:40 +0000 Subject: [PATCH 1/6] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20Cognitive=5FPlanning?= =?UTF-8?q?=5FID2015=5Ffor=5FACL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/.keep diff --git a/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/.keep b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/.keep new file mode 100644 index 000000000..e69de29bb -- Gitee From 4b30306ad57505cd81b0f13d7a3b922286562071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D=E5=BF=83=E6=82=A6?= <1090110089@qq.com> Date: Fri, 2 Dec 2022 03:36:18 +0000 Subject: [PATCH 2/6] readme MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 蓝心悦 <1090110089@qq.com> --- .../readme.md | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/readme.md diff --git a/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/readme.md b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/readme.md new file mode 100644 index 000000000..712fc2904 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/readme.md @@ -0,0 +1,84 @@ +# LEARNING-TO-SEE-IN-THE-DARK + +## 离线推理 + +### 1. 原始模型转pb + +``` +python3.7 ./ckpt2pb.py +``` + +转换好的pb模型,obs地址: +obs://sid-obs/inference/sid.pb + +### 2. pb转om模型 + +使用atc模型转换工具转换pb模型到om模型 + +``` +atc --model=/home/HwHiAiUser/AscendProjects/SID/pb_model/sid.pb --framework=3 --output=/home/HwHiAiUser/AscendProjects/SID/ --soc_version=Ascend310 --input_shape="Placeholdr:1,1424,2128,4; " --log=info --out_nodes="DepthToSpace:0" +``` + +转换好的OM模型,obs地址: +obs://sid-obs/inference/sid.om + +### 3. 数据处理 + +对输入的Bayer图片按照R,G,B分为4个通道,得到(1424,2128,4)的输入图片和(2848,4256,3)的真值图片。同时,将处理过的图片从ARW格式变为BIN格式。 + +``` +python3.7 ./data_pre.py +``` + +生成的输入数据bin文件,obs地址: +obs://sid-obs/inference/Sony/long +obs://sid-obs/inference/Sony/short + +### 4. 准备msame推理 + +参考[msame](https://gitee.com/ascend/modelzoo/wikis/离线推理案例/离线推理工具msame使用案例) + +### 5. om模型推理 + +使用如下命令进行性能测试: + +``` +./msame --model /home/HwHiAiUser/AscendProjects/SID/sid.om --input /home/HwHiAiUser/AscendProjects/SID/short --output /home/HwHiAiUser/AscendProjects/SID/out/ --outfmt BIN +``` + +测试结果如下: + +``` +[INFO] get max dynamic batch size success +[INFO] output data success +[INFO] destroy model input success +Inference average time : 426.47 ms +Inference average time without first time: 426.47 ms +[INFO] unload model success, model Id is 1 +[INFO] Execute sample success +[INFO] end to destroy stream +[INFO] end to destroy context +[INFO] end to reset device is 0 +[INFO] end to finalize acl +``` + +单张图片的平均推理时间为426.47ms + +### 6. om精度测试 + +使用如下命令进行精度测试: + +``` +python3.7.5 ./eval_om.py +``` + +测试结果如下: + +| | 原论文 | GPU | NPU | 离线推理 | +| ---- | ------ | ----- | ----- | -------- | +| PSNR | 28.88 | 27.63 | 28.26 | 28.64 | +| SSIM | 0.78 | 0.719 | 0.715 | 0.773 | + +离线推理精度达标 +om推理输出bin文件,obs地址: +obs://sid-obs/inference/Sony/om_out \ No newline at end of file -- Gitee From 02c8838bd165edb3ee1055cbffcbffbdea6e0a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D=E5=BF=83=E6=82=A6?= <1090110089@qq.com> Date: Fri, 2 Dec 2022 03:37:04 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ACL?= =?UTF-8?q?=5FTensorFlow/contrib/cv/Cognitive=5FPlanning=5FID2015=5Ffor=5F?= =?UTF-8?q?ACL/readme.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../readme.md | 84 ------------------- 1 file changed, 84 deletions(-) delete mode 100644 ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/readme.md diff --git a/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/readme.md b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/readme.md deleted file mode 100644 index 712fc2904..000000000 --- a/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/readme.md +++ /dev/null @@ -1,84 +0,0 @@ -# LEARNING-TO-SEE-IN-THE-DARK - -## 离线推理 - -### 1. 原始模型转pb - -``` -python3.7 ./ckpt2pb.py -``` - -转换好的pb模型,obs地址: -obs://sid-obs/inference/sid.pb - -### 2. pb转om模型 - -使用atc模型转换工具转换pb模型到om模型 - -``` -atc --model=/home/HwHiAiUser/AscendProjects/SID/pb_model/sid.pb --framework=3 --output=/home/HwHiAiUser/AscendProjects/SID/ --soc_version=Ascend310 --input_shape="Placeholdr:1,1424,2128,4; " --log=info --out_nodes="DepthToSpace:0" -``` - -转换好的OM模型,obs地址: -obs://sid-obs/inference/sid.om - -### 3. 数据处理 - -对输入的Bayer图片按照R,G,B分为4个通道,得到(1424,2128,4)的输入图片和(2848,4256,3)的真值图片。同时,将处理过的图片从ARW格式变为BIN格式。 - -``` -python3.7 ./data_pre.py -``` - -生成的输入数据bin文件,obs地址: -obs://sid-obs/inference/Sony/long -obs://sid-obs/inference/Sony/short - -### 4. 准备msame推理 - -参考[msame](https://gitee.com/ascend/modelzoo/wikis/离线推理案例/离线推理工具msame使用案例) - -### 5. om模型推理 - -使用如下命令进行性能测试: - -``` -./msame --model /home/HwHiAiUser/AscendProjects/SID/sid.om --input /home/HwHiAiUser/AscendProjects/SID/short --output /home/HwHiAiUser/AscendProjects/SID/out/ --outfmt BIN -``` - -测试结果如下: - -``` -[INFO] get max dynamic batch size success -[INFO] output data success -[INFO] destroy model input success -Inference average time : 426.47 ms -Inference average time without first time: 426.47 ms -[INFO] unload model success, model Id is 1 -[INFO] Execute sample success -[INFO] end to destroy stream -[INFO] end to destroy context -[INFO] end to reset device is 0 -[INFO] end to finalize acl -``` - -单张图片的平均推理时间为426.47ms - -### 6. om精度测试 - -使用如下命令进行精度测试: - -``` -python3.7.5 ./eval_om.py -``` - -测试结果如下: - -| | 原论文 | GPU | NPU | 离线推理 | -| ---- | ------ | ----- | ----- | -------- | -| PSNR | 28.88 | 27.63 | 28.26 | 28.64 | -| SSIM | 0.78 | 0.719 | 0.715 | 0.773 | - -离线推理精度达标 -om推理输出bin文件,obs地址: -obs://sid-obs/inference/Sony/om_out \ No newline at end of file -- Gitee From 58948ac0d2bf8b0537f0efb2d2eb0936b97871e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D=E5=BF=83=E6=82=A6?= <1090110089@qq.com> Date: Fri, 2 Dec 2022 04:08:06 +0000 Subject: [PATCH 4/6] add code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 蓝心悦 <1090110089@qq.com> --- .../pb_inference.py | 39 ++ .../readme.md | 73 +++ .../train_supervised_active_vision.py | 545 ++++++++++++++++++ 3 files changed, 657 insertions(+) create mode 100644 ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/pb_inference.py create mode 100644 ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/readme.md create mode 100644 ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/train_supervised_active_vision.py diff --git a/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/pb_inference.py b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/pb_inference.py new file mode 100644 index 000000000..15d5d67ce --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/pb_inference.py @@ -0,0 +1,39 @@ +import numpy as np +import tensorflow as tf +from tensorflow.python.platform import gfile +import cv2 +import numpy +# from keras.applications.xception import preprocess_input + + + +config = tf.ConfigProto() +sess = tf.Session(config=config) +with gfile.FastGFile('./placeholder_protobuf.pb', 'rb') as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) + sess.graph.as_default() + tf.import_graph_def(graph_def, name='') + +print('----------') + # 获取输入tensor +taskdata = tf.get_default_graph().get_tensor_by_name("taskdata:0") +taskdata_1 = tf.get_default_graph().get_tensor_by_name("taskdata_1:0") +taskdata_2 = tf.get_default_graph().get_tensor_by_name("taskdata_2:0") +print("input:", taskdata) +# 获取预测tensor +pred = tf.get_default_graph().get_tensor_by_name("policy/Reshape_3:0") # mobilenet_v2 +print(pred) + +x1 = np.fromfile('./taskdata_0.bin', dtype=np.float32) +x2 = np.fromfile('./taskdata_1.bin', dtype=np.float32) +x3 = np.fromfile('./taskdata_2.bin', dtype=np.float32) + +x1=x1.reshape([8, 20, 64, 64, 90]) +x2=x2.reshape([8, 20, 5]) +x3=x3.reshape([8, 20, 8]) + +res = sess.run(pred, feed_dict={taskdata: x1, taskdata_1: x2, taskdata_2: x3}) + +print(res.shape) +print(res) diff --git a/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/readme.md b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/readme.md new file mode 100644 index 000000000..f89e08feb --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/readme.md @@ -0,0 +1,73 @@ +# **Cognitive_Planning** + +## 离线推理 + +### 1. 原始模型转pb + +``` +python ./ckpt2pb.py +``` + + + +### 2. pb转om模型 + +使用atc模型转换工具转换pb模型到om模型。 + +``` +atc --model=/home/test_user07/Cognitive/pb/placeholder_protobuf.pb --framework=3 --output=/home/test_user07/Cognitive/om/cognitiveplanning_acc --soc_version=Ascend910 --input_shape="taskdata:8,20,64,64,90;taskdata_1:8,20,5;taskdata_2:8,20,8" --log=info --out_nodes="policy/Reshape_3:0" +``` + + + +### 3. 数据处理 + +对输入的数据进行处理,将数据变为BIN格式。在原环境中重新运行此py文件,此py文件添加了将数据处理为bin格式的相关代码。 + +``` +python train_supervised_active_vision.py --mode=train --logdir=checkpoint --modality_types=det --batch_size=8 --train_iters=800000 --lstm_cell_size=2048 --policy_fc_size=2048 --sequence_length=20 --max_eval_episode_length=100 --test_iters=194 --gin_config=envs/configs/active_vision_config.gin --gin_params="ActiveVisionDatasetEnv.dataset_root='AVD_Minimal'" --logtostderr +``` + +生成taskdata_0.bin、taskdata_1.bin、taskdata_2.bin三个bin文件。 + +### 4. pb推理 + +``` +python ./pb_inference.py +``` + +得到网络的输出结果 + +### 5. om模型推理 + +使用如下命令进行性能测试: + +``` +./msame --model "/home/test_user07/Cognitive/om/cognitiveplanning_acc.om" --input "/home/test_user07/Cognitive/bin/taskdata_0.bin,/home/test_user07/Cognitive/bin/taskdata_1.bin,/home/test_user07/Cognitive/bin/taskdata_2.bin" --output "/home/test_user07/Cognitive/outs/" --outfmt TXT --loop 1 +``` + +出现如下错误: + +``` +[INFO] acl init success +[INFO] open device 0 success +[INFO] create context success +[INFO] create stream success +[INFO] get run mode success +[INFO] load model /home/test_user07/Cognitive/om/cognitiveplanning_acc.om success +[INFO] create model description success +[INFO] get input dynamic gear count success +EH0001: Value [0] for [size] is invalid. Reason: size must be greater than zero. + +[ERROR] can't malloc buffer, size is 0, create output failed +[ERROR] create model output failed +[INFO] unload model success, model Id is 1 +[ERROR] Sample process failed +[INFO] end to destroy stream +[INFO] end to destroy context +[INFO] end to reset device is 0 +[INFO] end to finalize acl +``` + +由于pb推理可以成功,om推理出现问题,考虑工具还不支持静态输入,动态输出的相关问题,先提交于此,后面再做相关修改。 + diff --git a/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/train_supervised_active_vision.py b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/train_supervised_active_vision.py new file mode 100644 index 000000000..82d1c2b6c --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/train_supervised_active_vision.py @@ -0,0 +1,545 @@ +# Copyright 2018 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. +# ============================================================================== + +# pylint: disable=line-too-long +# pyformat: disable +"""Train and eval for supervised navigation training. + +For training: +python train_supervised_active_vision.py \ + --mode='train' \ + --logdir=$logdir/checkin_log_det/ \ + --modality_types='det' \ + --batch_size=8 \ + --train_iters=200000 \ + --lstm_cell_size=2048 \ + --policy_fc_size=2048 \ + --sequence_length=20 \ + --max_eval_episode_length=100 \ + --test_iters=194 \ + --gin_config=envs/configs/active_vision_config.gin \ + --gin_params='ActiveVisionDatasetEnv.dataset_root="$datadir"' \ + --logtostderr + +For testing: +python train_supervised_active_vision.py + --mode='eval' \ + --logdir=$logdir/checkin_log_det/ \ + --modality_types='det' \ + --batch_size=8 \ + --train_iters=200000 \ + --lstm_cell_size=2048 \ + --policy_fc_size=2048 \ + --sequence_length=20 \ + --max_eval_episode_length=100 \ + --test_iters=194 \ + --gin_config=envs/configs/active_vision_config.gin \ + --gin_params='ActiveVisionDatasetEnv.dataset_root="$datadir"' \ + --logtostderr +""" + +import collections +import os +import time +from absl import app +from absl import flags +from absl import logging +import networkx as nx +import numpy as np +import tensorflow as tf +import gin +import embedders +import policies +import tasks +import logging#打印日志 +from envs import active_vision_dataset_env +from envs import task_env + +logging.basicConfig(format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s', + level=logging.DEBUG, + filename='gpu_running.log', + filemode='a') + +slim = tf.contrib.slim + +flags.DEFINE_string('logdir', '', + 'Path to a directory to write summaries and checkpoints') +# Parameters controlling the training setup. In general one would not need to +# modify them. +flags.DEFINE_string('master', 'local', + 'BNS name of the TensorFlow master, or local.') +flags.DEFINE_integer('task_id', 0, + 'Task id of the replica running the training.') +flags.DEFINE_integer('ps_tasks', 0, + 'Number of tasks in the ps job. If 0 no ps job is used.') + +flags.DEFINE_integer('decay_steps', 1000, + 'Number of steps for exponential decay.') +flags.DEFINE_float('learning_rate', 0.0001, 'Learning rate.') +flags.DEFINE_integer('batch_size', 8, 'Batch size.') +flags.DEFINE_integer('sequence_length', 20, 'sequence length') +flags.DEFINE_integer('train_iters', 200000, 'number of training iterations.') +flags.DEFINE_integer('save_summaries_secs', 300, + 'number of seconds between saving summaries') +flags.DEFINE_integer('save_interval_secs', 300, + 'numer of seconds between saving variables') +flags.DEFINE_integer('log_every_n_steps', 20, 'number of steps between logging') +flags.DEFINE_string('modality_types', '', + 'modality names in _ separated format') +flags.DEFINE_string('conv_window_sizes', '8_4_3', + 'conv window size in separated by _') +flags.DEFINE_string('conv_strides', '4_2_1', '') +flags.DEFINE_string('conv_channels', '8_16_16', '') +flags.DEFINE_integer('embedding_fc_size', 128, + 'size of embedding for each modality') +flags.DEFINE_integer('obs_resolution', 64, + 'resolution of the input observations') +flags.DEFINE_integer('lstm_cell_size', 2048, 'size of lstm cell size') +flags.DEFINE_integer('policy_fc_size', 2048, + 'size of fully connected layers for policy part') +flags.DEFINE_float('weight_decay', 0.0002, 'weight decay') +flags.DEFINE_integer('goal_category_count', 5, 'number of goal categories') +flags.DEFINE_integer('action_size', 7, 'number of possible actions') +flags.DEFINE_integer('max_eval_episode_length', 100, + 'maximum sequence length for evaluation.') +flags.DEFINE_enum('mode', 'train', ['train', 'eval'], + 'indicates whether it is in training or evaluation') +flags.DEFINE_integer('test_iters', 194, + 'number of iterations that the eval needs to be run') +flags.DEFINE_multi_string('gin_config', [], + 'List of paths to a gin config files for the env.') +flags.DEFINE_multi_string('gin_params', [], + 'Newline separated list of Gin parameter bindings.') +flags.DEFINE_string( + 'resnet50_path', '', 'path to resnet50'#./resnet_v2_50_checkpoint/resnet_v2_50.ckpt + 'checkpoint') +flags.DEFINE_bool('freeze_resnet_weights', True, '') +flags.DEFINE_string( + 'eval_init_points_file_name', '', + 'Name of the file that contains the initial locations and' + 'worlds for each evaluation point') + +FLAGS = flags.FLAGS +TRAIN_WORLDS = [ + 'Home_001_1', 'Home_001_2', 'Home_002_1', 'Home_003_1', 'Home_003_2', 'Home_005_1', 'Home_005_2', 'Home_006_1', + 'Home_004_1', 'Home_004_2', 'Home_005_1', 'Home_005_2', 'Home_006_1', 'Home_010_1' +] +TEST_WORLDS = ['Home_011_1', 'Home_013_1', 'Home_016_1'] + + +def create_modality_types(): + """Parses the modality_types and returns a list of task_env.ModalityType.""" + if not FLAGS.modality_types: + raise ValueError('there needs to be at least one modality type') + modality_types = FLAGS.modality_types.split('_') + for x in modality_types: + if x not in ['image', 'sseg', 'det', 'depth']: + raise ValueError('invalid modality type: {}'.format(x)) + + conversion_dict = { + 'image': task_env.ModalityTypes.IMAGE, + 'sseg': task_env.ModalityTypes.SEMANTIC_SEGMENTATION, + 'depth': task_env.ModalityTypes.DEPTH, + 'det': task_env.ModalityTypes.OBJECT_DETECTION, + } + return [conversion_dict[k] for k in modality_types] + + +def create_task_io_config( + modality_types, + goal_category_count, + action_size, + sequence_length, +): + """Generates task io config.""" + shape_prefix = [sequence_length, FLAGS.obs_resolution, FLAGS.obs_resolution] + shapes = { + task_env.ModalityTypes.IMAGE: [sequence_length, 224, 224, 3], + task_env.ModalityTypes.DEPTH: shape_prefix + [ + 2, + ], + task_env.ModalityTypes.SEMANTIC_SEGMENTATION: shape_prefix + [1,], + task_env.ModalityTypes.OBJECT_DETECTION: shape_prefix + [90,] + } + types = {k: tf.float32 for k in shapes} + types[task_env.ModalityTypes.IMAGE] = tf.uint8 + inputs = collections.OrderedDict([[mtype, (types[mtype], shapes[mtype])] for mtype in modality_types]) + inputs[task_env.ModalityTypes.GOAL] = (tf.float32, + [sequence_length, goal_category_count]) + inputs[task_env.ModalityTypes.PREV_ACTION] = (tf.float32, [ + sequence_length, action_size + 1 + ]) + logging.info(inputs) + return tasks.UnrolledTaskIOConfig( + inputs=inputs, + output=(tf.float32, [sequence_length, action_size]), + query=None) + + +def map_to_embedder(modality_type): + """Maps modality_type to its corresponding embedder.""" + if modality_type == task_env.ModalityTypes.PREV_ACTION: + return None + if modality_type == task_env.ModalityTypes.GOAL: + return embedders.IdentityEmbedder() + if modality_type == task_env.ModalityTypes.IMAGE: + return embedders.ResNet50Embedder() + conv_window_sizes = [int(x) for x in FLAGS.conv_window_sizes.split('_')] + conv_channels = [int(x) for x in FLAGS.conv_channels.split('_')] + conv_strides = [int(x) for x in FLAGS.conv_strides.split('_')] + params = tf.contrib.training.HParams( + to_one_hot=modality_type == task_env.ModalityTypes.SEMANTIC_SEGMENTATION, + one_hot_length=10, + conv_sizes=conv_window_sizes, + conv_strides=conv_strides, + conv_channels=conv_channels, + embedding_size=FLAGS.embedding_fc_size, + weight_decay_rate=FLAGS.weight_decay, + ) + return embedders.SmallNetworkEmbedder(params) + + +def create_train_and_init_ops(policy, task): + """Creates training ops given the arguments. + + Args: + policy: the policy for the task. + task: the task instance. + + Returns: + train_op: the op that needs to be runned at each step. + summaries_op: the summary op that is executed. + init_fn: the op that initializes the variables if there is no previous + checkpoint. If Resnet50 is not used in the model it is None, otherwise + it reads the weights from FLAGS.resnet50_path and sets the init_fn + to the op that initializes the ResNet50 with the pre-trained weights. + """ + assert isinstance(task, tasks.GotoStaticXNoExplorationTask) + assert isinstance(policy, policies.Policy) + + inputs, _, gt_outputs, masks = task.tf_episode_batch(FLAGS.batch_size) + logging.info('6----------------\n---------------\n------------\n---------------') + logging.info(inputs) + + mtypes=[] + for mtype in list(inputs): # 提取出字典inputs中的键 + mtypes.append(mtype) + with tf.Session() as sess: + a = inputs[mtypes[0]].eval() # taskdata:0的 tensor2numpy + b = inputs[mtypes[1]].eval() # taskdata:1的 tensor2numpy + c = inputs[mtypes[2]].eval() # taskdata:2的 tensor2numpy + logging.info('10+++++++++++++\n+++++++++++++\n') + logging.info(a.shape) + logging.info(b.shape) + logging.info(c.shape) + a.tofile('taskdata_0.bin', ) + b.tofile('taskdata_1.bin') + c.tofile('taskdata_2.bin') + + # logging.info(input_palceholder[0].shape) + + outputs, _ = policy.build(inputs, None) + logging.info('7----------------\n---------------\n------------\n---------------') + logging.info(outputs) + masks = tf.cast(masks, dtype=tf.bool) + loss = task.target_loss(gt_outputs, outputs, masks) + logging.info('17----------------\n---------------\n------------\n---------------') + logging.info(loss) + + init_fn = None + + # If resnet is added to the graph, init_fn should initialize resnet weights + # if there is no previous checkpoint. + variables_assign_dict = {} + vars_list = [] + for v in slim.get_model_variables(): + if v.name.find('resnet') >= 0: + if not FLAGS.freeze_resnet_weights: + vars_list.append(v) + variables_assign_dict[v.name[v.name.find('resnet'):-2]] = v + else: + vars_list.append(v) + + global_step = tf.train.get_or_create_global_step() + learning_rate = tf.train.exponential_decay( + FLAGS.learning_rate, + global_step, + decay_steps=FLAGS.decay_steps, + decay_rate=0.98, + staircase=True) + optimizer = tf.train.AdamOptimizer(learning_rate) + train_op = slim.learning.create_train_op( + loss, + optimizer, + global_step=global_step, + variables_to_train=vars_list, + ) + + if variables_assign_dict: + init_fn = slim.assign_from_checkpoint_fn( + FLAGS.resnet50_path, + variables_assign_dict, + ignore_missing_vars=False) + scalar_summaries = {} + scalar_summaries['LR'] = learning_rate + scalar_summaries['loss'] = loss + + for name, summary in scalar_summaries.items(): + tf.summary.scalar(name, summary) + + return train_op, init_fn + + +def create_eval_ops(policy, config, possible_targets): + """Creates the necessary ops for evaluation.""" + inputs_feed = collections.OrderedDict([[ + mtype, tf.placeholder(config.inputs[mtype].type, [1] + config.inputs[mtype].shape)] for mtype in config.inputs]) + inputs_feed[task_env.ModalityTypes.PREV_ACTION] = tf.placeholder( + tf.float32, [1, 1] + [ + config.output.shape[-1] + 1, + ]) + prev_state_feed = [ + tf.placeholder( + tf.float32, [1, FLAGS.lstm_cell_size], name='prev_state_{}'.format(i)) + for i in range(2) + ] + policy_outputs = policy.build(inputs_feed, prev_state_feed) + logging.info('8----------------\n---------------\n------------\n---------------') + logging.info(inputs_feed) + logging.info('9----------------\n---------------\n------------\n---------------') + logging.info(prev_state_feed) + logging.info('10----------------\n---------------\n------------\n---------------') + logging.info(policy_outputs) + summary_feed = {} + for c in possible_targets + ['mean']: + summary_feed[c] = tf.placeholder( + tf.float32, [], name='eval_in_range_{}_input'.format(c)) + tf.summary.scalar('eval_in_range_{}'.format(c), summary_feed[c]) + + return inputs_feed, prev_state_feed, policy_outputs, (tf.summary.merge_all(), + summary_feed) + + +def unroll_policy_for_eval( + sess, + env, + inputs_feed, + prev_state_feed, + policy_outputs, + number_of_steps, + output_folder, +): + """unrolls the policy for testing. + + Args: + sess: tf.Session + env: The environment. + inputs_feed: dictionary of placeholder for the input modalities. + prev_state_feed: placeholder for the input to the prev_state of the model. + policy_outputs: tensor that contains outputs of the policy. + number_of_steps: maximum number of unrolling steps. + output_folder: output_folder where the function writes a dictionary of + detailed information about the path. The dictionary keys are 'states' and + 'distance'. The value for 'states' is the list of states that the agent + goes along the path. The value for 'distance' contains the length of + shortest path to the goal at each step. + + Returns: + states: list of states along the path. + distance: list of distances along the path. + """ + prev_state = [ + np.zeros((1, FLAGS.lstm_cell_size), dtype=np.float32) for _ in range(2) + ] + prev_action = np.zeros((1, 1, FLAGS.action_size + 1), dtype=np.float32) + obs = env.reset() + distances_to_goal = [] + states = [] + unique_id = '{}_{}'.format(env.cur_image_id(), env.goal_string) + for _ in range(number_of_steps): + distances_to_goal.append( + np.min([ + len( + nx.shortest_path(env.graph, env.pose_to_vertex(env.state()), + env.pose_to_vertex(target_view))) + for target_view in env.targets() + ])) + states.append(env.state()) + feed_dict = {inputs_feed[mtype]: [[obs[mtype]]] for mtype in inputs_feed} + feed_dict[prev_state_feed[0]] = prev_state[0] + feed_dict[prev_state_feed[1]] = prev_state[1] + logging.info('12----------------\n---------------\n------------\n---------------') + logging.info(policy_outputs) + action_values, prev_state = sess.run(policy_outputs, feed_dict=feed_dict) + logging.info('18----------------\n---------------\n------------\n---------------') + logging.info(action_values) + chosen_action = np.argmax(action_values[0]) + obs, _, done, info = env.step(np.int32(chosen_action)) + prev_action[0][0][chosen_action] = 1. + prev_action[0][0][-1] = float(info['success']) + # If the agent chooses action stop or the number of steps exceeeded + # env._episode_length. + if done: + break + + # logging.info('distance = %d, id = %s, #steps = %d', distances_to_goal[-1], + output_path = os.path.join(output_folder, unique_id + '.npy') + with tf.gfile.Open(output_path, 'w') as f: + print('saving path information to {}'.format(output_path)) + np.save(f, {'states': states, 'distance': distances_to_goal}) + return states, distances_to_goal + + +def init(sequence_length, eval_init_points_file_name, worlds): # 第一个被调用 构建网络 + + """Initializes the common operations between train and test.""" + modality_types = create_modality_types() # 返回'image', 'sseg', 'det', 'depth'中的一种 + logging.info('modality types: %r', modality_types) + # negative reward_goal_range prevents the env from terminating early when the + # agent is close to the goal. The policy should keep the agent until the end + # of the 100 steps either through chosing stop action or oscilating around + # the target. + + env = active_vision_dataset_env.ActiveVisionDatasetEnv( # ActiveVisionDatasetEnv的属性定义 + modality_types=modality_types + + [task_env.ModalityTypes.GOAL, task_env.ModalityTypes.PREV_ACTION], + reward_goal_range=-1, + eval_init_points_file_name=eval_init_points_file_name, + worlds=worlds, + output_size=FLAGS.obs_resolution, + ) + + config = create_task_io_config( + modality_types=modality_types, + goal_category_count=FLAGS.goal_category_count, + action_size=FLAGS.action_size, + sequence_length=sequence_length, + ) + task = tasks.GotoStaticXNoExplorationTask(env=env, config=config) + embedders_dict = {mtype: map_to_embedder(mtype) for mtype in config.inputs} + policy_params = tf.contrib.training.HParams( + lstm_state_size=FLAGS.lstm_cell_size, + fc_channels=FLAGS.policy_fc_size, + weight_decay=FLAGS.weight_decay, + target_embedding_size=FLAGS.embedding_fc_size, + ) + policy = policies.LSTMPolicy( + modality_names=config.inputs.keys(), + embedders_dict=embedders_dict, + action_size=FLAGS.action_size, + params=policy_params, + max_episode_length=sequence_length) + return env, config, task, policy + + +def test(): + """Contains all the operations for testing policies.""" + env, config, _, policy = init(1, 'all_init_configs', TEST_WORLDS) + inputs_feed, prev_state_feed, policy_outputs, summary_op = create_eval_ops( + policy, config, env.possible_targets) + + sv = tf.train.Supervisor(logdir=FLAGS.logdir) + prev_checkpoint = None + with sv.managed_session( + start_standard_services=False, + config=tf.ConfigProto(allow_soft_placement=True)) as sess: + while not sv.should_stop(): + while True: + new_checkpoint = tf.train.latest_checkpoint(FLAGS.logdir) + print('new_checkpoint ', new_checkpoint) + if not new_checkpoint: + time.sleep(1) + continue + if prev_checkpoint is None: + prev_checkpoint = new_checkpoint + break + if prev_checkpoint != new_checkpoint: + prev_checkpoint = new_checkpoint + break + else: # if prev_checkpoint == new_checkpoint, we have to wait more. + time.sleep(1) + + checkpoint_step = int(new_checkpoint[new_checkpoint.rfind('-') + 1:]) + sv.saver.restore(sess, new_checkpoint) + + print('evaluating checkpoint {}'.format(new_checkpoint)) + folder_path = os.path.join(FLAGS.logdir, 'evals', str(checkpoint_step)) + if not tf.gfile.Exists(folder_path): + tf.gfile.MakeDirs(folder_path) + eval_stats = {c: [] for c in env.possible_targets} + for test_iter in range(FLAGS.test_iters): + print('evaluating {} of {}'.format(test_iter, FLAGS.test_iters)) + _, distance_to_goal = unroll_policy_for_eval( + sess, + env, + inputs_feed, + prev_state_feed, + policy_outputs, + FLAGS.max_eval_episode_length, + folder_path, + + ) + print("1234") + print(inputs_feed) + print("12345") + print(policy_outputs) + print("123456") + print(prev_state_feed) + print("1234567") + print(distance_to_goal) + print('goal = {}'.format(env.goal_string)) + eval_stats[env.goal_string].append(float(distance_to_goal[-1] <= 7)) + eval_stats = {k: np.mean(v) for k, v in eval_stats.items()} + eval_stats['mean'] = np.mean(list(eval_stats.values())) + print(eval_stats) + feed_dict = {summary_op[1][c]: eval_stats[c] for c in eval_stats} + summary_str = sess.run(summary_op[0], feed_dict=feed_dict) + writer = sv.summary_writer + writer.add_summary(summary_str, checkpoint_step) + writer.flush() + + +def train(): + _, _, task, policy = init(FLAGS.sequence_length, None, TRAIN_WORLDS) + #print(FLAGS.save_summaries_secs) + #print(FLAGS.save_interval_secs) + #print(FLAGS.logdir) + + with tf.device( + tf.train.replica_device_setter(ps_tasks=FLAGS.ps_tasks, merge_devices=True)): + train_op, init_fn = create_train_and_init_ops(policy=policy, task=task) + #print(FLAGS.logdir) + slim.learning.train( + train_op=train_op, + init_fn=init_fn, + logdir=FLAGS.logdir, + is_chief=FLAGS.task_id == 0, + number_of_steps=FLAGS.train_iters, + save_summaries_secs=FLAGS.save_summaries_secs, + save_interval_secs=FLAGS.save_interval_secs, + session_config=tf.ConfigProto(allow_soft_placement=True), + ) + + +def main(_): + gin.parse_config_files_and_bindings(FLAGS.gin_config, FLAGS.gin_params) + if FLAGS.mode == 'train': + train() + else: + test() + + +if __name__ == '__main__': + app.run(main) -- Gitee From 2fcfa91e2e6fffb472d0df5cc2ff79f92be8dcb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D=E5=BF=83=E6=82=A6?= <1090110089@qq.com> Date: Fri, 2 Dec 2022 04:09:57 +0000 Subject: [PATCH 5/6] add ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/License. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 蓝心悦 <1090110089@qq.com> --- .../Cognitive_Planning_ID2015_for_ACL/License | 284 ++++++++++++++++++ 1 file changed, 284 insertions(+) create mode 100644 ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/License diff --git a/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/License b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/License new file mode 100644 index 000000000..d87a402b5 --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/License @@ -0,0 +1,284 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. + +------------------ +Files: third_party/compute_library/... + +MIT License + +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. + +------------------ +Files: ACKNOWLEDGEMENTS +LICENSE + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +------------------ +Files: third_party/hexagon + +Copyright (c) 2016-2019, The Linux Foundation. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted (subject to the limitations in the +disclaimer below) provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE +GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT +HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -- Gitee From cc92c2969061a05e54246226b59e1e524be6d7dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D=E5=BF=83=E6=82=A6?= <1090110089@qq.com> Date: Fri, 2 Dec 2022 04:10:58 +0000 Subject: [PATCH 6/6] add code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 蓝心悦 <1090110089@qq.com> --- .../ckpt2pb.py | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/ckpt2pb.py diff --git a/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/ckpt2pb.py b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/ckpt2pb.py new file mode 100644 index 000000000..7e35bbfcf --- /dev/null +++ b/ACL_TensorFlow/contrib/cv/Cognitive_Planning_ID2015_for_ACL/ckpt2pb.py @@ -0,0 +1,68 @@ +# +# 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. +# +#from npu_bridge.npu_init import * +import numpy as np +import math +import tensorflow as tf +import os, scipy.io +import os +from tensorflow.summary import FileWriter +from tensorflow.python.framework import graph_util + + +def freeze_graph(input_checkpoint, output_graph): + output_node_names = "policy/Reshape_3" + saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices = True) + graph = tf.get_default_graph() + input_graph_def = graph.as_graph_def() + + with tf.Session() as sess: + saver.restore(sess, input_checkpoint) + output_graph_def = graph_util.convert_variables_to_constants( + sess = sess, + input_graph_def = input_graph_def, + output_node_names = output_node_names.split(",") + ) + + with tf.gfile.GFile(output_graph, "wb") as f: # save model + f.write(output_graph_def.SerializeToString()) # sequence output + print("%d ops in the final graph." % len(output_graph_def.node)) + tf.train.write_graph(output_graph_def, "./pb/", "out.txt", as_text=True) + + +if __name__ == '__main__': + FLAGS = tf.flags.FLAGS + + # adding some parameters + tf.flags.DEFINE_string('input_checkpoint', './model.ckpt-67758', "input checkpoint.") + tf.flags.DEFINE_string('output_graph', './frozen_model.pb', "output_graph.") + + + freeze_graph(FLAGS.input_checkpoint, FLAGS.output_graph) \ No newline at end of file -- Gitee