Skip to content
Snippets Groups Projects
Commit 81852fd3 authored by ZJUTER0126's avatar ZJUTER0126
Browse files

[add] add Ascend910 training and Ascend310 inference for DDRNet

parent 662f35f0
No related branches found
No related tags found
No related merge requests found
Showing
with 1389 additions and 0 deletions
# 目录
<!-- TOC -->
- [目录](#目录)
- [DDRNet描述](#DDRNet描述)
- [数据集](#数据集)
- [特性](#特性)
- [混合精度](#混合精度)
- [环境要求](#环境要求)
- [脚本说明](#脚本说明)
- [脚本及样例代码](#脚本及样例代码)
- [脚本参数](#脚本参数)
- [训练和测试](#训练和测试)
- [导出过程](#导出过程)
- [导出](#导出)
- [推理过程](#推理过程)
- [推理](#推理)
- [模型描述](#模型描述)
- [性能](#性能)
- [评估性能](#评估性能)
- [ImageNet-1k上的DDRNet](#imagenet-1k上的DDRNet)
- [推理性能](#推理性能)
- [ImageNet-1k上的DDRNet](#imagenet-1k上的DDRNet-1)
- [ModelZoo主页](#modelzoo主页)
<!-- /TOC -->
# [DDRNet描述](#目录)
语义分割是自主车辆理解周围场景的关键技术。对于实际的自主车辆,不希望花费大量的推理时间来获得高精度的分割结果。使用轻量级架构(编码器解码器或双通道)或对低分辨率图像进行推理,最近的方法实现了非常快速的场景解析,甚至可以在单个1080Ti GPU上以100 FPS以上的速度运行。然而,在这些实时方法和基于膨胀主干的模型之间仍然存在明显的性能差距。
为了解决这个问题,受HRNet的启发,作者提出了一种具有深度高分辨率表示能力的深度双分辨率网络,用于高分辨率图像的实时语义分割,特别是道路行驶图像。作者提出了一种新的深度双分辨率网络用于道路场景的实时语义分割。 DDRNet从一个主干开始,然后被分成两个具有不同分辨率的平行深分支。一个深分支生成相对高分辨率的特征映射,另一个通过多次下采样操作提取丰富的上下文信息。为了有效的信息融合,在两个分支之间桥接多个双边连接。此外,我们还提出了一个新的模块DAPPM,它大大增加了接受域,比普通的PPM更充分地提取了上下文信息。
# [数据集](#目录)
使用的数据集:[ImageNet2012](http://www.image-net.org/)
- 数据集大小:共1000个类、224*224彩色图像
- 训练集:共1,281,167张图像
- 测试集:共50,000张图像
- 数据格式:JPEG
- 注:数据在dataset.py中处理。
- 下载数据集,目录结构如下:
```text
└─dataset
├─train # 训练数据集
└─val # 评估数据集
```
# [特性](#目录)
## 混合精度
采用[混合精度](https://www.mindspore.cn/docs/programming_guide/zh-CN/master/enable_mixed_precision.html)
的训练方法,使用支持单精度和半精度数据来提高深度学习神经网络的训练速度,同时保持单精度训练所能达到的网络精度。混合精度训练提高计算速度、减少内存使用的同时,支持在特定硬件上训练更大的模型或实现更大批次的训练。
# [环境要求](#目录)
- 硬件(Ascend)
- 使用Ascend来搭建硬件环境。
- 框架
- [MindSpore](https://www.mindspore.cn/install/en)
- 如需查看详情,请参见如下资源:
- [MindSpore教程](https://www.mindspore.cn/tutorials/zh-CN/r1.3/index.html)
- [MindSpore Python API](https://www.mindspore.cn/docs/api/zh-CN/r1.3/index.html)
# [脚本说明](#目录)
## 脚本及样例代码
```bash
├── DDRNet
├── README_CN.md // DDRNet相关说明
├── ascend310_infer // Ascend310推理需要的文件
├── scripts
├──run_standalone_train_ascend.sh // 单卡Ascend910训练脚本
├──run_distribute_train_ascend.sh // 多卡Ascend910训练脚本
├──run_eval_ascend.sh // 测试脚本
├──run_infer_310.sh // 310推理脚本
├── src
├──configs // DDRNet的配置文件
├──data // 数据集配置文件
├──imagenet.py // imagenet配置文件
├──augment // 数据增强函数文件
┕──data_utils // modelarts运行时数据集复制函数文件
│ ├──models // DDRNet定义文件
│ ├──trainers // 自定义TrainOneStep文件
│ ├──tools // 工具文件夹
├──callback.py // 自定义回调函数,训练结束测试
├──cell.py // 一些关于cell的通用工具函数
├──criterion.py // 关于损失函数的工具函数
├──get_misc.py // 一些其他的工具函数
├──optimizer.py // 关于优化器和参数的函数
┕──schedulers.py // 学习率衰减的工具函数
├── train.py // 训练文件
├── eval.py // 评估文件
├── export.py // 导出模型文件
├── postprocess.py // 推理计算精度文件
├── preprocess.py // 推理预处理图片文件
```
## 脚本参数
在config.py中可以同时配置训练参数和评估参数。
- 配置DDRNet和ImageNet-1k数据集。
```python
# Architecture Top1-75.9%
arch: DDRNet23 # 模型结构
# ===== Dataset ===== #
data_url: ./data/imagenet # 数据集地址
set: ImageNet # 数据集类别
num_classes: 1000 # 数据集种类数
mix_up: 0.8 # MixUp数据增强参数
cutmix: 1.0 # CutMix数据增强参数
color_jitter: 0.4 # color参数
auto_augment: rand-m9-mstd0.5-inc1 # auto_augment策略
interpolation: bicubic # 图像缩放插值方法
re_mode: pixel # 数据增强参数
re_count: 1 # 数据增强参数
mixup_prob: 1. # 数据增强参数
switch_prob: 0.5 # 数据增强参数
mixup_mode: batch # 数据增强参数
mixup_off_epoch: 0. # 使用多少轮mixup, 0为一直使用
image_size: 224 # 图像大小
crop_pct: 0.875 # 图像缩放比
# ===== Learning Rate Policy ======== #
optimizer: momentum # 优化器类别
use_nesterov: True # 是否使用牛顿法收敛
base_lr: 0.1 # 基础学习率
warmup_lr: 0.000001 # 学习率热身初始学习率
min_lr: 0.00001 # 最小学习率
lr_scheduler: cosine_lr # 学习率衰减策略
warmup_length: 10 # 学习率热身轮数
lr_adjust: 30 # for multistep lr # 多步学习率的衰减轮数
# ===== Network training config ===== #
amp_level: O2 # 混合精度策略
keep_bn_fp32: True # 保持bn为fp32
beta: [ 0.9, 0.999 ] # 优化器的beta参数
clip_global_norm_value: 5. # 全局梯度范数裁剪阈值
clip_global_norm: True # 是否使用全局梯度裁剪
is_dynamic_loss_scale: True # 是否使用动态损失缩放
epochs: 300 # 训练轮数
label_smoothing: 0.1 # 标签平滑参数
loss_scale: 1024 # 损失缩放
weight_decay: 0.0001 # 权重衰减参数
decay: 0.9 # for rmsprop # rmsprop的decay系数
momentum: 0.9 # 优化器动量
batch_size: 512 # 批次
# ===== Hardware setup ===== #
num_parallel_workers: 16 # 数据预处理线程数
device_target: Ascend # GPU或者Ascend
# ===== Model config ===== #
drop_path_rate: 0.1 # drop_path的概率
```
通过官方网站安装MindSpore后,您可以按照如下步骤进行训练和评估:
# [训练和测试](#目录)
- Ascend处理器环境运行
```bash
# 使用python启动单卡训练
python train.py --device_id 0 --device_target Ascend --ddr_config ./src/configs/ddrnet23_imagenet.yaml \
> train.log 2>&1 &
# 使用脚本启动单卡训练
bash ./scripts/run_standalone_train_ascend.sh [DEVICE_ID] [CONFIG_PATH]
# 使用脚本启动多卡训练
bash ./scripts/run_distribute_train_ascend.sh [RANK_TABLE_FILE] [CONFIG_PATH]
# 使用python启动单卡运行评估示例
python eval.py --device_id 0 --device_target Ascend --ddr_config ./src/configs/ddrnet23_imagenet.yaml \
--pretrained ./ckpt_0/DDRNet23.ckpt > ./eval.log 2>&1 &
# 使用脚本启动单卡运行评估示例
bash ./scripts/run_eval_ascend.sh [DEVICE_ID] [CONFIG_PATH] [CHECKPOINT_PATH]
# 运行推理示例
bash run_infer_310.sh [MINDIR_PATH] [DATASET_NAME(imagenet2012)] [DATASET_PATH] [DEVICE_ID(optional)]
```
对于分布式训练,需要提前创建JSON格式的hccl配置文件。
请遵循以下链接中的说明:
[hccl工具](https://gitee.com/mindspore/models/tree/master/utils/hccl_tools)
## 导出过程
### 导出
```shell
python export.py --pretrained [CKPT_FILE] --ddr_config [CONFIG_PATH] --device_target [DEVICE_TARGET]
```
导出的模型会以模型的结构名字命名并且保存在当前目录下
## 推理过程
### 推理
在进行推理之前我们需要先导出模型。mindir可以在任意环境上导出,air模型只能在昇腾910环境上导出。以下展示了使用mindir模型执行推理的示例。
- 在昇腾310上使用ImageNet-1k数据集进行推理
推理的结果保存在scripts目录下,在acc.log日志文件中可以找到类似以下的结果。
```shell
# Ascend310 inference
bash run_infer_310.sh [MINDIR_PATH] [DATASET_NAME] [DATASET_PATH] [DEVICE_ID]
Top1 acc: 0.76578
Top5 acc: 0.9331
```
# [模型描述](#目录)
## 性能
### 评估性能
#### ImageNet-1k上的DDRNet
| 参数 | Ascend |
| -------------------------- | ----------------------------------------------------------- |
|模型|DDRNet|
| 模型版本 | DDRNet23 |
| 资源 | Ascend 910 |
| 上传日期 | 2021-12-04 |
| MindSpore版本 | 1.3.0 |
| 数据集 | ImageNet-1k Train,共1,281,167张图像 |
| 训练参数 | epoch=300, batch_size=512 |
| 优化器 | Momentum |
| 损失函数 | SoftTargetCrossEntropy |
| 损失| 1.313|
| 输出 | 概率 |
| 分类准确率 | 八卡:top1:76.598% top5:93.312% |
| 速度 | 八卡:940.911 ms毫秒/步 |
| 训练耗时 |34h50min07s(run on ModelArts)|
### 推理性能
#### ImageNet-1k上的DDRNet
| 参数 | Ascend |
| -------------------------- | ----------------------------------------------------------- |
|模型 |DDRNet|
| 模型版本 | DDRNet23| |
| 资源 | Ascend 310 |
| 上传日期 | 2021-12-04 |
| MindSpore版本 | 1.3.0 |
| 数据集 | ImageNet-1k Val,共50,000张图像 |
| 分类准确率 | top1:76.578%,top5:93.31% |
| 速度 | 平均耗时3.29687 ms每张|
| 推理耗时| 约38min|
# ModelZoo主页
请浏览官网[主页](https://gitee.com/mindspore/models)
\ No newline at end of file
cmake_minimum_required(VERSION 3.14.1)
project(Ascend310Infer)
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -std=c++17 -Werror -Wall -fPIE -Wl,--allow-shlib-undefined")
set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/)
option(MINDSPORE_PATH "mindspore install path" "")
include_directories(${MINDSPORE_PATH})
include_directories(${MINDSPORE_PATH}/include)
include_directories(${PROJECT_SRC_ROOT})
find_library(MS_LIB libmindspore.so ${MINDSPORE_PATH}/lib)
file(GLOB_RECURSE MD_LIB ${MINDSPORE_PATH}/_c_dataengine*)
find_package(gflags REQUIRED)
add_executable(main src/main.cc src/utils.cc)
target_link_libraries(main ${MS_LIB} ${MD_LIB} gflags)
find_package(gflags REQUIRED)
#!/bin/bash
# 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.
# ============================================================================
if [ -d out ]; then
rm -rf out
fi
mkdir out
cd out || exit
if [ -f "Makefile" ]; then
make clean
fi
cmake .. \
-DMINDSPORE_PATH="`pip show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`"
make
/**
* 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.
*/
#ifndef MINDSPORE_INFERENCE_UTILS_H_
#define MINDSPORE_INFERENCE_UTILS_H_
#include <sys/stat.h>
#include <dirent.h>
#include <vector>
#include <string>
#include <memory>
#include "include/api/types.h"
std::vector<std::string> GetAllFiles(std::string_view dirName);
DIR *OpenDir(std::string_view dirName);
std::string RealPath(std::string_view path);
mindspore::MSTensor ReadFileToTensor(const std::string &file);
int WriteResult(const std::string& imageFile, const std::vector<mindspore::MSTensor> &outputs);
std::vector<std::string> GetAllFiles(std::string dir_name);
std::vector<std::vector<std::string>> GetAllInputData(std::string dir_name);
#endif
/**
* 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.
*/
#include <sys/time.h>
#include <gflags/gflags.h>
#include <dirent.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <iosfwd>
#include <vector>
#include <fstream>
#include <sstream>
#include "include/api/model.h"
#include "include/api/context.h"
#include "include/api/types.h"
#include "include/api/serialization.h"
#include "include/dataset/vision_ascend.h"
#include "include/dataset/execute.h"
#include "include/dataset/transforms.h"
#include "include/dataset/vision.h"
#include "inc/utils.h"
using mindspore::Context;
using mindspore::Serialization;
using mindspore::Model;
using mindspore::Status;
using mindspore::ModelType;
using mindspore::GraphCell;
using mindspore::kSuccess;
using mindspore::MSTensor;
using mindspore::dataset::Execute;
using mindspore::dataset::vision::Decode;
using mindspore::dataset::vision::Resize;
using mindspore::dataset::vision::CenterCrop;
using mindspore::dataset::vision::Normalize;
using mindspore::dataset::vision::HWC2CHW;
using mindspore::dataset::InterpolationMode;
DEFINE_string(mindir_path, "", "mindir path");
DEFINE_string(dataset_name, "imagenet2012", "imagenet2012");
DEFINE_string(input0_path, ".", "input0 path");
DEFINE_int32(device_id, 0, "device id");
int load_model(Model *model, std::vector<MSTensor> *model_inputs, std::string mindir_path, int device_id) {
if (RealPath(mindir_path).empty()) {
std::cout << "Invalid mindir" << std::endl;
return 1;
}
auto context = std::make_shared<Context>();
auto ascend310 = std::make_shared<mindspore::Ascend310DeviceInfo>();
ascend310->SetDeviceID(device_id);
context->MutableDeviceInfo().push_back(ascend310);
mindspore::Graph graph;
Serialization::Load(mindir_path, ModelType::kMindIR, &graph);
Status ret = model->Build(GraphCell(graph), context);
if (ret != kSuccess) {
std::cout << "ERROR: Build failed." << std::endl;
return 1;
}
*model_inputs = model->GetInputs();
if (model_inputs->empty()) {
std::cout << "Invalid model, inputs is empty." << std::endl;
return 1;
}
return 0;
}
int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
Model model;
std::vector<MSTensor> model_inputs;
load_model(&model, &model_inputs, FLAGS_mindir_path, FLAGS_device_id);
std::map<double, double> costTime_map;
struct timeval start = {0};
struct timeval end = {0};
if (FLAGS_dataset_name != "imagenet2012") {
std::cout << "ERROR: only support imagenet2012 dataset." << std::endl;
return 1;
} else {
auto input0_files = GetAllInputData(FLAGS_input0_path);
if (input0_files.empty()) {
std::cout << "ERROR: no input data." << std::endl;
return 1;
}
auto decode = Decode();
auto resize = Resize({256, 256}, InterpolationMode::kCubicPil);
auto centercrop = CenterCrop({224, 224});
auto normalize = Normalize({0.485 * 255, 0.456 * 255, 0.406 * 255}, {0.229 * 255, 0.224 * 255, 0.225 * 255});
auto hwc2chw = HWC2CHW();
Execute SingleOp({decode, resize, centercrop, normalize, hwc2chw});
size_t size = input0_files.size();
for (size_t i = 0; i < size; ++i) {
for (size_t j = 0; j < input0_files[i].size(); ++j) {
std::vector<MSTensor> inputs;
std::vector<MSTensor> outputs;
std::cout << "Start predict input files:" << input0_files[i][j] <<std::endl;
auto imgDvpp = std::make_shared<MSTensor>();
SingleOp(ReadFileToTensor(input0_files[i][j]), imgDvpp.get());
inputs.emplace_back(model_inputs[0].Name(), model_inputs[0].DataType(), model_inputs[0].Shape(),
imgDvpp->Data().get(), imgDvpp->DataSize());
gettimeofday(&start, nullptr);
Status ret = model.Predict(inputs, &outputs);
gettimeofday(&end, nullptr);
if (ret != kSuccess) {
std::cout << "Predict " << input0_files[i][j] << " failed." << std::endl;
return 1;
}
double startTimeMs;
double endTimeMs;
startTimeMs = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000;
endTimeMs = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000;
costTime_map.insert(std::pair<double, double>(startTimeMs, endTimeMs));
int rst = WriteResult(input0_files[i][j], outputs);
if (rst != 0) {
std::cout << "write result failed." << std::endl;
return rst;
}
}
}
}
double average = 0.0;
int inferCount = 0;
for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) {
double diff = 0.0;
diff = iter->second - iter->first;
average += diff;
inferCount++;
}
average = average / inferCount;
std::stringstream timeCost;
timeCost << "NN inference cost average time: "<< average << " ms of infer_count " << inferCount << std::endl;
std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << inferCount << std::endl;
std::string fileName = "./time_Result" + std::string("/test_perform_static.txt");
std::ofstream fileStream(fileName.c_str(), std::ios::trunc);
fileStream << timeCost.str();
fileStream.close();
costTime_map.clear();
return 0;
}
/**
* 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.
*/
#include <fstream>
#include <algorithm>
#include <iostream>
#include "inc/utils.h"
using mindspore::MSTensor;
using mindspore::DataType;
std::vector<std::vector<std::string>> GetAllInputData(std::string dir_name) {
std::vector<std::vector<std::string>> ret;
DIR *dir = OpenDir(dir_name);
if (dir == nullptr) {
return {};
}
struct dirent *filename;
std::vector<std::string> sub_dirs;
while ((filename = readdir(dir)) != nullptr) {
std::string d_name = std::string(filename->d_name);
// get rid of "." and ".."
if (d_name == "." || d_name == ".." || d_name.empty()) {
continue;
}
std::string dir_path = RealPath(std::string(dir_name) + "/" + filename->d_name);
struct stat s;
lstat(dir_path.c_str(), &s);
if (!S_ISDIR(s.st_mode)) {
continue;
}
sub_dirs.emplace_back(dir_path);
}
std::sort(sub_dirs.begin(), sub_dirs.end());
(void)std::transform(sub_dirs.begin(), sub_dirs.end(), std::back_inserter(ret),
[](const std::string &d) { return GetAllFiles(d); });
return ret;
}
std::vector<std::string> GetAllFiles(std::string dir_name) {
struct dirent *filename;
DIR *dir = OpenDir(dir_name);
if (dir == nullptr) {
return {};
}
std::vector<std::string> res;
while ((filename = readdir(dir)) != nullptr) {
std::string d_name = std::string(filename->d_name);
if (d_name == "." || d_name == ".." || d_name.size() <= 3) {
continue;
}
res.emplace_back(std::string(dir_name) + "/" + filename->d_name);
}
std::sort(res.begin(), res.end());
return res;
}
std::vector<std::string> GetAllFiles(std::string_view dirName) {
struct dirent *filename;
DIR *dir = OpenDir(dirName);
if (dir == nullptr) {
return {};
}
std::vector<std::string> res;
while ((filename = readdir(dir)) != nullptr) {
std::string dName = std::string(filename->d_name);
if (dName == "." || dName == ".." || filename->d_type != DT_REG) {
continue;
}
res.emplace_back(std::string(dirName) + "/" + filename->d_name);
}
std::sort(res.begin(), res.end());
for (auto &f : res) {
std::cout << "image file: " << f << std::endl;
}
return res;
}
int WriteResult(const std::string& imageFile, const std::vector<MSTensor> &outputs) {
std::string homePath = "./result_Files";
const int INVALID_POINTER = -1;
const int ERROR = -2;
for (size_t i = 0; i < outputs.size(); ++i) {
size_t outputSize;
std::shared_ptr<const void> netOutput;
netOutput = outputs[i].Data();
outputSize = outputs[i].DataSize();
int pos = imageFile.rfind('/');
std::string fileName(imageFile, pos + 1);
fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin");
std::string outFileName = homePath + "/" + fileName;
FILE *outputFile = fopen(outFileName.c_str(), "wb");
if (outputFile == nullptr) {
std::cout << "open result file " << outFileName << " failed" << std::endl;
return INVALID_POINTER;
}
size_t size = fwrite(netOutput.get(), sizeof(char), outputSize, outputFile);
if (size != outputSize) {
fclose(outputFile);
outputFile = nullptr;
std::cout << "write result file " << outFileName << " failed, write size[" << size <<
"] is smaller than output size[" << outputSize << "], maybe the disk is full." << std::endl;
return ERROR;
}
fclose(outputFile);
outputFile = nullptr;
}
return 0;
}
mindspore::MSTensor ReadFileToTensor(const std::string &file) {
if (file.empty()) {
std::cout << "Pointer file is nullptr" << std::endl;
return mindspore::MSTensor();
}
std::ifstream ifs(file);
if (!ifs.good()) {
std::cout << "File: " << file << " is not exist" << std::endl;
return mindspore::MSTensor();
}
if (!ifs.is_open()) {
std::cout << "File: " << file << "open failed" << std::endl;
return mindspore::MSTensor();
}
ifs.seekg(0, std::ios::end);
size_t size = ifs.tellg();
mindspore::MSTensor buffer(file, mindspore::DataType::kNumberTypeUInt8, {static_cast<int64_t>(size)}, nullptr, size);
ifs.seekg(0, std::ios::beg);
ifs.read(reinterpret_cast<char *>(buffer.MutableData()), size);
ifs.close();
return buffer;
}
DIR *OpenDir(std::string_view dirName) {
if (dirName.empty()) {
std::cout << " dirName is null ! " << std::endl;
return nullptr;
}
std::string realPath = RealPath(dirName);
struct stat s;
lstat(realPath.c_str(), &s);
if (!S_ISDIR(s.st_mode)) {
std::cout << "dirName is not a valid directory !" << std::endl;
return nullptr;
}
DIR *dir;
dir = opendir(realPath.c_str());
if (dir == nullptr) {
std::cout << "Can not open dir " << dirName << std::endl;
return nullptr;
}
std::cout << "Successfully opened the dir " << dirName << std::endl;
return dir;
}
std::string RealPath(std::string_view path) {
char realPathMem[PATH_MAX] = {0};
char *realPathRet = nullptr;
realPathRet = realpath(path.data(), realPathMem);
if (realPathRet == nullptr) {
std::cout << "File: " << path << " is not exist.";
return "";
}
std::string realPath(realPathMem);
std::cout << path << " realpath is: " << realPath << std::endl;
return realPath;
}
# 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.
# ============================================================================
"""eval"""
from mindspore import Model
from mindspore import context
from mindspore import nn
from mindspore.common import set_seed
from src.args import args
from src.tools.cell import cast_amp
from src.tools.criterion import get_criterion, NetWithLoss
from src.tools.get_misc import get_dataset, set_device, get_model, pretrained, get_train_one_step
from src.tools.optimizer import get_optimizer
set_seed(args.seed)
def main():
mode = {
0: context.GRAPH_MODE,
1: context.PYNATIVE_MODE
}
context.set_context(mode=mode[args.graph_mode], device_target=args.device_target)
context.set_context(enable_graph_kernel=False)
if args.device_target == "Ascend":
context.set_context(enable_auto_mixed_precision=True)
set_device(args)
# get model
net = get_model(args)
cast_amp(net)
criterion = get_criterion(args)
if args.pretrained:
pretrained(args, net)
net_with_loss = NetWithLoss(net, criterion)
data = get_dataset(args, training=False)
batch_num = data.val_dataset.get_dataset_size()
optimizer = get_optimizer(args, net, batch_num)
# save a yaml file to read to record parameters
net_with_loss = get_train_one_step(args, net_with_loss, optimizer)
eval_network = nn.WithEvalCell(net, criterion, args.amp_level in ["O2", "O3", "auto"])
eval_indexes = [0, 1, 2]
eval_metrics = {'Loss': nn.Loss(),
'Top1-Acc': nn.Top1CategoricalAccuracy(),
'Top5-Acc': nn.Top5CategoricalAccuracy()}
model = Model(net_with_loss, metrics=eval_metrics,
eval_network=eval_network,
eval_indexes=eval_indexes)
print(f"=> begin eval")
results = model.eval(data.val_dataset)
print(f"=> eval results:{results}")
print(f"=> eval success")
if __name__ == '__main__':
main()
# 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.
# ============================================================================
"""
##############export checkpoint file into air or mindir model#################
python export.py
"""
import numpy as np
from mindspore import Tensor, load_checkpoint, load_param_into_net, export, context
from mindspore import dtype as mstype
from src.args import args
from src.tools.cell import cast_amp
from src.tools.criterion import get_criterion, NetWithLoss
from src.tools.get_misc import get_model
context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target)
if args.device_target in ["Ascend", "GPU"]:
context.set_context(device_id=args.device_id)
if __name__ == '__main__':
net = get_model(args)
criterion = get_criterion(args)
cast_amp(net)
net_with_loss = NetWithLoss(net, criterion)
assert args.pretrained is not None, "checkpoint_path is None."
param_dict = load_checkpoint(args.pretrained)
load_param_into_net(net, param_dict)
net.set_train(False)
net.to_float(mstype.float32)
input_arr = Tensor(np.zeros([1, 3, args.image_size, args.image_size], np.float32))
export(net, input_arr, file_name=args.arch, file_format=args.file_format)
# 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.
# ============================================================================
"""postprocess for 310 inference"""
import argparse
import json
import os
import numpy as np
from mindspore.nn import Top1CategoricalAccuracy, Top5CategoricalAccuracy
parser = argparse.ArgumentParser(description="postprocess")
parser.add_argument("--result_dir", type=str, default="./result_Files", help="result files path.")
parser.add_argument('--dataset_name', type=str, choices=["imagenet2012"], default="imagenet2012")
args = parser.parse_args()
def calcul_acc(lab, preds):
return sum(1 for x, y in zip(lab, preds) if x == y) / len(lab)
if __name__ == '__main__':
batch_size = 1
top1_acc = Top1CategoricalAccuracy()
rst_path = args.result_dir
label_list = []
pred_list = []
file_list = os.listdir(rst_path)
top5_acc = Top5CategoricalAccuracy()
with open('./preprocess_Result/imagenet_label.json', "r") as label:
labels = json.load(label)
for f in file_list:
label = f.split("_0.bin")[0] + ".JPEG"
label_list.append(labels[label])
pred = np.fromfile(os.path.join(rst_path, f), np.float32)
pred = pred.reshape(batch_size, int(pred.shape[0] / batch_size))
top1_acc.update(pred, [labels[label],])
top5_acc.update(pred, [labels[label],])
print("Top1 acc: ", top1_acc.eval())
print("Top5 acc: ", top5_acc.eval())
# 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.
# ============================================================================
"""preprocess"""
import argparse
import json
import os
parser = argparse.ArgumentParser('preprocess')
parser.add_argument('--dataset_name', type=str, choices=["imagenet2012"], default="imagenet2012")
parser.add_argument('--data_path', type=str, default='', help='eval data dir')
def create_label(result_path, dir_path):
"""
create_label
"""
dirs = os.listdir(dir_path)
file_list = []
for file in dirs:
file_list.append(file)
file_list = sorted(file_list)
total = 0
img_label = {}
for i, file_dir in enumerate(file_list):
files = os.listdir(os.path.join(dir_path, file_dir))
for f in files:
img_label[f] = i
total += len(files)
json_file = os.path.join(result_path, "imagenet_label.json")
with open(json_file, "w+") as label:
json.dump(img_label, label)
print("[INFO] Completed! Total {} data.".format(total))
args = parser.parse_args()
if __name__ == "__main__":
create_label('./preprocess_Result/', args.data_path)
#!/bin/bash
# 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.
# ============================================================================
if [ $# -lt 2 ]
then
echo "Usage: bash ./scripts/run_distribute_train_ascend.sh [RANK_TABLE_FILE] [CONFIG_PATH]"
exit 1
fi
export RANK_TABLE_FILE=$1
CONFIG_PATH=$2
export RANK_SIZE=8
export DEVICE_NUM=8
cores=`cat /proc/cpuinfo|grep "processor" |wc -l`
echo "the number of logical core" $cores
avg_core_per_rank=`expr $cores \/ $RANK_SIZE`
core_gap=`expr $avg_core_per_rank \- 1`
echo "avg_core_per_rank" $avg_core_per_rank
echo "core_gap" $core_gap
for((i=0;i<RANK_SIZE;i++))
do
start=`expr $i \* $avg_core_per_rank`
export DEVICE_ID=$i
export RANK_ID=$i
export DEPLOY_MODE=0
export GE_USE_STATIC_MEMORY=1
end=`expr $start \+ $core_gap`
cmdopt=$start"-"$end
rm -rf train_parallel$i
mkdir ./train_parallel$i
cp -r ./src ./train_parallel$i
cp *.py ./train_parallel$i
cd ./train_parallel$i || exit
echo "start training for rank $i, device $DEVICE_ID rank_id $RANK_ID"
env > env.log
taskset -c $cmdopt python -u ../train.py \
--device_target Ascend \
--device_id $i \
--ddr_config=$CONFIG_PATH > log.txt 2>&1 &
cd ../
done
#!/bin/bash
# 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.
# ============================================================================
if [ $# -lt 3 ]
then
echo "Usage: bash ./scripts/run_eval_ascend.sh [DEVICE_ID] [CONFIG_PATH] [CHECKPOINT_PATH]"
exit 1
fi
export DEVICE_ID=$1
CONFIG_PATH=$2
CHECKPOINT_PATH=$3
export RANK_SIZE=1
export DEVICE_NUM=1
rm -rf evaluation_ascend
mkdir ./evaluation_ascend
cd ./evaluation_ascend || exit
echo "start training for device id $DEVICE_ID"
env > env.log
python ../eval.py \
--device_target=Ascend \
--device_id=$DEVICE_ID \
--ddr_config=$CONFIG_PATH \
--pretrained=$CHECKPOINT_PATH > eval.log 2>&1 &
cd ../
#!/bin/bash
# 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.
# ============================================================================
if [[ $# -lt 3 || $# -gt 4 ]]; then
echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATASET_NAME] [DATASET_PATH] [DEVICE_ID]
DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero"
exit 1
fi
get_real_path(){
if [ "${1:0:1}" == "/" ]; then
echo "$1"
else
echo "$(realpath -m $PWD/$1)"
fi
}
model=$(get_real_path $1)
if [ $2 == 'imagenet2012' ]; then
dataset_name=$2
else
echo "DATASET_NAME should be 'imagenet2012'"
exit 1
fi
dataset_path=$(get_real_path $3)
device_id=0
if [ $# == 4 ]; then
device_id=$4
fi
echo "mindir name: "$model
echo "dataset name: "$dataset_name
echo "dataset path: "$dataset_path
echo "device id: "$device_id
export ASCEND_HOME=/usr/local/Ascend/
if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then
export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH
export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/usr/local/lib:$ASCEND_HOME/ascend-toolkit/latest/atc/lib64:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH
export TBE_IMPL_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe
export PYTHONPATH=$ASCEND_HOME/fwkacllib/python/site-packages:${TBE_IMPL_PATH}:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/python/site-packages:$PYTHONPATH
export ASCEND_OPP_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp
else
export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH
export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH
export PYTHONPATH=$ASCEND_HOME/fwkacllib/python/site-packages:$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH
export ASCEND_OPP_PATH=$ASCEND_HOME/opp
fi
function preprocess_data()
{
if [ -d preprocess_Result ]; then
rm -rf ./preprocess_Result
fi
mkdir preprocess_Result
python ../preprocess.py --dataset_name=$dataset_name --data_path=$dataset_path
}
function compile_app()
{
cd ../ascend310_infer/ || exit
bash build.sh &> build.log
}
function infer()
{
cd - || exit
if [ -d result_Files ]; then
rm -rf ./result_Files
fi
if [ -d time_Result ]; then
rm -rf ./time_Result
fi
mkdir result_Files
mkdir time_Result
../ascend310_infer/out/main --mindir_path=$model --dataset_name=$dataset_name --input0_path=$dataset_path .\
--device_id=$device_id &> infer.log
}
function cal_acc()
{
python ../postprocess.py --dataset_name=$dataset_name &> acc.log
}
preprocess_data
if [ $? -ne 0 ]; then
echo "preprocess dataset failed"
exit 1
fi
compile_app
if [ $? -ne 0 ]; then
echo "compile app code failed"
exit 1
fi
infer
if [ $? -ne 0 ]; then
echo " execute inference failed"
exit 1
fi
cal_acc
if [ $? -ne 0 ]; then
echo "calculate accuracy failed"
exit 1
fi
#!/bin/bash
# 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.
# ============================================================================
if [ $# -lt 2 ]
then
echo "Usage: bash ./scripts/run_standalone_train_ascend.sh [DEVICE_ID] [CONFIG_PATH]"
exit 1
fi
export RANK_SIZE=1
export DEVICE_NUM=1
export DEVICE_ID=$1
CONFIG_PATH=$2
rm -rf train_standalone
mkdir ./train_standalone
cd ./train_standalone || exit
echo "start training for device id $DEVICE_ID"
env > env.log
python -u ../train.py \
--device_id=$DEVICE_ID \
--device_target="Ascend" \
--ddr_config=$CONFIG_PATH > log.txt 2>&1 &
cd ../
# 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.
# ============================================================================
"""global args for DDRNet"""
import argparse
import ast
import os
import sys
import yaml
from src.configs import parser as _parser
args = None
def parse_arguments():
"""parse_arguments"""
global args
parser = argparse.ArgumentParser(description="MindSpore DDRNet Training")
parser.add_argument("-a", "--arch", metavar="ARCH", default="ResNet18", help="model architecture")
parser.add_argument("--accumulation_step", default=1, type=int, help="accumulation step")
parser.add_argument("--amp_level", default="O1", choices=["O0", "O1", "O2", "O3"], help="AMP Level")
parser.add_argument("-b", "--batch_size", default=64, type=int, metavar="N",
help="mini-batch size (default: 256), this is the total "
"batch size of all GPUs on the current node when "
"using Data Parallel or Distributed Data Parallel")
parser.add_argument("--beta", default=[0.9, 0.999], type=lambda x: [float(a) for a in x.split(",")],
help="beta for optimizer")
parser.add_argument("--crop_pct", default=0.875, type=float, help="Crop Pct")
parser.add_argument("--clip_global_norm", default=False, type=ast.literal_eval, help="clip global norm")
parser.add_argument('--data_url', default="./data", help='Location of data.')
parser.add_argument('--clip_global_norm_value', default=5., type=float, help='clip_global_norm_value.')
parser.add_argument("--device_id", default=0, type=int, help="Device Id")
parser.add_argument("--device_num", default=1, type=int, help="device num")
parser.add_argument("--device_target", default="GPU", choices=["GPU", "Ascend", "CPU"], type=str)
parser.add_argument("--epochs", default=300, type=int, metavar="N", help="number of total epochs to run")
parser.add_argument("--eps", default=1e-8, type=float)
parser.add_argument("--file_format", type=str, choices=["AIR", "MINDIR"], default="MINDIR", help="file format")
parser.add_argument("--in_channel", default=3, type=int)
parser.add_argument("--is_dynamic_loss_scale", default=1, type=int, help="is_dynamic_loss_scale ")
parser.add_argument("--keep_checkpoint_max", default=20, type=int, help="keep checkpoint max num")
parser.add_argument("--optimizer", help="Which optimizer to use", default="sgd")
parser.add_argument("--use_nesterov", help="Whether use nesterov", default=False, type=ast.literal_eval)
parser.add_argument("--set", help="name of dataset", type=str, default="ImageNet")
parser.add_argument("--graph_mode", default=0, type=int, help="graph mode with 0, python with 1")
parser.add_argument("--mix_up", default=0.8, type=float, help="mix up")
parser.add_argument("--re_prob", default=0., type=float, help="erasing prob")
parser.add_argument("--mixup_off_epoch", default=0., type=int, help="mix_up off epoch")
parser.add_argument("--interpolation", default="bicubic", type=str)
parser.add_argument("-j", "--num_parallel_workers", default=20, type=int, metavar="N",
help="number of data loading workers (default: 20)")
parser.add_argument("--start_epoch", default=0, type=int, metavar="N",
help="manual epoch number (useful on restarts)")
parser.add_argument("--warmup_length", default=0, type=int, help="Number of warmup iterations")
parser.add_argument("--warmup_lr", default=5e-7, type=float, help="warm up learning rate")
parser.add_argument("--wd", "--weight_decay", default=0.0001, type=float, metavar="W",
help="weight decay (default: 1e-4)", dest="weight_decay")
parser.add_argument("--loss_scale", default=1024, type=int, help="loss_scale")
parser.add_argument("--base_lr", "--learning_rate", default=5e-4, type=float, help="initial lr", dest="base_lr")
parser.add_argument("--lr_scheduler", default="cosine_annealing", help="Schedule for the learning rate.")
parser.add_argument("--lr_adjust", default=30, type=float, help="Interval to drop lr")
parser.add_argument("--lr_gamma", default=0.97, type=int, help="Multistep multiplier")
parser.add_argument("--momentum", default=0.9, type=float, metavar="M", help="momentum")
parser.add_argument("--num_classes", default=1000, type=int)
parser.add_argument("--pretrained", dest="pretrained", default=None, type=str, help="use pre-trained model")
parser.add_argument("--ddr_config", help="Config file to use (see configs dir)", default=None, required=True)
parser.add_argument("--seed", default=0, type=int, help="seed for initializing training. ")
parser.add_argument("--save_every", default=50, type=int, help="save_every:50")
parser.add_argument("--label_smoothing", type=float, help="Label smoothing to use, default 0.0", default=0.1)
parser.add_argument("--image_size", default=224, help="Image Size.", type=int)
parser.add_argument('--train_url', default="./", help='Location of training outputs.')
parser.add_argument("--run_modelarts", type=ast.literal_eval, default=False, help="Whether run on modelarts")
args = parser.parse_args()
# Allow for use from notebook without config file
if len(sys.argv) > 1:
get_config()
def get_config():
"""get_config"""
global args
override_args = _parser.argv_to_vars(sys.argv)
print(f"=> Reading YAML config from {args.ddr_config}")
# load yaml file
if args.run_modelarts:
import moxing as mox
if not args.ddr_config.startswith("obs:/"):
args.ddr_config = "obs:/" + args.ddr_config
with mox.file.File(args.ddr_config, 'r') as f:
yaml_txt = f.read()
else:
yaml_txt = open(args.ddr_config).read()
# override args
loaded_yaml = yaml.load(yaml_txt, Loader=yaml.FullLoader)
for v in override_args:
loaded_yaml[v] = getattr(args, v)
args.__dict__.update(loaded_yaml)
print(args)
if "DEVICE_NUM" not in os.environ.keys():
os.environ["DEVICE_NUM"] = str(args.device_num)
os.environ["RANK_SIZE"] = str(args.device_num)
def run_args():
"""run and get args"""
global args
if args is None:
parse_arguments()
run_args()
# Architecture Top1-75.9%
arch: DDRNet23
# ===== Dataset ===== #
data_url: ./data/imagenet
set: ImageNet
num_classes: 1000
mix_up: 0.8
cutmix: 1.0
color_jitter: 0.4
auto_augment: rand-m9-mstd0.5-inc1
interpolation: bicubic
re_prob: 0.25
re_mode: pixel
re_count: 1
mixup_prob: 1.
switch_prob: 0.5
mixup_mode: batch
mixup_off_epoch: 0. # use mixup during training
image_size: 224
crop_pct: 0.875
# ===== Learning Rate Policy ======== #
optimizer: momentum
use_nesterov: True
base_lr: 0.1
warmup_lr: 0.000001
min_lr: 0.00001
lr_scheduler: cosine_lr
warmup_length: 10
lr_adjust: 30 # for multistep lr
# ===== Network training config ===== #
amp_level: O2
keep_bn_fp32: True
beta: [ 0.9, 0.999 ]
clip_global_norm_value: 5.
clip_global_norm: True
is_dynamic_loss_scale: True
epochs: 300
label_smoothing: 0.1
loss_scale: 1024
weight_decay: 0.0001
decay: 0.9 # for rmsprop
momentum: 0.9
batch_size: 128
# ===== Hardware setup ===== #
num_parallel_workers: 16
device_target: Ascend
# ===== Model config ===== #
drop_path_rate: 0.1
# 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.
# ============================================================================
"""parser function"""
USABLE_TYPES = set([float, int])
def trim_preceding_hyphens(st):
i = 0
while st[i] == "-":
i += 1
return st[i:]
def arg_to_varname(st: str):
st = trim_preceding_hyphens(st)
st = st.replace("-", "_")
return st.split("=")[0]
def argv_to_vars(argv):
var_names = []
for arg in argv:
if arg.startswith("-") and arg_to_varname(arg) != "ddr_config":
var_names.append(arg_to_varname(arg))
return var_names
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment