This commit is contained in:
guo_xuanworld
2022-07-31 19:39:29 +08:00
parent de0918b62b
commit a556c9cf0d
10 changed files with 395 additions and 41 deletions

View File

@@ -199,17 +199,20 @@ ATC run success, welcome to the next use.
## 5 编译与运行
**步骤1** 按照第2小结**环境依赖**中的步骤设置环境变量
**步骤1** 修改run.sh中的环境变量为正确的路径
**步骤2** 按照第 4 小节 **模型获取** 中的步骤获得模型文件,放置对应目录下。
**步骤2** 按照第 4 小节**模型获取** 中的步骤获得模型文件,放置对应目录下。
**步骤3** 运行。执行命令:
```shell
#task_type为detect、speed或eval
#image_set是一个文本文件每一行是一个不包含后缀的图片名eval时使用VOCdevkit/VOC2007/ImageSets/Main/test.txt
#path为image_set中图片所在目录例如VOCdevkit/VOC2007/JPEGImages/
bash run.sh [task_type][image_path][image_set][path]
#task_type为detect或speed时
#image_set是一个文本文件,每一行是一个不包含后缀的图片名,例如VOCdevkit/VOC2007/ImageSets/Main/test.txt
#image_dir为image_set中图片所在目录例如VOCdevkit/VOC2007/JPEGImages/
bash run.sh [task_type][image_set][image_dir]
#task_type为eval时
#dataset_path为标准VOC数据集路径例如./data/VOCdevkit/
bash run.sh [task_type][dataset_path]
```

View File

@@ -0,0 +1,94 @@
"""Reval = re-eval. Re-evaluate saved detections."""
import os, sys, argparse
import numpy as np
import _pickle as cPickle
from voc_eval import voc_eval
def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Re-evaluate results')
parser.add_argument('output_dir', nargs=1, help='results directory',
type=str)
parser.add_argument('--voc_dir', dest='voc_dir', default='data/VOCdevkit', type=str)
parser.add_argument('--year', dest='year', default='2007', type=str)
parser.add_argument('--image_set', dest='image_set', default='test', type=str)
parser.add_argument('--classes', dest='class_file', default='models/yolov5/voc.names', type=str)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
return args
def get_voc_results_file_template(image_set, out_dir = '.'):
filename = 'det_' + image_set + '_{:s}.txt'
path = os.path.join(out_dir, filename)
return path
def do_python_eval(devkit_path, year, image_set, classes, output_dir):
annopath = os.path.join(
devkit_path,
'VOC' + year,
'Annotations',
'{}.xml')
imagesetfile = os.path.join(
devkit_path,
'VOC' + year,
'ImageSets',
'Main',
image_set + '.txt')
cachedir = os.path.join(devkit_path, 'annotations_cache')
aps = []
# The PASCAL VOC metric changed in 2010
# use_07_metric = True if int(year) < 2010 else False
# print('VOC07 metric? ' + ('Yes' if use_07_metric else 'No'))
use_07_metric = False
print('devkit_path=',devkit_path,', year = ',year)
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
for i, cls in enumerate(classes):
if cls == '__background__':
continue
filename = get_voc_results_file_template(image_set,output_dir).format(cls)
rec, prec, ap = voc_eval(
filename, annopath, imagesetfile, cls, cachedir, ovthresh=0.55,
use_07_metric=use_07_metric)
aps += [ap]
print('AP for {} = {:.4f}'.format(cls, ap))
with open(os.path.join(output_dir, cls + '_pr.pkl'), 'wb') as f:
cPickle.dump({'rec': rec, 'prec': prec, 'ap': ap}, f)
print('Mean AP = {:.4f}'.format(np.mean(aps)))
print('~~~~~~~~')
print('Results:')
for ap in aps:
print('{:.3f}'.format(ap))
print('{:.3f}'.format(np.mean(aps)))
print('~~~~~~~~')
print('')
print('--------------------------------------------------------------')
print('Results computed with the **unofficial** Python eval code.')
print('Results should be very close to the official MATLAB eval code.')
print('-- Thanks, The Management')
print('--------------------------------------------------------------')
if __name__ == '__main__':
args = parse_args()
output_dir = os.path.abspath(args.output_dir[0])
print(output_dir);
with open(args.class_file, 'r') as f:
lines = f.readlines()
classes = [t.strip('\n') for t in lines]
print('Evaluating detections')
do_python_eval(args.voc_dir, args.year, args.image_set, classes, output_dir)

View File

