1. faster rcnn test demo ---repaired for video input and save the image, label, score et al. into .mat format
  2.  
  3. function script_faster_rcnn_demo()
  4. close all;
  5. clc;
  6. clear mex;
  7. clear is_valid_handle; % to clear init_key
  8. run(fullfile(fileparts(fileparts(mfilename('fullpath'))), 'startup'));
  9. %% -------------------- CONFIG --------------------
  10. opts.caffe_version = 'caffe_faster_rcnn';
  11. opts.gpu_id = auto_select_gpu;
  12. active_caffe_mex(opts.gpu_id, opts.caffe_version);
  13.  
  14. opts.per_nms_topN = 6000;
  15. opts.nms_overlap_thres = 0.7;
  16. opts.after_nms_topN = 300;
  17. opts.use_gpu = true;
  18.  
  19. opts.test_scales = 600;
  20.  
  21. % %% -------------------- INIT_MODEL --------------------
  22. % model_dir = fullfile(pwd, 'output', 'faster_rcnn_final', 'faster_rcnn_VOC0712_vgg_16layers'); %% VGG-16
  23. model_dir = fullfile(pwd, 'output', 'faster_rcnn_final', 'faster_rcnn_VOC0712_ZF'); %% ZF
  24. proposal_detection_model = load_proposal_detection_model(model_dir);
  25.  
  26. proposal_detection_model.conf_proposal.test_scales = opts.test_scales;
  27. proposal_detection_model.conf_detection.test_scales = opts.test_scales;
  28. if opts.use_gpu
  29. proposal_detection_model.conf_proposal.image_means = gpuArray(proposal_detection_model.conf_proposal.image_means);
  30. proposal_detection_model.conf_detection.image_means = gpuArray(proposal_detection_model.conf_detection.image_means);
  31. end
  32.  
  33. % caffe.init_log(fullfile(pwd, 'caffe_log'));
  34. % proposal net
  35. rpn_net = caffe.Net(proposal_detection_model.proposal_net_def, 'test');
  36. rpn_net.copy_from(proposal_detection_model.proposal_net);
  37. % fast rcnn net
  38. fast_rcnn_net = caffe.Net(proposal_detection_model.detection_net_def, 'test');
  39. fast_rcnn_net.copy_from(proposal_detection_model.detection_net);
  40.  
  41. % set gpu/cpu
  42. if opts.use_gpu
  43. caffe.set_mode_gpu();
  44. else
  45. caffe.set_mode_cpu();
  46. end
  47.  
  48. %% -------------------- WARM UP --------------------
  49. % the first run will be slower; use an empty image to warm up
  50.  
  51. for j = 1:2 % we warm up 2 times
  52. im = uint8(ones(375, 500, 3)*128);
  53. if opts.use_gpu
  54. im = gpuArray(im);
  55. end
  56. [boxes, scores] = proposal_im_detect(proposal_detection_model.conf_proposal, rpn_net, im);
  57. aboxes = boxes_filter([boxes, scores], opts.per_nms_topN, opts.nms_overlap_thres, opts.after_nms_topN, opts.use_gpu);
  58. if proposal_detection_model.is_share_feature
  59. [boxes, scores] = fast_rcnn_conv_feat_detect(proposal_detection_model.conf_detection, fast_rcnn_net, im, ...
  60. rpn_net.blobs(proposal_detection_model.last_shared_output_blob_name), ...
  61. aboxes(:, 1:4), opts.after_nms_topN);
  62. else
  63. [boxes, scores] = fast_rcnn_im_detect(proposal_detection_model.conf_detection, fast_rcnn_net, im, ...
  64. aboxes(:, 1:4), opts.after_nms_topN);
  65. end
  66. end
  67.  
  68. %% -------------------- TESTING --------------------
  69. % im_names = {'001763.jpg', '004545.jpg', '000542.jpg', '000456.jpg', '001150.jpg'};
  70. % these images can be downloaded with fetch_faster_rcnn_final_model.m
  71. % for j = 1:length(im_names)
  72. % im = imread(fullfile(pwd, im_names{j}));
  73.  
  74. %% -------------------------- Video ------------------
  75. %% initialization
  76. video_names = {};
  77.  
  78. %% --------------path for test ------------
  79. path = '/media/wangxiao/247317a3-e6b5-45d4-81d1-956930526746/video/test_video/';
  80. savePath = '/media/wangxiao/247317a3-e6b5-45d4-81d1-956930526746/video/test_video_images/';
  81.  
  82. %% ---------------path for the 4TB drive----------
  83. % path = '/media/wangxiao/Seagate/pedestrian data/';
  84. % savePath = '/media/wangxiao/Seagate/pedestrian data/pedestrian frame/';
  85.  
  86. % video_dir = dir(path);
  87.  
  88. video_dir = dir(fullfile(path, '*.avi'));
  89. % if isempty(video_dir)
  90. % video_dir = dir(fullfile(path, '*.mp4'));
  91. % end
  92. % if isempty(video_dir)
  93. % video_dir = dir(fullfile(path, '*.rmvb'));
  94. % end
  95. % if isempty(video_dir)
  96. % video_dir = dir(fullfile(path, '*.mkv'));
  97. % end
  98.  
  99. %%
  100. k =1;
  101. for i = 1:length(video_dir)
  102. if ~video_dir(i).isdir
  103. video_names{k} = video_dir(i).name; % 视频的名字 保存在video_name中;
  104. k = k+1;
  105. end
  106. end
  107. clear k;
  108. running_time = [];
  109.  
  110. %% 将视频load进来,切割为图像,存储
  111. for j = 1:length(video_dir) %视频级别;
  112.  
  113. % 弹出错误提示: Could not read file due to an unexpected error. Reason:
  114. % Unable to initialize the video obtain properties ...
  115. frames = VideoReader (strcat(path, video_dir(j).name));
  116. numFrames =frames.NumberOfFrames;
  117. for k = 1 : 30: numFrames
  118. disp(['saving the ',num2str(k),'-th frames, please waiting....']);
  119. frame = read(frames,k); %将frame存储起来;
  120. frame = imresize(frame, [200, 300]); % 是否要对图像进行resize ?
  121. imwrite(frame,[savePath,sprintf('%08d.png',k)]);
  122. end
  123. end
  124.  
  125. %% 读取图像,输入给 faster rcnn
  126. image_path = savePath;
  127. imageFile = dir([image_path, '*.png']);
  128.  
  129. for iii = 1:length(imageFile)
  130. im = imread([image_path, imageFile(iii).name]);
  131.  
  132. if opts.use_gpu
  133. im = gpuArray(im);
  134. end
  135.  
  136. % test proposal
  137. th = tic();
  138. [boxes, scores] = proposal_im_detect(proposal_detection_model.conf_proposal, rpn_net, im);
  139. t_proposal = toc(th);
  140. th = tic();
  141. aboxes = boxes_filter([boxes, scores], opts.per_nms_topN, opts.nms_overlap_thres, opts.after_nms_topN, opts.use_gpu);
  142. t_nms = toc(th);
  143.  
  144. % test detection
  145. th = tic();
  146. if proposal_detection_model.is_share_feature
  147. [boxes, scores] = fast_rcnn_conv_feat_detect(proposal_detection_model.conf_detection, fast_rcnn_net, im, ...
  148. rpn_net.blobs(proposal_detection_model.last_shared_output_blob_name), ...
  149. aboxes(:, 1:4), opts.after_nms_topN);
  150.  
  151. else
  152. [boxes, scores] = fast_rcnn_im_detect(proposal_detection_model.conf_detection, fast_rcnn_net, im, ...
  153. aboxes(:, 1:4), opts.after_nms_topN);
  154.  
  155. end
  156. t_detection = toc(th);
  157.  
  158. fprintf('%s (%dx%d): time %.3fs (resize+conv+proposal: %.3fs, nms+regionwise: %.3fs)\n', ['the ' num2str(j) '-th image '], size(im, 2), size(im, 1), t_proposal + t_nms + t_detection, t_proposal, t_nms+t_detection);
  159. running_time(end+1) = t_proposal + t_nms + t_detection;
  160.  
  161. %% visualize 可视化;
  162. classes = proposal_detection_model.classes; % 20类物体,包括行人这一类;
  163. boxes_cell = cell(length(classes), 1); % 20*1 cell;
  164. thres = 0.6;
  165.  
  166. for i = 1:length(boxes_cell)
  167. boxes_cell{i} = [boxes(:, (1+(i-1)*4):(i*4)), scores(:, i)];
  168. boxes_cell{i} = boxes_cell{i}(nms(boxes_cell{i}, 0.3), :);
  169.  
  170. I = boxes_cell{i}(:, 5) >= thres;
  171. boxes_cell{i} = boxes_cell{i}(I, :);
  172. end
  173.  
  174. figure(j);
  175.  
  176. [location, label, score] = output(im, boxes_cell, classes, 'voc');
  177. if (score==0)
  178. continue;
  179. else
  180. % disp(imageFile(iii).name);
  181. save(['./mat results/' imageFile(iii).name '.mat' ], 'location', 'label', 'score', 'im');
  182. end
  183.  
  184. showboxes(im, boxes_cell, classes, 'voc');
  185.  
  186. pause(0.1);
  187.  
  188. end
  189.  
  190. % eval(['movie_' num2str(k) '_' class_filenames{NUM} '= im_names;']);
  191. % clear im_names;
  192. %
  193. % eval(['movie_bb_' num2str(k) '_' class_filenames{NUM} '= video_cell;'])
  194. % clear video_cell;
  195. %
  196. % if ~exist([fullfile(pwd, 'video_frames'),'/','video.mat'],'file')
  197. % save('./video_frames/video.mat',['movie_' num2str(k) '_' class_filenames{NUM}]);
  198. % save('./video_frames/video_bb.mat',['movie_bb_' num2str(k) '_' class_filenames{NUM}]);
  199. % else
  200. % save('./video_frames/video.mat',['movie_' num2str(k) '_' class_filenames{NUM}],'-append');
  201. % save('./video_frames/video_bb.mat',['movie_bb_' num2str(k) '_' class_filenames{NUM}], '-append');
  202. % end
  203. %
  204. % eval(['clear movie_' num2str(k)]);
  205. % eval(['clear movie_bb_' num2str(k)]);
  206. % save('im_boxes.mat','im_cell');
  207.  
  208. fprintf('mean time: %.3fs\n', mean(running_time));
  209. caffe.reset_all();
  210. clear mex;
  211.  
  212. end
  213.  
  214. function proposal_detection_model = load_proposal_detection_model(model_dir)
  215. ld = load(fullfile(model_dir, 'model'));
  216. proposal_detection_model = ld.proposal_detection_model;
  217. clear ld;
  218.  
  219. proposal_detection_model.proposal_net_def ...
  220. = fullfile(model_dir, proposal_detection_model.proposal_net_def);
  221. proposal_detection_model.proposal_net ...
  222. = fullfile(model_dir, proposal_detection_model.proposal_net);
  223. proposal_detection_model.detection_net_def ...
  224. = fullfile(model_dir, proposal_detection_model.detection_net_def);
  225. proposal_detection_model.detection_net ...
  226. = fullfile(model_dir, proposal_detection_model.detection_net);
  227.  
  228. end
  229.  
  230. function aboxes = boxes_filter(aboxes, per_nms_topN, nms_overlap_thres, after_nms_topN, use_gpu)
  231. % to speed up nms
  232. if per_nms_topN > 0
  233. aboxes = aboxes(1:min(length(aboxes), per_nms_topN), :);
  234. end
  235. % do nms
  236. if nms_overlap_thres > 0 && nms_overlap_thres < 1
  237. aboxes = aboxes(nms(aboxes, nms_overlap_thres, use_gpu), :);
  238. end
  239. if after_nms_topN > 0
  240. aboxes = aboxes(1:min(length(aboxes), after_nms_topN), :);
  241. end
  242. end

