问题:在MMDetectoin中,YOLO可视化特征图时可能会导致不匹配的问题,如下所示:
img = visualizer.draw_featmap(featmap,
resized_image,
channel_reduction='squeeze_mean',
alpha=0.8)
分析:这是因为YOLOX、YOLOV5等模型特征图大小为(H,H)如果原始图片长宽不等或者不是H的倍数则有可能造成这种问题。所以首先采用同样的预处理方案“letterbox”,将原始图片resize为特征图(H,H)的整数倍
实现letterbox:首先将图像等比例缩放,使得宽高值最大为640;接着通过填充像素值的方式,使得宽高能被32整除;最后把不满足640的那边进行填充
def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
# Resize and pad image while meeting stride-multiple constraints
shape = im.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
# Scale ratio (new / old)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
if not scaleup: # only scale down, do not scale up (for better val mAP)
r = min(r, 1.0)
# Compute padding
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
if auto: # minimum rectangle
dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding
elif scaleFill: # stretch
dw, dh = 0.0, 0.0
new_unpad = (new_shape[1], new_shape[0])
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
# Add extra padding to the bottom to make the image 640x640
if im.shape[0] != new_shape[0]:
bottom = new_shape[0] - im.shape[0]
im = cv2.copyMakeBorder(im, 0, bottom, 0, 0, cv2.BORDER_CONSTANT, value=color)
return im