Permalink
Browse files

faster rcnn

  • Loading branch information...
0 parents commit 562fd88c60229a9dde22f5e018c618c49489d5e2 @ShaoqingRen committed Aug 6, 2015
Showing with 6,108 additions and 0 deletions.
  1. +17 −0 .gitattributes
  2. +54 −0 .gitignore
  3. BIN bin/nms_gpu_mex.mexw64
  4. BIN bin/nms_mex.mexw64
  5. +20 −0 experiments/+Dataset/voc0712_trainval.m
  6. +20 −0 experiments/+Dataset/voc0712_trainval_ss.m
  7. +20 −0 experiments/+Dataset/voc2007_test.m
  8. +20 −0 experiments/+Dataset/voc2007_test_ss.m
  9. +20 −0 experiments/+Dataset/voc2007_trainval.m
  10. +20 −0 experiments/+Dataset/voc2007_trainval_ss.m
  11. +11 −0 experiments/+Faster_RCNN_Train/do_fast_rcnn_test.m
  12. +14 −0 experiments/+Faster_RCNN_Train/do_fast_rcnn_train.m
  13. +42 −0 experiments/+Faster_RCNN_Train/do_proposal_test.m
  14. +14 −0 experiments/+Faster_RCNN_Train/do_proposal_train.m
  15. +84 −0 experiments/+Faster_RCNN_Train/gather_rpn_fast_rcnn_models.m
  16. +30 −0 experiments/+Faster_RCNN_Train/set_cache_folder.m
  17. +10 −0 experiments/+Model/VGG16_for_Fast_RCNN_VOC0712.m
  18. +10 −0 experiments/+Model/VGG16_for_Fast_RCNN_VOC2007.m
  19. +41 −0 experiments/+Model/VGG16_for_Faster_RCNN_VOC0712.m
  20. +41 −0 experiments/+Model/VGG16_for_Faster_RCNN_VOC2007.m
  21. +9 −0 experiments/+Model/ZF_for_Fast_RCNN_VOC0712.m
  22. +9 −0 experiments/+Model/ZF_for_Fast_RCNN_VOC2007.m
  23. +40 −0 experiments/+Model/ZF_for_Faster_RCNN_VOC0712.m
  24. +40 −0 experiments/+Model/ZF_for_Faster_RCNN_VOC2007.m
  25. +40 −0 experiments/+Model/ZF_for_Faster_RCNN_VOC2007_dbg.m
  26. +53 −0 experiments/script_fast_rcnn_VOC0712_VGG16.m
  27. +53 −0 experiments/script_fast_rcnn_VOC0712_ZF.m
  28. +53 −0 experiments/script_fast_rcnn_VOC2007_VGG16.m
  29. +53 −0 experiments/script_fast_rcnn_VOC2007_ZF.m
  30. +89 −0 experiments/script_faster_rcnn_VOC0712_VGG16.m
  31. +89 −0 experiments/script_faster_rcnn_VOC0712_ZF.m
  32. +89 −0 experiments/script_faster_rcnn_VOC2007_VGG16.m
  33. +89 −0 experiments/script_faster_rcnn_VOC2007_ZF.m
  34. +89 −0 experiments/script_faster_rcnn_VOC2007_ZF_dbg.m
  35. +115 −0 experiments/script_faster_rcnn_demo.m
  36. +26 −0 faster_rcnn_build.m
  37. +15 −0 fetch_data/fetch_ss_data_voc2007.m
  38. +26 −0 functions/fast_rcnn/fast_rcnn_bbox_transform.m
  39. +29 −0 functions/fast_rcnn/fast_rcnn_bbox_transform_inv.m
  40. +60 −0 functions/fast_rcnn/fast_rcnn_config.m
  41. +112 −0 functions/fast_rcnn/fast_rcnn_conv_feat_detect.m
  42. +43 −0 functions/fast_rcnn/fast_rcnn_generate_sliding_windows.m
  43. +151 −0 functions/fast_rcnn/fast_rcnn_get_minibatch.m
  44. +121 −0 functions/fast_rcnn/fast_rcnn_im_detect.m
  45. +17 −0 functions/fast_rcnn/fast_rcnn_map_im_rois_to_feat_rois.m
  46. +145 −0 functions/fast_rcnn/fast_rcnn_prepare_image_roidb.m
  47. +239 −0 functions/fast_rcnn/fast_rcnn_test.m
  48. +266 −0 functions/fast_rcnn/fast_rcnn_train.m
  49. +75 −0 functions/nms/nms.m
  50. +160 −0 functions/nms/nms_gpu_mex.cu
  51. +110 −0 functions/nms/nms_mex.cpp
  52. +66 −0 functions/nms/nms_multiclass.m
  53. +158 −0 functions/nms/nms_multiclass_mex.cpp
  54. +51 −0 functions/nms/nvmex.m
  55. +39 −0 functions/rpn/proposal_calc_output_size.m
  56. +74 −0 functions/rpn/proposal_config.m
  57. +73 −0 functions/rpn/proposal_generate_anchors.m
  58. +128 −0 functions/rpn/proposal_generate_minibatch.m
  59. +128 −0 functions/rpn/proposal_im_detect.m
  60. +25 −0 functions/rpn/proposal_kmeans_box_transform.m
  61. +30 −0 functions/rpn/proposal_kmeans_box_transform_inv.m
  62. +73 −0 functions/rpn/proposal_label_cluster.m
  63. +49 −0 functions/rpn/proposal_locate_anchors.m
  64. +189 −0 functions/rpn/proposal_prepare_image_roidb.m
  65. +92 −0 functions/rpn/proposal_test.m
  66. +332 −0 functions/rpn/proposal_train.m
  67. +67 −0 functions/rpn/proposal_visual_debug.m
  68. +17 −0 imdb/get_ilsvrc_opts.m
  69. +14 −0 imdb/get_voc_opts.m
  70. +168 −0 imdb/imdb_eval_ilsvrc.m
  71. +100 −0 imdb/imdb_eval_voc.m
  72. +130 −0 imdb/imdb_from_ilsvrc.m
  73. +89 −0 imdb/imdb_from_voc.m
  74. +226 −0 imdb/roidb_from_ilsvrc.m
  75. +58 −0 imdb/roidb_from_proposal.m
  76. +205 −0 imdb/roidb_from_voc.m
  77. +44 −0 startup.m
  78. +6 −0 utils/RectLTRB2LTWH.m
  79. +6 −0 utils/RectLTWH2LTRB.m
  80. +21 −0 utils/active_caffe_mex.m
  81. +12 −0 utils/active_cvmex.m
  82. +27 −0 utils/auto_select_gpu.m
  83. +40 −0 utils/boxoverlap.m
  84. +11 −0 utils/im_list_to_blob.m
  85. +6 −0 utils/mkdir_if_missing.m
  86. +18 −0 utils/parse_rst.m
  87. +13 −0 utils/prep_im_for_blob.m
  88. +11 −0 utils/prep_im_for_blob_size.m
  89. +19 −0 utils/procid.m
  90. +27 −0 utils/seed_rand.m
  91. +76 −0 utils/showboxes.m
  92. +46 −0 utils/subsample_images.m
  93. +66 −0 utils/subsample_images_per_class.m
  94. +15 −0 utils/symbolic_link.m
  95. +27 −0 utils/tic_toc_print.m
  96. +12 −0 utils/vis_label.m
  97. +10 −0 utils/xVOCap.m
  98. +15 −0 utils/xVOChash_init.m
  99. +6 −0 utils/xVOChash_lookup.m
