Skip to content
Snippets Groups Projects
Commit 33174eb6 authored by unknown's avatar unknown
Browse files

hed

parent 14fcd4dd
No related branches found
No related tags found
No related merge requests found
Showing
with 1697 additions and 0 deletions
# Contents
- [HED Description](#hed-description)
- [Model Architecture](#model-architecture)
- [Dataset](#dataset)
- [Environment Requirements](#environment-requirements)
- [Quick Start](#quick-start)
- [Script Description](#script-description)
- [Script and Sample Code](#script-and-sample-code)
- [Script Parameters](#script-parameters)
- [Parameter configuration](#parameter-configuration)
- [Training Process](#training-process)
- [Training](#training)
- [Evaluation Process](#evaluation-process)
- [Evaluation](#evaluation)
- [Model Description](#model-description)
- [Performance](#performance)
- [Training Performance](#training-performance)
- [Evaluation Performance](#evaluation-performance)
- [Description of Random Situation](#description-of-random-situation)
- [ModelZoo Homepage](#modelzoo-homepage)
## [HED Description](#contents)
HED, a new edge detection algorithm that tackles two important issues in this long-standing vision problem: (1) holistic image training and prediction; and (2) multi-scale and multi-level feature learning. Our proposed method, holistically-nested edge detection (HED), performs image-to-image prediction by means of a deep learning model that leverages fully convolutional neural networks and deeply-supervised nets. HED automatically learns rich hierarchical representations (guided by deep supervision on side responses) that are important in order to approach the human ability resolve the challenging ambiguity in edge and object boundary detection. We significantly advance the state-of-the-art on the BSD500 dataset (ODS F-score of .782) and the NYU Depth dataset (ODS F-score of .746), and do so with an improved speed (0.4 second per image) that is orders of magnitude faster than some recent CNN-based edge detection algorithms.
[Paper](): Saining Xie, Zhuowen Tu. Holistically-Nested Edge Detection. arXiv preprint arXiv:1504.06375, 2015.(https://arxiv.org/abs/1504.06375)
## [Model Architecture](#contents)
HED network is based on vgg19 as the backbone, which is mainly composed of several basic modules (including convolution and pool layer) and deconvolution layer.
## [Dataset](#contents)
Note that you can run the scripts based on the dataset mentioned in original paper or widely used in relevant domain/network architecture. In the following sections, we will introduce how to run the scripts using the related dataset below.
### Dataset used: [BSDS500](https://www2.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/resources.html)
- Dataset size: ~81M, 500 colorful images
- Train: 200 images
- Val: 100 images
- Test: 200 images
- Data format: RGB images
- Note: Data will be processed in src/dataset.py
## [Features](#contents)
## [Environment Requirements](#contents)
- Hardware(Ascend)
- Prepare hardware environment with Ascend processor.
- Framework
- [MindSpore](https://www.mindspore.cn/install/en)
- For more information, please check the resources below:
- [MindSpore Tutorials](https://www.mindspore.cn/tutorials/en/master/index.html)
- [MindSpore Python API](https://www.mindspore.cn/docs/api/en/master/index.html)
## [Quick Start](#contents)
After installing MindSpore via the official website, you can start training and evaluation as follows:
- Running on Ascend
For distributed training, a hccl configuration file with JSON format needs to be created in advance.
Please follow the instructions in the link below:
<https://gitee.com/mindspore/models/tree/master/utils/hccl_tools>
- Running on [ModelArts](https://support.huaweicloud.com/modelarts/)
## [Script Description](#contents)
### [Script and Sample Code](#contents)
```text
├── cv
├── hed
├── README.md // descriptions about hed
├── ascend310_infer
├── ├── CMakeLists.txt // CMake
├── ├── build.sh // shell file of build
├── ├── inc
├── ├── ├── utils.h // utils.h
├── ├── src
├── ├── ├── main.cc // main file
├── ├── ├── utils.cc // utils.cc
├── scripts
│ ├── run_infer_310.sh // shell script for infer on Ascend 310
├── run_single_train.sh // shell script for training
├── run_single_eval.sh // shell script for eval
├── src
│ ├── model_utils
│ │ ├── __init__.py // init file
│ │ ├── config.py // Parse arguments
│ │ ├── device_adapter.py // Device adapter for ModelArts
│ │ ├── local_adapter.py // Local adapter
│ │ ├── moxing_adapter.py // Moxing adapter for ModelArts
│ ├── impl
│ │ ├── __init__.py // init file
│ │ ├── bwmorph_thin.py // bwmorph thin
│ │ ├── correspond_pixels.py // correspond pixels
│ │ ├── edges_eval_dir.py // edges eval dir
│ │ ├── edges_eval_plot.py // edges eval plot
│ │ ├── toolbox.py // box tool
│ ├── dataset.py // creating dataset
├── loss.py // loss
├── lr_schedule.py // learning rate set
│ ├── eval_edge.py // edge eval
│ ├── model.py // hed network define
│ ├── nms_process.py // nms process
├── eval.py // evaluation script
├── export.py // export script
├── postprocess.py // postprocess script
├── preprocess.py // preprocess script
├── default_config.yaml // Configurations
├── train.py // train script
├── prepare.py // prepare script
├── requirements.txt // requirements
```
### [Script Parameters](#contents)
#### Training
#### Evaluation
### [Parameter configuration](#contents)
Parameters for both training and evaluation can be set in default_config_910/default_config_gpu.
### [Training Process](#contents)
#### Training
- vgg_ckpt_path
the ckpt used in HED is pretrained vgg16 model provided by HUAWEI.(https://download.mindspore.cn/model_zoo/r1.2/vgg16_ascend_v120_imagenet2012_official_cv_bs32_acc73/)
##### Run HED on Ascend
- Training using single device(1p), using BSDS500 dataset in default
```bash
# when using BSDS500 dataset
python prepare.py --config_path [CONFIG_PATH]
bash scripts/run_single_train.sh [DEVICE_ID] [CONFIG_PATH]
```
- `CONFIG_PATH` is the absolute path of config file
- `DEVICE_ID` is the ID of device
> **Attention** This will bind the processor cores according to the `device_num` and total processor numbers. If you don't expect to run pretraining with binding processor cores, remove the operations about `taskset` in `scripts/run_distribute_train.sh`
```text
# training process
epoch: 497 step: 20, loss is 0.10391282
2022-04-15 20:44:31.396521 end epoch 497 1.7445739e-06 0.10391282
epoch: 498 step: 20, loss is 0.07053816
2022-04-15 20:44:42.065112 end epoch 498 1.6104921e-06 0.07053816
epoch: 499 step: 20, loss is 0.09414602
2022-04-15 20:44:52.756103 end epoch 499 1.5289875e-06 0.09414602
epoch: 500 step: 20, loss is 0.09735751
2022-04-15 20:45:04.091150 end epoch 500 1.5000658e-06 0.09735751
```
##### Run HED on GPU
- Training using single device(1p), using BSDS500 dataset in default
```bash
# when using BSDS500 dataset
bash scripts/run_single_train.sh [DEVICE_ID] [CONFIG_PATH]
```
- `CONFIG_PATH` is the absolute path of config file
- `DEVICE_ID` is the ID of device
> **Attention** This will bind the processor cores according to the `device_num` and total processor numbers. If you don't expect to run pretraining with binding processor cores, remove the operations about `taskset` in `scripts/run_distribute_train.sh`
```bash
# training process
epoch: 2 step: 20, loss is 0.67871356
epoch time: 4363.766, per step time: 218.188
2022-04-20 11:41:23.881890 end epoch 2 0.0015 0.67871356
epoch: 3 step: 20, loss is 0.4893721
epoch time: 4466.146, per step time: 223.307
2022-04-20 11:41:28.348629 end epoch 3 0.0015 0.4893721
epoch: 4 step: 20, loss is 0.46001524
epoch time: 4358.786, per step time: 217.939
2022-04-20 11:41:32.707885 end epoch 4 0.0015 0.46001524
epoch: 5 step: 20, loss is 0.28899446
epoch time: 4635.880, per step time: 231.794
2022-04-20 11:41:37.344164 end epoch 5 0.0015 0.28899446
```
### [Evaluation Process](#contents)
#### Evaluation
- Do eval as follows, need to specify dataset type as "BSDS500"
```bash
# when using BSDS500 dataset
Before doing eval, please make the preparations according to ./scripts/README.md
cd src/cxx/src
source build.sh
cd ..
cd ..
cd ..
python prepare.py --config_path [CONFIG_PATH]
bash scripts/run_single_eval.sh [DEVICE_ID] [CONFIG_PATH]
```
- `CONFIG_PATH` is the absolute path of config file
- `DEVICE_ID` is the ID of device
- The above python command will run in the background, you can view the results through the file `eval.log`. You will get the accuracy as following:
```text
# grep "ODS=" output.eval.log
ODS=0.773, OIS=0.793, AP=0.800, R50=0.906 - HED
```
## Inference Process
### [Export MindIR](#contents)
```shell
python prepare.py --config_path [CONFIG_PATH]
python export.py --config_path [CONFIG_PATH] --file_format [FILE_FORMAT]
```
- `CONFIG_PATH` is the absolute path of config file
- `FILE_FORMAT` is chosen from ["AIR", "MINDIR"]
### Infer on Ascend310
Before performing inference, the mindir file must be exported by `export.py` script. We only provide an example of inference using MINDIR model.
Current batch_Size for BSDS500 dataset can only be set to 1.
```shell
Before performing following operations, please refer to /scripts/README.md.
cd src/cxx/src
source build.sh
cd ..
cd ..
cd ..
cd scripts/
bash run_infer_310.sh [MINDIR_PATH] [NEED_PREPROCESS] [DEVICE_ID] [CONFIG_PATH]
```
- `MINDIR_PATH` means the path of mindir file.
- `NEED_PREPROCESS` means weather need preprocess or not, it's value is 'y' or 'n', if you choose y, the cifar10 dataset will be processed in bin format, the imagenet2012 dataset will generate label json file.
- `DEVICE_ID` is optional, default value is 0.
### result
Inference result is saved in current path, you can find result like this in acc.log file.
```text
ODS=0.773, OIS=0.793, AP=0.800, R50=0.906 - HED
```
## [Model Description](#contents)
### [Performance](#contents)
#### Training Performance
#### Evaluation Performance
| Parameters | HED(Ascend) |HED(GPU)|
| ------------------- |--------------------- |------|
| Model Version | HED |HED|
| Resource | Ascend |GPU|
| Uploaded Date | 30/12/2021 |15/4/2022|
| MindSpore Version | 1.5.0 |1.5.0|
| Dataset |BSDS500, 500 images |BSDS500, 500 images |
| batch_size | 10 |10 |
| outputs | ODS | ODS |
| Accuracy | 77.3% |77.3% |
## [Description of Random Situation](#contents)
In dataset.py, we set the seed inside "create_dataset" function. We also use random seed in train.py.
## [ModelZoo Homepage](#contents)
Please check the official [homepage](https://gitee.com/mindspore/models).
cmake_minimum_required(VERSION 3.14.1)
project(Ascend310Infer)
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -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)
WARNING: Ignoring invalid distribution -umpy (/home/miniconda/envs/310/lib/python3.7/site-packages)
-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/local/gcc/bin/gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/local/gcc/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/stu/cyf/415/ascend310_infer/out
Scanning dependencies of target main
[ 33%] Building CXX object CMakeFiles/main.dir/src/main.cc.o
[ 66%] Building CXX object CMakeFiles/main.dir/src/utils.cc.o
[100%] Linking CXX executable main
[100%] Built target main
#!/bin/bash
# Copyright 2022 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 2022 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);
int WriteResultSingle(const std::string& imageFile, const mindspore::MSTensor &output);
#endif
/**
* Copyright 2022 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/execute.h"
#include "include/dataset/vision.h"
#include "inc/utils.h"
using mindspore::Context;
using mindspore::Serialization;
using mindspore::Model;
using mindspore::Status;
using mindspore::MSTensor;
using mindspore::dataset::Execute;
using mindspore::ModelType;
using mindspore::GraphCell;
using mindspore::kSuccess;
DEFINE_string(mindir_path, "", "mindir path");
DEFINE_string(input0_path, ".", "input0 path");
DEFINE_int32(device_id, 0, "device id");
int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (RealPath(FLAGS_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(FLAGS_device_id);
context->MutableDeviceInfo().push_back(ascend310);
mindspore::Graph graph;
Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph);
Model model;
Status ret = model.Build(GraphCell(graph), context);
if (ret != kSuccess) {
std::cout << "ERROR: Build failed." << std::endl;
return 1;
}
std::vector<MSTensor> model_inputs = model.GetInputs();
if (model_inputs.empty()) {
std::cout << "Invalid model, inputs is empty." << std::endl;
return 1;
}
auto input0_files = GetAllFiles(FLAGS_input0_path);
if (input0_files.empty()) {
std::cout << "ERROR: input data empty." << std::endl;
return 1;
}
std::map<double, double> costTime_map;
size_t size = input0_files.size();
for (size_t i = 0; i < size; ++i) {
struct timeval start = {0};
struct timeval end = {0};
double startTimeMs;
double endTimeMs;
std::vector<MSTensor> inputs;
std::vector<MSTensor> outputs;
std::cout << "Start predict input files:" << input0_files[i] << std::endl;
auto input0 = ReadFileToTensor(input0_files[i]);
inputs.emplace_back(model_inputs[0].Name(), model_inputs[0].DataType(), model_inputs[0].Shape(),
input0.Data().get(), input0.DataSize());
gettimeofday(&start, nullptr);
ret = model.Predict(inputs, &outputs);
gettimeofday(&end, nullptr);
if (ret != kSuccess) {
std::cout << "Predict " << input0_files[i] << " failed." << std::endl;
return 1;
}
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));
// WriteResult(input0_files[i], outputs);
WriteResultSingle(input0_files[i], outputs[5]);
}
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 2022 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::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";
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);
std::string outFileName = homePath + "/" + fileName;
FILE * outputFile = fopen(outFileName.c_str(), "wb");
fwrite(netOutput.get(), outputSize, sizeof(char), outputFile);
fclose(outputFile);
outputFile = nullptr;
}
return 0;
}
int WriteResultSingle(const std::string& imageFile, const MSTensor &output) {
std::string homePath = "./result_Files";
size_t outputSize;
std::shared_ptr<const void> netOutput;
netOutput = output.Data();
outputSize = output.DataSize();
int pos = imageFile.rfind('/');
std::string fileName(imageFile, pos + 1);
std::string outFileName = homePath + "/" + fileName;
FILE * outputFile = fopen(outFileName.c_str(), "wb");
fwrite(netOutput.get(), outputSize, sizeof(char), outputFile);
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;
}
# Builtin Configurations(DO NOT CHANGE THESE CONFIGURATIONS unless you know exactly what you are doing)
enable_modelarts: False
# Url for modelarts
data_url: ""
vgg_ckpt_url: ""
train_url: ""
checkpoint_url: ""
# Path for local
vgg_ckpt_path: "/cache/vggckpt_path"
output_path: "/home/stu/cyf/415/"
data_path: "/home/stu/cyf/BSR"
load_path: "/cache/checkpoint_path"
cxx_path: "/home/stu/cyf/415/src/cxx"
device_target: "Ascend"
need_modelarts_dataset_unzip: False
modelarts_dataset_unzip_name: ""
# ==============================================================================
# Train options
lr: 1.5e-3
epoch_size: 600
weight_decay: 0.0002
momentum: 0.9
distribute: 1
rank: 0
para_workers: 24
save_checkpoint_epochs: 100
keep_checkpoint_max: 5
# Eval options
log_path: "outputs/"
ckpt_path: './ckpt'
res_output_path : '/home/stu/cyf/415/res'
# Test option
ckpt_p: "hed_mindspore-500_200.ckpt"
alg: "HED"
show_plot: False
model_name_list: "hed"
result_dir: ""
save_dir: ""
gt_dir: ""
key: "result"
file_format_eval: ".mat"
workers: -1
# Export options
testing_shape: [321, 481]
device_id: 0
batch_size: 1
ckpt_file: "/home/cyf/hed_mindspore-600_20.ckpt"
file_name: "hed"
file_format: "MINDIR"
#310 infer-related
pre_result_path: './preprocess_Result'
post_result_path: './result_Files'
---
# Help description for each configuration
# Train options
lr: "learning rate for hed"
\ No newline at end of file
# Builtin Configurations(DO NOT CHANGE THESE CONFIGURATIONS unless you know exactly what you are doing)
enable_modelarts: False
# Url for modelarts
data_url: ""
vgg_ckpt_url: ""
train_url: ""
checkpoint_url: ""
# Path for local
data_path: "/home/cyf/BSR"
output_path: "./outputs"
load_path: "/home/cyf/hed/LOG0/outputs/ckpt_0/hed_mindspore-500_20.ckpt"
vgg_ckpt_path: "/home/cyf/vgg16.ckpt"
cxx_path: "/cache/cxx"
device_target: "Ascend"
need_modelarts_dataset_unzip: False
modelarts_dataset_unzip_name: ""
vgg16_caffe: ""
# ==============================================================================
# Train options
lr: 1.5e-3
epoch_size: 500
weight_decay: 0.0002
momentum: 0.9
ckpt_path: "outputs/"
distribute: 0
rank: 0
para_workers: 24
save_checkpoint_epochs: 10
keep_checkpoint_max: 5
# Eval options
log_path: "outputs/"
ckpt_path: './ckpt'
save_checkpoint_path: "/cache/train/checkpoint"
save_checkpoint_path_dis: "./ckpt"
res_output_path : './hed'
# Test option
ckpt_p: "hed_mindspore-500_200.ckpt"
alg: "HED"
model_name_list: "hed"
result_dir: ""
save_dir: ""
gt_dir: ""
key: "result"
file_format_eval: ".mat"
workers: -1
# Export options
device_id: 0
batch_size: 10
ckpt_file: ""
file_format: "AIR"
img_id_file_path: ""
result_files: './result_Files'
---
# Help description for each configuration
# Train options
data_dir: "Train dataset directory."
per_batch_size: "Batch size for Training."
pretrained_backbone: "The ckpt file of CspDarkNet53."
resume_yolov4: "The ckpt file of YOLOv4, which used to fine tune."
pretrained_checkpoint: "The ckpt file of YoloV4CspDarkNet53."
filter_weight: "Filter the last weight parameters"
lr_scheduler: "Learning rate scheduler, options: exponential, cosine_annealing."
lr: "Learning rate."
lr_epochs: "Epoch of changing of lr changing, split with ','."
lr_gamma: "Decrease lr by a factor of exponential lr_scheduler."
eta_min: "Eta_min in cosine_annealing scheduler."
t_max: "T-max in cosine_annealing scheduler."
max_epoch: "Max epoch num to train the model."
warmup_epochs: "Warmup epochs."
weight_decay: "Weight decay factor."
momentum: "Momentum."
loss_scale: "Static loss scale."
label_smooth: "Whether to use label smooth in CE."
label_smooth_factor: "Smooth strength of original one-hot."
log_interval: "Logging interval steps."
ckpt_path: "Checkpoint save location."
ckpt_interval: "Save checkpoint interval."
is_save_on_master: "Save ckpt on master or all rank, 1 for master, 0 for all ranks."
is_distributed: "Distribute train or not, 1 for yes, 0 for no."
rank: "Local rank of distributed."
group_size: "World size of device."
need_profiler: "Whether use profiler. 0 for no, 1 for yes."
training_shape: "Fix training shape."
resize_rate: "Resize rate for multi-scale training."
run_eval: "Run evaluation when training."
save_best_ckpt: "Save best checkpoint when run_eval is True."
eval_start_epoch: "Evaluation start epoch when run_eval is True."
eval_interval: "Evaluation interval when run_eval is True"
ann_file: "path to annotation"
each_multiscale: "Apply multi-scale for each scale"
detect_head_loss_coff: "the loss coefficient of detect head.
The order of coefficients is large head, medium head and small head"
bbox_class_loss_coff: "bbox and class loss coefficient.
The order of coefficients is ciou loss, confidence loss and class loss"
labels: "the label of train data"
mosaic: "use mosaic data augment"
multi_label: "use multi label to nms"
multi_label_thresh: "multi label thresh"
# Eval options
pretrained: "model_path, local pretrained model to load"
log_path: "checkpoint save location"
ann_val_file: "path to annotation"
# Export options
device_id: "Device id for export"
batch_size: "batch size for export"
testing_shape: "shape for test"
ckpt_file: "Checkpoint file path for export"
file_name: "output file name for export"
file_format: "file format for export"
keep_detect: "keep the detect module or not, default: True"
img_id_file_path: 'path of image dataset'
result_files: 'path to 310 infer result floder'
# Builtin Configurations(DO NOT CHANGE THESE CONFIGURATIONS unless you know exactly what you are doing)
enable_modelarts: False
# Url for modelarts
data_url: ""
vgg_ckpt_url: ""
train_url: ""
checkpoint_url: ""
# Path for local
data_path: "/DATA_1/cyf/hed/BSR"
output_path: "./outputs"
load_path: "/DATA_1/cyf/hed/111/hed_pr/hed/LOG5/outputs/ckpt_0/hed_mindspore-500_20.ckpt"
vgg_ckpt_path: "/DATA_1/cyf/hed/BSR/vggckpt/vgg16.ckpt"
cxx_path: "/cache/cxx"
device_target: "GPU"
need_modelarts_dataset_unzip: False
modelarts_dataset_unzip_name: ""
vgg16_caffe: ""
# ==============================================================================
# Train options
lr: 1.5e-3
epoch_size: 500
weight_decay: 0.0002
momentum: 0.9
ckpt_path: "outputs/"
distribute: 0
rank: 0
para_workers: 24
save_checkpoint_epochs: 10
keep_checkpoint_max: 5
# Eval options
log_path: "outputs/"
ckpt_path: './ckpt'
save_checkpoint_path: "/cache/train/checkpoint"
save_checkpoint_path_dis: "./ckpt"
res_output_path : './hed'
# Test option
ckpt_p: "hed_mindspore-500_200.ckpt"
alg: "HED"
model_name_list: "hed"
result_dir: ""
save_dir: ""
gt_dir: ""
key: "result"
file_format_eval: ".mat"
workers: -1
# Export options
device_id: 0
batch_size: 10
ckpt_file: ""
file_format: "AIR"
img_id_file_path: ""
result_files: './result_Files'
---
# Help description for each configuration
# Train options
data_dir: "Train dataset directory."
per_batch_size: "Batch size for Training."
pretrained_backbone: "The ckpt file of CspDarkNet53."
resume_yolov4: "The ckpt file of YOLOv4, which used to fine tune."
pretrained_checkpoint: "The ckpt file of YoloV4CspDarkNet53."
filter_weight: "Filter the last weight parameters"
lr_scheduler: "Learning rate scheduler, options: exponential, cosine_annealing."
lr: "Learning rate."
lr_epochs: "Epoch of changing of lr changing, split with ','."
lr_gamma: "Decrease lr by a factor of exponential lr_scheduler."
eta_min: "Eta_min in cosine_annealing scheduler."
t_max: "T-max in cosine_annealing scheduler."
max_epoch: "Max epoch num to train the model."
warmup_epochs: "Warmup epochs."
weight_decay: "Weight decay factor."
momentum: "Momentum."
loss_scale: "Static loss scale."
label_smooth: "Whether to use label smooth in CE."
label_smooth_factor: "Smooth strength of original one-hot."
log_interval: "Logging interval steps."
ckpt_path: "Checkpoint save location."
ckpt_interval: "Save checkpoint interval."
is_save_on_master: "Save ckpt on master or all rank, 1 for master, 0 for all ranks."
is_distributed: "Distribute train or not, 1 for yes, 0 for no."
rank: "Local rank of distributed."
group_size: "World size of device."
need_profiler: "Whether use profiler. 0 for no, 1 for yes."
training_shape: "Fix training shape."
resize_rate: "Resize rate for multi-scale training."
run_eval: "Run evaluation when training."
save_best_ckpt: "Save best checkpoint when run_eval is True."
eval_start_epoch: "Evaluation start epoch when run_eval is True."
eval_interval: "Evaluation interval when run_eval is True"
ann_file: "path to annotation"
each_multiscale: "Apply multi-scale for each scale"
detect_head_loss_coff: "the loss coefficient of detect head.
The order of coefficients is large head, medium head and small head"
bbox_class_loss_coff: "bbox and class loss coefficient.
The order of coefficients is ciou loss, confidence loss and class loss"
labels: "the label of train data"
mosaic: "use mosaic data augment"
multi_label: "use multi label to nms"
multi_label_thresh: "multi label thresh"
# Eval options
pretrained: "model_path, local pretrained model to load"
log_path: "checkpoint save location"
ann_val_file: "path to annotation"
# Export options
device_id: "Device id for export"
batch_size: "batch size for export"
testing_shape: "shape for test"
ckpt_file: "Checkpoint file path for export"
file_name: "output file name for export"
file_format: "file format for export"
keep_detect: "keep the detect module or not, default: True"
img_id_file_path: 'path of image dataset'
result_files: 'path to 310 infer result floder'
# Copyright 2022 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'''
import os
from mindspore.train.serialization import load_checkpoint, load_param_into_net
import mindspore.ops as ops
from mindspore import context
import scipy.io
from src.dataset import create_dataset
from src.model import HED
from src.model_utils.config import config
from src.model_utils.moxing_adapter import moxing_wrapper
def modelarts_pre_process():
'''modelarts pre process function.'''
def unzip(zip_file, save_dir):
import zipfile
s_time = time.time()
if not os.path.exists(os.path.join(save_dir, config.modelarts_dataset_unzip_name)):
zip_isexist = zipfile.is_zipfile(zip_file)
if zip_isexist:
fz = zipfile.ZipFile(zip_file, 'r')
data_num = len(fz.namelist())
print("Extract Start...")
print("unzip file num: {}".format(data_num))
data_print = int(data_num / 100) if data_num > 100 else 1
i = 0
for file in fz.namelist():
if i % data_print == 0:
print("unzip percent: {}%".format(int(i * 100 / data_num)), flush=True)
i += 1
fz.extract(file, save_dir)
print("cost time: {}min:{}s.".format(int((time.time() - s_time) / 60),
int(int(time.time() - s_time) % 60)))
print("Extract Done.")
else:
print("This is not zip.")
else:
print("Zip has been extracted.")
if config.need_modelarts_dataset_unzip:
zip_file_1 = os.path.join(config.data_path, config.modelarts_dataset_unzip_name + ".zip")
save_dir_1 = os.path.join(config.data_path)
sync_lock = "/tmp/unzip_sync.lock"
# Each server contains 8 devices as most.
if get_device_id() % min(get_device_num(), 8) == 0 and not os.path.exists(sync_lock):
print("Zip file path: ", zip_file_1)
print("Unzip file save dir: ", save_dir_1)
unzip(zip_file_1, save_dir_1)
print("===Finish extract data synchronization===")
try:
os.mknod(sync_lock)
except IOError:
pass
while True:
if os.path.exists(sync_lock):
break
time.sleep(1)
print("Device: {}, Finish sync unzip data from {} to {}.".format(get_device_id(), zip_file_1, save_dir_1))
def get_files(folder, name_filter=None, extension_filter=None):
'''get file'''
if not os.path.isdir(folder):
raise RuntimeError("\"{0}\" is not a folder.".format(folder))
if name_filter is None:
def name_cond(filename):
return True
else:
def name_cond(filename):
return name_filter in filename
if extension_filter is None:
def ext_cond(filename):
return True
else:
def ext_cond(filename):
return filename.endswith(extension_filter)
filtered_files = []
for path, _, files in os.walk(folder):
files.sort()
for file in files:
if name_cond(file) and ext_cond(file):
full_path = os.path.join(path, file)
filtered_files.append(full_path)
return filtered_files
@moxing_wrapper(pre_process=modelarts_pre_process)
def test_hed():
'''test'''
device_id = int(os.getenv('DEVICE_ID'))
context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target,
save_graphs=False, device_id=device_id)
net = HED()
train_path = os.path.join(config.data_path, 'output/train.lst')
test_path = os.path.join(config.data_path, 'output/test.lst')
val_path = os.path.join(config.data_path, 'output/val.lst')
img_extension = '.jpg'
lbl_extension = '.jpg'
train_img = get_files(os.path.join(config.data_path, 'BSDS500/data/images/train'),
extension_filter=img_extension)
train_label = get_files(os.path.join(config.data_path, 'BSDS500/data/labels/train'),
extension_filter=lbl_extension)
test_img = get_files(os.path.join(config.data_path, 'BSDS500/data/images/test'),
extension_filter=img_extension)
test_label = get_files(os.path.join(config.data_path, 'BSDS500/data/labels/test'),
extension_filter=lbl_extension)
val_img = get_files(os.path.join(config.data_path, 'BSDS500/data/images/val'),
extension_filter=img_extension)
val_label = get_files(os.path.join(config.data_path, 'BSDS500/data/labels/val'),
extension_filter=lbl_extension)
f = open(train_path, "w")
for img, label in zip(train_img, train_label):
f.write(str(img) + " " + str(label))
f.write('\n')
f.close()
f = open(test_path, "w")
for img, label in zip(test_img, test_label):
f.write(str(img) + " " + str(label))
f.write('\n')
f = open(val_path, "w")
for img, label in zip(val_img, val_label):
f.write(str(img) + " " + str(label))
f.write('\n')
f.close()
test_loader = create_dataset(test_path, is_training=False, is_shuffle=False)
print("evaluation image number:", test_loader.get_dataset_size())
net = HED()
# load HED ckpt
param_dict = load_checkpoint(config.load_path)
load_param_into_net(net, param_dict)
print("load hed success!")
idx = 0
# test.lst路径
with open(test_path, 'r') as f:
test_list = f.readlines()
test_list = [i.rstrip() for i in test_list]
result_path = os.path.join(config.res_output_path, 'result/hed_result')
if not os.path.exists(result_path):
for data in test_loader.create_dict_iterator():
results = net(data['test'])
squeeze = ops.Squeeze()
# fuse
result = squeeze(results[-1])
result = result.asnumpy()
filename, _ = test_list[idx].split()
filename = filename.split('/')[-1]
filename = filename.split('.')[0]
print(filename)
if not os.path.exists(result_path):
os.makedirs(result_path)
try:
result_path_mat = os.path.join(result_path, "{}.mat".format(filename))
scipy.io.savemat(result_path_mat, {'result': result})
except OSError:
pass
print("running test [%d]" % (idx + 1))
idx += 1
print("begin test...")
else:
print("begin test...")
def main():
test_hed()
config.result_dir = os.path.join(config.res_output_path, 'result/hed_result')
config.save_dir = os.path.join(config.res_output_path, 'result/hed_eval_result')
config.gt_dir = os.path.join(config.data_path, 'BSDS500/data/groundTruth/test')
alg = [config.alg] # algorithms for plotting
model_name_list = [config.model_name_list] # model name
result_dir = os.path.abspath(config.result_dir) # forward result directory
save_dir = os.path.abspath(config.save_dir) # nms result directory
gt_dir = os.path.abspath(config.gt_dir) # ground truth directory
key = config.key # x = scipy.io.loadmat(filename)[key]
file_format_eval = config.file_format_eval # ".mat" or ".npy"
workers = config.workers # number workers
nms_process(model_name_list, result_dir, save_dir, key, file_format_eval)
eval_edge(alg, model_name_list, save_dir, gt_dir, workers)
if __name__ == '__main__':
from src.nms_process import nms_process
from src.eval_edge import eval_edge
main()
# Copyright 2022 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'''
import os
import numpy as np
import mindspore
from mindspore import context, Tensor
from mindspore.train.serialization import export, load_checkpoint, load_param_into_net
from src.model import HED
from src.model_utils.config import config
from src.model_utils.moxing_adapter import moxing_wrapper
def modelarts_pre_process():
'''modelarts pre process function.'''
config.file_name = os.path.join(config.output_path, config.file_name)
@moxing_wrapper(pre_process=modelarts_pre_process)
def run_export():
'''run export.'''
context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
if config.device_target == "Ascend":
context.set_context(device_id=config.device_id)
h, w = config.testing_shape
network = HED()
network.set_train(False)
param_dict = load_checkpoint(config.ckpt_file)
load_param_into_net(network, param_dict)
input_data = Tensor(np.zeros([config.batch_size, 3, h, w]), mindspore.float32)
export(network, input_data, file_name=config.file_name, file_format=config.file_format)
if __name__ == "__main__":
run_export()
# Copyright 2022 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.
# ============================================================================
"""hed 310 infer."""
import os
from src.model_utils.config import config
def get_acc():
'''get acc'''
config.result_dir = config.post_result_path
config.save_dir = os.path.join(config.res_output_path, 'result/hed_eval_result')
config.gt_dir = os.path.join(config.data_path, 'BSDS500/data/groundTruth/test')
alg = [config.alg] # algorithms for plotting
model_name_list = [config.model_name_list] # model name
result_dir = os.path.abspath(config.result_dir) # forward result directory
save_dir = os.path.abspath(config.save_dir) # nms result directory
gt_dir = os.path.abspath(config.gt_dir) # ground truth directory
workers = config.workers # number workers
nms_process(model_name_list, result_dir, save_dir, file_format=".bin")
eval_edge(alg, model_name_list, save_dir, gt_dir, workers)
if __name__ == "__main__":
from src.nms_process import nms_process
from src.eval_edge import eval_edge
get_acc()
# Copyright 2022 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.
# ============================================================================
'''BSDS500'''
import os
import errno
import imageio
from scipy import io
import numpy as np
from src.model_utils.config import config
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: raise
GT_PATH = os.path.join(os.path.join(config.data_path, 'BSDS500/data/groundTruth'))
label_train_path = os.path.join(config.data_path, 'BSDS500/data/labels/train/')
if not os.path.isdir(label_train_path):
os.makedirs(label_train_path)
train_lst_path = os.path.join(os.path.join(config.data_path, 'output/train.lst'))
if not os.path.isdir(train_lst_path):
mkdir_p(os.path.dirname(train_lst_path))
test_lst_path = os.path.join(os.path.join(config.data_path, 'output/test.lst'))
if not os.path.isdir(test_lst_path):
mkdir_p(os.path.dirname(test_lst_path))
val_lst_path = os.path.join(os.path.join(config.data_path, 'output/val.lst'))
if not os.path.isdir(val_lst_path):
mkdir_p(os.path.dirname(val_lst_path))
train_list = os.listdir(GT_PATH+'/train/')
print(len(train_list))
for index in train_list:
name = index.split('.')[0]
print(name)
train = io.loadmat(GT_PATH+'/train/'+index)
a = np.array(1024)
a = train['groundTruth'][0][0][0][0][1]
print(a)
a = a*255
print(label_train_path+str(name))
imageio.imsave(label_train_path+str(name)+'.jpg', a)
label_test_path = os.path.join(config.data_path, 'BSDS500/data/labels/test/')
if not os.path.isdir(label_test_path):
os.makedirs(label_test_path)
test_list = os.listdir(GT_PATH+'/test/')
print(len(test_list))
for index in test_list:
name = index.split('.')[0]
print(name)
test = io.loadmat(GT_PATH+'/test/'+index)
#print(train)
a = np.array(1024)
a = test['groundTruth'][0][0][0][0][1]
print(a)
a = a*255
print(label_test_path+str(name))
imageio.imsave(label_test_path+str(name)+'.jpg', a)
label_val_path = os.path.join(config.data_path, 'BSDS500/data/labels/val/')
if not os.path.isdir(label_val_path):
os.makedirs(label_val_path)
val_list = os.listdir(GT_PATH+'/val/')
print(len(val_list))
for index in val_list:
name = index.split('.')[0]
print(name)
val = io.loadmat(GT_PATH+'/val/'+index)
#print(train)
a = np.array(1024)
a = val['groundTruth'][0][0][0][0][1]
print(a)
a = a*255
print(label_val_path+str(name))
imageio.imsave(label_val_path+str(name)+'.jpg', a)
# Copyright 2022 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 os
import numpy as np
from eval import get_files
from src.dataset import create_dataset
from src.model_utils.config import config
def get_bin():
'''generate bin files.'''
test_path = os.path.join(config.data_path, 'output/test.lst')
test_img = get_files(os.path.join(config.data_path, 'BSDS500/data/images/test'),
extension_filter='.jpg')
test_label = get_files(os.path.join(config.data_path, 'BSDS500/data/labels/test'),
extension_filter='.jpg')
f = open(test_path, "w")
for img, label in zip(test_img, test_label):
f.write(str(img) + " " + str(label))
f.write('\n')
# test.lst路径
with open(test_path, 'r') as f:
test_list = f.readlines()
test_list = [i.rstrip() for i in test_list]
test_dataset = create_dataset(test_path, is_training=False, is_shuffle=False)
print("evaluation image number:", test_dataset.get_dataset_size())
test_loader = test_dataset.create_dict_iterator(num_epochs=1, output_numpy=True)
print("Dataset creation Done!")
bin_images_path = os.path.join(config.pre_result_path, "00_images")
if not os.path.exists(bin_images_path):
os.makedirs(bin_images_path)
for idx, data in enumerate(test_loader):
images = data['test'].astype(np.float32)
filename, _ = test_list[idx].split()
filename = filename.split('/')[-1]
filename = filename.split('.')[0]
print(filename)
try:
result_path_bin = os.path.join(bin_images_path, "{}.bin".format(filename))
images.tofile(result_path_bin)
except OSError:
pass
print("=" * 10, "export input bin files finished.", "=" * 10)
if __name__ == '__main__':
get_bin()
numpy
opencv-python
pyyaml
pillow
pycocotools
```bash
git clone https://github.com/Walstruzz/edge_eval_python.git
cd edge_eval_python/
cp -R impl ../src
cp eval_edge.py ../src
cp nms_process.py ../src
in edges_eval_plot.py:
delete line 71-73 and 78
in edges_eval_dir.py:
change line 10: from src.impl.bwmorph_thin import bwmorph_thin
change line 11: from src.impl.correspond_pixels import correspond_pixels
after line 50(for g in gt:) add:
h, w = g.shape
if h > w:
g = np.flip(g,axis=1)
g = np.transpose(g)
after line 78(for _g in _gt:) add:
h, w = _g.shape
if h > w:
_g = np.flip(_g,axis=1)
_g = np.transpose(_g)
delete line 105-107(from if to return) and add:
np.savetxt(out, info, fmt="%10g")
in nms_process.py:
add in line 5: from src.model_utils.config import config
change line 6: from src.impl.toolbox import conv_tri, grad2
change line 16: solver = cdll.LoadLibrary(os.path.join(config.cxx_path, "lib/solve_csa.so"))
change line 53: assert file_format in {".mat", ".npy", ".bin"}
after line 72(image = np.load(abs_path)) add:
elif file_format == ".bin":
image = np.fromfile(abs_path, dtype=np.float32).reshape(321, 481)
in eval_edge.py:
change line 4: from src.impl.edges_eval_dir import edges_eval_dir
change line 5: from src.impl.edges_eval_plot import edges_eval_plot
in correspond_pixels.py:
add in line 5: from src.model_utils.config import config
add in line 10: import os
change line 11: solver = cdll.LoadLibrary(os.path.join(config.cxx_path, "lib/solve_csa.so"))
cp -R cxx ../src
```
#!/bin/bash
# Copyright 2022 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: bash run_infer_310.sh [MINDIR_PATH] [NEED_PREPROCESS] [DEVICE_ID]
DEVICE_TARGET must choose from ['GPU', 'CPU', 'Ascend']
NEED_PREPROCESS means weather need preprocess or not, it's value is 'y' or 'n'.
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" == "y" ] || [ "$2" == "n" ];then
need_preprocess=$2
else
echo "weather need preprocess or not, it's value must be in [y, n]"
exit 1
fi
device_id=0
if [ $# == 3 ]; then
device_id=$3
fi
echo "mindir name: "$model
echo "need preprocess: "$need_preprocess
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()
{
echo "preprocess_data doing..."
if [ -d preprocess_Result ]; then
rm -rf ./preprocess_Result
fi
mkdir preprocess_Result
python ../preprocess.py
echo "preprocess_data done."
}
function compile_app()
{
echo "compile_app doing..."
cd ../ascend310_infer || exit
bash build.sh &> build.log
echo "compile_app done."
}
function infer()
{
echo "infer doing..."
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 --input0_path=./preprocess_Result/00_images --device_id=$device_id &> infer.log
echo "infer done."
}
function cal_acc()
{
echo "cal_acc doing"
python ../postprocess.py &> acc.log
echo "cal_acc done."
}
if [ $need_preprocess == "y" ]; then
preprocess_data
if [ $? -ne 0 ]; then
echo "preprocess dataset failed"
exit 1
fi
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 2022 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
# 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.
# ============================================================================
echo "=============================================================================================================="
echo "Please run the script as: "
echo "sh run_single_eval.sh DEVICE_ID CONFIG_PATH"
echo "for example: sh run_single_eval.sh 0 home/hed/config/default_config_910.yaml"
echo "It is better to use absolute path."
echo "================================================================================================================="
if [ $# != 2 ]
then
echo "Usage: sh run_single_eval.sh [DEVICE_ID] [CONFIG_PATH]"
exit 1
fi
echo "After running the script, the network runs in the background. The log will be generated in LOGx/log.txt"
export DEVICE_ID=$1
CONFIG_PATH=$2
rm -rf evalLOG$1
mkdir ./evalLOG$1
cp ./*.py ./evalLOG$1
cp -r ./src ./evalLOG$1
cd ./evalLOG$1 || exit
echo "start eval for device $1"
env > env.log
python eval.py \
--device_id=$DEVICE_ID \
--config_path=$CONFIG_PATH \
--alg "HED" \
--model_name_list "hed" \
--result_dir result/hed_result \
--save_dir result/hed_eval_result \
--gt_dir /disk3/dataset/BSR/BSDS500/data/groundTruth/test \
--key result \
--file_format .mat \
--workers -1 > log.txt 2>&1 &
cd ../
#!/bin/bash
# Copyright 2022 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
# 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.
# ============================================================================
echo "=============================================================================================================="
echo "Please run the script as: "
echo "bash run_single_train.sh DEVICE_ID CONFIG_PATH"
echo "for example: sh run_single_train.sh 0 home/hed/config/default_config_910.yaml"
echo "It is better to use absolute path."
echo "================================================================================================================="
if [ $# != 2 ]
then
echo "Usage: bash run_single_train.sh [DEVICE_ID] [CONFIG_PATH]"
exit 1
fi
echo "After running the script, the network runs in the background. The log will be generated in LOGx/log.txt"
export DEVICE_ID=$1
CONFIG_PATH=$2
rm -rf LOG$1
mkdir ./LOG$1
cp ./*.py ./LOG$1
cp -r ./src ./LOG$1
cp ./config/*.yaml ./LOG$1
cd ./LOG$1 || exit
echo "start training for device $1"
env > env.log
python train.py \
--config_path=$CONFIG_PATH \
--device_id=$DEVICE_ID > log.txt 2>&1 &
cd ../
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