Faster R-CNN系列之MATLAB篇

我发现,我是个懒人。不对,我一直是个懒人。

但是!电光火石间!不知怎么地!我决定!我写个博客吧害羞


=====================我是废话的分割线===================


最开始接触Faster R-CNN,先尝试跑的其实是PYTHON版,但是编译过程中出错了,我又从来没接触过python,自己稍稍处理了一下没成功,于是就放弃了,跑去跑MATLAB版了......看!再次证明我是有多懒!不过后来因为需要还是没躲过PYTHON,后续在别的博客中再详细说。


恩,首先是各种乱七八糟的Caffe之类的环境配置,因为之前都配好了这里就不说了,恩还有装MATLAB。

然后,就按https://github.com/ShaoqingRen/faster_rcnn这里的一步步来就好了。运行demo可以看到定位识别效果。


接下来重点说明一下怎么用自己的数据训练模型。


首先,要先把自己的数据做成VOC2007的格式。

1.标注图像。就是框图啦,把结果保存到TXT中,matlab代码如下:

  1. close all;  
  2. clc  
  3.   
  4. label = 'XXX';  
  5. folder_path ='pics/';  
  6.   
  7. img_path_list = dir(folder_path);  
  8. img_name_tmp = {img_path_list.name};  
  9. img_name_all = img_name_tmp(3:end);  
  10.   
  11. img_num = length(img_name_all);  % get file num  
  12.   
  13. if img_num > 0  
  14.     for j=1:img_num  
  15.         im_names{j} = [folder_path, img_name_all{j}];  
  16.     end  
  17. end  
  18.   
  19. %  
  20. f = fopen('XXX_label.txt','a+t');  
  21. for j = 1:length(im_names)  
  22.     im = imread(im_names{j});  
  23.     figure,imshow(im);  
  24.    
  25.     fprintf(f,'%s',img_name_tmp{j+2});  
  26.       
  27.     n=input('input the number of XXX:');     
  28.     for i=1:n1   
  29.         [I,RECT] = imcrop();  %RECT:[XMIN YMIN WIDTH HEIGHT]  
  30.         fprintf(f, ' %s  %f  %f  %f  %f',label,RECT(1),RECT(2),RECT(1)+RECT(3),RECT(2)+RECT(4));     %[XMIN YMIN XMAX YMAX]  
  31.     end  
  32.       
  33.     fprintf(f,'\n');  
  34.     close;  
  35. end  
  36. fclose(f);  

生成的txt文件内容大概是酱紫的:

  1. 000001.jpg xxx  2.510000  4.510000  256.490000  252.490000  
  2. 000002.jpg xxx  302.510000  23.510000  346.490000  215.490000  

