Hengyuan cloud_ Share a skill! Data label problem easily ignored in CV training

Article source| Hengyuan cloud community

Original address| Data label problem

Today, Xiao Bian visited the community and found that a CV training tip shared by a little partner was a little content. Therefore, Xiao Bian moved it to show it to interested partners without stopping.

The following is the original text:

When training the detection model, in the face of data of more than 10000 magnitude, many friends may just randomly sample thousands of pictures to have a look, and will not carefully check whether each picture anno is correct. At this time, a common label dislocation problem may be ignored. This paper will briefly introduce this problem, hoping to be helpful to you.

This problem usually occurs in the pictures taken by the mobile phone. The appearance is that when you read the images with the PIL library, you will find that some images are misaligned with the detection box. Intuitively, if you rotate the pictures 90 or 180 or 270 degrees, they can correspond to the label box.

If your training set has a large number of such graphs, it is likely to lead to poor training results.

The reason for this phenomenon is that many pictures taken by mobile phones carry exif information, including camera rotation angle information. If you open the picture with the picture viewing software provided by windows, you will find that the picture will be rotated. The same is true for opencv loaded pictures, which will automatically rotate (90, 180, 270 degrees) according to exif information.

Similarly, if the tool labeling the data reads the image and rotates it according to the exif information, eg: label me, then the anno obviously corresponds to the rotated image.

However, PIL library loading pictures does not automatically process exif information, and many open source models use PIL library when loading data sets, which affects the training effect. In order to avoid label dislocation, there are usually the following methods:
1. Therefore, we should pay attention to this problem when reading pictures with pil. Try to read with cv2. If you really want to use pil, read with cv2 first and then convert to pil.
2. Check the exif information of the picture read by pil (img cannot call convert('RGB ') operation before that, and the exif information will be lost.

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. Use cv2 and other libraries to process the pictures, remove the exif information, and then submit the pictures to the marking team for marking.

The above is all the content. I hope I can help my partners in need.

Tags: Algorithm Deep Learning CV

Posted on Fri, 03 Dec 2021 00:06:30 -0500 by J-C