faster rcnn test demo ---repaired for video input and save the image, label, score et al. into .mat format的更多相关文章

  1. Faster RCNN算法demo代码解析

    一. Faster-RCNN代码解释 先看看代码结构: Data: This directory holds (after you download them): Caffe models pre-t ...

  2. Windows下如何采用微软的Caffe配置Faster R-CNN

    前言 比较简单的一篇博客.https://github.com/microsoft/caffe 微软的Caffe以在Windows下编译简单而受到了很多人的喜爱(包括我),只用改改prop配置然后无脑 ...

  3. faster rcnn 源码学习-------数据读入及RoIDataLayer相关模块解读

    参考博客:::https://www.cnblogs.com/Dzhen/p/6845852.html 非常全面的解读参考:::https://blog.csdn.net/DaVinciL/artic ...

  4. caffe学习一:ubuntu16.04下跑Faster R-CNN demo (基于caffe). (亲测有效,记录经历两天的吐血经历)

    兜兜转转,兜兜转转; 一次有一次,这次终于把Faster R-CNN 跑通了. 重要提示1:在开始跑Faster R-CNN之前一定要搞清楚用的是Python2 还是Python3. 不然你会无限次陷 ...

  5. faster rcnn训练详解

    http://blog.csdn.net/zy1034092330/article/details/62044941 py-faster-rcnn训练自己的数据:流程很详细并附代码 https://h ...

  6. (原)faster rcnn的tensorflow代码的理解

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/10043864.html 参考网址: 论文:https://arxiv.org/abs/1506.01 ...

  7. faster r-cnn 在CPU配置下训练自己的数据

    因为没有GPU,所以在CPU下训练自己的数据,中间遇到了各种各样的坑,还好没有放弃,特以此文记录此过程. 1.在CPU下配置faster r-cnn,参考博客:http://blog.csdn.net ...

  8. 如何才能将Faster R-CNN训练起来?

    如何才能将Faster R-CNN训练起来? 首先进入 Faster RCNN 的官网啦,即:https://github.com/rbgirshick/py-faster-rcnn#installa ...

  9. 新人如何运行Faster RCNN的tensorflow代码

    0.目的 刚刚学习faster rcnn目标检测算法,在尝试跑通github上面Xinlei Chen的tensorflow版本的faster rcnn代码时候遇到很多问题(我真是太菜),代码地址如下 ...

