faster-rcnn中utils下几个重要函数的解析

utils下的函数主要是为了整个工程调用的方便而设计的,这里讲解其中几个比较重要的函数。
1、boxoverlap.m
用来计算两个boxes中box间的重叠系数。元组o中的元素是矩阵,矩阵o{i}代表boxes a和box b(i)之间的交叉系数,size(o{i})=(m,1)。m–表示boxes a中有m个box。而size(o)=n。n–表示boxes b中有n个box,最后返回cell2mat(o),size为(m,n)
这个函数主要在选择产生的roi时会用到,用于选择满足一定重叠系数的roi.

function o = boxoverlap(a, b)
% Compute the symmetric intersection over union overlap between a set of
% bounding boxes in a and a single bounding box in b.
%
% a a matrix where each row specifies a bounding box
% b a matrix where each row specifies a bounding box
-------------------------------------------------------

o = cell(1, size(b, 1));
for i = 1:size(b, 1)
    x1 = max(a(:,1), b(i,1));%Create a matrix and return the largest value between each of its elements compared to a scalar.
    y1 = max(a(:,2), b(i,2));
    x2 = min(a(:,3), b(i,3));
    y2 = min(a(:,4), b(i,4));

    w = x2-x1+1;
    h = y2-y1+1;
    inter = w.*h;
    aarea = (a(:,3)-a(:,1)+1) .* (a(:,4)-a(:,2)+1);
    barea = (b(i,3)-b(i,1)+1) * (b(i,4)-b(i,2)+1);
    % intersection over union overlap
    o{i} = inter ./ (aarea+barea-inter);
    % set invalid entries to 0 overlap
    o{i}(w <= 0) = 0;
    o{i}(h <= 0) = 0;
end

o = cell2mat(o);

2、im_list_to_blob.m
输入元组ims,ims{i}为一张图片,将ims转换为blob(w,h,3,n)

function blob = im_list_to_blob(ims)%ims--cell
    max_shape = max(cell2mat(cellfun(@size, ims(:), 'UniformOutput', false)), [], 1);
    assert(all(cellfun(@(x) size(x, 3), ims, 'UniformOutput', true) == 3));
    num_images = length(ims);
    blob = zeros(max_shape(1), max_shape(2), 3, num_images, 'single');

    for i = 1:length(ims)
        im = ims{i};
        blob(1:size(im, 1), 1:size(im, 2), :, i) = im; 
    end
end

3、
prep_im_for_blob.m 根据输入图片,图片均值,目标尺寸,返回目标图片和缩放系数。
prep_im_for_blob_size.m 按输入图片的尺寸和目标尺寸,返回尺寸缩放系数。

function [im, im_scale] = prep_im_for_blob(im, im_means, target_size, max_size)
    im = single(im);

    if ~isa(im, 'gpuArray')
        try
            im = bsxfun(@minus, im, im_means);
        catch
            im_means = imresize(im_means, [size(im, 1), size(im, 2)], 'bilinear', 'antialiasing', false);    
            im = bsxfun(@minus, im, im_means);
        end
        im_scale = prep_im_for_blob_size(size(im), target_size, max_size);

        target_size = round([size(im, 1), size(im, 2)] * im_scale);
        im = imresize(im, target_size, 'bilinear', 'antialiasing', false);
    else
        % for im as gpuArray
        try
            im = bsxfun(@minus, im, im_means);
        catch
            im_means_scale = max(double(size(im, 1)) / size(im_means, 1), double(size(im, 2)) / size(im_means, 2));
            im_means = imresize(im_means, im_means_scale);    
            y_start = floor((size(im_means, 1) - size(im, 1)) / 2) + 1;
            x_start = floor((size(im_means, 2) - size(im, 2)) / 2) + 1;
            im_means = im_means(y_start:(y_start+size(im, 1)-1), x_start:(x_start+size(im, 2)-1));
            im = bsxfun(@minus, im, im_means);
        end

        im_scale = prep_im_for_blob_size(size(im), target_size, max_size);
        im = imresize(im, im_scale);
    end
end

function im_scale = prep_im_for_blob_size(im_size, target_size, max_size)

    im_size_min = min(im_size(1:2));
    im_size_max = max(im_size(1:2));
    im_scale = double(target_size) / im_size_min;

    % Prevent the biggest axis from being more than MAX_SIZE
    if round(im_scale * im_size_max) > max_size
        im_scale = double(max_size) / double(im_size_max);
    end
