Skip to content
Snippets Groups Projects
Commit 1e555c52 authored by i-robot's avatar i-robot Committed by Gitee
Browse files

!1010 upload nima

Merge pull request !1010 from gengdongjie/upload_nima
parents 4c623590 ddf10397
No related branches found
No related tags found
No related merge requests found
Showing
with 1941 additions and 0 deletions
# 目录
<!-- TOC -->
- [目录](#目录)
- [ResNet描述](#resnet描述)
- [概述](#概述)
- [论文](#论文)
- [模型架构](#模型架构)
- [数据集](#数据集)
- [特性](#特性)
- [混合精度](#混合精度)
- [环境要求](#环境要求)
- [快速入门](#快速入门)
- [脚本说明](#脚本说明)
- [脚本及样例代码](#脚本及样例代码)
- [脚本参数](#脚本参数)
- [训练过程](#训练过程)
- [用法](#用法)
- [Ascend处理器环境运行](#ascend处理器环境运行)
- [运行参数服务器模式训练](#运行参数服务器模式训练)
- [训练时推理](#训练时推理)
- [结果](#结果)
- [评估过程](#评估过程)
- [用法](#用法-1)
- [Ascend处理器环境运行](#ascend处理器环境运行-1)
- [结果](#结果-1)
- [推理过程](#推理过程)
- [导出MindIR](#导出mindir)
- [在Ascend310执行推理](#在ascend310执行推理)
- [结果](#结果-2)
- [模型描述](#模型描述)
- [性能](#性能)
- [评估性能](#评估性能)
- [AVA_Dataset上的ResNet50](#cifar-10上的resnet50)
- [随机情况说明](#随机情况说明)
- [ModelZoo主页](#modelzoo主页)
<!-- /TOC -->
# ResNet描述
## 概述
残差神经网络(ResNet)由微软研究院何凯明等五位华人提出,通过ResNet单元,成功训练152层神经网络,赢得了ILSVRC2015冠军。ResNet前五项的误差率为3.57%,参数量低于VGGNet,因此效果非常显著。传统的卷积网络或全连接网络或多或少存在信息丢失的问题,还会造成梯度消失或爆炸,导致深度网络训练失败,ResNet则在一定程度上解决了这个问题。通过将输入信息传递给输出,确保信息完整性。整个网络只需要学习输入和输出的差异部分,简化了学习目标和难度。ResNet的结构大幅提高了神经网络训练的速度,并且大大提高了模型的准确率。
如下为MindSpore使用AVA_Dataset数据集对ResNet50进行训练的示例。ResNet50可参考[论文1](https://arxiv.org/pdf/1512.03385.pdf)
## 论文
1. [论文](https://arxiv.org/pdf/1512.03385.pdf):Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun."Deep Residual Learning for Image Recognition"
2. [论文](https://arxiv.org/abs/1709.05424):H. Talebi and P. Milanfar, "NIMA: Neural Image Assessment"
# 模型架构
ResNet的总体网络架构如下:
[链接](https://arxiv.org/pdf/1512.03385.pdf)
预训练模型:
[链接](https://download.mindspore.cn/model_zoo/r1.3/resnet50_ascend_v130_imagenet2012_official_cv_bs256_top1acc76.97__top5acc_93.44/)
# 数据集
## 下载数据集, 并划分训练集与测试集
使用的数据集:[AVA_Dataset](<https://github.com/mtobeiyf/ava_downloader/tree/master/AVA_dataset>)
使用label:[AVA.txt](https://github.com/mtobeiyf/ava_downloader/blob/master/AVA_dataset/AVA.txt)
准备好数据,执行下面python命令划分数据集
```text
python ./src/dividing_label.py --config_path=~/config.yaml
#更改配置文件:data_path、label_path、val_label_path、train_label_path
```
- 数据集大小:255,502张彩色图像
- 训练集:229,905张图像
- 测试集:25,597张图像
- 数据格式:JEPG图像
# 特性
## 混合精度
采用[混合精度](https://www.mindspore.cn/docs/programming_guide/zh-CN/master/enable_mixed_precision.html)的训练方法使用支持单精度和半精度数据来提高深度学习神经网络的训练速度,同时保持单精度训练所能达到的网络精度。混合精度训练提高计算速度、减少内存使用的同时,支持在特定硬件上训练更大的模型或实现更大批次的训练。
以FP16算子为例,如果输入数据类型为FP32,MindSpore后台会自动降低精度来处理数据。用户可打开INFO日志,搜索“reduce precision”查看精度降低的算子。
# 环境要求
- 硬件(Ascend/GPU)
- 准备Ascend或GPU处理器搭建硬件环境。
- 框架
- [MindSpore](https://www.mindspore.cn/install/en)
- 如需查看详情,请参见如下资源:
- [MindSpore教程](https://www.mindspore.cn/tutorials/zh-CN/master/index.html)
- [MindSpore Python API](https://www.mindspore.cn/docs/api/zh-CN/master/index.html)
# 快速入门
通过官方网站安装MindSpore后,您可以按照如下步骤进行训练和评估:
- Ascend处理器环境运行
```text
# 运行训练示例
python train.py --config_path=./config.yaml >train.log 2>&1 &
# 分布式训练
bash ./scripts/run_train_ascend.sh ~/hccl_8p.json
# 运行评估示例
python eval.py --config_path ./config.yaml >eval.log 2>&1 &
```
如果要在modelarts上进行模型的训练,可以参考modelarts的官方指导文档(https://support.huaweicloud.com/modelarts/)
开始进行模型的训练和推理,具体操作如下:
```python
# 在modelarts上使用分布式训练的示例:
# (1) 在 config.yaml 文件中设置 "enable_modelarts=True","is_distributed=True",并设置其他参数,
# 如:data_path、output_path、train_data_path、val_data_path、checkpoint_path等。
# (2) 在modelarts的界面上设置代码目录"~/NIMA/"。
# (3) 在modelarts的界面上设置模型的启动文件 "~/NIMA/train.py" 。
# (4) 在modelarts的界面上添加运行参数 config_path = "~/NIMA/config.yaml"
# (5) 在modelarts的界面上设置模型的日志路径 "Job log path" 。
# (6) 开始模型的训练。
# 在modelarts上使用模型推理的示例
# (1) 把训练好的模型地方到桶的对应位置。
# (2) 在 config.yaml 文件中设置 "enable_modelarts=True",并设置如下参数:
# data_path、val_data_path、ckpt_file
# (3) 在modelarts的界面上设置代码目录"~/NIMA/"。
# (4) 在modelarts的界面上设置模型的启动文件 "eval.py" 。
# (5) 在modelarts的界面上添加运行参数 config_path = "~/config.yaml"
# (6) 在modelarts的界面上设置模型的日志路径 "Job log path" 。
# (7) 开始模型的推理。
```
# 脚本说明
## 脚本及样例代码
```shell
.
├──NIMA
├── README.md #相关说明
├──ascend310_infer #实现310推理源代码
├──model #预训练模型
├──ascend.ckpt
├──scripts
├──run_eval.sh #910评估shell脚本
├──run_infer_310.sh #310推理shell脚本
├──run_train_ascend.sh #910训练shell脚本
├──src
├──resnet.py #主干网络架构
├──test_data.py #生成310推理数据集
├──config.py #参数配置
├──device_adapter.py #设备适配
├──dividing_label.py #划分数据集
├──callback.py #回调
├──dataset.py #数据处理
├──metric.py #损失及指标
├──eval.py #评估脚本
├──export.py #将checkpoint文件导出到mindir
├──postprocess.py #310推理后处理脚本
├──train.py #训练脚本
├──AVA_train.txt #训练集label
├──AVA_test.txt #测试集label
```
## 脚本参数
```python
"device_target": "Ascend" #运行设备
"batch_size": 256 #训练批次大小
"epoch_size": 50 #总计训练epoch数
"num_parallel_workers": 16 #进程数
"learning_rate": 0.001 #学习率
"momentum": 0.95 #动量
"weight_decay": 0.001 #权值衰减值
"bf_crop_size": 256 #裁剪前图片大小
"image_size": 224 #实际送入网络图片大小
"train_label_path": "AVA_train.txt" #训练集绝对路径
"val_label_path": "AVA_test.txt" #测试集绝对路径
"keep_checkpoint_max": 10 #保存 checkpoint 的最大数量
"checkpoint_path": "./resnet50.ckpt" #预训练模型的绝对路径
"ckpt_save_dir": "./ckpt/" #模型保存路径
"is_distributed": False #是否分布式训练,默认False
"enable_modelarts": False #是否使用modelarts训练,默认False
"output_path": "./" #modelarts训练时,将ckpt_save_dir文件复制到桶
```
## 训练过程
### 用法
#### Ascend处理器环境运行
```text
# 单机训练
python train.py --config_path=./config.yaml >train.log
```
可指定`config.yaml`中的`device_id`
运行上述python命令后,您可以通过`train.log`文件查看结果
```text
# 分布式训练
Usage:bash scripts/run_train_ascend.sh [RANK_TABLE_FILE] [CONFIG_PATH]
#example: bash ./scripts/run_train_ascend.sh ~/hccl_8p.json ~/config.yaml
```
分布式训练需要提前创建JSON格式的HCCL配置文件。
具体操作,参见[hccn_tools](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/utils/hccl_tools)中的说明。
训练结果保存在示例路径中,文件夹名称以“train”或“train_parallel”开头。您可在此路径下的日志中找到检查点文件以及结果。
运行单卡用例时如果想更换运行卡号,可以通过配置环境中设置`device_id=x`或者在context中设置 `device_id=x`指定相应的卡号。
### 结果
```text
# 分布式训练结果(8P)
epoch: 1 step: 898, loss is 0.08514725
epoch: 2 step: 898, loss is 0.072653964
epoch: 3 step: 898, loss is 0.06939027
epoch: 4 step: 898, loss is 0.087793864
epoch: 5 step: 898, loss is 0.08969345
...
```
## 评估过程
### 用法
#### Ascend处理器环境运行
```text
# 运行评估示例
Usage:bash run_eval.sh [CONFIG_PATH]
#example: bash scripts/run_eval.sh config.yaml >export.log
```
更改配置文件`config.yaml``data_path``val_data_path``ckpt_file`即可
### 结果
评估结果保存在示例文件`eval.log`中。您可在此文件中找到的日志找到如下结果:
```bash
SRCC: 0.657146300995645
```
## 推理过程
### [导出MindIR](#contents)
数据准备
```shell
python ./src/test_data.py --config_path=config.yaml
```
确保`data_path``val_data_path`路径正确
执行该命令后会按照 `val_data_path` 生成310推理的数据集
导出mindir模型
```shell
python export.py --config_path=config.yaml >export.log
```
更改`ckpt_file``file_name`即可
### 在Ascend310执行推理
在执行推理前,mindir文件必须通过`export.py`脚本导出。以下展示了使用mindir模型执行推理的示例。
目前仅支持batch_Size为1的推理。
```shell
# Ascend310 inference
bash ./scripts/run_infer_310.sh [MODEL_PATH] [VAL_DATA_PATH] [DEVICE_ID]
# example: bash ./scripts/run_infer_310.sh ~/model/NIMA.mindir ~/test_data/ 0
```
- `DEVICE_ID` 可选,默认值为0。
### 结果
```shell
python ./postprocess.py --config_path=config.yaml &> acc.log
```
推理结果保存在脚本执行的当前路径,你可以在`acc.log`中看到以下精度计算结果。
```shell
cat acc.log
SRCC: 0.6571463000995645.
```
# 模型描述
## 性能
### 评估性能
#### AVA_Dataset上的ResNet50
| 参数 | Ascend 910 |
| ---------------------- | -------------------------------------- |
| 模型版本 | ResNet50 |
| 资源 | Ascend 910;CPU 2.60GHz,192核;内存 720G;系统 Euler2.8 |
| 上传日期 | 2021-11-19 ; |
| MindSpore版本 | 1.3.0 |
| 数据集 | AVA_Dataset |
| 训练参数 | epoch=50, steps per epoch=898, batch_size = 256|
| 优化器 | SGD |
| 损失函数 | EmdLoss(推土机距离) |
| 输出 | 概率分布 |
| 损失 | 0.05819133 |
| 速度 | 356毫秒/步(8卡) |
| 总时长 | 174分钟 |
| 参数(M) | 25.57 |
| 微调检查点 | 195M(.ckpt文件) |
| 配置文件 | [链接](https://gitee.com/mindspore/models/blob/master/official/cv/nima/config.yaml) |
# 随机情况说明
`dividing_label.py`中设置了random.seed(10),`train.py`中同样设置了set_seed(10)。
# ModelZoo主页
请浏览官网[主页](https://gitee.com/mindspore/mindspore/tree/master/model_zoo)
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*)
add_executable(main src/main.cc src/utils.cc)
target_link_libraries(main ${MS_LIB} ${MD_LIB} gflags)
#!/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
mkdir out
fi
cd out || exit
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);
#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 "../inc/utils.h"
#include "include/dataset/execute.h"
#include "include/dataset/transforms.h"
#include "include/dataset/vision.h"
#include "include/dataset/vision_ascend.h"
#include "include/api/types.h"
#include "include/api/model.h"
#include "include/api/serialization.h"
#include "include/api/context.h"
using mindspore::Serialization;
using mindspore::Model;
using mindspore::Context;
using mindspore::Status;
using mindspore::ModelType;
using mindspore::Graph;
using mindspore::GraphCell;
using mindspore::kSuccess;
using mindspore::MSTensor;
using mindspore::DataType;
using mindspore::dataset::Execute;
using mindspore::dataset::TensorTransform;
using mindspore::dataset::vision::Decode;
using mindspore::dataset::vision::Resize;
using mindspore::dataset::vision::Normalize;
using mindspore::dataset::vision::HWC2CHW;
using mindspore::dataset::InterpolationMode;
using mindspore::dataset::transforms::TypeCast;
DEFINE_string(model_path, "", "model path");
DEFINE_string(dataset_path, ".", "dataset path");
DEFINE_int32(input_width, 960, "input width");
DEFINE_int32(input_height, 576, "inputheight");
DEFINE_int32(device_id, 0, "device id");
DEFINE_string(precision_mode, "allow_fp32_to_fp16", "precision mode");
DEFINE_string(op_select_impl_mode, "", "op select impl mode");
DEFINE_string(aipp_path, "./aipp.cfg", "aipp path");
DEFINE_string(device_target, "Ascend310", "device target");
int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (RealPath(FLAGS_model_path).empty()) {
std::cout << "Invalid model" << std::endl;
return 1;
}
auto context = std::make_shared<Context>();
auto ascend310_info = std::make_shared<mindspore::Ascend310DeviceInfo>();
ascend310_info->SetDeviceID(FLAGS_device_id);
context->MutableDeviceInfo().push_back(ascend310_info);
Graph graph;
Status ret = Serialization::Load(FLAGS_model_path, ModelType::kMindIR, &graph);
if (ret != kSuccess) {
std::cout << "Load model failed." << std::endl;
return 1;
}
Model model;
ret = model.Build(GraphCell(graph), context);
if (ret != kSuccess) {
std::cout << "ERROR: Build failed." << std::endl;
return 1;
}
std::vector<MSTensor> modelInputs = model.GetInputs();
auto all_files = GetAllFiles(FLAGS_dataset_path);
if (all_files.empty()) {
std::cout << "ERROR: no input data." << std::endl;
return 1;
}
auto decode = Decode();
auto resize = Resize({224, 224}, InterpolationMode::kCubic);
auto normalize = Normalize({0.485, 0.456, 0.406}, {0.229, 0.224, 0.225});
auto hwc2chw = HWC2CHW();
auto typeCast = TypeCast(DataType::kNumberTypeFloat32);
mindspore::dataset::Execute transformDecode(decode);
mindspore::dataset::Execute transform({resize, normalize, hwc2chw});
mindspore::dataset::Execute transformCast(typeCast);
std::map<double, double> costTime_map;
size_t size = all_files.size();
for (size_t i = 0; i < size; ++i) {
struct timeval start;
struct timeval end;
double startTime_ms;
double endTime_ms;
std::vector<MSTensor> inputs;
std::vector<MSTensor> outputs;
std::cout << "Start predict input files:" << all_files[i] << std::endl;
mindspore::MSTensor image = ReadFileToTensor(all_files[i]);
transformDecode(image, &image);
std::vector<int64_t> shape = image.Shape();
transform(image, &image);
transformCast(image, &image);
inputs.emplace_back(modelInputs[0].Name(), modelInputs[0].DataType(), modelInputs[0].Shape(),
image.Data().get(), image.DataSize());
gettimeofday(&start, NULL);
model.Predict(inputs, &outputs);
std::cout << " infer result:" << all_files[i] << std::endl;
gettimeofday(&end, NULL);
startTime_ms = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000;
endTime_ms = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000;
costTime_map.insert(std::pair<double, double>(startTime_ms, endTime_ms));
WriteResult(all_files[i], outputs);
}
double average = 0.0;
double Fps = 0.0;
int infer_cnt = 0;
for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) {
double diff = 0.0;
std::cout << "time_time_1"<< iter->second << "time_time_0 "
<< iter->first << iter->second - iter->first << std::endl;
diff = iter->second - iter->first;
average += diff;
infer_cnt++;
}
Fps = infer_cnt*1000 / average;
average = average / infer_cnt;
std::stringstream timeCost;
std::stringstream fps;
fps << "Image processing speed is: " << Fps << "imgs/s" << std::endl;
timeCost << "NN inference cost average time: "<< average << " ms of infer_count " << infer_cnt << std::endl;
std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << infer_cnt << std::endl;
std::string file_name = "./time_Result" + std::string("/test_perform_static.txt");
std::ofstream file_stream(file_name.c_str(), std::ios::trunc);
file_stream << fps.str();
file_stream << timeCost.str();
file_stream.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 "inc/utils.h"
#include <fstream>
#include <algorithm>
#include <iostream>
#include <sstream>
using mindspore::MSTensor;
using mindspore::DataType;
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 file_name1 = "./time_Result" + std::string("/test.txt");
for (auto tensor : outputs) {
std::cout << "tensor name is:" << tensor.Name() << " tensor size is:" << tensor.DataSize()
<< " tensor elements num is:" << tensor.ElementNum() << std::endl;
auto out_data = reinterpret_cast<const float *>(tensor.Data().get());
std::cout << "output data is:";
std::ofstream file_stream1(file_name1.c_str(), std::ios::app);
file_stream1 << imageFile << ":";
for (int j = 0; j < tensor.ElementNum() && j <= 10; j++) {
std::stringstream out_data_1;
out_data_1 << out_data[j] << " ";
std::cout << out_data[j];
file_stream1 << out_data_1.str();
}
file_stream1 << ";\n";
file_stream1.close();
std::cout << std::endl;
}
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 = 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;
}
# Builtin Configurations(DO NOT CHANGE THESE CONFIGURATIONS unless you know exactly what you are doing)
enable_modelarts: False
is_distributed: False
# Path for local or modelarts
data_path: "~/NIMA/data/"
label_path: "~/NIMA/AVA.txt"
output_path: "s3://~output/"
# ==============================================================================
# options
device_target: "Ascend"
dataset_name: "AVA_Dataset"
batch_size: 256
epoch_size: 50
num_parallel_workers: 16
learning_rate: 0.001
momentum: 0.95
weight_decay: 0.001
bf_crop_size: 256
image_size: 224
train_label_path: "~/NIMA/AVA_train.txt"
val_label_path: "~/NIMA/AVA_test.txt"
keep_checkpoint_max: 10
checkpoint_path: "~/NIMA/model/resnet50_ascend_v130_imagenet2012_official_cv_bs256_top1acc76.97__top5acc_93.44.ckpt"
ckpt_filename: "NIMA"
ckpt_save_dir: "./model/"
device_id: 0
# eval or export option
val_data_path: "~/NIMA/test_data/"
ckpt_file: "~/NIMA/model/NIMA-2_898.ckpt"
file_name: "NIMA"
file_format: "MINDIR"
file_save: "./model/"
---
# Help description for each configuration
enable_modelarts: "Whether training on modelarts, default: False"
is_distributed: "Whether training use multiple cards, default: False"
data_url: "Url for modelarts"
train_url: "Url for modelarts"
output_path: "The location of the output file that uses modelarts."
data_path: "The location of the input data."
device_target: 'Target device type'
enable_profiling: 'Whether enable profiling while training, default: False'
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import numpy as np
import scipy.stats
from mindspore import load_checkpoint
import mindspore.context as context
from src.resnet import resnet50 as resnet
from src.dataset import create_dataset
from src.config import config
if __name__ == "__main__":
args = config
if args.enable_modelarts:
import moxing as mox
mox.file.shift('os', 'mox')
config.device_num = 1
config.rank = config.device_id
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=False,
device_id=args.device_id, reserve_class_name_in_scope=False)
ds_val, steps_per_epoch_val = create_dataset(args, data_mode='val')
net = resnet(10)
load_checkpoint(args.ckpt_file, net=net)
total_score = []
total_gt = []
SCORE_LIST = np.array([x for x in range(1, 11)])
for i, (data, gt_classes) in enumerate(ds_val):
net.set_train(False)
gt_classes = gt_classes.asnumpy()
output = net(data)
output = output.asnumpy()
gt = np.sum(gt_classes * np.array(SCORE_LIST), axis=1)
score = np.sum(output * np.array(SCORE_LIST), axis=1)
total_score += score.tolist()
total_gt += gt.tolist()
total_score = np.array(total_score)
total_gt = np.array(total_gt)
print('mse:', np.mean(np.power((total_score-total_gt), 2)))
print('deal imgs is:', total_score.shape[0])
print('SRCC:', scipy.stats.spearmanr(total_score, total_gt)[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.
# ============================================================================
import os
import numpy as np
from mindspore.common import dtype as mstype
from mindspore import Tensor, load_checkpoint, export
from src.resnet import resnet50 as resnet
from src.config import config
if __name__ == "__main__":
path = config.ckpt_file
net = resnet(10)
load_checkpoint(path, net=net)
img = np.random.randint(0, 255, size=(1, 3, config.image_size, config.image_size))
img = Tensor(np.array(img), mstype.float32)
export(net, img, file_name=os.path.join(config.file_save, config.file_name), file_format=config.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.
# ============================================================================
"""Evaluation for NIMA"""
import numpy as np
import scipy.stats
from src.config import config
dic = {}
with open(config.val_label_path) as l:
for lst in l.readlines():
dic[lst.split(',')[1]] = float(lst.split(',')[-1])
with open('./time_Result/test.txt') as f:
y_pred = f.readlines()
scores = []
gt = []
SCORE_LIST = np.array([x for x in range(1, 11)])
for i in y_pred:
pic = i.split(':')[0].split('/')[-1]
score_list = [float(j) for j in i.split(':')[1].split()[:-1]]
score = np.sum(np.array(score_list) * SCORE_LIST)
scores.append(score)
gt.append(dic[pic])
scores = np.array(scores)
gt = np.array(gt)
result = sum([(scores > 5) & (gt > 5)][0]) + sum([(scores <= 5) & (gt <= 5)][0])
print('mse:', np.mean(np.power((scores - gt), 2)))
print('acc: ', result/gt.shape[0])
print('SRCC: ', scipy.stats.spearmanr(scores, gt)[0])
#! /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 1 || $# -gt 1 ]]; then
echo "Usage: sh eval.sh [CONFIG_PATH]
CONFIG_PATH Must Be Provided!"
exit 1
fi
if [ ! -f $1 ]
then
echo "error: CONFIG_PATH=$1 is not a file"
exit 1
fi
config_path=$1
echo 'config_path:' $config_path
python eval.py --config_path=$config_path &> eval.log
#!/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 || $# -gt 3 ]]; then
echo "Usage: sh run_infer_310.sh [MODEL_PATH] [VAL_DATA_PATH] [DEVICE_ID]
DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero"
exit 1
fi
if [ ! -f $1 ]
then
echo "error: MODEL_PATH=$1 is not a file"
exit 1
fi
if [ ! -d $2 ]
then
echo "error: DATA_PATH=$2 is not a folder"
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)
data_path=$(get_real_path $2)
if [ $# == 3 ]; then
device_id=$3
elif [ $# == 2 ]; then
if [ -z $device_id ]; then
device_id=0
else
device_id=$device_id
fi
fi
echo $model
echo $data_path
echo $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 compile_app()
{
cd ./ascend310_infer || exit
if [ -f "Makefile" ]; then
make clean
fi
sh build.sh &> build.log
if [ $? -ne 0 ]; then
echo "compile app code failed"
exit 1
fi
cd - || exit
}
function infer()
{
if [ -d time_Result ]; then
rm -rf ./time_Result
fi
mkdir time_Result
./ascend310_infer/out/main --model_path=$model --dataset_path=$data_path --device_id=$device_id &> infer.log
if [ $? -ne 0 ]; then
echo "execute inference failed"
exit 1
fi
}
compile_app
infer
#! /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 1 || $# -gt 2 ]]; then
echo "Usage: sh run_train_ascend.sh [RANK_TABLE_FILE] [CONFIG_PATH]
Single-card training is used by default."
exit 1
fi
EXECUTE_PATH=$(pwd)
if [ ! -f $1 ]
then
echo "error: RANK_TABLE_FILE=$1 is not a file"
exit 1
fi
if [ $# == 1 ]; then
config_path="${EXECUTE_PATH}/config.yaml"
echo "config path is : ${config_path}"
else
config_path="$(realpath $2)"
echo "config path is : ${config_path}"
if [ ! -f $2 ]
then
echo "error: CONFIG_PATH=$2 is not a file"
exit 1
fi
fi
export DEVICE_NUM=8
export RANK_SIZE=8
PATH1=$(realpath $1)
export RANK_TABLE_FILE=$PATH1
echo "RANK_TABLE_FILE=${PATH1}"
export SERVER_ID=0
rank_start=$((DEVICE_NUM * SERVER_ID))
cpus=`cat /proc/cpuinfo| grep "processor"| wc -l`
avg=`expr $cpus \/ $DEVICE_NUM`
gap=`expr $avg \- 1`
for((i=0; i<${DEVICE_NUM}; i++))
do
start=`expr $i \* $avg`
end=`expr $start \+ $gap`
cmdopt=$start"-"$end
export DEVICE_ID=$i
export RANK_ID=$((rank_start + i))
rm -rf ./train_parallel$i
mkdir ./train_parallel$i
cp -r ./src ./train_parallel$i
cp -r ./*.yaml ./train_parallel$i
cp ./train.py ./train_parallel$i
echo "start training for rank $RANK_ID, device $DEVICE_ID"
cd ./train_parallel$i ||exit
env > env.log
taskset -c $cmdopt python train.py --config_path=$config_path> log.log 2>&1 &
cd ..
done
# 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 mindspore.train.callback import Callback
class EvalCallBack(Callback):
def __init__(self, model, eval_dataset, eval_per_epoch, epoch_per_eval):
self.model = model
self.eval_dataset = eval_dataset
self.eval_per_epoch = eval_per_epoch
self.epoch_per_eval = epoch_per_eval
def epoch_end(self, run_context):
cb_param = run_context.original_args()
cur_epoch = cb_param.cur_epoch_num
if cur_epoch % self.eval_per_epoch == 0:
acc = self.model.eval(self.eval_dataset, dataset_sink_mode=False)
self.epoch_per_eval["epoch"].append(cur_epoch)
self.epoch_per_eval["spearman"].append(acc["spearman"])
print('val_acc', acc)
# 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.
# ============================================================================
"""Parse arguments"""
import os
import ast
import argparse
from pprint import pprint, pformat
import yaml
class Config:
"""
Configuration namespace. Convert dictionary to members.
"""
def __init__(self, cfg_dict):
for k, v in cfg_dict.items():
if isinstance(v, (list, tuple)):
setattr(self, k, [Config(x) if isinstance(x, dict) else x for x in v])
else:
setattr(self, k, Config(v) if isinstance(v, dict) else v)
def __str__(self):
return pformat(self.__dict__)
def __repr__(self):
return self.__str__()
def parse_cli_to_yaml(parser, cfg, helper=None, choices=None, cfg_path="../config.yaml"):
"""
Parse command line arguments to the configuration according to the default yaml.
Args:
parser: Parent parser.
cfg: Base configuration.
helper: Helper description.
cfg_path: Path to the default yaml config.
"""
parser = argparse.ArgumentParser(description="[REPLACE THIS at config.py]",
parents=[parser])
helper = {} if helper is None else helper
choices = {} if choices is None else choices
for item in cfg:
if not isinstance(cfg[item], list) and not isinstance(cfg[item], dict):
help_description = helper[item] if item in helper else "Please reference to {}".format(cfg_path)
choice = choices[item] if item in choices else None
if isinstance(cfg[item], bool):
parser.add_argument("--" + item, type=ast.literal_eval, default=cfg[item], choices=choice,
help=help_description)
else:
parser.add_argument("--" + item, type=type(cfg[item]), default=cfg[item], choices=choice,
help=help_description)
args = parser.parse_args()
return args
def parse_yaml(yaml_path):
"""
Parse the yaml config file.
Args:
yaml_path: Path to the yaml config.
"""
with open(yaml_path, 'r') as fin:
try:
cfgs = yaml.load_all(fin.read(), Loader=yaml.FullLoader)
cfgs = [x for x in cfgs]
if len(cfgs) == 1:
cfg_helper = {}
cfg = cfgs[0]
cfg_choices = {}
elif len(cfgs) == 2:
cfg, cfg_helper = cfgs
cfg_choices = {}
elif len(cfgs) == 3:
cfg, cfg_helper, cfg_choices = cfgs
else:
raise ValueError("At most 3 docs (config, description for help, choices) are supported in config yaml")
print(cfg_helper)
except:
raise ValueError("Failed to parse yaml")
return cfg, cfg_helper, cfg_choices
def merge(args, cfg):
"""
Merge the base config from yaml file and command line arguments.
Args:
args: Command line arguments.
cfg: Base configuration.
"""
args_var = vars(args)
for item in args_var:
cfg[item] = args_var[item]
return cfg
def get_config():
"""
Get Config according to the yaml file and cli arguments.
"""
parser = argparse.ArgumentParser(description="default name", add_help=False)
current_dir = os.path.dirname(os.path.abspath(__file__))
parser.add_argument("--config_path", type=str, default=os.path.join(current_dir, "../config.yaml"),
help="Config file path")
path_args, _ = parser.parse_known_args()
if path_args.config_path.startswith('s3://'):
import moxing as mox
mox.file.shift('os', 'mox')
default, helper, choices = parse_yaml(path_args.config_path)
pprint(default)
args = parse_cli_to_yaml(parser=parser, cfg=default, helper=helper, choices=choices, cfg_path=path_args.config_path)
final_config = merge(args, default)
return Config(final_config)
config = get_config()
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import os
import numpy as np
import mindspore
import mindspore.dataset as ds
from mindspore.dataset.vision import Inter
from mindspore.dataset.vision import c_transforms as v_ct
from mindspore.dataset.transforms import c_transforms as t_ct
class Dataset:
def __init__(self, image_list, label_list):
super(Dataset, self).__init__()
self.imgs = image_list
self.labels = label_list
def __getitem__(self, index):
with open(self.imgs[index], 'rb') as f:
img_ = f.read()
img = np.frombuffer(img_, np.uint8)
return img, self.labels[index]
def __len__(self):
return len(self.imgs)
def score_lst(lst):
lst = np.array(lst).astype(int)
res = lst / sum(lst)
return res
def create_dataset(args, data_mode='train'):
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
rank_id = args.rank
rank_size = args.device_num
if data_mode == 'train':
with open(args.train_label_path, 'r') as f:
datafile = f.readlines()
transform_img = t_ct.Compose([
v_ct.Decode(),
v_ct.Resize([args.bf_crop_size, args.bf_crop_size], Inter.BICUBIC),
v_ct.RandomCrop(args.image_size),
v_ct.RandomHorizontalFlip(prob=0.5),
v_ct.Normalize(mean=mean, std=std),
v_ct.HWC2CHW()])
else:
with open(args.val_label_path, 'r') as f:
datafile = f.readlines()
transform_img = t_ct.Compose([
v_ct.Decode(),
v_ct.Resize([args.image_size, args.image_size], Inter.BICUBIC),
v_ct.RandomHorizontalFlip(prob=0.5),
v_ct.Normalize(mean=mean, std=std),
v_ct.HWC2CHW()])
transform_label = t_ct.TypeCast(mindspore.dtype.float32)
save_image_list = [os.path.join(args.data_path, i.split(',')[1]) for i in datafile]
save_label_list = [score_lst(i.split(',')[2:12]) for i in datafile]
dataset = Dataset(save_image_list, save_label_list)
if data_mode == 'train':
if rank_size == 1:
de_dataset = ds.GeneratorDataset(dataset, column_names=["image", "label"],
shuffle=True, num_parallel_workers=args.num_parallel_workers)
else:
de_dataset = ds.GeneratorDataset(dataset, column_names=["image", "label"],
shuffle=True, num_parallel_workers=args.num_parallel_workers,
num_shards=rank_size, shard_id=rank_id)
drop_remainder = True
else:
de_dataset = ds.GeneratorDataset(dataset, column_names=["image", "label"],
shuffle=False, num_parallel_workers=args.num_parallel_workers)
drop_remainder = False
de_dataset = de_dataset.map(input_columns="image", operations=transform_img)
de_dataset = de_dataset.map(input_columns="label", operations=transform_label)
de_dataset = de_dataset.batch(args.batch_size,
drop_remainder=drop_remainder)
de_dataset = de_dataset.repeat(1)
steps_per_epoch = de_dataset.get_dataset_size()
return de_dataset, steps_per_epoch
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import os
def get_device_id():
device_id = os.getenv('DEVICE_ID', '0')
return int(device_id)
def get_device_num():
device_num = os.getenv('RANK_SIZE', '1')
return int(device_num)
def _get_rank_info():
"""
get rank size and rank id
"""
rank_size = int(os.environ.get("RANK_SIZE", 1))
if rank_size > 1:
from mindspore.communication.management import get_rank, get_group_size
rank_size = get_group_size()
rank_id = get_rank()
else:
rank_size = rank_id = None
return rank_size, rank_id
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import os
import random
import pandas as pd
from config import config
random.seed(10)
if config.enable_modelarts:
import moxing as mox
mox.file.shift('os', 'mox')
pic_names = os.listdir(config.data_path)
dic = []
with open(config.label_path) as f:
for line in f:
name = line.split()[1]+'.jpg'
lst = map(int, line.split()[2:12])
lst = list(lst)
score = round(sum([(i+1)*j for i, j in enumerate(lst)])/sum(lst), 7)
dic.append([name]+line.split()[2:12]+[score])
df = pd.DataFrame(dic)
df_new = df[df[0].isin(pic_names)].copy()
df_new.reset_index(drop=True, inplace=True)
test_img = random.sample(pic_names, 25597)
test_label = df_new[df_new[0].isin(test_img)].copy()
train_label = df_new[~df_new[0].isin(test_img)].copy()
test_label.to_csv(config.val_label_path, header=0)
train_label.to_csv(config.train_label_path, header=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.
# ============================================================================
import time
import numpy as np
import scipy.stats
import mindspore.nn as nn
import mindspore.ops as P
from mindspore.train.callback import Callback
class EmdLoss(nn.Cell):
def __init__(self):
super(EmdLoss, self).__init__()
self.square = P.Square()
self.reduce_mean = P.ReduceMean()
self.cumsum = P.CumSum()
self.sqrt = P.Sqrt()
def construct(self, data, label):
data = self.cumsum(data, 1)
label = self.cumsum(label, 1)
diff = data - label
emd = self.sqrt(self.reduce_mean(self.square(diff), 1))
return self.reduce_mean(emd)
class PrintFps(Callback):
def __init__(self, train_data_num, start_time, end_time):
self.train_data_num = train_data_num
self.start_time = start_time
self.end_time = end_time
def epoch_begin(self, run_context):
self.start_time = time.time()
def epoch_end(self, run_context):
self.end_time = time.time()
cb_param = run_context.original_args()
cur_epoch = cb_param.cur_epoch_num
fps = self.train_data_num / (self.end_time - self.start_time)
loss = cb_param.net_outputs
used_time = self.end_time - self.start_time
print("Epoch:{} ,used time is:{:.2f}, fps: {:.2f}imgs/sec".format(cur_epoch, used_time, fps))
print('Step_end loss is', loss)
class spearman(nn.Accuracy):
def clear(self):
self._correct_num = []
self._total_num = []
def update(self, *inputs):
y_pred = self._convert_data(inputs[0])
y = self._convert_data(inputs[1])
SCORE_LIST = [[i for i in range(1, 11)]] * inputs[0].shape[0]
gt = np.sum(y * np.array(SCORE_LIST), axis=1)
score = np.sum(y_pred * np.array(SCORE_LIST), axis=1)
self._correct_num += gt.tolist()
self._total_num += score.tolist()
def eval(self):
return scipy.stats.spearmanr(self._correct_num, self._total_num)
# 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.
# ============================================================================
"""ResNet."""
import math
import numpy as np
from scipy.stats import truncnorm
import mindspore.nn as nn
import mindspore.common.dtype as mstype
from mindspore.ops import operations as P
from mindspore.ops import functional as F
from mindspore.common.tensor import Tensor
def conv_variance_scaling_initializer(in_channel, out_channel, kernel_size):
fan_in = in_channel * kernel_size * kernel_size
scale = 1.0
scale /= max(1., fan_in)
stddev = (scale ** 0.5) / .87962566103423978
mu, sigma = 0, stddev
weight = truncnorm(-2, 2, loc=mu, scale=sigma).rvs(out_channel * in_channel * kernel_size * kernel_size)
weight = np.reshape(weight, (out_channel, in_channel, kernel_size, kernel_size))
return Tensor(weight, dtype=mstype.float32)
def _weight_variable(shape, factor=0.01):
init_value = np.random.randn(*shape).astype(np.float32) * factor
return Tensor(init_value)
def calculate_gain(nonlinearity, param=None):
"""calculate_gain"""
linear_fns = ['linear', 'conv1d', 'conv2d', 'conv3d', 'conv_transpose1d', 'conv_transpose2d', 'conv_transpose3d']
res = 0
if nonlinearity in linear_fns or nonlinearity == 'sigmoid':
res = 1
elif nonlinearity == 'tanh':
res = 5.0 / 3
elif nonlinearity == 'relu':
res = math.sqrt(2.0)
elif nonlinearity == 'leaky_relu':
if param is None:
neg_slope = 0.01
elif not isinstance(param, bool) and isinstance(param, int) or isinstance(param, float):
neg_slope = param
else:
raise ValueError("neg_slope {} not a valid number".format(param))
res = math.sqrt(2.0 / (1 + neg_slope ** 2))
else:
raise ValueError("Unsupported nonlinearity {}".format(nonlinearity))
return res
def _calculate_fan_in_and_fan_out(tensor):
"""_calculate_fan_in_and_fan_out"""
dimensions = len(tensor)
if dimensions < 2:
raise ValueError("Fan in and fan out can not be computed for tensor with fewer than 2 dimensions")
if dimensions == 2: # Linear
fan_in = tensor[1]
fan_out = tensor[0]
else:
num_input_fmaps = tensor[1]
num_output_fmaps = tensor[0]
receptive_field_size = 1
if dimensions > 2:
receptive_field_size = tensor[2] * tensor[3]
fan_in = num_input_fmaps * receptive_field_size
fan_out = num_output_fmaps * receptive_field_size
return fan_in, fan_out
def _calculate_correct_fan(tensor, mode):
mode = mode.lower()
valid_modes = ['fan_in', 'fan_out']
if mode not in valid_modes:
raise ValueError("Unsupported mode {}, please use one of {}".format(mode, valid_modes))
fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor)
return fan_in if mode == 'fan_in' else fan_out
def kaiming_normal(inputs_shape, a=0, mode='fan_in', nonlinearity='leaky_relu'):
fan = _calculate_correct_fan(inputs_shape, mode)
gain = calculate_gain(nonlinearity, a)
std = gain / math.sqrt(fan)
return np.random.normal(0, std, size=inputs_shape).astype(np.float32)
def kaiming_uniform(inputs_shape, a=0., mode='fan_in', nonlinearity='leaky_relu'):
fan = _calculate_correct_fan(inputs_shape, mode)
gain = calculate_gain(nonlinearity, a)
std = gain / math.sqrt(fan)
bound = math.sqrt(3.0) * std # Calculate uniform bounds from standard deviation
return np.random.uniform(-bound, bound, size=inputs_shape).astype(np.float32)
def _conv3x3(in_channel, out_channel, stride=1, use_se=False, res_base=False):
if use_se:
weight = conv_variance_scaling_initializer(in_channel, out_channel, kernel_size=3)
else:
weight_shape = (out_channel, in_channel, 3, 3)
weight = Tensor(kaiming_normal(weight_shape, mode="fan_out", nonlinearity='relu'))
if res_base:
return nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=stride,
padding=1, pad_mode='pad', weight_init=weight)
return nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=stride,
padding=0, pad_mode='same', weight_init=weight)
def _conv1x1(in_channel, out_channel, stride=1, use_se=False, res_base=False):
if use_se:
weight = conv_variance_scaling_initializer(in_channel, out_channel, kernel_size=1)
else:
weight_shape = (out_channel, in_channel, 1, 1)
weight = Tensor(kaiming_normal(weight_shape, mode="fan_out", nonlinearity='relu'))
if res_base:
return nn.Conv2d(in_channel, out_channel, kernel_size=1, stride=stride,
padding=0, pad_mode='pad', weight_init=weight)
return nn.Conv2d(in_channel, out_channel, kernel_size=1, stride=stride,
padding=0, pad_mode='same', weight_init=weight)
def _conv7x7(in_channel, out_channel, stride=1, use_se=False, res_base=False):
if use_se:
weight = conv_variance_scaling_initializer(in_channel, out_channel, kernel_size=7)
else:
weight_shape = (out_channel, in_channel, 7, 7)
weight = Tensor(kaiming_normal(weight_shape, mode="fan_out", nonlinearity='relu'))
if res_base:
return nn.Conv2d(in_channel, out_channel,
kernel_size=7, stride=stride, padding=3, pad_mode='pad', weight_init=weight)
return nn.Conv2d(in_channel, out_channel,
kernel_size=7, stride=stride, padding=0, pad_mode='same', weight_init=weight)
def _bn(channel, res_base=False):
if res_base:
return nn.BatchNorm2d(channel, eps=1e-5, momentum=0.1,
gamma_init=1, beta_init=0, moving_mean_init=0, moving_var_init=1)
return nn.BatchNorm2d(channel, eps=1e-4, momentum=0.9,
gamma_init=1, beta_init=0, moving_mean_init=0, moving_var_init=1)
def _bn_last(channel):
return nn.BatchNorm2d(channel, eps=1e-4, momentum=0.9,
gamma_init=0, beta_init=0, moving_mean_init=0, moving_var_init=1)
def _fc(in_channel, out_channel, use_se=False):
if use_se:
weight = np.random.normal(loc=0, scale=0.01, size=out_channel * in_channel)
weight = Tensor(np.reshape(weight, (out_channel, in_channel)), dtype=mstype.float32)
else:
weight_shape = (out_channel, in_channel)
weight = Tensor(kaiming_uniform(weight_shape, a=math.sqrt(5)))
return nn.Dense(in_channel, out_channel, has_bias=True, weight_init=weight, bias_init=0)
class ResidualBlock(nn.Cell):
"""
ResNet V1 residual block definition.
Args:
in_channel (int): Input channel.
out_channel (int): Output channel.
stride (int): Stride size for the first convolutional layer. Default: 1.
use_se (bool): Enable SE-ResNet50 net. Default: False.
se_block(bool): Use se block in SE-ResNet50 net. Default: False.
Returns:
Tensor, output tensor.
Examples:
>>> ResidualBlock(3, 256, stride=2)
"""
expansion = 4
def __init__(self,
in_channel,
out_channel,
stride=1,
use_se=False, se_block=False):
super(ResidualBlock, self).__init__()
self.stride = stride
self.use_se = use_se
self.se_block = se_block
channel = out_channel // self.expansion
self.conv1 = _conv1x1(in_channel, channel, stride=1, use_se=self.use_se)
self.bn1 = _bn(channel)
if self.use_se and self.stride != 1:
self.e2 = nn.SequentialCell([_conv3x3(channel, channel, stride=1, use_se=True), _bn(channel),
nn.ReLU(), nn.MaxPool2d(kernel_size=2, stride=2, pad_mode='same')])
else:
self.conv2 = _conv3x3(channel, channel, stride=stride, use_se=self.use_se)
self.bn2 = _bn(channel)
self.conv3 = _conv1x1(channel, out_channel, stride=1, use_se=self.use_se)
self.bn3 = _bn(out_channel)
if self.se_block:
self.se_global_pool = P.ReduceMean(keep_dims=False)
self.se_dense_0 = _fc(out_channel, int(out_channel / 4), use_se=self.use_se)
self.se_dense_1 = _fc(int(out_channel / 4), out_channel, use_se=self.use_se)
self.se_sigmoid = nn.Sigmoid()
self.se_mul = P.Mul()
self.relu = nn.ReLU()
self.down_sample = False
if stride != 1 or in_channel != out_channel:
self.down_sample = True
self.down_sample_layer = None
if self.down_sample:
if self.use_se:
if stride == 1:
self.down_sample_layer = nn.SequentialCell([_conv1x1(in_channel, out_channel,
stride, use_se=self.use_se), _bn(out_channel)])
else:
self.down_sample_layer = nn.SequentialCell([nn.MaxPool2d(kernel_size=2, stride=2, pad_mode='same'),
_conv1x1(in_channel, out_channel, 1,
use_se=self.use_se), _bn(out_channel)])
else:
self.down_sample_layer = nn.SequentialCell([_conv1x1(in_channel, out_channel, stride,
use_se=self.use_se), _bn(out_channel)])
def construct(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
if self.use_se and self.stride != 1:
out = self.e2(out)
else:
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.se_block:
out_se = out
out = self.se_global_pool(out, (2, 3))
out = self.se_dense_0(out)
out = self.relu(out)
out = self.se_dense_1(out)
out = self.se_sigmoid(out)
out = F.reshape(out, F.shape(out) + (1, 1))
out = self.se_mul(out, out_se)
if self.down_sample:
identity = self.down_sample_layer(identity)
out = out + identity
out = self.relu(out)
return out
class ResNet(nn.Cell):
"""
ResNet architecture.
Args:
block (Cell): Block for network.
layer_nums (list): Numbers of block in different layers.
in_channels (list): Input channel in each layer.
out_channels (list): Output channel in each layer.
strides (list): Stride size in each layer.
num_classes (int): The number of classes that the training images are belonging to.
use_se (bool): Enable SE-ResNet50 net. Default: False.
se_block(bool): Use se block in SE-ResNet50 net in layer 3 and layer 4. Default: False.
res_base (bool): Enable parameter setting of resnet18. Default: False.
Returns:
Tensor, output tensor.
Examples:
>>> ResNet(ResidualBlock,
>>> [3, 4, 6, 3],
>>> [64, 256, 512, 1024],
>>> [256, 512, 1024, 2048],
>>> [1, 2, 2, 2],
>>> 10)
"""
def __init__(self,
block,
layer_nums,
in_channels,
out_channels,
strides,
num_classes,
use_se=False,
res_base=False):
super(ResNet, self).__init__()
if not len(layer_nums) == len(in_channels) == len(out_channels) == 4:
raise ValueError("the length of layer_num, in_channels, out_channels list must be 4!")
self.use_se = use_se
self.res_base = res_base
self.se_block = False
if self.use_se:
self.se_block = True
if self.use_se:
self.conv1_0 = _conv3x3(3, 32, stride=2, use_se=self.use_se)
self.bn1_0 = _bn(32)
self.conv1_1 = _conv3x3(32, 32, stride=1, use_se=self.use_se)
self.bn1_1 = _bn(32)
self.conv1_2 = _conv3x3(32, 64, stride=1, use_se=self.use_se)
else:
self.conv1 = _conv7x7(3, 64, stride=2, res_base=self.res_base)
self.bn1 = _bn(64, self.res_base)
self.relu = P.ReLU()
if self.res_base:
self.pad = nn.Pad(paddings=((0, 0), (0, 0), (1, 1), (1, 1)))
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="valid")
else:
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="same")
self.layer1 = self._make_layer(block,
layer_nums[0],
in_channel=in_channels[0],
out_channel=out_channels[0],
stride=strides[0],
use_se=self.use_se)
self.layer2 = self._make_layer(block,
layer_nums[1],
in_channel=in_channels[1],
out_channel=out_channels[1],
stride=strides[1],
use_se=self.use_se)
self.layer3 = self._make_layer(block,
layer_nums[2],
in_channel=in_channels[2],
out_channel=out_channels[2],
stride=strides[2],
use_se=self.use_se,
se_block=self.se_block)
self.layer4 = self._make_layer(block,
layer_nums[3],
in_channel=in_channels[3],
out_channel=out_channels[3],
stride=strides[3],
use_se=self.use_se,
se_block=self.se_block)
self.mean = P.ReduceMean(keep_dims=True)
self.flatten = nn.Flatten()
self.end_point = _fc(out_channels[3], 1000, use_se=self.use_se)
self.out = nn.SequentialCell([nn.Dense(1000, num_classes),# nn.Dropout(0.75),
nn.Softmax()
])
def _make_layer(self, block, layer_num, in_channel, out_channel, stride, use_se=False, se_block=False):
"""
Make stage network of ResNet.
Args:
block (Cell): Resnet block.
layer_num (int): Layer number.
in_channel (int): Input channel.
out_channel (int): Output channel.
stride (int): Stride size for the first convolutional layer.
se_block(bool): Use se block in SE-ResNet50 net. Default: False.
Returns:
SequentialCell, the output layer.
Examples:
>>> _make_layer(ResidualBlock, 3, 128, 256, 2)
"""
layers = []
resnet_block = block(in_channel, out_channel, stride=stride, use_se=use_se)
layers.append(resnet_block)
if se_block:
for _ in range(1, layer_num - 1):
resnet_block = block(out_channel, out_channel, stride=1, use_se=use_se)
layers.append(resnet_block)
resnet_block = block(out_channel, out_channel, stride=1, use_se=use_se, se_block=se_block)
layers.append(resnet_block)
else:
for _ in range(1, layer_num):
resnet_block = block(out_channel, out_channel, stride=1, use_se=use_se)
layers.append(resnet_block)
return nn.SequentialCell(layers)
def construct(self, x):
if self.use_se:
x = self.conv1_0(x)
x = self.bn1_0(x)
x = self.relu(x)
x = self.conv1_1(x)
x = self.bn1_1(x)
x = self.relu(x)
x = self.conv1_2(x)
else:
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
if self.res_base:
x = self.pad(x)
c1 = self.maxpool(x)
c2 = self.layer1(c1)
c3 = self.layer2(c2)
c4 = self.layer3(c3)
c5 = self.layer4(c4)
out = self.mean(c5, (2, 3))
out = self.flatten(out)
out = self.end_point(out)
out = self.out(out)
return out
def resnet50(class_num=10):
"""
Get ResNet50 neural network.
Args:
class_num (int): Class number.
Returns:
Cell, cell instance of ResNet50 neural network.
Examples:
>>> net = resnet50(10)
"""
return ResNet(ResidualBlock,
[3, 4, 6, 3],
[64, 256, 512, 1024],
[256, 512, 1024, 2048],
[1, 2, 2, 2],
class_num)
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