判定深度学习的图片和标注文件是否存在问题

深度学习过程中,数据的标注一般是由标注工程师完成,因为一些未知的失误或者数据传输存在的问题,导致图片损失或者xml标注文件存在错误,极易导致训练失败,让我们无法正确的完成训练,如下所示:

Corrupt JPEG data: 469 extraneous bytes before marker 0xd9

因此,在训练开始之前做数据清理(data cleaning)就显得十分必要。

数据处理–图片清理:

图片不完整的判定

def check_valid_image(sample_root, img_format=['.jpg', '.png', '.JPG', 'jpeg']):
    for root, _, file_lst in os.walk(sample_root):
        pbar = tqdm(file_lst, file=sys.stdout)
        for file in pbar:
            if os.path.splitext(file)[-1] in img_format:
                category = root.split('\\')[-1]
                img_path = os.path.join(root, file)
                try:
                    # img.shape
                    img2 = Image.open(img_path).load()
                except Exception as e:
                    print(img_path)
                    tqdm.write('[ERROR]:{}, {}'.format(category, file))
                    os.unlink(img_path)
                pbar.set_description('Processing [{}]'.format(category))
    print('Finish checking images.')

数据处理–xml处理

xml是否有误的操作

def check_xml_is_valid_full(sample_root, remove_or_not=False):
    for root, _, file_lst in os.walk(sample_root):
        pbar = tqdm(file_lst, file=sys.stdout)
        for file in pbar:
            if os.path.splitext(file)[-1] in ['.xml']:
                category = root.split('\\')[-1]
                xml_path = os.path.join(root, file)
                try:
                    tree = ET.parse(xml_path)
                    root1 = tree.getroot()
                    size = root.findall('size')
                    # without gt labels
                    if len(size) == 0:
                        print(xml_path)
                        if remove_or_not:
                            os.remove(xml_path)
                        # size is not correct
                    obj = root1.findall('object')
                    if len(obj) == 0:
                        if remove_or_not:
                            os.remove(xml_path)
                except:
                    tqdm.write('[ERROR]:{}, {}'.format(category, file))
                pbar.set_description('Processing [{}]'.format(category))
        print('Finish checking xmls.')

使用方式

if __name__ == '__main__':
    sample_root = '你的图片xml地址'
    check_valid_image(sample_root)
    check_xml_is_valid_full(sample_root)

mark一下,用作记录!

图片出现有一半为灰色不完整情况

较好的方法就是判断jpg、jpeg、png文件结尾的标识。

JPG文件结尾标识:\xff\xd9
JPEG文件结尾标识:\xff\xd9
PNG文件结尾标识:\xaeB`\x82
代码后续验证了再发
参考:检测下载不完整、半截灰色的JPG、JPEG、PNG图片脚本

–END–

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI小花猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值