Browse Source

for deeptext 310 inferenct

pull/14847/head
yuzhenhua 5 years ago
parent
commit
327d725473
11 changed files with 706 additions and 1 deletions
  1. +34
    -1
      model_zoo/official/cv/deeptext/README.md
  2. +14
    -0
      model_zoo/official/cv/deeptext/ascend310_infer/CMakeLists.txt
  3. +23
    -0
      model_zoo/official/cv/deeptext/ascend310_infer/build.sh
  4. +32
    -0
      model_zoo/official/cv/deeptext/ascend310_infer/inc/utils.h
  5. +154
    -0
      model_zoo/official/cv/deeptext/ascend310_infer/src/main.cc
  6. +130
    -0
      model_zoo/official/cv/deeptext/ascend310_infer/src/utils.cc
  7. +53
    -0
      model_zoo/official/cv/deeptext/export.py
  8. +123
    -0
      model_zoo/official/cv/deeptext/postprocess.py
  9. +107
    -0
      model_zoo/official/cv/deeptext/scripts/run_infer_310.sh
  10. +10
    -0
      model_zoo/official/cv/deeptext/src/Deeptext/deeptext_vgg16.py
  11. +26
    -0
      model_zoo/official/cv/deeptext/src/aipp.cfg

+ 34
- 1
model_zoo/official/cv/deeptext/README.md View File

@@ -64,9 +64,11 @@ Here we used 4 datasets for training, and 1 datasets for Evaluation.
.
└─deeptext
├─README.md
├─ascend310_infer #application for 310 inference
├─scripts
├─run_standalone_train_ascend.sh # launch standalone training with ascend platform(1p)
├─run_distribute_train_ascend.sh # launch distributed training with ascend platform(8p)
├─run_infer_310.sh # shell script for 310 inference
└─run_eval_ascend.sh # launch evaluating with ascend platform
├─src
├─DeepText
@@ -81,12 +83,14 @@ Here we used 4 datasets for training, and 1 datasets for Evaluation.
├─rpn.py # region-proposal network
└─vgg16.py # backbone
├─config.py # training configuration
├─aipp.cfg # aipp config file
├─dataset.py # data proprocessing
├─lr_schedule.py # learning rate scheduler
├─network_define.py # network definition
└─utils.py # some functions which is commonly used
├─eval.py # eval net
├─export.py # export checkpoint, surpport .onnx, .air, .mindir convert
├─postprogress.py # post process for 310 inference
└─train.py # train net
```

@@ -168,6 +172,35 @@ Evaluation result will be stored in the example path, you can find result like t
class 1 precision is 88.01%, recall is 82.77%
```

## Model Export

```shell
python export.py --ckpt_file [CKPT_PATH] --device_target [DEVICE_TARGET] --file_format[EXPORT_FORMAT]
```

`EXPORT_FORMAT` should be in ["AIR", "MINDIR"]

## Inference Process

### Usage

Before performing inference, the air file must bu exported by export script on the Ascend910 environment.

```shell
# Ascend310 inference
bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [LABEL_PATH] [DEVICE_ID]
```

### result

Inference result is saved in current path, you can find result like this in acc.log file.

```python
========================================

class 1 precision is 84.24%, recall is 87.40%, F1 is 85.79%
```

# [Model description](#contents)

## [Performance](#contents)
@@ -177,7 +210,7 @@ class 1 precision is 88.01%, recall is 82.77%
| Parameters | Ascend |
| -------------------------- | ------------------------------------------------------------ |
| Model Version | Deeptext |
| Resource | Ascend 910, cpu:2.60GHz 192cores, memory:755G |
| Resource | Ascend 910, cpu:2.60GHz 192cores, memory:755G, OS:Euler2.8 |
| uploaded Date | 12/26/2020 |
| MindSpore Version | 1.1.0 |
| Dataset | 66040 images |


+ 14
- 0
model_zoo/official/cv/deeptext/ascend310_infer/CMakeLists.txt View File

