lufficc / SSD

High quality, fast, modular reference implementation of SSD in PyTorch
MIT License
1.51k stars 385 forks source link

Wrong resizing of boxes? #182

Closed Shakesbeer333 closed 3 years ago

Shakesbeer333 commented 3 years ago

Hi @lufficc,

I have the feeling that the resizing of the (ground truth) boxes is wrong. The resizing is "bypassed" by taking the relative values of x1,y1,x2,y2 with regards to the original image shape. However, this leads to wrong results, as the images are resized to the squared shape = size x size (i.e. 512 x 512 or 300 x 300), thus, discarding the original aspect ration.

A possible solution would be to change the Resize class in the following way:

`class Resize(object): def init(self, size=300): self.size = size

def __call__(self, image, boxes=None, labels=None):

    old_size = image.shape[:2]
    ratio = float(self.size) / max(old_size)
    new_size = tuple([int(x * ratio) for x in old_size])

    image = cv2.resize(image, (new_size[1], new_size[0]))

    delta_w = self.size - new_size[1]
    delta_h = self.size - new_size[0]
    top, bottom = delta_h // 2, delta_h - (delta_h // 2)
    left, right = delta_w // 2, delta_w - (delta_w // 2)

    color = [0, 0, 0]
    image = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT,
                               value=color)

    return image, boxes, labels

`

This pads the shorter images size, such that the original aspect ration is kept.

Please let me know your thoughts about this.

lufficc commented 3 years ago

SSD is trained without considering image aspect ration. So there is no need to pad image like in Faster R-CNN.

ToPercentCoords is called before Resize, so it's OK for Resize to discard the original aspect ration. https://github.com/lufficc/SSD/blob/50373c79b861d5d239be4206fafc6661cea040b4/ssd/data/transforms/__init__.py#L14-L15