forked from huawei/mindspore2022
ascend310 inferenct for resnet50,resnet101
This commit is contained in:
parent
311818d4e1
commit
4a3f3bdddc
|
|
@ -521,18 +521,51 @@ Current batch_Size can only be set to 1. The precision calculation process needs
|
|||
|
||||
```shell
|
||||
# Ascend310 inference
|
||||
bash run_infer_310.sh [MINDIR_PATH] [NET_TYPE] [DATA_PATH] [DEVICE_ID]
|
||||
bash run_infer_310.sh [MINDIR_PATH] [NET_TYPE] [DATASET] [DATA_PATH] [DEVICE_ID]
|
||||
```
|
||||
|
||||
- `NET_TYPE` can choose from [resnet18, se-resnet50].
|
||||
- `NET_TYPE` can choose from [resnet18, se-resnet50, resnet50, resnet101].
|
||||
- `DATASET` can choose from [cifar10, imagenet].
|
||||
- `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.
|
||||
|
||||
- Evaluating ResNet18 with CIFAR-10 dataset
|
||||
|
||||
```bash
|
||||
top1_accuracy:70.42, top5_accuracy:89.7
|
||||
Total data: 10000, top1 accuracy: 0.94.26, top5 accuracy: 0.9987.
|
||||
```
|
||||
|
||||
- Evaluating ResNet18 with ImageNet2012 dataset
|
||||
|
||||
```bash
|
||||
Total data: 50000, top1 accuracy: 0.70668, top5 accuracy: 0.89698.
|
||||
```
|
||||
|
||||
- Evaluating ResNet50 with CIFAR-10 dataset
|
||||
|
||||
```bash
|
||||
Total data: 10000, top1 accuracy: 0.9310, top5 accuracy: 0.9980.
|
||||
```
|
||||
|
||||
- Evaluating ResNet50 with ImageNet2012 dataset
|
||||
|
||||
```bash
|
||||
Total data: 50000, top1 accuracy: 0.0.7696, top5 accuracy: 0.93432.
|
||||
```
|
||||
|
||||
- Evaluating ResNet101 with ImageNet2012 dataset
|
||||
|
||||
```bash
|
||||
Total data: 50000, top1 accuracy: 0.7871, top5 accuracy: 0.94354.
|
||||
```
|
||||
|
||||
- Evaluating SE-ResNet50 with ImageNet2012 dataset
|
||||
|
||||
```bash
|
||||
Total data: 50000, top1 accuracy: 0.76844, top5 accuracy: 0.93522.
|
||||
```
|
||||
|
||||
# [Model Description](#contents)
|
||||
|
|
|
|||
|
|
@ -487,18 +487,51 @@ python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [
|
|||
|
||||
```shell
|
||||
# Ascend310 inference
|
||||
bash run_infer_310.sh [MINDIR_PATH] [NET_TYPE] [DATA_PATH] [DEVICE_ID]
|
||||
bash run_infer_310.sh [MINDIR_PATH] [NET_TYPE] [DATASET] [DATA_PATH] [DEVICE_ID]
|
||||
```
|
||||
|
||||
- `NET_TYPE` 选择范围:[resnet18, se-resnet50]。
|
||||
- `NET_TYPE` 选择范围:[resnet18, se-resnet50, resnet50, resnet101]。
|
||||
- `DATASET` 选择范围:[cifar10, imagenet]。
|
||||
- `DEVICE_ID` 可选,默认值为0。
|
||||
|
||||
### 结果
|
||||
|
||||
推理结果保存在脚本执行的当前路径,你可以在acc.log中看到以下精度计算结果。
|
||||
|
||||
- 使用CIFAR-10数据集评估ResNet18
|
||||
|
||||
```bash
|
||||
top1_accuracy:70.42, top5_accuracy:89.7
|
||||
Total data: 10000, top1 accuracy: 0.94.26, top5 accuracy: 0.9987.
|
||||
```
|
||||
|
||||
- 使用ImageNet2012数据集评估ResNet18
|
||||
|
||||
```bash
|
||||
Total data: 50000, top1 accuracy: 0.70668, top5 accuracy: 0.89698.
|
||||
```
|
||||
|
||||
- 使用CIFAR-10数据集评估ResNet50
|
||||
|
||||
```text
|
||||
Total data: 10000, top1 accuracy: 0.9310, top5 accuracy: 0.9980.
|
||||
```
|
||||
|
||||
- 使用ImageNet2012数据集评估ResNet50
|
||||
|
||||
```text
|
||||
Total data: 50000, top1 accuracy: 0.0.7696, top5 accuracy: 0.93432.
|
||||
```
|
||||
|
||||
- 使用ImageNet2012数据集评估ResNet101
|
||||
|
||||
```text
|
||||
Total data: 50000, top1 accuracy: 0.7871, top5 accuracy: 0.94354.
|
||||
```
|
||||
|
||||
- 使用ImageNet2012数据集评估SE-ResNet50
|
||||
|
||||
```text
|
||||
Total data: 50000, top1 accuracy: 0.76844, top5 accuracy: 0.93522.
|
||||
```
|
||||
|
||||
# 模型描述
|
||||
|
|
|
|||
|
|
@ -24,12 +24,10 @@
|
|||
#include <memory>
|
||||
#include "include/api/types.h"
|
||||
|
||||
std::vector<std::string> GetAllFiles(std::string_view dirName);
|
||||
DIR *OpenDir(std::string_view dirName);
|
||||
std::string RealPath(std::string_view path);
|
||||
mindspore::MSTensor ReadFileToTensor(const std::string &file);
|
||||
int WriteResult(const std::string& imageFile, const std::vector<mindspore::MSTensor> &outputs);
|
||||
std::vector<std::string> GetAllFiles(std::string dir_name);
|
||||
std::vector<std::vector<std::string>> GetAllInputData(std::string dir_name);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -50,106 +50,112 @@ using mindspore::kSuccess;
|
|||
using mindspore::MSTensor;
|
||||
using mindspore::dataset::Execute;
|
||||
|
||||
|
||||
DEFINE_string(mindir_path, "", "mindir path");
|
||||
DEFINE_string(dataset_path, ".", "dataset path");
|
||||
DEFINE_string(network, "resnet18", "networktype");
|
||||
DEFINE_string(dataset, "imagenet", "dataset");
|
||||
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;
|
||||
}
|
||||
|
||||
auto all_files = GetAllInputData(FLAGS_dataset_path);
|
||||
if (all_files.empty()) {
|
||||
std::cout << "ERROR: no input data." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::map<double, double> costTime_map;
|
||||
size_t size = all_files.size();
|
||||
|
||||
std::shared_ptr<TensorTransform> decode(new Decode());
|
||||
std::shared_ptr<TensorTransform> resize(new Resize({256}));
|
||||
std::shared_ptr<TensorTransform> centercrop(new CenterCrop({224}));
|
||||
std::shared_ptr<TensorTransform> normalize(new Normalize({123.675, 116.28, 103.53},
|
||||
{58.395, 57.12, 57.375}));
|
||||
std::shared_ptr<TensorTransform> hwc2chw(new HWC2CHW());
|
||||
|
||||
std::shared_ptr<TensorTransform> sr_resize(new Resize({292}));
|
||||
std::shared_ptr<TensorTransform> sr_centercrop(new CenterCrop({256}));
|
||||
std::shared_ptr<TensorTransform> sr_normalize(new Normalize({123.68, 116.78, 103.94},
|
||||
{1.0, 1.0, 1.0}));
|
||||
|
||||
std::vector<std::shared_ptr<TensorTransform>> trans_list;
|
||||
|
||||
if (FLAGS_network == "se-resnet50") {
|
||||
trans_list = {decode, sr_resize, sr_centercrop, sr_normalize, hwc2chw};
|
||||
} else {
|
||||
trans_list = {decode, resize, centercrop, normalize, hwc2chw};
|
||||
}
|
||||
mindspore::dataset::Execute SingleOp(trans_list);
|
||||
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
for (size_t j = 0; j < all_files[i].size(); ++j) {
|
||||
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:" << all_files[i][j] <<std::endl;
|
||||
auto imgDvpp = std::make_shared<MSTensor>();
|
||||
SingleOp(ReadFileToTensor(all_files[i][j]), imgDvpp.get());
|
||||
|
||||
inputs.emplace_back(imgDvpp->Name(), imgDvpp->DataType(), imgDvpp->Shape(),
|
||||
imgDvpp->Data().get(), imgDvpp->DataSize());
|
||||
gettimeofday(&start, nullptr);
|
||||
ret = model.Predict(inputs, &outputs);
|
||||
gettimeofday(&end, nullptr);
|
||||
if (ret != kSuccess) {
|
||||
std::cout << "Predict " << all_files[i][j] << " failed." << std::endl;
|
||||
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
||||
if (RealPath(FLAGS_mindir_path).empty()) {
|
||||
std::cout << "Invalid mindir" << 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(all_files[i][j], outputs);
|
||||
}
|
||||
}
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
auto all_files = GetAllFiles(FLAGS_dataset_path);
|
||||
if (all_files.empty()) {
|
||||
std::cout << "ERROR: no input data." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<MSTensor> modelInputs = model.GetInputs();
|
||||
std::map<double, double> costTime_map;
|
||||
size_t size = all_files.size();
|
||||
|
||||
std::shared_ptr<TensorTransform> decode = std::make_shared<Decode>();
|
||||
std::shared_ptr<TensorTransform> hwc2chw = std::make_shared<HWC2CHW>();
|
||||
|
||||
std::shared_ptr<TensorTransform> resize = std::make_shared<Resize>(std::vector<int>{256});
|
||||
std::shared_ptr<TensorTransform> centercrop = std::make_shared<CenterCrop>(std::vector<int>{224});
|
||||
std::shared_ptr<TensorTransform> normalize = std::make_shared<Normalize>(
|
||||
std::vector<float>{123.675, 116.28, 103.53}, std::vector<float>{58.395, 57.12, 57.375});
|
||||
|
||||
std::shared_ptr<TensorTransform> normalizeResnet101 = std::make_shared<Normalize>(
|
||||
std::vector<float>{121.125, 115.005, 99.96}, std::vector<float>{70.125, 68.085, 70.89});
|
||||
|
||||
std::shared_ptr<TensorTransform> sr_resize = std::make_shared<Resize>(std::vector<int>{292});
|
||||
std::shared_ptr<TensorTransform> sr_centercrop = std::make_shared<CenterCrop>(std::vector<int>{256});
|
||||
std::shared_ptr<TensorTransform> sr_normalize = std::make_shared<Normalize>(
|
||||
std::vector<float>{123.68, 116.78, 103.94}, std::vector<float>{1.0, 1.0, 1.0});
|
||||
|
||||
std::vector<std::shared_ptr<TensorTransform>> trans_list;
|
||||
|
||||
if (FLAGS_network == "se-resnet50") {
|
||||
trans_list = {decode, sr_resize, sr_centercrop, sr_normalize, hwc2chw};
|
||||
} else if (FLAGS_network == "resnet101") {
|
||||
trans_list = {decode, resize, centercrop, normalizeResnet101, hwc2chw};
|
||||
} else {
|
||||
trans_list = {decode, resize, centercrop, normalize, hwc2chw};
|
||||
}
|
||||
mindspore::dataset::Execute SingleOp(trans_list);
|
||||
|
||||
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:" << all_files[i] <<std::endl;
|
||||
|
||||
MSTensor image = ReadFileToTensor(all_files[i]);
|
||||
if (FLAGS_dataset == "imagenet") {
|
||||
SingleOp(image, &image);
|
||||
}
|
||||
|
||||
inputs.emplace_back(modelInputs[0].Name(), modelInputs[0].DataType(), modelInputs[0].Shape(),
|
||||
image.Data().get(), image.DataSize());
|
||||
gettimeofday(&start, nullptr);
|
||||
ret = model.Predict(inputs, &outputs);
|
||||
gettimeofday(&end, nullptr);
|
||||
if (ret != kSuccess) {
|
||||
std::cout << "Predict " << all_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(all_files[i], outputs);
|
||||
}
|
||||
double average = 0.0;
|
||||
int inferCount = 0;
|
||||
|
||||
for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) {
|
||||
average += iter->second - iter->first;
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,84 +22,44 @@
|
|||
using mindspore::MSTensor;
|
||||
using mindspore::DataType;
|
||||
|
||||
|
||||
std::vector<std::vector<std::string>> GetAllInputData(std::string dir_name) {
|
||||
std::vector<std::vector<std::string>> ret;
|
||||
|
||||
DIR *dir = OpenDir(dir_name);
|
||||
if (dir == nullptr) {
|
||||
return {};
|
||||
}
|
||||
struct dirent *filename;
|
||||
/* read all the files in the dir ~ */
|
||||
std::vector<std::string> sub_dirs;
|
||||
while ((filename = readdir(dir)) != nullptr) {
|
||||
std::string d_name = std::string(filename->d_name);
|
||||
// get rid of "." and ".."
|
||||
if (d_name == "." || d_name == ".." || d_name.empty()) {
|
||||
continue;
|
||||
std::vector<std::string> GetAllFiles(std::string dirName) {
|
||||
struct dirent *filename;
|
||||
DIR *dir = OpenDir(dirName);
|
||||
if (dir == nullptr) {
|
||||
return {};
|
||||
}
|
||||
std::string dir_path = RealPath(std::string(dir_name) + "/" + filename->d_name);
|
||||
struct stat s;
|
||||
lstat(dir_path.c_str(), &s);
|
||||
if (!S_ISDIR(s.st_mode)) {
|
||||
continue;
|
||||
std::vector<std::string> dirs;
|
||||
std::vector<std::string> files;
|
||||
while ((filename = readdir(dir)) != nullptr) {
|
||||
std::string dName = std::string(filename->d_name);
|
||||
if (dName == "." || dName == "..") {
|
||||
continue;
|
||||
} else if (filename->d_type == DT_DIR) {
|
||||
dirs.emplace_back(std::string(dirName) + "/" + filename->d_name);
|
||||
} else if (filename->d_type == DT_REG) {
|
||||
files.emplace_back(std::string(dirName) + "/" + filename->d_name);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
sub_dirs.emplace_back(dir_path);
|
||||
}
|
||||
std::sort(sub_dirs.begin(), sub_dirs.end());
|
||||
|
||||
(void)std::transform(sub_dirs.begin(), sub_dirs.end(), std::back_inserter(ret),
|
||||
[](const std::string &d) { return GetAllFiles(d); });
|
||||
|
||||
return ret;
|
||||
for (auto d : dirs) {
|
||||
dir = OpenDir(d);
|
||||
while ((filename = readdir(dir)) != nullptr) {
|
||||
std::string dName = std::string(filename->d_name);
|
||||
if (dName == "." || dName == ".." || filename->d_type != DT_REG) {
|
||||
continue;
|
||||
}
|
||||
files.emplace_back(std::string(d) + "/" + filename->d_name);
|
||||
}
|
||||
}
|
||||
std::sort(files.begin(), files.end());
|
||||
for (auto &f : files) {
|
||||
std::cout << "image file: " << f << std::endl;
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string> GetAllFiles(std::string dir_name) {
|
||||
struct dirent *filename;
|
||||
DIR *dir = OpenDir(dir_name);
|
||||
if (dir == nullptr) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<std::string> res;
|
||||
while ((filename = readdir(dir)) != nullptr) {
|
||||
std::string d_name = std::string(filename->d_name);
|
||||
if (d_name == "." || d_name == ".." || d_name.size() <= 3) {
|
||||
continue;
|
||||
}
|
||||
res.emplace_back(std::string(dir_name) + "/" + filename->d_name);
|
||||
}
|
||||
std::sort(res.begin(), res.end());
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string> GetAllFiles(std::string_view dirName) {
|
||||
struct dirent *filename;
|
||||
DIR *dir = OpenDir(dirName);
|
||||
if (dir == nullptr) {
|
||||
return {};
|
||||
}
|
||||
std::vector<std::string> res;
|
||||
while ((filename = readdir(dir)) != nullptr) {
|
||||
std::string dName = std::string(filename->d_name);
|
||||
if (dName == "." || dName == ".." || filename->d_type != DT_REG) {
|
||||
continue;
|
||||
}
|
||||
res.emplace_back(std::string(dirName) + "/" + filename->d_name);
|
||||
}
|
||||
std::sort(res.begin(), res.end());
|
||||
for (auto &f : res) {
|
||||
std::cout << "image file: " << f << std::endl;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
int WriteResult(const std::string& imageFile, const std::vector<MSTensor> &outputs) {
|
||||
std::string homePath = "./result_Files";
|
||||
for (size_t i = 0; i < outputs.size(); ++i) {
|
||||
|
|
|
|||
|
|
@ -21,12 +21,45 @@ from src.config import config2 as config
|
|||
|
||||
batch_size = 1
|
||||
parser = argparse.ArgumentParser(description="resnet inference")
|
||||
parser.add_argument("--dataset", type=str, required=True, help="dataset type.")
|
||||
parser.add_argument("--result_path", type=str, required=True, help="result files path.")
|
||||
parser.add_argument("--label_path", type=str, required=True, help="image file path.")
|
||||
args = parser.parse_args()
|
||||
|
||||
def get_top5_acc(top5_arg, gt_class):
|
||||
sub_count = 0
|
||||
for top5, gt in zip(top5_arg, gt_class):
|
||||
if gt in top5:
|
||||
sub_count += 1
|
||||
return sub_count
|
||||
|
||||
def get_result(result_path, label_path):
|
||||
def cal_acc_cifar10(result_path, label_path):
|
||||
img_tot = 0
|
||||
top1_correct = 0
|
||||
top5_correct = 0
|
||||
img_tot = 0
|
||||
|
||||
result_shape = (1, 10)
|
||||
|
||||
files = os.listdir(result_path)
|
||||
for file in files:
|
||||
full_file_path = os.path.join(result_path, file)
|
||||
if os.path.isfile(full_file_path):
|
||||
result = np.fromfile(full_file_path, dtype=np.float32).reshape(result_shape)
|
||||
label_file = os.path.join(label_path, file.split(".bin")[0][:-2] + ".bin")
|
||||
gt_classes = np.fromfile(label_file, dtype=np.int32)
|
||||
|
||||
top1_output = np.argmax(result, (-1))
|
||||
top5_output = np.argsort(result)[:, -5:]
|
||||
|
||||
t1_correct = np.equal(top1_output, gt_classes).sum()
|
||||
top1_correct += t1_correct
|
||||
top5_correct += get_top5_acc(top5_output, [gt_classes])
|
||||
img_tot += 1
|
||||
|
||||
print(f"Total data: {img_tot}, top1 accuracy: {top1_correct / img_tot}, top5 accuracy: {top5_correct / img_tot}.")
|
||||
|
||||
def cal_acc_imagenet(result_path, label_path):
|
||||
files = os.listdir(result_path)
|
||||
with open(label_path, "r") as label:
|
||||
labels = json.load(label)
|
||||
|
|
@ -48,4 +81,7 @@ def get_result(result_path, label_path):
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
get_result(args.result_path, args.label_path)
|
||||
if args.dataset.lower() == "cifar10":
|
||||
cal_acc_cifar10(args.result_path, args.label_path)
|
||||
else:
|
||||
cal_acc_imagenet(args.result_path, args.label_path)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
# 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.
|
||||
# ============================================================================
|
||||
"""train resnet."""
|
||||
import os
|
||||
import argparse
|
||||
from src.dataset import create_dataset1 as create_dataset
|
||||
|
||||
parser = argparse.ArgumentParser(description='preprocess data')
|
||||
parser.add_argument('--dataset_path', type=str, default=None, help='Dataset path')
|
||||
parser.add_argument('--output_path', type=str, default=None, help='output path')
|
||||
args_opt = parser.parse_args()
|
||||
|
||||
if __name__ == '__main__':
|
||||
# create dataset
|
||||
dataset = create_dataset(dataset_path=args_opt.dataset_path, do_train=False, batch_size=1,
|
||||
target="Ascend")
|
||||
step_size = dataset.get_dataset_size()
|
||||
|
||||
img_path = os.path.join(args_opt.output_path, "img_data")
|
||||
label_path = os.path.join(args_opt.output_path, "label")
|
||||
os.makedirs(img_path)
|
||||
os.makedirs(label_path)
|
||||
|
||||
for idx, data in enumerate(dataset.create_dict_iterator(output_numpy=True, num_epochs=1)):
|
||||
img_data = data["image"]
|
||||
img_label = data["label"]
|
||||
|
||||
file_name = "google_cifar10_1_" + str(idx) + ".bin"
|
||||
img_file_path = os.path.join(img_path, file_name)
|
||||
img_data.tofile(img_file_path)
|
||||
|
||||
label_file_path = os.path.join(label_path, file_name)
|
||||
img_label.tofile(label_file_path)
|
||||
|
||||
print("=" * 20, "export bin files finished", "=" * 20)
|
||||
|
|
@ -14,9 +14,9 @@
|
|||
# limitations under the License.
|
||||
# ============================================================================
|
||||
|
||||
if [[ $# -lt 3 || $# -gt 4 ]]; then
|
||||
echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [NET_TYPE] [DATA_PATH] [DEVICE_ID]
|
||||
NET_TYPE can choose from [resnet18, se-resnet50]
|
||||
if [[ $# -lt 4 || $# -gt 5 ]]; then
|
||||
echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [NET_TYPE] [DATASET] [DATA_PATH] [DEVICE_ID]
|
||||
NET_TYPE can choose from [resnet18, se-resnet50, resnet50, resnet101]
|
||||
DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero"
|
||||
exit 1
|
||||
fi
|
||||
|
|
@ -29,23 +29,25 @@ get_real_path(){
|
|||
fi
|
||||
}
|
||||
model=$(get_real_path $1)
|
||||
if [ $2 == 'resnet18' ] || [ $2 == 'se-resnet50' ]; then
|
||||
if [ $2 == 'resnet18' ] || [ $2 == 'se-resnet50' ] || [ $2 == 'resnet50' ] || [ $2 == 'resnet101' ]; then
|
||||
network=$2
|
||||
else
|
||||
echo "NET_TYPE can choose from [resnet18, se-resnet50]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
data_path=$(get_real_path $3)
|
||||
dataset=$3
|
||||
data_path=$(get_real_path $4)
|
||||
|
||||
device_id=0
|
||||
if [ $# == 4 ]; then
|
||||
device_id=$4
|
||||
if [ $# == 5 ]; then
|
||||
device_id=$5
|
||||
fi
|
||||
|
||||
echo "mindir name: "$model
|
||||
echo "dataset path: "$data_path
|
||||
echo "network: "$network
|
||||
echo "dataset: "$dataset
|
||||
echo "device id: "$device_id
|
||||
|
||||
export ASCEND_HOME=/usr/local/Ascend/
|
||||
|
|
@ -71,6 +73,16 @@ function compile_app()
|
|||
bash build.sh &> build.log
|
||||
}
|
||||
|
||||
function preprocess_data()
|
||||
{
|
||||
if [ -d preprocess_Result ]; then
|
||||
rm -rf ./preprocess_Result
|
||||
fi
|
||||
mkdir preprocess_Result
|
||||
|
||||
python3.7 ../preprocess.py --dataset_path=$data_path --output_path=./preprocess_Result
|
||||
}
|
||||
|
||||
function infer()
|
||||
{
|
||||
cd - || exit
|
||||
|
|
@ -82,15 +94,28 @@ function infer()
|
|||
fi
|
||||
mkdir result_Files
|
||||
mkdir time_Result
|
||||
../ascend310_infer/src/main --mindir_path=$model --dataset_path=$data_path --network=$network --device_id=$device_id &> infer.log
|
||||
../ascend310_infer/src/main --mindir_path=$model --dataset_path=$data_path --network=$network --dataset=$dataset --device_id=$device_id &> infer.log
|
||||
}
|
||||
|
||||
function cal_acc()
|
||||
{
|
||||
python3.7 ../create_imagenet2012_label.py --img_path=$data_path
|
||||
python3.7 ../postprocess.py --result_path=./result_Files --label_path=./imagenet_label.json &> acc.log &
|
||||
if [ "x${dataset}" == "xcifar10" ] || [ "x${dataset}" == "xCifar10" ]; then
|
||||
python ../postprocess.py --dataset=$dataset --label_path=./preprocess_Result/label --result_path=result_Files &> acc.log
|
||||
else
|
||||
python3.7 ../create_imagenet2012_label.py --img_path=$data_path
|
||||
python3.7 ../postprocess.py --dataset=$dataset --result_path=./result_Files --label_path=./imagenet_label.json &> acc.log
|
||||
fi
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "calculate accuracy failed"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
if [ "x${dataset}" == "xcifar10" ] || [ "x${dataset}" == "xCifar10" ]; then
|
||||
preprocess_data
|
||||
data_path=./preprocess_Result/img_data
|
||||
fi
|
||||
|
||||
compile_app
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "compile app code failed"
|
||||
|
|
|
|||
Loading…
Reference in New Issue