end

4、
RectLTRB2LTWH
RectLTWH2LTRB
坐标标定的不同方式之间的转换。

function [ rectsLTWH ] = RectLTRB2LTWH( rectsLTRB ) %rects (l, t, r, b) to (l, t, w, h) rectsLTWH = [rectsLTRB(:, 1), rectsLTRB(:, 2), rectsLTRB(:, 3)-rectsLTRB(:,1)+1, rectsLTRB(:,4)-rectsLTRB(:,2)+1];
end

function [ rectsLTRB ] = RectLTWH2LTRB(rectsLTWH) %rects (l, t, r, b) to (l, t, w, h) rectsLTRB = [rectsLTWH(:, 1), rectsLTWH(:, 2), rectsLTWH(:, 1)+rectsLTWH(:,3)-1, rectsLTWH(:,2)+rectsLTWH(:,4)-1];
end

5、
输入一张图片,含多个box坐标信息的元组boxes,每个box对应的标签legends,对图片进行信息的标注。输出图片尺寸固定,要注意对应box坐标的缩放。

function showboxes(im, boxes, legends, color_conf)
% Draw bounding boxes on top of an image.
% showboxes(im, boxes)
%
% -------------------------------------------------------

fix_width = 800;
if isa(im, 'gpuArray')
    im = gather(im);
end
imsz = size(im);
scale = fix_width / imsz(2);
im = imresize(im, scale);

if size(boxes{1}, 2) >= 5
    boxes = cellfun(@(x) [x(:, 1:4) * scale, x(:, 5)], boxes, 'UniformOutput', false);
else
    boxes = cellfun(@(x) x(:, 1:4) * scale, boxes, 'UniformOutput', false);
end

if ~exist('color_conf', 'var')
    color_conf = 'default';
end

image(im); 
axis image;
axis off;
set(gcf, 'Color', 'white');

valid_boxes = cellfun(@(x) ~isempty(x), boxes, 'UniformOutput', true);
valid_boxes_num = sum(valid_boxes);
%选择每个class进行box标注时对应的颜色
if valid_boxes_num > 0
    switch color_conf
        case 'default'
            colors_candidate = colormap('hsv');
            colors_candidate = colors_candidate(1:(floor(size(colors_candidate, 1)/valid_boxes_num)):end, :);
            colors_candidate = mat2cell(colors_candidate, ones(size(colors_candidate, 1), 1))'; colors = cell(size(valid_boxes)); colors(valid_boxes) = colors_candidate(1:sum(valid_boxes)); case 'voc'
            colors_candidate = colormap('hsv');
            colors_candidate = colors_candidate(1:(floor(size(colors_candidate, 1)/20)):end, :);
            colors_candidate = mat2cell(colors_candidate, ones(size(colors_candidate, 1), 1))'; colors = colors_candidate; end for i = 1:length(boxes) if isempty(boxes{i}) continue; end for j = 1:size(boxes{i}) box = boxes{i}(j, 1:4); if size(boxes{i}, 2) >= 5 score = boxes{i}(j, end); linewidth = 2 + min(max(score, 0), 1) * 2; rectangle('Position', RectLTRB2LTWH(box), 'LineWidth', linewidth, 'EdgeColor', colors{i});
                label = sprintf('%s : %.3f', legends{i}, score);
                text(double(box(1))+2, double(box(2)), label, 'BackgroundColor', 'w');
            else
                linewidth = 2;
                rectangle('Position', RectLTRB2LTWH(box), 'LineWidth', linewidth, 'EdgeColor', colors{i});
                label = sprintf('%s(%d)', legends{i}, i);
                text(double(box(1))+2, double(box(2)), label, 'BackgroundColor', 'w');
            end
        end

    end
end
end

6、

function tic_toc_print(fmt, varargin)
% Print only after 1 second has passed since the last print. 
% Arguments are the same as for fprintf.
% -------------------------------------------------------

persistent th;

if isempty(th)
  th = tic();
end

if toc(th) > 1
  fprintf(fmt, varargin{:});
  drawnow;
  th = tic();
end

你可能感兴趣的:(深度学习,faster-rcn)