@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.14.1)
project(Ascend310Infer)
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -std=c++17 -Werror -Wall -fPIE -Wl,--allow-shlib-undefined")
set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/)
option(MINDSPORE_PATH "mindspore install path" "")
include_directories(${MINDSPORE_PATH})
include_directories(${MINDSPORE_PATH}/include)
include_directories(${PROJECT_SRC_ROOT})
find_library(MS_LIB libmindspore.so ${MINDSPORE_PATH}/lib)
file(GLOB_RECURSE MD_LIB ${MINDSPORE_PATH}/_c_dataengine*)

add_executable(main src/main.cc src/utils.cc)
target_link_libraries(main ${MS_LIB} ${MD_LIB} gflags)

+ 23
- 0
model_zoo/official/cv/deeptext/ascend310_infer/build.sh View File

@@ -0,0 +1,23 @@
#!/bin/bash
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

if [ ! -d out ]; then
mkdir out
fi
cd out
cmake .. \
-DMINDSPORE_PATH="`pip show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`"
make

+ 32
- 0
model_zoo/official/cv/deeptext/ascend310_infer/inc/utils.h View File

@@ -0,0 +1,32 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MINDSPORE_INFERENCE_UTILS_H_
#define MINDSPORE_INFERENCE_UTILS_H_

#include <sys/stat.h>
#include <dirent.h>
#include <vector>
#include <string>
#include <memory>
#include "include/api/types.h"

std::vector<std::string> GetAllFiles(std::string_view dirName);
DIR *OpenDir(std::string_view dirName);
std::string RealPath(std::string_view path);
mindspore::MSTensor ReadFileToTensor(const std::string &file);
int WriteResult(const std::string& imageFile, const std::vector<mindspore::MSTensor> &outputs);
#endif

+ 154
- 0
model_zoo/official/cv/deeptext/ascend310_infer/src/main.cc View File

@@ -0,0 +1,154 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <sys/time.h>
#include <gflags/gflags.h>
#include <dirent.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <iosfwd>
#include <vector>
#include <fstream>
#include <sstream>

#include "../inc/utils.h"
#include "minddata/dataset/include/execute.h"
#include "minddata/dataset/include/transforms.h"
#include "minddata/dataset/include/vision.h"
#include "minddata/dataset/include/vision_ascend.h"
#include "include/api/types.h"
#include "include/api/model.h"
#include "include/api/serialization.h"
#include "include/api/context.h"


using mindspore::Context;
using mindspore::Serialization;
using mindspore::Model;
using mindspore::Status;
using mindspore::dataset::Execute;
using mindspore::MSTensor;
using mindspore::ModelType;
using mindspore::GraphCell;
using mindspore::kSuccess;
using mindspore::Graph;
using mindspore::dataset::vision::DvppDecodeResizeJpeg;

DEFINE_string(model_path, "", "model path");
DEFINE_string(dataset_path, ".", "dataset path");
DEFINE_int32(input_width, 960, "input width");
DEFINE_int32(input_height, 576, "inputheight");
DEFINE_int32(device_id, 0, "device id");
DEFINE_string(precision_mode, "allow_fp32_to_fp16", "precision mode");
DEFINE_string(op_select_impl_mode, "", "op select impl mode");
DEFINE_string(aipp_path, "./aipp.cfg", "aipp path");
DEFINE_string(dump_config_file, "./acl.json", "dump config file");
DEFINE_string(device_target, "Ascend310", "device target");

int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (RealPath(FLAGS_model_path).empty()) {
std::cout << "Invalid mindir" << std::endl;
return 1;
}
if (RealPath(FLAGS_aipp_path).empty()) {
std::cout << "Invalid aipp path" << std::endl;
return 1;
}

auto context = std::make_shared<Context>();
auto ascend310_info = std::make_shared<mindspore::Ascend310DeviceInfo>();
ascend310_info->SetDeviceID(FLAGS_device_id);
ascend310_info->SetInsertOpConfigPath(FLAGS_aipp_path);
context->MutableDeviceInfo().push_back(ascend310_info);

Graph graph;
Status ret = Serialization::Load(FLAGS_model_path, ModelType::kMindIR, &graph);
if (ret != kSuccess) {
std::cout << "Load model failed." << std::endl;
return 1;
}