@@ -0,0 +1,194 @@
import xml.etree.ElementTree as ET
import os
import _pickle as cPickle
import numpy as np
def parse_rec(filename):
""" Parse a PASCAL VOC xml file """
tree = ET.parse(filename)
objects = []
for obj in tree.findall('object'):
obj_struct = {}
obj_struct['name'] = obj.find('name').text
#obj_struct['pose'] = obj.find('pose').text
#obj_struct['truncated'] = int(obj.find('truncated').text)
obj_struct['difficult'] = int(obj.find('difficult').text)
bbox = obj.find('bndbox')
obj_struct['bbox'] = [int(bbox.find('xmin').text),
int(bbox.find('ymin').text),
int(bbox.find('xmax').text),
int(bbox.find('ymax').text)]
objects.append(obj_struct)
return objects
def voc_ap(rec, prec, use_07_metric=False):
""" ap = voc_ap(rec, prec, [use_07_metric])
Compute VOC AP given precision and recall.
If use_07_metric is true, uses the
VOC 07 11 point method (default:False).
"""
if use_07_metric:
# 11 point metric
ap = 0.
for t in np.arange(0., 1.1, 0.1):
if np.sum(rec >= t) == 0:
p = 0
else:
p = np.max(prec[rec >= t])
ap = ap + p / 11.
else:
# correct AP calculation
# first append sentinel values at the end
mrec = np.concatenate(([0.], rec, [1.]))
mpre = np.concatenate(([0.], prec, [0.]))
# compute the precision envelope
for i in range(mpre.size - 1, 0, -1):
mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])
# to calculate area under PR curve, look for points
# where X axis (recall) changes value
i = np.where(mrec[1:] != mrec[:-1])[0]
# and sum (\Delta recall) * prec
ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
return ap
def voc_eval(detpath,
annopath,
imagesetfile,
classname,
cachedir,
ovthresh=0.5,
use_07_metric=False):
"""rec, prec, ap = voc_eval(detpath,
annopath,
imagesetfile,
classname,
[ovthresh],
[use_07_metric])
Top level function that does the PASCAL VOC evaluation.
detpath: Path to detections
detpath.format(classname) should produce the detection results file.
annopath: Path to annotations
annopath.format(imagename) should be the xml annotations file.
imagesetfile: Text file containing the list of images, one image per line.
classname: Category name (duh)
cachedir: Directory for caching the annotations
[ovthresh]: Overlap threshold (default = 0.5)
[use_07_metric]: Whether to use VOC07's 11 point AP computation
(default False)
"""
# assumes detections are in detpath.format(classname)
# assumes annotations are in annopath.format(imagename)
# assumes imagesetfile is a text file with each line an image name
# cachedir caches the annotations in a pickle file
# first load gt
if not os.path.isdir(cachedir):
os.mkdir(cachedir)
cachefile = os.path.join(cachedir, 'annots.pkl')
# read list of images
with open(imagesetfile, 'r') as f:
lines = f.readlines()
imagenames = [x.strip() for x in lines]
if not os.path.isfile(cachefile):
# load annots
recs = {}
for i, imagename in enumerate(imagenames):
recs[imagename] = parse_rec(annopath.format(imagename))
#if i % 100 == 0:
#print('Reading annotation for {:d}/{:d}').format(i + 1, len(imagenames))
# save
#print('Saving cached annotations to {:s}').format(cachefile)
with open(cachefile, 'wb') as f:
cPickle.dump(recs, f)
else:
# load
print('!!! cachefile = ',cachefile)
with open(cachefile, 'rb') as f:
recs = cPickle.load(f)
# extract gt objects for this class
class_recs = {}
npos = 0
for imagename in imagenames:
R = [obj for obj in recs[imagename] if obj['name'] == classname]
bbox = np.array([x['bbox'] for x in R])
difficult = np.array([x['difficult'] for x in R]).astype(np.bool)
det = [False] * len(R)
npos = npos + sum(~difficult)
class_recs[imagename] = {'bbox': bbox,
'difficult': difficult,
'det': det}
# read dets
detfile = detpath.format(classname)
with open(detfile, 'r') as f:
lines = f.readlines()
splitlines = [x.strip().split(' ') for x in lines]
image_ids = [x[0] for x in splitlines]
confidence = np.array([float(x[1]) for x in splitlines])
BB = np.array([[float(z) for z in x[2:]] for x in splitlines])
# sort by confidence
sorted_ind = np.argsort(-confidence)
sorted_scores = np.sort(-confidence)
BB = BB[sorted_ind, :]
image_ids = [image_ids[x] for x in sorted_ind]
# go down dets and mark TPs and FPs
nd = len(image_ids)
tp = np.zeros(nd)
fp = np.zeros(nd)
for d in range(nd):
R = class_recs[image_ids[d]]
bb = BB[d, :].astype(float)
ovmax = -np.inf
BBGT = R['bbox'].astype(float)
if BBGT.size > 0:
# compute overlaps
# intersection
ixmin = np.maximum(BBGT[:, 0], bb[0])
iymin = np.maximum(BBGT[:, 1], bb[1])
ixmax = np.minimum(BBGT[:, 2], bb[2])
iymax = np.minimum(BBGT[:, 3], bb[3])
iw = np.maximum(ixmax - ixmin + 1., 0.)
ih = np.maximum(iymax - iymin + 1., 0.)
inters = iw * ih
# union
uni = ((bb[2] - bb[0] + 1.) * (bb[3] - bb[1] + 1.) +
(BBGT[:, 2] - BBGT[:, 0] + 1.) *
(BBGT[:, 3] - BBGT[:, 1] + 1.) - inters)
overlaps = inters / uni
ovmax = np.max(overlaps)
jmax = np.argmax(overlaps)
if ovmax > ovthresh:
if not R['difficult'][jmax]:
if not R['det'][jmax]:
tp[d] = 1.
R['det'][jmax] = 1
else:
fp[d] = 1.
else:
fp[d] = 1.
# compute precision recall
fp = np.cumsum(fp)
tp = np.cumsum(tp)
rec = tp / float(npos)
# avoid divide by zero in case the first detection matches a difficult
# ground truth
prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps)
ap = voc_ap(rec, prec, use_07_metric)
return rec, prec, ap