@@ -0,0 +1,17 @@
+# Auto detect text files and perform LF normalization
+* text=auto
+
+# Custom for Visual Studio
+*.cs diff=csharp
+
+# Standard to msysgit
+*.doc diff=astextplain
+*.DOC diff=astextplain
+*.docx diff=astextplain
+*.DOCX diff=astextplain
+*.dot diff=astextplain
+*.DOT diff=astextplain
+*.pdf diff=astextplain
+*.PDF diff=astextplain
+*.rtf diff=astextplain
+*.RTF diff=astextplain
@@ -0,0 +1,54 @@
+# Windows image file caches
+Thumbs.db
+ehthumbs.db
+
+# Folder config file
+Desktop.ini
+
+# Recycle Bin used on file shares
+$RECYCLE.BIN/
+
+# User Ingore
+models/
+data/
+datasets/
+external/
+output/
+cachedir/
+imdb/cache
+*.caffemodel
+*.mat
+
+# Windows Installer files
+*.cab
+*.msi
+*.msm
+*.msp
+
+# Windows shortcuts
+*.lnk
+
+# =========================
+# Operating System Files
+# =========================
+
+# OSX
+# =========================
+
+.DS_Store
+.AppleDouble
+.LSOverride
+
+# Thumbnails
+._*
+
+# Files that might appear on external disk
+.Spotlight-V100
+.Trashes
+
+# Directories potentially created on remote AFP share
+.AppleDB
+.AppleDesktop
+Network Trash Folder
+Temporary Items
+.apdisk
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,20 @@
+function dataset = voc0712_trainval(dataset, usage, use_flip)
+% Pascal voc 2007 test set
+% set opts.imdb_train opts.roidb_train
+% or set opts.imdb_test opts.roidb_train
+
+% change to point to your devkit install
+devkit = './datasets/VOCdevkit0712';
+
+switch usage
+ case {'train'}
+ dataset.imdb_train = { imdb_from_voc(devkit, 'trainval', '0712', use_flip) };
+ dataset.roidb_train = cellfun(@(x) x.roidb_func(x), dataset.imdb_train, 'UniformOutput', false);
+ case {'test'}
+ dataset.imdb_test = imdb_from_voc(dataset.devkit, 'trainval', '0712', use_flip) ;
+ dataset.roidb_test = dataset.imdb_test.roidb_func(dataset.imdb_test);
+ otherwise
+ error('usage = ''train'' or ''test''');
+end
+
+end
@@ -0,0 +1,20 @@
+function dataset = voc0712_trainval_ss(dataset, usage, use_flip)
+% Pascal voc 2007 test set with selective search
+% set opts.imdb_train opts.roidb_train
+% or set opts.imdb_test opts.roidb_train
+
+% change to point to your devkit install
+devkit = './datasets/VOCdevkit0712';
+
+switch usage
+ case {'train'}
+ dataset.imdb_train = { imdb_from_voc(devkit, 'trainval', '0712', use_flip) };
+ dataset.roidb_train = cellfun(@(x) x.roidb_func(x, 'with_selective_search', true), dataset.imdb_train, 'UniformOutput', false);
+ case {'test'}
+ dataset.imdb_test = imdb_from_voc(dataset.devkit, 'trainval', '0712', use_flip) ;
+ dataset.roidb_test = dataset.imdb_test.roidb_func(dataset.imdb_test, 'with_selective_search', true);
+ otherwise
+ error('usage = ''train'' or ''test''');
+end
+
+end
@@ -0,0 +1,20 @@
+function dataset = voc2007_test(dataset, usage, use_flip)
+% Pascal voc 2007 test set with selective search
+% set opts.imdb_train opts.roidb_train
+% or set opts.imdb_test opts.roidb_train
+
+% change to point to your devkit install
+devkit = './datasets/VOCdevkit2007';
+
+switch usage
+ case {'train'}
+ dataset.imdb_train = { imdb_from_voc(devkit, 'test', '2007', use_flip) };
+ dataset.roidb_train = cellfun(@(x) x.roidb_func(x), dataset.imdb_train, 'UniformOutput', false);
+ case {'test'}
+ dataset.imdb_test = imdb_from_voc(devkit, 'test', '2007', use_flip) ;
+ dataset.roidb_test = dataset.imdb_test.roidb_func(dataset.imdb_test);
+ otherwise
+ error('usage = ''train'' or ''test''');
+end
+
+end
@@ -0,0 +1,20 @@
+function dataset = voc2007_test_ss(dataset, usage, use_flip)
+% Pascal voc 2007 test set with selective search
+% set opts.imdb_train opts.roidb_train
+% or set opts.imdb_test opts.roidb_train
+
+% change to point to your devkit install
+devkit = './datasets/VOCdevkit2007';
+
+switch usage
+ case {'train'}
+ dataset.imdb_train = { imdb_from_voc(devkit, 'test', '2007', use_flip) };
+ dataset.roidb_train = cellfun(@(x) x.roidb_func(x, 'with_selective_search', true), dataset.imdb_train, 'UniformOutput', false);
+ case {'test'}
+ dataset.imdb_test = imdb_from_voc(devkit, 'test', '2007', use_flip) ;
+ dataset.roidb_test = dataset.imdb_test.roidb_func(dataset.imdb_test, 'with_selective_search', true);
+ otherwise
+ error('usage = ''train'' or ''test''');
+end
+
+end
@@ -0,0 +1,20 @@
+function dataset = voc2007_trainval(dataset, usage, use_flip)
+% Pascal voc 2007 test set
+% set opts.imdb_train opts.roidb_train
+% or set opts.imdb_test opts.roidb_train
+
+% change to point to your devkit install
+devkit = './datasets/VOCdevkit2007';
+
+switch usage
+ case {'train'}
+ dataset.imdb_train = { imdb_from_voc(devkit, 'trainval', '2007', use_flip) };
+ dataset.roidb_train = cellfun(@(x) x.roidb_func(x), dataset.imdb_train, 'UniformOutput', false);
+ case {'test'}
+ dataset.imdb_test = imdb_from_voc(dataset.devkit, 'trainval', '2007', use_flip) ;
+ dataset.roidb_test = dataset.imdb_test.roidb_func(dataset.imdb_test);
+ otherwise
+ error('usage = ''train'' or ''test''');
+end
+
+end
@@ -0,0 +1,20 @@
+function dataset = voc2007_trainval_ss(dataset, usage, use_flip)
+% Pascal voc 2007 test set with selective search
+% set opts.imdb_train opts.roidb_train
+% or set opts.imdb_test opts.roidb_train
+
+% change to point to your devkit install
+devkit = './datasets/VOCdevkit2007';
+
+switch usage
+ case {'train'}
+ dataset.imdb_train = { imdb_from_voc(devkit, 'trainval', '2007', use_flip) };
+ dataset.roidb_train = cellfun(@(x) x.roidb_func(x, 'with_selective_search', true), dataset.imdb_train, 'UniformOutput', false);
+ case {'test'}
+ dataset.imdb_test = imdb_from_voc(dataset.devkit, 'trainval', '2007', use_flip) ;
+ dataset.roidb_test = dataset.imdb_test.roidb_func(dataset.imdb_test, 'with_selective_search', true);
+ otherwise
+ error('usage = ''train'' or ''test''');
+end
+
+end
@@ -0,0 +1,11 @@
+function mAP = do_fast_rcnn_test(conf, model_stage, imdb, roidb, ignore_cache)
+ if ~exist('ignore_cache', 'var')
+ ignore_cache = false;
+ end
+
+ mAP = fast_rcnn_test(conf, imdb, roidb, ...
+ 'net_def_file', model_stage.test_net_def_file, ...
+ 'net_file', model_stage.output_model_file, ...
+ 'cache_name', model_stage.cache_name, ...
+ 'ignore_cache', ignore_cache);
+end
@@ -0,0 +1,14 @@
+function model_stage = do_fast_rcnn_train(conf, dataset, model_stage, do_val)
+ if ~do_val
+ dataset.imdb_test = struct();
+ dataset.roidb_test = struct();
+ end
+
+ model_stage.output_model_file = fast_rcnn_train(conf, dataset.imdb_train, dataset.roidb_train, ...
+ 'do_val', do_val, ...
+ 'imdb_val', dataset.imdb_test, ...
+ 'roidb_val', dataset.roidb_test, ...
+ 'solver_def_file', model_stage.solver_def_file, ...
+ 'net_file', model_stage.init_net_file, ...
+ 'cache_name', model_stage.cache_name);
+end
@@ -0,0 +1,42 @@
+function roidb_new = do_proposal_test(conf, model_stage, imdb, roidb)
+ aboxes = proposal_test(conf, imdb, ...
+ 'net_def_file', model_stage.test_net_def_file, ...
+ 'net_file', model_stage.output_model_file, ...
+ 'cache_name', model_stage.cache_name);
+
+ aboxes = boxes_filter(aboxes, model_stage.per_nms_topN, model_stage.nms_overlap_thres, model_stage.after_nms_topN, conf.use_gpu);
+
+ roidb_regions = make_roidb_regions(aboxes, imdb.image_ids);
+
+ roidb_new = roidb_from_proposal(imdb, roidb, roidb_regions, ...
+ 'keep_raw_proposal', false);
+end
+
+function aboxes = boxes_filter(aboxes, per_nms_topN, nms_overlap_thres, after_nms_topN, use_gpu)
+ % to speed up nms
+ if per_nms_topN > 0
+ aboxes = cellfun(@(x) x(1:min(length(x), per_nms_topN), :), aboxes, 'UniformOutput', false);
+ end
+ % do nms
+ if nms_overlap_thres > 0 && nms_overlap_thres < 1
+ if use_gpu
+ for i = 1:length(aboxes)
+ aboxes{i} = aboxes{i}(nms(aboxes{i}, nms_overlap_thres, use_gpu), :);
+ end
+ else
+ parfor i = 1:length(aboxes)
+ aboxes{i} = aboxes{i}(nms(aboxes{i}, nms_overlap_thres), :);
+ end
+ end
+ end
+ aver_boxes_num = mean(cellfun(@(x) size(x, 1), aboxes, 'UniformOutput', true));
+ fprintf('aver_boxes_num = %d, select top %d\n', round(aver_boxes_num), after_nms_topN);
+ if after_nms_topN > 0
+ aboxes = cellfun(@(x) x(1:min(length(x), after_nms_topN), :), aboxes, 'UniformOutput', false);
+ end
+end
+
+function regions = make_roidb_regions(aboxes, images)
+ regions.boxes = aboxes;
+ regions.images = images;
+end
@@ -0,0 +1,14 @@
+function model_stage = do_proposal_train(conf, dataset, model_stage, do_val)
+ if ~do_val
+ dataset.imdb_test = struct();
+ dataset.roidb_test = struct();
+ end
+
+ model_stage.output_model_file = proposal_train(conf, dataset.imdb_train, dataset.roidb_train, ...
+ 'do_val', do_val, ...
+ 'imdb_val', dataset.imdb_test, ...
+ 'roidb_val', dataset.roidb_test, ...
+ 'solver_def_file', model_stage.solver_def_file, ...
+ 'net_file', model_stage.init_net_file, ...
+ 'cache_name', model_stage.cache_name);
+end
@@ -0,0 +1,84 @@
+function gather_rpn_fast_rcnn_models(conf_proposal, conf_fast_rcnn, model, dataset)
+ cachedir = fullfile(pwd, 'output', 'rpn_fast_rcnn', model.final_model.cache_name);
+ mkdir_if_missing(cachedir);
+
+ % find latest model for rpn and fast rcnn
+ [rpn_test_net_def_file, rpn_output_model_file] = find_last_output_model_file(model.stage1_rpn, model.stage2_rpn);
+ [fast_rcnn_test_net_def_file, fast_rcnn_output_model_file] = find_last_output_model_file(model.stage1_fast_rcnn, model.stage2_fast_rcnn);
+
+ % check whether feature shared and find the indexs of shared layers
+ [is_share_feature, ~, fast_rcnn_weights, shared_layer_idx] = ...
+ check_proposal_fast_rcnn_model(rpn_test_net_def_file, rpn_output_model_file, ...
+ fast_rcnn_test_net_def_file, fast_rcnn_output_model_file);
+
+ proposal_detection_model.classes = dataset.imdb_test.classes;
+ proposal_detection_model.image_means = conf_proposal.image_means;
+ proposal_detection_model.conf_proposal = conf_proposal;
+ proposal_detection_model.conf_detection = conf_fast_rcnn;
+
+ % copy rpn and fast rcnn models into cachedir
+ [~, test_net_proposal_name, test_net_proposal_ext] = fileparts(rpn_test_net_def_file);
+ proposal_detection_model.proposal_net_def = ['proposal_', test_net_proposal_name, test_net_proposal_ext];
+ [~, proposal_model_name, proposal_model_ext] = fileparts(rpn_output_model_file);
+ proposal_detection_model.proposal_net = ['proposal_', proposal_model_name, proposal_model_ext];
+ [~, test_net_fast_rcnn_name, test_net_fast_rcnn_ext] = fileparts(fast_rcnn_test_net_def_file);
+ proposal_detection_model.detection_net_def = ['detection_', test_net_fast_rcnn_name, test_net_fast_rcnn_ext];
+ [~, fast_rcnn_model_name, fast_rcnn_model_ext] = fileparts(fast_rcnn_output_model_file);
+ proposal_detection_model.detection_net = ['detection_', fast_rcnn_model_name, fast_rcnn_model_ext];
+
+ copyfile(rpn_test_net_def_file, fullfile(cachedir, proposal_detection_model.proposal_net_def));
+ copyfile(rpn_output_model_file, fullfile(cachedir, proposal_detection_model.proposal_net));
+ copyfile(fast_rcnn_test_net_def_file, fullfile(cachedir, proposal_detection_model.detection_net_def));
+ copyfile(fast_rcnn_output_model_file, fullfile(cachedir, proposal_detection_model.detection_net));
+
+ proposal_detection_model.is_share_feature = is_share_feature;
+ if is_share_feature
+ proposal_detection_model.last_shared_layer_idx = max(shared_layer_idx);
+ proposal_detection_model.last_shared_layer_detection = ...
+ fast_rcnn_weights(proposal_detection_model.last_shared_layer_idx).layer_names;
+ fprintf('please modify %s file for sharing conv layers with proposal model (delete layers until %s)\n', ...
+ proposal_detection_model.detection_net_def, proposal_detection_model.last_shared_layer_detection);
+ end
+
+ save(fullfile(cachedir, 'model'), 'proposal_detection_model');
+end
+
+function [is_share_feature, proposal_weights, fast_rcnn_weights, shared_layer_idx] = check_proposal_fast_rcnn_model(proposal_model_net, proposal_model_bin, ...
+ fast_rcnn_model_net, fast_rcnn_model_bin)
+
+ init_net(fast_rcnn_model_net, fast_rcnn_model_bin, '//log//', 0);
+ init_net(proposal_model_net, proposal_model_bin, '//log//', 1);
+ fast_rcnn_weights = caffe('get_weights', 0);
+ proposal_weights = caffe('get_weights', 1);
+
+ is_share_feature = true;
+ shared_layer_idx = [];
+ for i = 1:min(length(fast_rcnn_weights), length(proposal_weights))
+ if ~strcmp(fast_rcnn_weights(i).layer_names, proposal_weights(i).layer_names)
+ break;
+ end
+
+ if ~isequal(fast_rcnn_weights(i).weights, proposal_weights(i).weights)
+ is_share_feature = false;
+ else
+ shared_layer_idx(end+1) = i;
+ end
+ end
+
+ caffe('release', 0);
+ caffe('release', 1);
+end
+
+function [test_net_def_file, output_model_file] = find_last_output_model_file(stage1, stage2)
+ if isfield(stage2, 'output_model_file') && exist(stage2.output_model_file, 'file')
+ output_model_file = stage2.output_model_file;
+ test_net_def_file = stage2.test_net_def_file;
+ return;
+ end
+ if isfield(stage1, 'output_model_file') && exist(stage1.output_model_file, 'file')
+ output_model_file = stage1.output_model_file;
+ test_net_def_file = stage1.test_net_def_file;
+ return;
+ end
+ error('find_last_output_model_file:: no trained models');
+end
Oops, something went wrong.

0 comments on commit 562fd88

Please sign in to comment.