MatConvNet包:运行fast_rcnn_demo.m

2024-01-14 14:18
文章标签 运行 demo fast rcnn matconvnet

本文主要是介绍MatConvNet包:运行fast_rcnn_demo.m,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

准备工作

首先,在以下路径中可以找到fast_rcnn_demo.m:

  • matconvnet/examples/fast_rcnn/fast_rcnn_demo.m

  • 除此以外,还包含了两个重要文件fast_rcnn_train.m(用于训练模型)和fast_rcnn_demo.m(用于测试模型的准确率)。注意:这两个文件都需要数据库依赖,本文不涉及。

    1. 在官网下载预训练好的网络:http://pjreddie.com/projects/pascal-voc-dataset-mirror/。

    2. 把网络文件解压后放到 matconvnet/data/models 文件夹下 。
      fast_rcnn_net位置

matlab命令行窗口运行 fast_rcnn_demo()

function fast_rcnn_demo(varargin)
%FAST_RCNN_DEMO  Demonstrates Fast-RCNN
%
% Copyright (C) 2016 Abhishek Dutta and Hakan Bilen.
% All rights reserved.
%
% This file is part of the VLFeat library and is made available under
% the terms of the BSD license (see the COPYING file).# 自动设置路径
run(fullfile(fileparts(mfilename('fullpath')), ...'..', '..', 'matlab', 'vl_setupnn.m')) ;# bounding box相关的functions的路径添加到matlab
addpath(fullfile(vl_rootnn,'examples','fast_rcnn','bbox_functions')) ;# opts结构体??
opts.modelPath = '' ;
opts.classes = {'car'} ;
opts.gpu = [] ;
opts.confThreshold = 0.5 ;
opts.nmsThreshold = 0.3 ;
opts = vl_argparse(opts, varargin) ;# 看这里的代码,其实可以发现,如果把下载的.mat文件放在paths所列路径中的任何一个就可以了
# paths是待选模型路径列表
% Load or download the Fast RCNN model
paths = {opts.modelPath, ...'./fast-rcnn-vgg16-dagnn.mat', ...fullfile(vl_rootnn, 'data', 'models', 'fast-rcnn-vgg16-pascal07-dagnn.mat'), ...fullfile(vl_rootnn, 'data', 'models-import', 'fast-rcnn-vgg16-pascal07-dagnn.mat')} ;# cellfun(@(x)exist(x,'file'), paths)
# 以cell数组paths的members作为输入x,输入到exist(x,'file')
# 得到输出数组,分别代表各个路径是否存在
# find()返回其index
# 优先选取参数最小的:即paths中的排序靠前的为优先考虑的路径
ok = min(find(cellfun(@(x)exist(x,'file'), paths))) ;# 本地存在.mat则把相应路径设置opts.modelPath,否则自动下载
# http://www.vlfeat.org/matconvnet/models/fast-rcnn-vgg16-pascal07-dagnn.mat
if isempty(ok)fprintf('Downloading the Fast RCNN model ... this may take a while\n') ;opts.modelPath = fullfile(vl_rootnn, 'data', 'models', 'fast-rcnn-vgg16-pascal07-dagnn.mat') ;mkdir(fileparts(opts.modelPath)) ;urlwrite('http://www.vlfeat.org/matconvnet/models/fast-rcnn-vgg16-pascal07-dagnn.mat', ...opts.modelPath) ;
elseopts.modelPath = paths{ok} ;
end# 加载,设置‘测试’模式
% Load the network and put it in test mode.
net = load(opts.modelPath) ;
net = dagnn.DagNN.loadobj(net);
net.mode = 'test' ;# 分类和边界框预测标为'precious',避免评价期间优化它们
# 即设置网络的输出层,避免其继续迭代(fast_rcnn会交互迭代bbox层和cls层)
% Mark class and bounding box predictions as `precious` 
% so they are not optimized away during evaluation.
net.vars(net.getVarIndex('cls_prob')).precious = 1 ;
net.vars(net.getVarIndex('bbox_pred')).precious = 1 ;# 加载一幅测试图像和bounding box竞选者(一系列bounding box坐标)
% Load a test image and candidate bounding boxes.
im = single(imread('000004.jpg')) ;
imo = im; % keep original image
boxes = load('000004_boxes.mat') ;# 转置`,并保存原boxes
boxes = single(boxes.boxes`) + 1 ; 
% keep original boxes
boxeso = boxes - 1; # 把图像和boxes调整到适应网络的大小
% Resize images and boxes to a size compatible with the network.
imageSize = size(im) ;# normalization.imagesize为224*224*3
# .cropSize为小数表示的裁剪倍数,原始尺寸fullImageSize经过crop后得到的imagesize,所以这里是以下公式
fullImageSize = net.meta.normalization.imageSize(1)/ net.meta.normalization.cropSize ;# 缩放倍数选择更大的,使缩放后的图像大小不小于标准的600*600
scale = max(fullImageSize ./ imageSize(1:2)) ;# interpolatoin选择插值方式(双三),缩小图像时不消除锯齿,
im=imresize(im,scale,net.meta.normalization.interpolation,'antialiasing', false) ;
# 变换boxes
boxes = bsxfun(@times, boxes - 1, scale) + 1 ;% Remove the average color from the input image.
imNorm = bsxfun(@minus, im, net.meta.normalization.averageImage) ;boxes是4*2888的向量,rois多一行(第一行)全是1
% Convert boxes into ROIs by prepending the image index. There is only
% one image in this batch.
rois = [ones(1,size(boxes,2)) ; boxes] ;% Evaluate network either on CPU or GPU.
if numel(opts.gpu) > 0gpuDevice(opts.gpu) ;imNorm = gpuArray(imNorm) ;rois = gpuArray(rois) ;net.move('gpu') ;
endnet.conserveMemory = false ;# 输入data和感兴趣区域rois,进行评价,
# 结果在net.vars最后一层的value中
net.eval({'data', imNorm, 'rois', rois});# 提取结果,包括分类结果'cls_prob'
# 和对应的bounding_box位置'bbox_pred'
% Extract class probabilities and  bounding box refinements
probs = squeeze(gather(net.vars(net.getVarIndex('cls_prob')).value)) ;
deltas = squeeze(gather(net.vars(net.getVarIndex('bbox_pred')).value)) ;# 依次可视化
% Visualize results for one class at a time
for i = 1:numel(opts.classes)# 获取opts.classes{i}所代表的类别在probs(k,:)中的indexc = find(strcmp(opts.classes{i}, net.meta.classes.name)) ;# 获取'cars'的概率矩阵cprobs:1*2888# 4*(c-1)+(1:4)为boxeso`(k,:)中'cars'的对应下标# 列出每个'cars'类candidates的boxeso`(k,:)下标,cdeltas:1*4# 合并为cboxes:2888*5cprobs = probs(c,:) ;cdeltas = deltas(4*(c-1)+(1:4),:)` ;cboxes = bbox_transform_inv(boxeso`, cdeltas);cls_dets = [cboxes cprobs`] ;# 采用‘非最大值抑制’,保留cls_dets中prob大于阈值0.3的boxeskeep = bbox_nms(cls_dets, opts.nmsThreshold) ;cls_dets = cls_dets(keep, :) ;# 再次筛选分类概率大于0.5的bbox选框sel_boxes = find(cls_dets(:,end) >= opts.confThreshold) ;# 显示bbox并在标题上说明类别imo = bbox_draw(imo/255,cls_dets(sel_boxes,:));title(sprintf('Detections for class ''%s''', opts.classes{i})) ;# 在屏幕上打印分类结果fprintf('Detections for category ''%s'':\n', opts.classes{i});for j=1:size(sel_boxes,1)bbox_id = sel_boxes(j,1);fprintf('\t(%.1f,%.1f)\t(%.1f,%.1f)\tprobability=%.6f\n', ...cls_dets(bbox_id,1), cls_dets(bbox_id,2), ...cls_dets(bbox_id,3), cls_dets(bbox_id,4), ...cls_dets(bbox_id,end));end
end
  • vl_rootnn是matconvnet/matlab/vl_rootnn.m,直接产生根路径。

这篇关于MatConvNet包:运行fast_rcnn_demo.m的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/605455

相关文章

通过prometheus监控Tomcat运行状态的操作流程

《通过prometheus监控Tomcat运行状态的操作流程》文章介绍了如何安装和配置Tomcat,并使用Prometheus和TomcatExporter来监控Tomcat的运行状态,文章详细讲解了... 目录Tomcat安装配置以及prometheus监控Tomcat一. 安装并配置tomcat1、安装

mysqld_multi在Linux服务器上运行多个MySQL实例

《mysqld_multi在Linux服务器上运行多个MySQL实例》在Linux系统上使用mysqld_multi来启动和管理多个MySQL实例是一种常见的做法,这种方式允许你在同一台机器上运行多个... 目录1. 安装mysql2. 配置文件示例配置文件3. 创建数据目录4. 启动和管理实例启动所有实例

IDEA运行spring项目时,控制台未出现的解决方案

《IDEA运行spring项目时,控制台未出现的解决方案》文章总结了在使用IDEA运行代码时,控制台未出现的问题和解决方案,问题可能是由于点击图标或重启IDEA后控制台仍未显示,解决方案提供了解决方法... 目录问题分析解决方案总结问题js使用IDEA,点击运行按钮,运行结束,但控制台未出现http://

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

Linux使用nohup命令在后台运行脚本

《Linux使用nohup命令在后台运行脚本》在Linux或类Unix系统中,后台运行脚本是一项非常实用的技能,尤其适用于需要长时间运行的任务或服务,本文我们来看看如何使用nohup命令在后台... 目录nohup 命令简介基本用法输出重定向& 符号的作用后台进程的特点注意事项实际应用场景长时间运行的任务服

如何在一台服务器上使用docker运行kafka集群

《如何在一台服务器上使用docker运行kafka集群》文章详细介绍了如何在一台服务器上使用Docker运行Kafka集群,包括拉取镜像、创建网络、启动Kafka容器、检查运行状态、编写启动和关闭脚本... 目录1.拉取镜像2.创建集群之间通信的网络3.将zookeeper加入到网络中4.启动kafka集群

PostgreSQL如何用psql运行SQL文件

《PostgreSQL如何用psql运行SQL文件》文章介绍了两种运行预写好的SQL文件的方式:首先连接数据库后执行,或者直接通过psql命令执行,需要注意的是,文件路径在Linux系统中应使用斜杠/... 目录PostgreSQ编程L用psql运行SQL文件方式一方式二总结PostgreSQL用psql运

如何用Docker运行Django项目

本章教程,介绍如何用Docker创建一个Django,并运行能够访问。 一、拉取镜像 这里我们使用python3.11版本的docker镜像 docker pull python:3.11 二、运行容器 这里我们将容器内部的8080端口,映射到宿主机的80端口上。 docker run -itd --name python311 -p

跨系统环境下LabVIEW程序稳定运行

在LabVIEW开发中,不同电脑的配置和操作系统(如Win11与Win7)可能对程序的稳定运行产生影响。为了确保程序在不同平台上都能正常且稳定运行,需要从兼容性、驱动、以及性能优化等多个方面入手。本文将详细介绍如何在不同系统环境下,使LabVIEW开发的程序保持稳定运行的有效策略。 LabVIEW版本兼容性 LabVIEW各版本对不同操作系统的支持存在差异。因此,在开发程序时,尽量使用