Skip to content

[Ten Crops]多样本检测

在检测阶段,采集一张样本中的多个裁剪,平均其预测结果,有助于更高的检测精度

完整实现参考py/data_preprocessing/ten-crops.py

FiveCrops/TenCrops

常用的有FiveCropsTenCrops

  • FiveCrops:裁剪图像中心和4个角
  • TenCrops:裁剪图像中心和4个角,以及翻转图像后的5个裁剪

PyTorch实现

PyTorch提供了函数torchvision.transforms.FiveCroptorchvision.transforms.TenCrop。裁剪5张图像并显示

def plot(src, dsts):
    f = plt.figure()

    cols = 3
    rows = 2

    plt.subplot(rows, cols, 1)
    plt.title('src')
    plt.imshow(src)

    for i in range(rows):
        for j in range(cols):
            if (i * cols + j) == 5:
                break

            plt.subplot(rows, cols, i * cols + j + 2)
            plt.imshow(dsts[i * cols + j])

    plt.show()


def draw_five_crop():
    src = Image.open('./data/butterfly.jpg')

    transform = transforms.Compose([
        transforms.Resize(256),
        transforms.FiveCrop(224),  # this is a list of PIL Images
    ])

    dsts = transform(src)
    print(len(dsts))
    plot(src, dsts)

相关阅读