Navigation

    Gpushare.com

    • Register
    • Login
    • Search
    • Popular
    • Categories
    • Recent
    • Tags

    分享一个技巧!CV训练时容易忽视的数据标签问题

    CV领域
    2
    2
    109
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 1
      189****3901 last edited by Alice_恒源云

      在训练检测模型时,面对万以上量级的数据,可能很多朋友只是随机抽样个几千张图看一下,而并不会仔细检查每一张图片anno是否正确。这时候可能会忽视一种常见的标签错位问题,本文将简要介绍该问题,希望对大家有所帮助。

      这种问题一般出现在手机拍摄的图片中,表象是:当你用PIL库读取图像时,会发现有些图像与检测框错位,直观上如果将图片旋转90 or 180 or 270度,正好可以和标签框对应上。

      如果你的训练集有大量这种图,很可能导致训练结果不佳。

      造成这种现象的原因是手机拍摄的图片很多都带了exif信息,其中包含了摄像头旋转角度信息。如果你用windows自带的看图软件打开图片,会发现图片会被旋转。opencv加载图片也是如此,会根据exif信息自动旋转(90、180、270度)。

      同理,如果标注数据的工具读取该图片的时候根据exif信息做了旋转,eg:labelme,那么anno显然对应于旋转后的图片。

      但是PIL库加载图片并不会自动处理exif信息,而很多开源模型在加载数据集时采用的都是PIL库,从而影响训练效果。为了避免出现标签错位问题,通常有如下几种方法:

      1、所以使用pil读图需要注意该问题。尽量用cv2读取,实在要用pil,先cv2读取再转成pil。

      2、对pil读取的图片做exif信息检查 (在此之前img不可以调用convert(‘RGB’)操作,会丢失exif信息。

      def apply_exif_orientation(image, file):
          try:
              exif = image._getexif()
          except AttributeError:
              exif = None
      
          if exif is None:
              return image
      
          exif = {
              PIL.ExifTags.TAGS[k]: v
              for k, v in exif.items()
              if k in PIL.ExifTags.TAGS
          }
      
          orientation = exif.get('Orientation', None)
      
          if orientation == 1:
              # do nothing
              return image
          elif orientation == 2:
              # left-to-right mirror
              print('left-to-right mirror : {}'.format(file))
              return PIL.ImageOps.mirror(image)
          elif orientation == 3:
              # rotate 180
              print('rotate 180 : {}'.format(file))
              return image.transpose(PIL.Image.ROTATE_180)
          elif orientation == 4:
              # top-to-bottom mirror
              print('top-to-bottom mirror : {}'.format(file))
              return PIL.ImageOps.flip(image)
          elif orientation == 5:
              # top-to-left mirror
              print('top-to-left mirror : {}'.format(file))
              return PIL.ImageOps.mirror(image.transpose(PIL.Image.ROTATE_270))
          elif orientation == 6:
              # rotate 270
              print('rotate 270 : {}'.format(file))
              return image.transpose(PIL.Image.ROTATE_270)
          elif orientation == 7:
              # top-to-right mirror
              print('top-to-right mirror : {}'.format(file))
              return PIL.ImageOps.mirror(image.transpose(PIL.Image.ROTATE_90))
          elif orientation == 8:
              # rotate 90
              print('rotate 90 : {}'.format(file))
              return image.transpose(PIL.Image.ROTATE_90)
          else:
              return image
      

      3、统一用cv2等库对图片进行处理,去掉exif信息。然后再将图片交给标注团队进行打标。

      1 Reply Last reply Reply Quote 2
      • Alice_恒源云
        Alice_恒源云 last edited by

        数据处理技巧get!

        1 Reply Last reply Reply Quote 1
        • First post
          Last post