用PaddlePaddle 实现目标检测任务——Paddle Fluid v1.1深度测评
CPU/GPU占用率:正常训练情况下CPU占用率在40%-60%之间,GPU占用率稳定在50%左右。 CPU/GPU使用情况 4.6模型评估在PaddlePaddle的SSD模型中,可以使用eval.py脚本进行模型评估,可以选择11point、integral等方法来计算模型在验证集上的mAP。 python eval.py --dataset='pascalvoc' --model_dir='train_pascal_model/best_model' --data_dir='data/pascalvoc' --test_list='test.txt' --ap_version='11point' --nms_threshold=0.45 其中,model_dir是我们训练好的模型的保存目录,data_dir是数据集目录,test_list是作为验证集的文件列表(txt文件),前提是这些文件必须要有对应的标签文件,ap_version是计算mAP的方法,nms_threshold是分类阈值。最后我们得到PaddlePaddle SSD模型在Pascal VOC数据集上的mAP为73.32%[2] 4.7模型预测及可视化4.7.1模型预测模型训练完成后,用test_program = fluid.default_main_program().clone(for_test=True)将Program转换到test模式,然后把要预测的数据feed进Executor执行Program就可以计算得到图像的分类标签、目标框的得分、xmin、ymin、xmax、ymax。具体过程如下: test_program = fluid.default_main_program().clone(for_test=True) image = fluid.layers.data(name='image', shape=image_shape, dtype='float32') locs, confs, box, box_var = mobile_net(num_classes, image, image_shape) nmsed_out = fluid.layers.detection_output( locs, confs, box, box_var, nms_threshold=args.nms_threshold) place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace() exe = fluid.Executor(place) nmsed_out_v, = exe.run(test_program, feed=feeder.feed([[data]]), fetch_list=[nmsed_out], return_numpy=False) nmsed_out_v = np.array(nmsed_out_v) 4.7.2预测结果可视化对于目标检测任务,我们通常需要对预测结果进行可视化进而获得对结果的感性认识。我们可以编写一个程序,让它在原图像上画出预测框,核心代码如下: def draw_bounding_box_on_image(image_path, nms_out, confs_threshold, label_list): image = Image.open(image_path) draw = ImageDraw.Draw(image) im_width, im_height = image.size for dt in nms_out: if dt[1] < confs_threshold: continue category_id = dt[0] bbox = dt[2:] xmin, ymin, xmax, ymax = clip_bbox(dt[2:]) (left, right, top, bottom) = (xmin * im_width, xmax * im_width, ymin * im_height, ymax * im_height) draw.line( [(left, top), (left, bottom), (right, bottom), (right, top), (left, top)], width=4, fill='red') if image.mode == 'RGB': draw.text((left, top), label_list[int(category_id)], (255, 255, 0)) image_name = image_path.split('/')[-1] print("image with bbox drawed saved as {}".format(image_name)) image.save(image_name) 这样,我们可以很直观的看到预测结果: 令人欣喜的是,PaddlePaddle的SSD模型中帮我们实现了完整的一套预测流程,我们可以直接运行SSD model下的infer.py脚本使用训练好的模型对图片进行预测: python infer.py --dataset='coco' --nms_threshold=0.45 --model_dir='pretrained/ssd_mobilenet_v1_coco' --image_path='./data/ pascalvoc/VOCdevkit/VOC2012/JPEGImages/2007_002216.jpg' 4.8模型部署PaddlePaddle的模型部署需要先安装编译C++预测库,可以在http://www.paddlepaddle.org/documentation/docs/zh/1.1/user_guides/howto/inference/build_and_install_lib_cn.html下载安装。预测库中提供了Paddle的预测API,预测部署过程大致分为三个步骤:1.创建PaddlePredictor;2.创建PaddleTensor传入PaddlePredictor中;3.获取输出 PaddleTensor,输出结果。这部分操作也并不复杂,而且Paddle的教程中也提供了一份部署详细代码参考,大家可以很快地利用这个模板完成模型部署(https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/fluid/inference/api/demo_ci) 5.使用感受
(编辑:成都站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |