• 目标检测 YOLOv5 - 模型推理预处理 letterbox


    目标检测 YOLOv5 - 模型推理预处理 letterbox

    flyfish

    版本:YOLOv5 6.2
    假如图片大小是1080 * 1920 (height * width )
    width = 1920
    height = 1080

    当模型输入是 640 * 640时
    shapes = (1080, 1920), (0.33, 0.33), (0.0, 140.0)

    640/ 1920 = 0.33
    图片缩放到 360 * 640 (height * width )
    在这里插入图片描述

    当前 模型 输入 必须 640 * 640 时
    就将 360 * 640 的图片 装进 640 * 640中
    多余的部分都是灰色边框
    140是图片上下边框各自的高度
    在这里插入图片描述

    当模型输入 不必须 640 * 640 时
    可以做成填充最小矩形灰色边框能被32整除
    360 / 32 = 11.25
    384 / 32 = 12
    360 * 640 变成了 384 * 640
    在这里插入图片描述
    这样图片上下有很少的灰色边框
    1080 * 1920 (height * width )变 360 * 640 (height * width )

    def load_image(self, i):
        # Loads 1 image from dataset index 'i', returns (im, original hw, resized hw)
        im, f, fn = self.ims[i], self.im_files[i], self.npy_files[i],
        if im is None:  # not cached in RAM
            if fn.exists():  # load npy
                im = np.load(fn)
            else:  # read image
                im = cv2.imread(f)  # BGR
                assert im is not None, f'Image Not Found {f}'
            h0, w0 = im.shape[:2]  # orig hw
            r = self.img_size / max(h0, w0)  # ratio
            if r != 1:  # if sizes are not equal
                interp = cv2.INTER_LINEAR if (self.augment or r > 1) else cv2.INTER_AREA
                im = cv2.resize(im, (int(w0 * r), int(h0 * r)), interpolation=interp)
            return im, (h0, w0), im.shape[:2]  # im, hw_original, hw_resized
        return self.ims[i], self.im_hw0[i], self.im_hw[i]  # im, hw_original, hw_resized
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    360 * 640 (height * width )变 384 * 640 (height * width )

    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
        return im, ratio, (dw, dh)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
  • 相关阅读:
    async-validator
    一条龙-T检验+绘制boxplot
    23062C++&QTday7
    SnakeYaml的不出网反序列化利用分析
    聊天没有表情包被嘲讽,程序员直接用python爬取了十万张表情包
    Vite创建Vue2项目
    nodejs:path路径模块
    thrust工程化学习(四)----降采样操作
    详解算法题目:幼稚班作业
    spring 扩展点之后置处理器(PostProcessor)及Aware接口
  • 原文地址:https://blog.csdn.net/flyfish1986/article/details/127450753