wingedrasengan927/pytorch-tutorials

Creation of biased detectors

kalikhademi opened this issue · 4 comments

I have one question. I am interested to use your code to create object detector which is biased with respect to size. for instance if there are multiple bounding boxes are predicted which overlap it only keeps the biggest one. run it on the images and see how it would impact the results. If i want to implement something like this in your code how do you think I can accomplish this?

I appreciate your help!

You would need to write your own NMS function and replace it in the inference method in model.py line 143

Writing a custom nms function for your use case from the top of my head. It might not be accurate, please verify.

def nms(boxes, scores, iou_thresh=0.5):
    """Non-maximum suppression.
    Args:
        boxes (array): An array of shape (n, 4).
        scores (array): An array of shape (n,).
        iou_thresh (float): A threshold value.
    Returns:
        array: An array of shape (k,).
    """
    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]
    areas = (x2 - x1 + 1) * (y2 - y1 + 1)
    order = scores.argsort()[::-1]

    keep = [areas.argmax()]
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xx1 = np.maximum(x1[i], x1[order[1:]])
        yy1 = np.maximum(y1[i], y1[order[1:]])
        xx2 = np.minimum(x2[i], x2[order[1:]])
        yy2 = np.minimum(y2[i], y2[order[1:]])

        w = np.maximum(0.0, xx2 - xx1 + 1)
        h = np.maximum(0.0, yy2 - yy1 + 1)
        inter = w * h
        ovr = inter / (areas[i] + areas[order[1:]] - inter)

        inds = np.where(ovr <= iou_thresh)[0]
        order = order[inds + 1]

    return list(set(keep))

I think this is actually what I meant. Do you think it is possible to compute metrics such as intensity in the same way?
My goal is to create biased detectors based on some visual features.

My suggestion is to let the model learn these biased boxes. For example, if you'd want big boxes, then while labelling itself make the boxes big. I'm 100% confident model will learn to predict big boxes if they're properly labelled. Same goes if you want the model to be biased towards certain visual features. All the magic happens during labelling itself.

However, if you want to bias it later on. Then you need to build custom post processing function like I've done in the nms function. However, this approach is not recommended.