TVM上YOLO-DarkNet的部署已经在之前的文章TVM上部署YOLO-DarkNet及单图性能对比中介绍了。在单图测试结果中,TVM的速度提升约为1.27x。测出的时间数据显示,TVM测试代码中的STAGE1,也就是将模型导入Relay、编译模型的阶段是耗时最长的部分,而导入检测图片和执行检测图片的过程耗时较少。于是本文进一步使用多张图片进行测试。
第一部分 不使用TVM运行YOLO-DarkNet
YOLO-DarkNet的环境配置已经在之前的文章中介绍了。
之前讲到,运用YOLO进行单张图片检测的命令是:
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
如果要进行多张图片检测的话,需要对程序进行修改,实现批量测试图片并保存在自定义文件夹下,主要修改的是examples
目录下的detector.c
文件。
第一步,用下面的代码替换detector.c
中的test_detector
函数,请注意有三处路径需要改成自己的路径:
void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen)
{
list *options = read_data_cfg(datacfg);
char *name_list = option_find_str(options, "names", "data/names.list");
char **names = get_labels(name_list);
image **alphabet = load_alphabet();
network *net = load_network(cfgfile, weightfile, 0);
set_batch_network(net, 1);
srand(2222222);
double time;
char buff[256];
char *input = buff;
float nms=.45;
int i=0;
while(1){
if(filename){
strncpy(input, filename, 256);
image im = load_image_color(input,0,0);
image sized = letterbox_image(im, net->w, net->h);
//image sized = resize_image(im, net->w, net->h);
//image sized2 = resize_max(im, net->w);
//image sized = crop_image(sized2, -((net->w - sized2.w)/2), -((net->h - sized2.h)/2), net->w, net->h);
//resize_network(net, sized.w, sized.h);
layer l = net->layers[net->n-1];
float *X = sized.data;
time=what_time_is_it_now();
network_predict(net, X);
printf("%s: Predicted in %f seconds.\n", input, what_time_is_it_now()-time);
int nboxes = 0;
detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);
//printf("%d\n", nboxes);
//if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
draw_detections(im, dets, nboxes, thresh, names, alphabet, l.classes);
free_detections(dets, nboxes);
if(outfile)
{
save_image(im, outfile);
}
else{
save_image(im, "predictions");
#ifdef OPENCV
cvNamedWindow("predictions", CV_WINDOW_NORMAL);
if(fullscreen){
cvSetWindowProperty("predictions", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
}
show_image(im, "predictions");
cvWaitKey(0);
cvDestroyAllWindows();
#endif
}
free_image(im);
free_image(sized);
if (filename) break;
}
else {
</