View File

@@ -7,7 +7,7 @@ d='.' # unzip directory
url=https://github.com/ultralytics/yolov5/releases/download/v1.0/
f1=VOCtrainval_06-Nov-2007.zip # 446MB, 5012 images
f2=VOCtest_06-Nov-2007.zip # 438MB, 4953 images
for f in $f3 $f2 $f1; do
for f in $f2 $f1; do
echo 'Downloading' $url$f '...'
curl -L $url$f -o $f && unzip -q $f -d $d && rm $f & # download, unzip, remove in background
done

View File

@@ -24,8 +24,8 @@
#include "color.h"
float pad_w, pad_h;
float ratio;
float pad_w = 0.0, pad_h = 0.0;
float ratio = 1.0;
namespace {
@@ -227,18 +227,27 @@ void SaveTxt(const std::string& result, const std::string& line){
int main(int argc, char* argv[])
{
if(argc < 2){
std::string msg = "usage : bash run.sh [image_set] [pipline_file]";
cout<<msg;
if(argc < 3){
std::string msg = "usage : bash run.sh [task_type][image_set][image_dir] or bash run.sh eval [dataset_path]";
std::cout<<msg<<std::endl;
return 1;
}
const std::string image_set_file = argv[1];
const std::string pipelineConfigPath = argv[2];
const std::string task = argv[1];
std::string image_set_file = argv[2];
std::string image_set_path = argv[3];
std::string pipelineConfigPath = "";
if(task == "eval"){
pipelineConfigPath = "pipeline/eval.pipeline";
}else if(task == "speed" || task == "detect"){
pipelineConfigPath = "pipeline/detect.pipeline";
}else {
std::cout<<"Undefined task!"<<std::endl;
return 1;
}
bool save_image = false, save_txt = false;
bool eval = pipelineConfigPath.find("eval") != std::string::npos;
bool save_image = false, save_txt = true, speed = false;
if(task == "eval") save_txt = true;
if(task == "detect") save_image = true;
double time_min = DBL_MAX;
double time_max = -DBL_MAX;
@@ -246,7 +255,6 @@ int main(int argc, char* argv[])
long loop_num = 0;
std::ifstream in(image_set_file);
std::ofstream *outfile;
std::string line;
if(save_image){
@@ -282,22 +290,26 @@ int main(int argc, char* argv[])
while(getline(in, line)){
loop_num++;
std::string streamName = "detection";
std::string img_path = "/home/wangshengke3/VOCdevkit/VOC2007/JPEGImages/"+line+".jpg";
cv::Mat src = cv::imread(img_path);
cv::Mat img = letterBox(src);
cv::imwrite("./tmp.jpg", img);
std::string img_path = image_set_path+'/'+line+".jpg";
MxStream::MxstDataInput dataBuffer;
ret = ReadFile("./tmp.jpg", dataBuffer);
cv::Mat src;
auto start = clock();
if(task == "eval"){
src = cv::imread(img_path);
cv::Mat img = letterBox(src);
cv::imwrite("./tmp.jpg", img);
ret = ReadFile("./tmp.jpg", dataBuffer);
}else if(task == "detect"){
src = cv::imread(img_path);
ret = ReadFile(img_path,dataBuffer);
}else{
ret = ReadFile(img_path,dataBuffer);
}
if (ret != APP_ERR_OK) {
LogError << GetError(ret) << "Failed to read image file.";
return ret;
}
auto start = clock();
// send data into stream
ret = mxStreamManager.SendData(streamName, inPluginId, dataBuffer);
if (ret != APP_ERR_OK) {
@@ -319,7 +331,6 @@ int main(int argc, char* argv[])
time_max = (std::max)(time_max, time);
time_avg += time;
std::string result = std::string((char *)output->dataPtr, output->dataSize);
// LogInfo <<"Results:" << result;
if(save_image == true)
SaveImage(result, src, line);
@@ -331,14 +342,15 @@ int main(int argc, char* argv[])
dataBuffer.dataPtr = nullptr;
}
time_avg /= loop_num;
char msg[256];
sprintf(msg,"image count = %ld\n min = %.2fms max = %.2fms avg = %.2fms \n avg fps = %.2f", loop_num, time_min *1000, time_max*1000, time_avg*1000, 1000/time_max*1000);
LogInfo<<"推理时间统计:";
LogInfo<<msg;
}
in.close();
mxStreamManager.DestroyAllStreams();
mxStreamManager.DestroyAllStreams();
char msg[256];
sprintf(msg,"image count = %ld \nmin = %.2fms max = %.2fms avg = %.2fms \navg fps = %.2f fps\n", loop_num, time_min *1000, time_max*1000, time_avg*1000, 1000/(time_avg*1000));
std::cout<<"时间统计:\n";
std::cout<< msg;
return 0;
}

View File

@@ -3,7 +3,7 @@ source env.sh
atc \
--model=prune55_t.onnx \
--framework=5 \
--output=./prune55_t_rgb \
--output=./prune55_t \
--input_format=NCHW \
--input_shape="images:1,3,512,512" \
--enable_small_channel=1 \

View File

@@ -3,7 +3,7 @@
"stream_config": {
"deviceId": "0"
},
"mxpi_imagedecoder0": {
"mxpi_imagedecoder0": {
"factory": "mxpi_imagedecoder",
"next": "mxpi_imageresize0"
},
@@ -19,7 +19,7 @@
},
"mxpi_modelinfer0": {
"props": {
"parentName": "mxpi_imagedecoder0",
"parentName": "mxpi_imageresize0",
"modelPath": "models/yolov5/prune55_t.om",
"postProcessConfigPath": "models/yolov5/yolov5_detect.cfg",
"labelPath": "models/yolov5/voc.names",

View File

@@ -11,7 +11,7 @@
"props": {
"parentName": "mxpi_imagedecoder0",
"modelPath": "models/yolov5/prune55_t.om",
"postProcessConfigPath": "models/yolov5/yolov5.cfg",
"postProcessConfigPath": "models/yolov5/yolov5_eval.cfg",
"labelPath": "models/yolov5/voc.names",
"postProcessLibPath": "libMpYOLOv5PostProcessor.so"
},

View File

@@ -5,9 +5,51 @@
# Author: MindX SDK
# Create: 2020
# History: NA
if [ $# -le 1 ]
then
echo "Usage:"
echo "bash run.sh [task_type][image_set][image_dir] or bash run.sh eval [dataset_path]"
exit 1
fi
get_real_path() {
if [ "${1:0:1}" == "/" ]; then
echo "$1"
else
echo "$(realpath -m $PWD/$1)"
fi
}
if [[ "${1}"x = "eval"x ]]
then
task_type=$1
dataset_path=$(get_real_path $2)
image_set=$(get_real_path $2)"/VOC2007/ImageSets/Main/test.txt"
image_dir=$(get_real_path $2)"/VOC2007/JPEGImages"
elif [[ "${1}"x = "detect"x || "${1}"x = "speed"x ]]
then
task_type=$1
image_set=$(get_real_path $2)
image_dir=$(get_real_path $3)
else
echo "Undefined task!"
exit 1
fi
if [[ ! -f $image_set ]]
then
echo "error : $image_set is not a file"
exit 1
fi
if [[ ! -d $image_dir ]]
then
echo "error : $image_dir is not a dir"
exit 1
fi
if [[ ! -d $dataset_path ]]
then
echo "error : $dataset_path is not a dir"
exit 1
fi
set -e
CUR_PATH=$(cd "$(dirname "$0")" || { warn "Failed to check path/to/run.sh" ; exit ; } ; pwd)
# Simple log helper functions
@@ -23,9 +65,18 @@ rm -rf ./build
# complie
cmake -S . -Bbuild
make -C ./build -j
echo "build done"
export LD_LIBRARY_PATH="${MX_SDK_HOME}/lib":"${MX_SDK_HOME}/opensource/lib":"${MX_SDK_HOME}/opensource/lib64":${LD_LIBRARY_PATH}
# run
./main
echo "start" $task_type "task"
echo "image_set" $image_set
echo "image_dir" $image_dir
./main $task_type $image_set $image_dir
if [[ "$task_type"x = "eval"x ]]
then
echo "compute mAP..."
python compute_mAP/reval_voc.py txt_result --voc_dir $dataset_path
fi
exit 0