diff --git a/TensorFlow/contrib/cv/Keras-TCN_ID2665_for_TensorFlow/README.md b/TensorFlow/contrib/cv/Keras-TCN_ID2665_for_TensorFlow/README.md index f79cc76fcf86b863dd4ec546dfcd844b1e17213b..7b35f3153fae8ab269d45fd39c66023e7d36fe02 100644 --- a/TensorFlow/contrib/cv/Keras-TCN_ID2665_for_TensorFlow/README.md +++ b/TensorFlow/contrib/cv/Keras-TCN_ID2665_for_TensorFlow/README.md @@ -1,109 +1,219 @@ -# TCN-KERAS -## 模型简介 -TCN = 1D fully-convolutional network (FCN) + casual convolution -### task -The adding problem -输入一个深度为2,长度为n的序列,其中一维是取值在[0,1]内的实数,另一维除了两个元素为1,其余元素均为0,将这两维的数字做内积,得到最终的结果是一个实数,取值范围是[0,2]。使用TCN网络预测最终的结果,与真实值比较,误差使用MSE衡量。 -#### Input shape -3D tensor with shape `(batch_size, timesteps, input_dim)`. -#### Output shape -2D tensor with shape `(batch_size, nb_filters)`. -## 数据集 +- [基本信息](#基本信息.md) +- [概述](#概述.md) +- [训练环境准备](#训练环境准备.md) +- [快速上手](#快速上手.md) +- [训练结果](#训练结果.md) +- [高级参考](#高级参考.md) +

基本信息

+ +**发布者(Publisher):Huawei** + +**应用领域(Application Domain):Computer Vision** + +**修改时间(Modified) :2022.03.31** + +**大小(Size):1.18 MB** + +**框架(Framework):TensorFlow 1.15.0** + +**模型格式(Model Format):h5** + +**精度(Precision):fp32** + +**处理器(Processor):昇腾910** + +**应用级别(Categories):Research** + +**描述(Description):基于TensorFlow框架验证时域卷积网络长期依赖性的训练代码** + +

概述

+ + +在序列建模中我们通常使用循环和递归结构,但最近的研究表明,特殊的卷积结构在一些任务中表现得更好。时域卷积网络(TCN)主要包括两个部分:一维的全卷积网络和因果卷积。选择The adding problem测试网络的长期依赖性。 + +The adding problem:输入一个深度为2,长度为n的序列,其中一维是取值在[0,1]内的实数,另一维除了两个元素为1,其余元素均为0。将这两维数字做内积,最终得到的结果是一个实数,取值范围是[0,2]。使用TCN网络预测结果,与真实值比较,误差使用MSE衡量。 + +- 参考论文: + + http://export.arxiv.org/pdf/1803.01271 + +- 参考实现: + + https://github.com/philipperemy/keras-tcn + +- 适配昇腾 AI 处理器的实现: + + https://gitee.com/ascend/ModelZoo-TensorFlow/tree/master/TensorFlow/contrib/cv/Keras-TCN_ID2665_for_TensorFlow + + + +- 通过Git获取对应commit\_id的代码方法如下: + + ``` + git clone {repository_url} # 克隆仓库的代码 + cd {repository_name} # 切换到模型的代码仓目录 + git checkout {branch} # 切换到对应分支 + git reset --hard {commit_id} # 代码设置到对应的commit_id + cd {code_path} # 切换到模型代码所在路径,若仓库下只有该模型,则无需切换 + ``` + +## 默认配置 + +- Input shape + + - 3D tensor with shape `(batch_size, timesteps, input_dim)`. + +- Output shape + + - 2D tensor with shape `(batch_size, nb_filters)`. + +- 训练超参 + + - Batch size: 512 + - Train epoch: 50 + - Train step: 391 + + +## 支持特性 + +| 特性列表 | 是否支持 | +|-------|------| +| 分布式训练 | 否 | +| 混合精度 | 否 | +| 并行数据 | 否 | + + +

训练环境准备

