Bunch-of-Keys Network 논문 연구를 수행하기 위해서 GIoU와 CIoU를 활용할 필요가 있었다. 아래 연구를 위해 작성한 Matlab 코드를 첨부한다.

 


Generalized IoU

function [giou,iou] = gIoU(bboxes1, bboxes2)
    
    % Reference : Generalized Intersection over Union: A Metric and A Loss for Bounding Box Regression
    % Calculate generalized Intersection over Union
    
    % define point of first bounding boxes.
    x1p = bboxes1(:,1);
    x2p = bboxes1(:,1) + bboxes1(:,3);
    y1p = bboxes1(:,2);
    y2p = bboxes1(:,2) + bboxes1(:,4);
    
    % define point of second bounding boxes.
    x1g = bboxes2(:,1);
    x2g = bboxes2(:,1) + bboxes2(:,3);
    y1g = bboxes2(:,2);
    y2g = bboxes2(:,2) + bboxes2(:,4);
    
    % Calculate Area of Bboxes1&2
    area_1 = (x2p - x1p) .* (y2p - y1p);
    area_2 = (x2g - x1g) .* (y2g - y1g);
    
    %cal intersection
    IoU_min = bboxOverlapRatio(bboxes1,bboxes2, 'Min');
    Overlap = IoU_min .* min(area_1, area_2');
    All_Area = (max(x2p, x2g') - min(x1p,x1g')).*(max(y2p,y2g') - min(y1p,y1g'));
    C_boxes = (All_Area - (area_1+area_2'-Overlap))./All_Area;
    
    iou = bboxOverlapRatio(bboxes1, bboxes2);
    giou = iou - C_boxes;
end

DIoU & CIoU

function [diou, ciou] = CIoU(bboxes1, bboxes2)
	% Reference :  Distance-IoU Loss Fater and Better Learning for Bounding Box Regression
    % define point of first bounding boxes.
    x1p = bboxes1(:,1);
    y1p = bboxes1(:,2);
    
    % define point of second bounding boxes.
    x1g = bboxes2(:,1);
    y1g = bboxes2(:,2);

    bboxes1_cent = [x1p+bboxes1(:,3)/2 y1p+bboxes1(:,4)/2];
    bboxes2_cent = [x1g+bboxes2(:,3)/2 y1g+bboxes2(:,4)/2];
    cent_dist = pdist2(bboxes1_cent, bboxes2_cent, 'squaredeuclidean');   
    C = (max(x1p+bboxes1(:,3), (x1g+bboxes2(:,3))') - min(x1p,x1g')).^2 +(max(y1p+bboxes1(:,4),(y1g+bboxes2(:,4))') - min(y1p,y1g')).^2;
    R_DIoU = (cent_dist.^2)./C;
    eps = 4/(pi().^2) .* ((atan(bboxes1(:,3)./bboxes1(:,4)) - (atan(bboxes2(:,3)./bboxes2(:,4)))').^2);
    IoU = bboxOverlapRatio(bboxes1,bboxes2);
    alpha = eps./(1-IoU+eps);
    R_CIoU = R_DIoU + alpha .* eps;
    diou = IoU - R_DIoU;
    ciou = IoU - R_CIoU;
end

 

+ Recent posts