Model model;
ret = model.Build(GraphCell(graph), context);
if (ret != kSuccess) {
std::cout << "ERROR: Build failed." << std::endl;
return 1;
}

std::vector<MSTensor> modelInputs = model.GetInputs();

auto all_files = GetAllFiles(FLAGS_dataset_path);
if (all_files.empty()) {
std::cout << "ERROR: no input data." << std::endl;
return 1;
}

std::map<double, double> costTime_map;
size_t size = all_files.size();

Execute transform(std::shared_ptr<DvppDecodeResizeJpeg>(new DvppDecodeResizeJpeg({576, 960})));

for (size_t i = 0; i < size; ++i) {
struct timeval start;
struct timeval end;
double startTime_ms;
double endTime_ms;
std::vector<MSTensor> inputs;
std::vector<MSTensor> outputs;

std::cout << "Start predict input files:" << all_files[i] << std::endl;

mindspore::MSTensor imgDvpp;
transform(ReadFileToTensor(all_files[i]), &imgDvpp);

inputs.emplace_back(modelInputs[0].Name(), modelInputs[0].DataType(), modelInputs[0].Shape(),
imgDvpp.Data().get(), imgDvpp.DataSize());

gettimeofday(&start, NULL);
ret = model.Predict(inputs, &outputs);
gettimeofday(&end, NULL);
if (ret != kSuccess) {
std::cout << "Predict " << all_files[i] << " failed." << std::endl;
return 1;
}
startTime_ms = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000;
endTime_ms = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000;
costTime_map.insert(std::pair<double, double>(startTime_ms, endTime_ms));
WriteResult(all_files[i], outputs);
}
double average = 0.0;
int infer_cnt = 0;

for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) {
double diff = 0.0;
diff = iter->second - iter->first;
average += diff;
infer_cnt++;
}

average = average/infer_cnt;
std::stringstream timeCost;
timeCost << "NN inference cost average time: "<< average << "ms of infer_count " << infer_cnt << std::endl;
std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << infer_cnt << std::endl;
std::string file_name = "./time_Result" + std::string("/test_perform_static.txt");
std::ofstream file_stream(file_name.c_str(), std::ios::trunc);
file_stream << timeCost.str();
file_stream.close();
costTime_map.clear();
return 0;
}

+ 130
- 0
model_zoo/official/cv/deeptext/ascend310_infer/src/utils.cc View File

@@ -0,0 +1,130 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "inc/utils.h"

#include <fstream>
#include <algorithm>
#include <iostream>

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);
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(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;
}

+ 53
- 0
model_zoo/official/cv/deeptext/export.py View File