+ +1. 硬件环境准备请参见各硬件产品文档"[驱动和固件安装升级指南]( https://support.huawei.com/enterprise/zh/category/ai-computing-platform-pid-1557196528909)"。需要在硬件设备上安装与CANN版本配套的固件与驱动。 +2. 宿主机上需要安装Docker并登录[Ascend Hub中心](https://ascendhub.huawei.com/#/detail?name=ascend-tensorflow-arm)获取镜像。 + + 当前模型支持的镜像列表如[表1](#zh-cn_topic_0000001074498056_table1519011227314)所示。 + + **表 1** 镜像列表 + + + + + + + + + + + + +

镜像名称

+

镜像版本

+

配套CANN版本

+
+

20.2.0

+

20.2

+
+ + +

快速上手

+ +- 数据集准备 + 由`utils.py`生成 Length of the adding problem data = 600 \# of data in the train set = 200000 \# of data in the validation set = 40000 -## 原始模型 -参考论文: -http://export.arxiv.org/pdf/1803.01271 -参考实现: -https://github.com/philipperemy/keras-tcn -## 代码及路径解释 -``` -├── train.py               //训练代码 -├── utils.py               //数据生成代码 -├── tcn.py            //模型代码 -├── freeze_graph.py           //固化代码 -├── scripts -│ ├──train_1p_ci.sh //训练脚本 -│ ├──freeze_graph.sh //固化脚本 -``` -## 训练命令 -`bash train_1p_ci.sh` -## GPU和NPU训练效果 -* GPU -[GPU训练精度性能完整数据及日志 提取码:1234](https://pan.baidu.com/s/1sHniPqIwLn7VC2lWdQK0wQ) -``` -Epoch 1/50 391/391 - 345s 883ms/step - loss: 0.5514 - val_loss: 0.04493 -Epoch 2/50 391/391 - 133s 340ms/step - loss: 0.0141 - val_loss: 0.0121 -[...] -Epoch 50/50 391/391 - 136s 347ms/step - loss: 2.2935e-04 - val_loss: 2.1177e-04 -``` -loss: + +## 模型训练 + +- 单击“立即下载”,并选择合适的下载方式下载源码包。 + +- 启动训练之前,首先要配置程序运行相关环境变量。 + + 环境变量配置信息参见: + + [Ascend 910训练平台环境变量设置](https://gitee.com/ascend/modelzoo/wikis/Ascend%20910%E8%AE%AD%E7%BB%83%E5%B9%B3%E5%8F%B0%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E8%AE%BE%E7%BD%AE?sort_id=3148819) + +- 单卡训练 + + 1. 配置训练参数。 + + 首先在脚本test/train_full_1p.sh中,配置batch_size、learning_rate、dropout_rate、epochs等参数,请用户根据实际路径配置路径,或者在启动训练的命令行中以参数形式下发。 + + + 2. 启动训练。 + + 启动单卡训练 (脚本为Keras-TCN_ID2665_for_TensorFlow/test/train_full_1p.sh) + + ``` + bash train_full_1p.sh + ``` + +

训练结果

+ +- 精度结果比对 + +|精度指标项|论文发布|GPU实测|NPU实测| +|---------|--------|------|-------| +|loss|6.9630e-04|2.2935e-04|4.7023e-04| +|val_loss|3.7180e-04|2.1177e-04|5.4636e-04| + +GPU loss:
-val_loss: +GPU val_loss:
-
- -* NPU -[NPU训练精度性能完整数据及日志 提取码:1234](https://pan.baidu.com/s/18nDU6eFti0vAeTSapk_iVw) -``` -Epoch 1/50 391/391 - 235s 601ms/step - loss: 0.3362 - val_loss: 0.1427 -Epoch 2/50 391/391 - 58s 148ms/step - loss: 0.0232 - val_loss: 0.0094 -[...] -Epoch 50/50 391/391 - 56s 143ms/step - loss: 4.7023e-04 - val_loss: 5.4636e-04 -``` -loss: + +NPU loss:
-val_loss: +NPU val_loss:
-
+ + +- 性能结果比对 + +|性能指标项|GPU实测|NPU实测| +|---------|-------|-------| +|1 step|347ms|143ms| + + +

高级参考

+ +## 脚本和示例代码 + +``` +├── train.py //网络训练与测试代码 +├── README.md //代码说明文档 +├── freeze_graph.py //训练模型固化为pb模型代码 +├── utils.py                //数据生成代码 +├── tcn.py            //模型代码 +├── requirements.txt //训练python依赖列表 +├── test +│ ├──train_performance_1p.sh //单卡训练验证性能启动脚本 +│ ├──train_full_1p.sh //单卡全量训练启动脚本 -## pb模型 -`bash freeze_graph.sh` -## om模型 -使用ATC模型转换工具进行模型转换时可以参考如下指令: ``` -/home/HwHiAiUser/Ascend/ascend-toolkit/5.0.3.alpha001/x86_64-linux/atc/bin/atc ---model=/root/msame/model/tcn.pb --framework=3 --output=/root/msame/model/tcn --input_shape="input_1:1,600,2" --soc_version=Ascend310 --precision_mode=allow_fp32_to_fp16 + +## 脚本参数 + ``` -## 链接 -[GPU上得到的h5、pb及om 提取码:1234](https://pan.baidu.com/s/1Ix3yPq0C7fVm4f0pC-jUmA) -[NPU上得到的h5、pb及om 提取码:1234](https://pan.baidu.com/s/16ZqMKL_jUNK3a-yGfKaqWQ) -## 使用msame推理 -参考 https://gitee.com/ascend/tools/tree/master/msame, 获取msame推理工具及使用方法。 - -获取到msame可执行文件之后,将待检测om文件放在model文件夹,然后进行性能测试。 -## 性能测试 -数据转换bin文件:使用numpy.array的tofile函数 -[bin文件及其生成代码 提取码:1234](https://pan.baidu.com/s/1KTNYgG8VZzK9nUui3qJ_8A) -使用msame推理工具,参考如下命令,发起推理性能测试: +--batch_size 每个NPU的batch size,默认:512 +--learing_rata 初始学习率,默认:0.005 +--dropout_rate 随机失活的概率,默认:0.1 +--epochs 训练epcoh数量,默认:50 ``` -./msame --model "/root/msame/model/tcn.om" --output "/root/msame/out/" --input "/root/msame/mydata" --outfmt TXT -``` -## 精度测试 -分别使用利用GPU和NPU生成的pb文件转om文件进行推理,结果对比如下: -均方误差: -|GPU | NPU | -|:-----------------------:| :----------------------: | -|0.1777398184542767 | 0.27024116107347707 | -部分结果: -|序号 | 真实值 | GPU | NPU | -|:------:| --------: | -----: | ----: | -|1 | 1.4733921519255126 | 1.49268 | 1.46504 | -|2 | 0.8314698301349642 | 0.790464 | 0.755314 | -|3 | 0.8891396232000033 | 0.909286 | 0.928128 | -|4 | 1.0107378695468388 | 0.760409 | 0.703838 | -|5 | 0.9851944977337316 | 1.08851 | 1.46067 | -|6 | 1.689383561485223 | 0.940242 | 1.52666 | -|7 | 0.637807723655282 | 0.918035 | 1.43259 | -|8 | 0.5436813711189764 | 1.0083 | 0.960144 | -|9 | 1.7295318130559836 | 1.03426 | 1.18166 | -|10 | 1.1142951232471812 | 0.898364 | 1.38794 | -[完整结果 提取码:1234](https://pan.baidu.com/s/1iQfMOF2KUj6zDozJz3X6Ew) \ No newline at end of file + +## 训练过程 + +1. 通过“模型训练”中的训练指令启动单卡卡训练。 + +2. 参考脚本的模型存储路径为./output/tcn.h5。 + +链接: +[GPU训练精度性能完整数据及日志 提取码:1234](https://pan.baidu.com/s/1sHniPqIwLn7VC2lWdQK0wQ) + +[NPU训练精度性能完整数据及日志 提取码:1234](https://pan.baidu.com/s/18nDU6eFti0vAeTSapk_iVw) + +