码迷,mamicode.com
首页 > 其他好文 > 详细

Matlab 进阶学习记录

时间:2016-07-31 17:22:47      阅读:648      评论:0      收藏:0      [点我收藏+]

标签:

 

最近在看 Faster RCNN的Matlab code,发现很多matlab技巧,在此记录:

 

1. conf_proposal  =  proposal_config(‘image_means‘, model.mean_image, ‘feat_stride‘, model.feat_stride);

  

function conf = proposal_config(varargin)
% conf = proposal_config(varargin)
% --------------------------------------------------------
% Faster R-CNN
% Copyright (c) 2015, Shaoqing Ren 
% Licensed under The MIT License [see LICENSE for details]
% --------------------------------------------------------
    
    ip = inputParser ;  
    
    %% training
    ip.addParamValue(‘use_gpu‘,         gpuDeviceCount > 0, ...            
                                                        @islogical);
                                    
    % whether drop the anchors that has edges outside of the image boundary
    ip.addParamValue(‘drop_boxes_runoff_image‘, ...
                                        true,           @islogical);
    
    % Image scales -- the short edge of input image                                                                                                
    ip.addParamValue(‘scales‘,          600,            @ismatrix);
    % Max pixel size of a scaled input image
    ip.addParamValue(‘max_size‘,        1000,           @isscalar);
    % Images per batch, only supports ims_per_batch = 1 currently
    ip.addParamValue(‘ims_per_batch‘,   1,              @isscalar);
    % Minibatch size
    ip.addParamValue(‘batch_size‘,      256,            @isscalar);
    % Fraction of minibatch that is foreground labeled (class > 0)
    ip.addParamValue(‘fg_fraction‘,     0.5,           @isscalar);
    % weight of background samples, when weight of foreground samples is
    % 1.0
    ip.addParamValue(‘bg_weight‘,       1.0,            @isscalar);
    % Overlap threshold for a ROI to be considered foreground (if >= fg_thresh)
    ip.addParamValue(‘fg_thresh‘,       0.7,            @isscalar);
    % Overlap threshold for a ROI to be considered background (class = 0 if
    % overlap in [bg_thresh_lo, bg_thresh_hi))
    ip.addParamValue(‘bg_thresh_hi‘,    0.3,            @isscalar);
    ip.addParamValue(‘bg_thresh_lo‘,    0,              @isscalar);
    % mean image, in RGB order
    ip.addParamValue(‘image_means‘,     128,            @ismatrix);
    % Use horizontally-flipped images during training ?
    ip.addParamValue(‘use_flipped‘,     true,           @islogical);
    % Stride in input image pixels at ROI pooling level (network specific)
    % 16 is true for {Alex,Caffe}Net, VGG_CNN_M_1024, and VGG16
    ip.addParamValue(‘feat_stride‘,     16,             @isscalar);
    % train proposal target only to labled ground-truths or also include
    % other proposal results (selective search, etc.)
    ip.addParamValue(‘target_only_gt‘,  true,           @islogical);

    % random seed                    
    ip.addParamValue(‘rng_seed‘,        6,              @isscalar);

    
    %% testing
    ip.addParamValue(‘test_scales‘,     600,            @isscalar);
    ip.addParamValue(‘test_max_size‘,   1000,           @isscalar);
    ip.addParamValue(‘test_nms‘,        0.3,            @isscalar);
    ip.addParamValue(‘test_binary‘,     false,          @islogical);
    ip.addParamValue(‘test_min_box_size‘,16,            @isscalar);
    ip.addParamValue(‘test_drop_boxes_runoff_image‘, ...
                                        false,          @islogical);
    
    ip.parse(varargin{:});
    conf = ip.Results;
    
    assert(conf.ims_per_batch == 1, ‘currently rpn only supports ims_per_batch == 1‘);
    
    % if image_means is a file, load it...
    if ischar(conf.image_means)
        s = load(conf.image_means);
        s_fieldnames = fieldnames(s);
        assert(length(s_fieldnames) == 1);
        conf.image_means = s.(s_fieldnames{1});
    end
end

  

The inputParser object allows you to manage inputs to a function by creating an input scheme. To check the input, you can define validation functions for required arguments, optional arguments, and name-value pair arguments. Optionally, you can set properties to adjust the parsing behavior, such as handling case sensitivity, structure array inputs, and inputs that are not in the input scheme.

After calling the parse method to parse the inputs, the inputParser saves names and values of inputs that match the input scheme (stored in Results), names of inputs that are not passed to the function and, therefore, are assigned default values (stored in UsingDefaults), and names and values of inputs that do not match the input scheme (stored in Unmatched).

  

Check the validity of required and optional function inputs.

Create a custom function with required and optional inputs in the file findArea.m.

function a = findArea(width,varargin)
   p = inputParser;
   defaultHeight = 1;
   defaultUnits = ‘inches‘;
   defaultShape = ‘rectangle‘;
   expectedShapes = {‘square‘,‘rectangle‘,‘parallelogram‘};

   addRequired(p,‘width‘,@isnumeric);
   addOptional(p,‘height‘,defaultHeight,@isnumeric);
   addParameter(p,‘units‘,defaultUnits);
   addParameter(p,‘shape‘,defaultShape,...
                 @(x) any(validatestring(x,expectedShapes)));

   parse(p,width,varargin{:});
   a = p.Results.width .* p.Results.height;
The input parser checks whether width and height are numeric, and whether the shape matches a string in cell array expectedShapes. @ indicates a function handle, and the syntax @(x) creates an anonymous function with input x. Call the function with inputs that do not match the scheme. For example, specify a nonnumeric value for the width input: findArea(‘text‘) Error using findArea (line 14) The value of ‘width‘ is invalid. It must satisfy the function: isnumeric. Specify an unsupported value for shape: findArea(4,‘shape‘,‘circle‘) Error using findArea (line 14) The value of ‘shape‘ is invalid. Expected input to match one of these strings: square, rectangle, parallelogram The input, ‘‘circle‘‘, did not match any of the valid strings.

 

  

 

Matlab 进阶学习记录

标签:

原文地址:http://www.cnblogs.com/wangxiaocvpr/p/5723312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!