@@ -0,0 +1,53 @@
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""export checkpoint file into air, mindir models"""
import argparse
import numpy as np

import mindspore as ms
from mindspore import Tensor, load_checkpoint, load_param_into_net, export, context

from src.Deeptext.deeptext_vgg16 import Deeptext_VGG16_Infer
from src.config import config

parser = argparse.ArgumentParser(description='deeptext export')
parser.add_argument("--device_id", type=int, default=0, help="Device id")
parser.add_argument("--batch_size", type=int, default=1, help="batch size")
parser.add_argument("--file_name", type=str, default="deeptext", help="output file name.")
parser.add_argument("--file_format", type=str, choices=["AIR", "MINDIR"], default="MINDIR", help="file format")
parser.add_argument("--device_target", type=str, choices=["Ascend", "GPU", "CPU"], default="Ascend",
help="device target")
parser.add_argument('--ckpt_file', type=str, default='', help='deeptext ckpt file.')
args = parser.parse_args()

config.test_batch_size = args.batch_size
context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target)
context.set_context(device_id=args.device_id)

if __name__ == '__main__':
net = Deeptext_VGG16_Infer(config=config)
net.set_train(False)

param_dict = load_checkpoint(args.ckpt_file)

param_dict_new = {}
for key, value in param_dict.items():
param_dict_new["network." + key] = value

load_param_into_net(net, param_dict_new)

img_data = Tensor(np.zeros([config.test_batch_size, 3, config.img_height, config.img_width]), ms.float16)

export(net, img_data, file_name=args.file_name, file_format=args.file_format)

+ 123
- 0
model_zoo/official/cv/deeptext/postprocess.py View File

@@ -0,0 +1,123 @@
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

"""Evaluation for Deeptext"""
import argparse
import os

import numpy as np
from src.config import config
from src.utils import metrics
from PIL import Image
import mmcv

parser = argparse.ArgumentParser(description="Deeptext evaluation")
parser.add_argument("--result_path", type=str, required=True, help="result file path")
parser.add_argument("--label_path", type=str, required=True, help="label path")
parser.add_argument("--img_path", type=str, required=True, help="img path")
args_opt = parser.parse_args()

config.test_batch_size = 1

def get_pred(file, result_path):
file_name = file.split('.')[0][3:]
all_bbox_file = os.path.join(result_path, file_name + "_0.bin")
all_label_file = os.path.join(result_path, file_name + "_1.bin")
all_mask_file = os.path.join(result_path, file_name + "_2.bin")
all_bbox = np.fromfile(all_bbox_file, dtype=np.float16).reshape(config.test_batch_size, 1000, 5)
all_label = np.fromfile(all_label_file, dtype=np.int32).reshape(config.test_batch_size, 1000, 1)
all_mask = np.fromfile(all_mask_file, dtype=np.bool).reshape(config.test_batch_size, 1000, 1)

return all_bbox, all_label, all_mask

def get_gt_bboxes_labels(label_file, img_file):
img_data = np.array(Image.open(img_file))
img_data, w_scale, h_scale = mmcv.imresize(
img_data, (config.img_width, config.img_height), return_scale=True)
scale_factor = np.array(
[w_scale, h_scale, w_scale, h_scale], dtype=np.float32)
img_shape = (config.img_height, config.img_width, 1.0)
img_shape = np.asarray(img_shape, dtype=np.float32)

file = open(label_file)
lines = file.readlines()
boxes = []
gt_label = []
for line in lines:
label_info = line.split(",")
boxes.append([float(label_info[0]), float(label_info[1]), float(label_info[2]), float(label_info[3])])
gt_label.append(int(1))

gt_bboxes = np.array(boxes)
gt_bboxes = gt_bboxes * scale_factor

gt_bboxes[:, 0::2] = np.clip(gt_bboxes[:, 0::2], 0, img_shape[1] - 1)
gt_bboxes[:, 1::2] = np.clip(gt_bboxes[:, 1::2], 0, img_shape[0] - 1)

return gt_bboxes, gt_label

def deeptext_eval_test(result_path='', label_path='', img_path=''):
eval_iter = 0

print("\n========================================\n")
print("Processing, please wait a moment.")
max_num = 32

pred_data = []
files = os.listdir(label_path)
for file in files:
eval_iter = eval_iter + 1
img_file = os.path.join(img_path, file.split('gt_')[1].replace("txt", "jpg"))

label_file = os.path.join(label_path, file)
gt_bboxes, gt_labels = get_gt_bboxes_labels(label_file, img_file)

gt_bboxes = np.array(gt_bboxes).astype(np.float32)

all_bbox, all_label, all_mask = get_pred(file, result_path)
all_label = all_label + 1


for j in range(config.test_batch_size):
all_bbox_squee = np.squeeze(all_bbox[j, :, :])
all_label_squee = np.squeeze(all_label[j, :, :])
all_mask_squee = np.squeeze(all_mask[j, :, :])

all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :]
all_labels_tmp_mask = all_label_squee[all_mask_squee]

if all_bboxes_tmp_mask.shape[0] > max_num:
inds = np.argsort(-all_bboxes_tmp_mask[:, -1])
inds = inds[:max_num]
all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds]
all_labels_tmp_mask = all_labels_tmp_mask[inds]

pred_data.append({"boxes": all_bboxes_tmp_mask,
"labels": all_labels_tmp_mask,
"gt_bboxes": gt_bboxes,
"gt_labels": gt_labels})

precisions, recalls = metrics(pred_data)
print("\n========================================\n")
for i in range(config.num_classes - 1):
j = i + 1
f1 = (2 * precisions[j] * recalls[j]) / (precisions[j] + recalls[j] + 1e-6)
print("class {} precision is {:.2f}%, recall is {:.2f}%,"
"F1 is {:.2f}%".format(j, precisions[j] * 100, recalls[j] * 100, f1 * 100))
if config.use_ambigous_sample:
break

if __name__ == '__main__':
deeptext_eval_test(args_opt.result_path, args_opt.label_path, args_opt.img_path)

+ 107
- 0
model_zoo/official/cv/deeptext/scripts/run_infer_310.sh View File

@@ -0,0 +1,107 @@
#!/bin/bash
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

if [[ $# -lt 3 || $# -gt 4 ]]; then
echo "Usage: sh run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [LABEL_PATH] [DEVICE_ID]
DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero"
exit 1
fi

get_real_path(){
if [ "${1:0:1}" == "/" ]; then
echo "$1"
else
echo "$(realpath -m $PWD/$1)"
fi
}

model=$(get_real_path $1)
data_path=$(get_real_path $2)
label_path=$(get_real_path $3)
if [ $# == 4 ]; then
device_id=$4
elif [ $# == 3 ]; then
if [ -z $device_id ]; then
device_id=0
else
device_id=$device_id
fi
fi

echo $model
echo $data_path
echo $label_path
echo $device_id

export ASCEND_HOME=/usr/local/Ascend/
if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then
export PATH=$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH
export LD_LIBRARY_PATH=/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=${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/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH
export LD_LIBRARY_PATH=/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/atc/python/site-packages:$PYTHONPATH
export ASCEND_OPP_PATH=$ASCEND_HOME/opp
fi

function compile_app()
{
cd ../ascend310_infer
if [ -f "Makefile" ]; then
make clean
fi
sh build.sh &> build.log

if [ $? -ne 0 ]; then
echo "compile app code failed"
exit 1
fi
cd -
}

function infer()
{
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 --model_path=$model --dataset_path=$data_path --device_id=$device_id --aipp_path ../src/aipp.cfg &> infer.log

if [ $? -ne 0 ]; then
echo "execute inference failed"
exit 1
fi
}

function cal_acc()
{
python ../postprocess.py --label_path=$label_path --result_path=result_Files --img_path=$data_path &> acc.log &
if [ $? -ne 0 ]; then
echo "calculate accuracy failed"
exit 1
fi
}

compile_app
infer
cal_acc

+ 10
- 0
model_zoo/official/cv/deeptext/src/Deeptext/deeptext_vgg16.py View File

@@ -430,3 +430,13 @@ class Deeptext_VGG16(nn.Cell):
multi_level_anchors += (Tensor(anchors.astype(np.float32)),)

return multi_level_anchors

class Deeptext_VGG16_Infer(nn.Cell):
def __init__(self, config):
super(Deeptext_VGG16_Infer, self).__init__()
self.network = Deeptext_VGG16(config)
self.network.set_train(False)

def construct(self, img_data):
output = self.network(img_data, None, None, None, None)
return output

+ 26
- 0
model_zoo/official/cv/deeptext/src/aipp.cfg View File

@@ -0,0 +1,26 @@
aipp_op {
aipp_mode : static
input_format : YUV420SP_U8
related_input_rank : 0
csc_switch : true
rbuv_swap_switch : false
matrix_r0c0 : 256
matrix_r0c1 : 0
matrix_r0c2 : 359
matrix_r1c0 : 256
matrix_r1c1 : -88
matrix_r1c2 : -183
matrix_r2c0 : 256
matrix_r2c1 : 454
matrix_r2c2 : 0
input_bias_0 : 0
input_bias_1 : 128
input_bias_2 : 128
mean_chn_0 : 124
mean_chn_1 : 117
mean_chn_2 : 104
var_reci_chn_0 : 0.0171247538316637
var_reci_chn_1 : 0.0175070028011204
var_reci_chn_2 : 0.0174291938997821
}

Loading…
Cancel
Save