2.生成XML,放到自己建的VOCxxx/Annotations中,matlab代码如下:

  1. path_image='/home/vision/jinglihua/data_lable/xxx/';  
  2. path_label = 'xxx_label.txt';  
  3. label = 'xxx ';  
  4. files_all=dir(path_image);  
  5.   
  6. fo = fopen(path_label);  
  7. msg = textread(path_label,'%s');  
  8.   
  9. h=1;  
  10. for i = 3:length(files_all)        
  11.       msg1 = fgetl(fo);      
  12.       people = strfind(msg1,label);  
  13.       label_num = length(people);  
  14.       clear rec;  
  15.       clear rec1;  
  16.       path = ['./VOCxxx/Annotations/' msg{h}(1:end-4'.xml'];  
  17.       file=fopen(path,'w');  
  18.       fprintf(file,'<annotation>\n');  
  19.       rec.folder = 'VOCxxx';  
  20.       rec.filename = msg{h}  
  21.       h = h +1;  
  22.       rec.source.database = 'The XXX Database';  
  23.       rec.source.annotation = 'The XXX Database';  
  24.       rec.source.image = 'jinglihua';  
  25.       rec.source.flickrid = '20160504';  
  26.   
  27.       rec.owner.flickrid = 'I do not know';  
  28.       rec.owner.name = 'I do not know';  
  29.    
  30.       img = imread(['/home/vision/jinglihua/data_lable/xxx/' files_all(i).name]);  
  31.    
  32.       rec.size.width = int2str(size(img,2));  
  33.       rec.size.height = int2str(size(img,1));  
  34.       rec.size.depth = int2str(size(img,3));  
  35.    
  36.       rec.segmented = '0';  
  37.       writexml(file,rec,1);            
  38.            
  39.      for j = 1:label_num     
  40.            rec1.object.name = msg{h};  
  41.            h = h +1;  
  42.            rec1.object.pose = 'Unspecified';  
  43.            rec1.object.truncated = '0';  
  44.            rec1.object.difficult = '0';        
  45.            rec1.object.bndbox.xmin = msg{h};  
  46.            h = h +1;  
  47.            rec1.object.bndbox.ymin = msg{h};  
  48.            h = h +1;  
  49.            rec1.object.bndbox.xmax = msg{h};  
  50.            h = h +1;  
  51.            rec1.object.bndbox.ymax = msg{h};  
  52.            h = h +1;  
  53.            writexml(file,rec1,1);  
  54.      end  
  55.    fprintf(file,'</annotation>\n');     
  56.    fclose(file);  
  57. end  
  58. fclose(fo);  
  1. %writexml.m  
  2. function xml = writexml(fid,rec,depth)  
  3. %WRITEXML Summary of this function goes here  
  4. %   Detailed explanation goes here  
  5.   
  6.   
  7. fn=fieldnames(rec);  
  8. for i=1:length(fn)  
  9.     f=rec.(fn{i});  
  10.     if ~isempty(f)  
  11.         if isstruct(f)  
  12.             for j=1:length(f)              
  13.                 fprintf(fid,'%s',repmat(char(9),1,depth));  
  14.                 a=repmat(char(9),1,depth);  
  15.                 fprintf(fid,'<%s>\n',fn{i});  
  16.                 writexml(fid,rec.(fn{i})(j),depth+1);  
  17.                 fprintf(fid,'%s',repmat(char(9),1,depth));                
  18.                 fprintf(fid,'</%s>\n',fn{i});  
  19.                 
  20.             end  
  21.         else  
  22.             if ~iscell(f)  
  23.                 f={f};  
  24.             end         
  25.             for j=1:length(f)  
  26.                 fprintf(fid,'%s',repmat(char(9),1,depth));  
  27.                 fprintf(fid,'<%s>',fn{i});  
  28.                 if ischar(f{j})  
  29.                     fprintf(fid,'%s',f{j});  
  30.                 elseif isnumeric(f{j})&&numel(f{j})==1  
  31.                     fprintf(fid,'%s',num2str(f{j}));  
  32.                 else  
  33.                     error('unsupported type');  
  34.                 end  
  35.                 fprintf(fid,'</%s>\n',fn{i});  
  36.             end  
  37.         end  
  38.     end  
  39. end  

3.生成训练集/验证集/测试集相关的4个txt文件,放到VOCxxx/ImageSets/Main中,matlab代码如下:
  1. %writetxt.m  
  2. clear  
  3. close all  
  4. clc  
  5.   
  6. file = dir('/home/vision/jinglihua/data_lable/VOCxxx/Annotations');  
  7. len = length(file)-2;  
  8.   
  9. num_trainval=sort(randperm(len, floor(9*len/10)));  
  10. num_train=sort(num_trainval(randperm(length(num_trainval), floor(5*length(num_trainval)/6))));  
  11. num_val=setdiff(num_trainval,num_train);  
  12. num_test=setdiff(1:len,num_trainval);  
  13. path = '/home/vision/jinglihua/data_lable/VOCxxx/ImageSets/Main/';  
  14.   
  15. fid=fopen(strcat(path, 'trainval.txt'),'a+');  
  16. for i=1:length(num_trainval)  
  17.     s = sprintf('%s',file(num_trainval(i)+2).name);  
  18.     fprintf(fid,[s(1:length(s)-4'\n']);  
  19. end  
  20. fclose(fid);  
  21.   
  22. fid=fopen(strcat(path, 'train.txt'),'a+');  
  23. for i=1:length(num_train)  
  24.     s = sprintf('%s',file(num_train(i)+2).name);  
  25.     fprintf(fid,[s(1:length(s)-4'\n']);  
  26. end  
  27. fclose(fid);  
  28.   
  29. fid=fopen(strcat(path, 'val.txt'),'a+');  
  30. for i=1:length(num_val)  
  31.     s = sprintf('%s',file(num_val(i)+2).name);  
  32.     fprintf(fid,[s(1:length(s)-4'\n']);  
  33. end  
  34. fclose(fid);  
  35.   
  36. fid=fopen(strcat(path, 'test.txt'),'a+');  
  37. for i=1:length(num_test)  
  38.     s = sprintf('%s',file(num_test(i)+2).name);  
  39.     fprintf(fid,[s(1:length(s)-4'\n']);  
  40. end  
  41. fclose(fid);  


4.把标注的图像源文件拷到VOCxxx/JPEGImages目录下。然后,当当当当!数据准备完成!VOCxxx目录下架构如下:


5.训练过程。训练前需要修改一些文件,具体的情况http://blog.csdn.net/sinat_30071459/article/details/50546891这篇博客中讲的很清楚啦,按着来就好~


恩,MATLAB版的暂时就先写这么多啦啦啦

  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值