天天看点

matlab yolov2对整个测试集的图片进行预测的代码

这个是matlab代码对yolov2生成的模型进行测试的代码,他会在图片生成boundingbox.

close all;clear;clc
gpuDevice(1);

load standard_cell2_detector_yolov2_0611_epoch50_changeanchor.mat;
test_image_folder = './predictdata_900_900_crop';

test_output_folder = 'test_cell_detection;
#带有boundingbox的图片测试结果的输出路径
bbox_output_folder = 'cell2_yolov2_bbox_black_white'
test_files = dir(test_image_folder);
length(test_files)

%make output folder
if ~isdir(test_output_folder)
    fprintf(1, 'Making directory %s\n', test_output_folder);
    mkdir(test_output_folder);
end

if ~isdir(bbox_output_folder)
    fprintf(1, 'Making directory %s\n', test_output_folder);
    mkdir(bbox_output_folder);
end

for k = 3 : length(test_files)
%for k = 3 : 10
    baseFileName = test_files(k).name;
    fullFileName = fullfile(test_image_folder, baseFileName);
    tic;
    I = imread(fullFileName);
    [bboxes,scores,labels] = detect(detector,I,'Threshold',0.5); %original = 0.5
    [bboxes,scores,labels] = selectStrongestBboxMulticlass(bboxes, scores, labels, ...
                'RatioType','Union', ... %original = 'Union'
                'OverlapThreshold',0.01); %original = 0.01
    % Annotate detections in the image.
    if ~isempty(labels)
        I = insertObjectAnnotation(I,'rectangle',bboxes,scores,'LineWidth',3); %original=scores
    end
   #这里可以改为inshertshape的方法,这里是生成预测出来的黑白图,可以忽略
    bbb=size(bboxes);
    im=zeros(900,900);
    for g=1:bbb(1)           
        imshow(im);
        a=[bboxes(g,1),bboxes(g,2)];
        b=[bboxes(g,3)-5,bboxes(g,4)-5];
        im=drawRect(im,a,b,1);
    end
    imwrite(im,[bbox_output_folder,'/',int2str(k),'.png']);
   ############################ 
    
    [folder,output_base_name,] = fileparts(fullFileName);
    imwrite(I,[test_output_folder,'/',output_base_name,'_output','.png']);
    fprintf(1, 'Processing time = %.3f seconds; Writing file %s\n', toc, [test_output_folder,'/',output_base_name,'_output','.png']);
end
           

继续阅读