随机推荐

  1. 修改Azure Website 移动服务 默认时区

    Azure Website 默认时区为国际标准时间,对中国用户来说不太方便友好,如何设置成北京时间呢? 打开Azure Website的“配置”页,找到“应用设置”节点. 在应用设置中添加设置项,密钥 ...

  2. 使用HackRF+GNU Radio 破解吉普车钥匙信号

    引文 我最近对软件定义的无线电技术(SDR)产生了浓厚的兴趣,而我对其中一款流行的SDR平台(HackRF)也产生了兴趣,而其频率接收的范围也在1MHz ~6GHz之间(范围较广).而这里也需要提及一 ...

  3. python3爬虫初探(三)之正则表达式

    前面已经写了如何获取网页源码,那么接下来就是该解析网页并提取需要的数据了.这里简单写一下正则表达的用法. 首先,找个要抓取图片的网站,获取源码. import requests import re # ...

  4. Swift编程规范

    文档编号: 应用开发Swift编码规范 (版本v1.0.0)       成文信息 主题词: Swift开发编码规范 作  者: 周少停 文档类别: 开发规范 审  核: 批  准: 文档性质: 初稿 ...

  5. Palindrome_滚动数组&&DP

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  6. 为什么web标准中无法设置IE浏览器滚动条颜色了?

    <!doctype html><html> <head> <meta charset="UTF-8"> <meta name= ...

  7. 《MORE EFFECTIVE C++》条款20 条款21

    条款20 协助编译器实现返回值优化 当重载运算符的时候,比如+ - * / 这类运算符,该函数返回的值一定是个右值(即不能是引用),那么执行一次运算的开销可能会在临时对象上调用多次构造函数和析构函数, ...

  8. LeetCode Search a 2D Matrix(二分查找)

    题意: 有一个矩阵,每行都有序,每行接在上一行尾后仍然有序.在此矩阵中查找是否存在某个数target. 思路: 这相当于用一个指针连续扫二维数组一样,一直p++就能到最后一个元素了.由于用vector ...

  9. postgresql 分区表创建及测试

    1      建立分区 1.1.  创建主表 CREATE TABLE measurement ( city_id         int not null, logdate        date ...

  10. web app 开发必不可少的滑动插件 Flipsnap

    flipsnap.js一个轻量级的滑动效果JS开发库,仅有8k大小(压缩版),包含了10种滑动方式,是web app开发必备的js库,除了兼容主流的智能手机浏览器(iossafari,android, ...