coco数据集格式的RandomCrop

本文讨论了在transforms.py中加入RandomCrop函数,以及train.py中的应用,同时提及了训练过程中遇到的lossNaN问题。

transforms.py文件的改进

添加 RandomCrop 函数

class RandomCrop(object):
    """随机裁剪图像以及bboxes"""
    def __init__(self, output_size):
        self.output_size = output_size

    def __call__(self, image, target):
        height, width = image.shape[-2:]
        th = self.output_size
        tw = self.output_size

        if width == tw and height == th:
            return image, target

        x = random.randint(0, width - tw)
        y = random.randint(0, height - th)

        image = image[:, y:y+th, x:x+tw]

        bbox = target["boxes"]
        bbox[:, [0, 2]] = bbox[:, [0, 2]] - x
        bbox[:, [1, 3]] = bbox[:, [1, 3]] - y
        target["boxes"] = bbox

        if "masks" in target:
            target["masks"] = target["masks"][:, y:y+th, x:x+tw]

        return image, target

train.py文件中的改进

添加RandomCrop模块

    data_transform = {
        "train": transforms.Compose([transforms.ToTensor(),
                                     transforms.RandomHorizontalFlip(0.5),
                                      transforms.RandomCrop(1024)
                                    ]),
        "val": transforms.Compose([transforms.ToTensor()])
    }

训练中出现错误:

loss达到了50.0+

训练中途loss超过100的的时候会出现 loss is nan的报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值