Skip to content
Snippets Groups Projects
Commit 48f30e33 authored by i-robot's avatar i-robot Committed by Ren Wenhao
Browse files

!759 fix resnet101 accuracy

Merge pull request !759 from panfengfeng/fix_reset101_master
parents 7f9e57fd e6a947fa
No related merge requests found
......@@ -12,6 +12,7 @@
- [Training](#training)
- [Evaluation Process](#evaluation-process)
- [Evaluation](#evaluation)
- [Ascend310 infer](#evaluation-process)
- [Model Description](#model-description)
- [Performance](#performance)
- [Evaluation Performance](#evaluation-performance)
......@@ -90,10 +91,16 @@ After installing mindspree through the official website, you can follow the foll
```python
├── SiamFC
├── README.md // Notes on siamfc
├── ascend310_infer // Implementation inference script on ascend310
├──inc //Head file
├──src //Main.cc and utils.cc file
├──build.sh //Build file
├──CMakeLists.txt //Required library files
├── scripts
├──ma-pre-start.sh // Create environment before modelarts training
├──run_standalone_train_ascend.sh // Single card training in ascend
├──run_distribution_ascend.sh // Multi card distributed training in ascend
├──run_infer_310.sh //310infer scripts
├── src
├──alexnet.py // Create dataset
├──config.py // Alexnet architecture
......@@ -175,6 +182,22 @@ Check the checkpoint path used for evaluation before running the following comma
SiamFC_159_50_6650.ckpt -prec_score:0.777 -succ_score:0.589 _succ_rate:0.754
```
## Ascend310 infer
Check the checkpoint path used for evaluation before running the following command.
Run this reference scripts need two different MINDIR
```bash
python export.py --device_id=${DEVICE_ID} --model_path=${MODEL_PATH} --file_name_export1=${SAVE_MODEL_PATH1} --file_name_export2=${SAVE_MODEL_PATH2} --file_name=${FILE_FORMAT} --device_target=${DEVICE_TARGET}
```
- Running in ascend310 device processor environment
```bash
bash run_infer_310.sh [MODEL_PATH1] [MODEL_PATH2] [DATASET_PATH] [CODE_PATH] [DEVICE_TARGET] [DEVICE_ID]
```
# [Model description](#Contents)
## performance
......@@ -193,3 +216,18 @@ Check the checkpoint path used for evaluation before running the following comma
|total time |about 5 hours |
|Script URL |<https://gitee.com/mindspore/models/tree/master/research/cv/SiamFC> |
|Random number seed |set_seed = 1234 |
## performance
### Inference on Ascend310 Performance
|parameter | Ascend |
| -------------------------- | --------------------------------------------|
|Model Version | SiamFC |
|Upload date |2021.11.1 |
|mindspore version |mindspore1.3.0 |
|Dataset | OTB2013 |
|total time |about 5 minutes |
|outputs |probability |
|Accuracy |prec_score:0.779 -succ_score:0.588 _succ_rate:0.756 |
cmake_minimum_required(VERSION 3.14.1)
project(Ascend310Infer)
find_package(OpenCV 2 REQUIRED)
find_package(gflags REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -g -std=c++17 -Werror -Wall -fPIE -Wl,--allow-shlib-undefined -D_GLIBCXX_USE_CXX11_ABI=0")
set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/)
option(MINDSPORE_PATH "mindspore install path" "")
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${MINDSPORE_PATH})
message(STATUS "mindsporelibs:${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} ${OpenCV_LIBS} 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
rm -rf out
fi
mkdir out
cd out || exit
if [ -f "Makefile" ]; then
make clean
fi
cmake .. \
-DMINDSPORE_PATH="`pip3.7 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 <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/types_c.h>
#include <sys/stat.h>
#include <dirent.h>
#include <vector>
#include <string>
#include <memory>
#include "include/api/types.h"
std::vector<std::string> GetAllFiles(const std::string_view& dirName,const std::string& seq_name);
DIR *OpenDir(const std::string_view& dirName);
std::string RealPath(const std::string_view& path);
mindspore::MSTensor ReadFileToTensor(const std::string &file);
int WriteResult(const std::string& imageFile, const std::vector<mindspore::MSTensor> &outputs);
cv::Mat BGRToRGB(cv::Mat img);
cv::Mat crop_and_pad(cv::Mat img, float cx, float cy, float size_z, float s_z);
std::vector<double> Getpos(const std::string &dirName);
float sumMat(cv::Mat& inputImg);
#endif
This diff is collapsed.
/**
* 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 <dirent.h>
#include <opencv2/imgproc/types_c.h>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/opencv.hpp>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
const int DAVID_DATA_SIZE = 471;
const int DAVID_DATA_BEGIN = 299;
const int DAVID_DATA_END = 770;
const int FOOTBALL_DATA_SIZE = 74;
const int FREEMAN3_DATA_SIZE = 460;
const int FREEMAN4_DATA_SIZE = 283;
const int DIVING_DATA_SIZE = 215;
using mindspore::DataType;
using mindspore::MSTensor;
using namespace std;
std::vector<std::string> GetAllFiles(const std::string_view& dirName,
const std::string& seq_name) {
struct dirent* filename;
string seqName = string(dirName) + "/" + seq_name + "/img";
DIR* dir = OpenDir(seqName);
if (dir == nullptr) {
cout << "no dir" << endl;
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(string(dirName) + "/" + seq_name + "/img/" +
filename->d_name);
}
std::sort(res.begin(), res.end());
std::vector<std::string> res_all;
if (seq_name == "David") {
res_all.resize(DAVID_DATA_SIZE);
std::copy(res.begin() + DAVID_DATA_BEGIN, res.begin() + DAVID_DATA_END,
res_all.begin());
} else if (seq_name == "Football1") {
res_all.resize(FOOTBALL_DATA_SIZE);
std::copy(res.begin(), res.begin() + FOOTBALL_DATA_SIZE, res_all.begin());
} else if (seq_name == "Freeman3") {
res_all.resize(FREEMAN3_DATA_SIZE);
std::copy(res.begin(), res.begin() + FREEMAN3_DATA_SIZE, res_all.begin());
} else if (seq_name == "Freeman4") {
res_all.resize(FREEMAN4_DATA_SIZE);
std::copy(res.begin(), res.begin() + FREEMAN4_DATA_SIZE, res_all.begin());
} else if (seq_name == "Diving") {
res_all.resize(FREEMAN4_DATA_SIZE);
std::copy(res.begin(), res.begin() + FREEMAN4_DATA_SIZE, res_all.begin());
} else {
for (size_t i = 0; i < res.size(); i++) {
res_all.emplace_back(res[i]);
}
}
return res_all;
}
std::vector<double> Getpos(const std::string& dirName) {
std::ifstream infile;
infile.open(dirName.c_str());
std::string s;
getline(infile, s);
std::stringstream ss;
ss << s;
double temp;
std::vector<double> data;
while (ss >> temp) {
data.push_back(temp);
if (ss.peek() == ',' || ss.peek() == ' ' || ss.peek() == '\t') {
ss.ignore();
}
}
infile.close();
return data;
}
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 = outputs[i].Data();
outputSize = outputs[i].DataSize();
int pos = imageFile.rfind('/');
std::string fileName(imageFile, pos + 1);
fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'),
'_' + std::to_string(i) + ".bin");
std::string outFileName = homePath + "/" + fileName;
FILE* outputFile = fopen(outFileName.c_str(), "wb");
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(const 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(const 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;
}
cv::Mat BGRToRGB(cv::Mat& img) {
cv::Mat image(img.rows, img.cols, CV_8UC3);
for (int i = 0; i < img.rows; ++i) {
cv::Vec3b* p1 = img.ptr<cv::Vec3b>(i);
cv::Vec3b* p2 = image.ptr<cv::Vec3b>(i);
for (int j = 0; j < img.cols; ++j) {
p2[j][2] = p1[j][0];
p2[j][1] = p1[j][1];
p2[j][0] = p1[j][2];
}
}
return image;
}
cv::Mat crop_and_pad(cv::Mat img, float cx, float cy, float size_z, float s_z) {
float xmin = cx - s_z / 2;
float xmax = cx + s_z / 2;
float ymin = cy - s_z / 2;
float ymax = cy + s_z / 2;
int w = img.cols;
int h = img.rows;
int left = 0;
int right = 0;
int top = 0;
int bottom = 0;
if (xmin < 0) left = static_cast<int>(abs(xmin));
if (xmax > w) right = static_cast<int>(xmax - w);
if (ymin < 0) top = static_cast<int>(abs(ymin));
if (ymax > h) bottom = static_cast<int>(ymax - h);
xmin = std::max(0, static_cast<int>(xmin));
xmax = std::min(w, static_cast<int>(xmax));
ymin = std::max(0, static_cast<int>(ymin));
ymax = std::min(h, static_cast<int>(ymax));
cv::Mat im_patch = img(cv::Range(ymin, ymax), cv::Range(xmin, xmax));
if (left != 0 || right != 0 || top != 0 || bottom != 0) {
cv::Scalar tempVal = cv::mean(img);
tempVal.val[0] = static_cast<int>(tempVal.val[0]);
tempVal.val[1] = static_cast<int>(tempVal.val[1]);
tempVal.val[2] = static_cast<int>(tempVal.val[2]);
cv::copyMakeBorder(im_patch, im_patch, top, bottom, left, right,
cv::BORDER_CONSTANT, tempVal);
}
if (size_z != s_z) {
cv::resize(im_patch, im_patch, cv::Size(size_z, size_z));
}
return im_patch;
}
float sumMat(cv::Mat& inputImg) {
float sum = 0.0;
int rowNumber = inputImg.rows;
int colNumber = inputImg.cols * inputImg.channels();
for (int i = 0; i < rowNumber; i++) {
uchar* data = inputImg.ptr<uchar>(i);
for (int j = 0; j < colNumber; j++) {
sum = data[j] + sum;
}
}
return sum;
}
......@@ -26,8 +26,9 @@ sys.path.append(os.getcwd())
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='siamfc tracking')
parser.add_argument('--device_id', type=int, default=0, help='device id of GPU or Ascend')
parser.add_argument('--model_path', default='/root/SiamFC/models/siamfc_{}.ckpt/SiamFC-6650.ckpt'
parser.add_argument('--device_id', type=int, default=7
, help='device id of GPU or Ascend')
parser.add_argument('--model_path', default='/root/models/siamfc_{}.ckpt/SiamFC_177-47_6650.ckpt'
, type=str, help='eval one special video')
parser.add_argument('--dataset_path', default='/root/datasets/OTB2013', type=str)
......
......@@ -19,14 +19,16 @@ import mindspore as ms
from mindspore import Tensor, context
from mindspore.train.serialization import load_checkpoint, export, load_param_into_net
from src.alexnet import SiameseAlexNet
parser = argparse.ArgumentParser(description='siamfc export')
parser.add_argument("--device_id", type=int, default=0, help="Device id")
parser.add_argument('--model_path', default='/root/HRBEU-MedAI/SiamFC/models/siamfc_{}.ckpt/',
type=str, help='eval one special video')
parser.add_argument('--file_name', type=str, default='/root/HRBEU-MedAI/SiamFC/models',
parser.add_argument("--device_id", type=int, default=7, help="Device id")
parser.add_argument('--model_path', default='/root/models/siamfc_{}.ckpt/SiamFC_177-47_6650.ckpt'
, type=str, help='eval one special video')
parser.add_argument('--file_name_export1', type=str, default='/root/SiamFC/models1',
help='SiamFc output file name.')
parser.add_argument('--file_name_export2', type=str, default='/root/SiamFC/models2',
help='SiamFc output file name.')
parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"], default='MINDIR',
help='file format')
parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"], default='MINDIR', help='file format')
parser.add_argument("--device_target", type=str, choices=["Ascend", "GPU", "CPU"], default="Ascend",
help="device target")
args = parser.parse_args()
......@@ -34,12 +36,19 @@ context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target)
if args.device_target == "Ascend":
context.set_context(device_id=args.device_id)
if __name__ == "__main__":
net = SiameseAlexNet(train=False)
load_param_into_net(net, load_checkpoint(args.model_path), strict_load=True)
net.set_train(False)
input_data_exemplar = Tensor(np.zeros([3, 256, 6, 6]), ms.float32)
input_data_instance = Tensor(np.zeros([3, 3, 255, 255]), ms.float32)
export(net, input_data_exemplar, input_data_instance, file_name=args.file_name,
file_format=args.file_format)
if __name__ == "__main__":
net1 = SiameseAlexNet(train=False)
load_param_into_net(net1, load_checkpoint(args.model_path), strict_load=True)
net1.set_train(False)
net2 = SiameseAlexNet(train=False)
load_param_into_net(net2, load_checkpoint(args.model_path), strict_load=True)
net2.set_train(False)
input_data_exemplar1 = Tensor(np.zeros([1, 3, 127, 127]), ms.float32)
input_data_instance1 = Tensor(np.zeros(1), ms.float32)
input_data_exemplar2 = Tensor(np.ones([1, 256, 6, 6]), ms.float32)
input_data_instance2 = Tensor(np.ones([1, 3, 255, 255]), ms.float32)
input1 = [input_data_exemplar1, input_data_instance1]
input2 = [input_data_exemplar2, input_data_instance2]
export(net1, *input1, file_name=args.file_name_export1, file_format=args.file_format)
export(net2, *input2, file_name=args.file_name_export2, file_format=args.file_format)
print("-- complete --")
\ No newline at end of file
# 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.
# ============================================================================
"""post process for 310 inference"""
from __future__ import absolute_import
import argparse
import os
import sys
from got10k.experiments import ExperimentOTB
from mindspore import context
from src import SiamFCTracker
sys.path.append(os.getcwd())
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='siamfc tracking')
parser.add_argument('--device_id', type=int, default=7
, help='device id of GPU or Ascend')
parser.add_argument('--dataset_path', default='/root/datasets/OTB2013', type=str)
args = parser.parse_args()
context.set_context(
mode=context.GRAPH_MODE,
device_id=args.device_id,
save_graphs=False,
device_target='Ascend')
root_dir = os.path.abspath(args.dataset_path)
e = ExperimentOTB(root_dir, version=2013)
prec_score = e.report(['SiamFC'])['SiamFC']['overall']
score = ['success_score', 'precision_score', 'success_rate']
mydic = []
for key in score:
mydic.append(prec_score[key])
ss = '-prec_score:%.3f -succ_score:%.3f -succ_rate:%.3f' % (float(mydic[1]),
float(mydic[0]),
float(mydic[2]))
print(ss)
\ No newline at end of file
......@@ -118,7 +118,14 @@ class SiameseAlexNet(nn.Cell):
score_map = n_p.transpose(score_map, (1, 0, 2, 3))
score_map = score_map*1e-3+self.corr_bias
score = self.loss(score_map, self.train_gt)/8
elif x.shape.as_list()[0] == 1 :
exemplar = x
instance = y
if exemplar.size is not None and instance.size == 1:
exemplar = self.seq(exemplar)
return exemplar
instance = self.seq(instance)
score = self.Conv2D_1(instance, exemplar)
else:
exemplar = x
instance = y
......
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