ACCV2020きめ細かい競争記録-データ処理とベースライン結果の共有

コンテストリンク:

  • accv公式ウェブサイト:https://sites.google.com/view/webfg2020
  • コンテストのウェブサイト:https://www.cvmart.net/race/9917/base

データのダウンロード:

  • Baidu / Baiduクラウドディスク:
    リンク:https
    ://pan.baidu.com/s/1P0rpg0J34IUL5bvuA5f-pg抽出コード:cg9z
  • Google / Google Cloud Disk
    https://drive.google.com/drive/folders/1ruvJow2Srp3wuqG1sYd_unG1EHBxYvI-?usp=sharing

Linuxでマージおよび解凍:cattrain.tar.gz。* | tar -zxv

1.データのクリーニング

トレーニングセット:5000カテゴリ、557,169枚の写真

テストセット:5000カテゴリ、100,000枚の写真

データはインターネットからのものであるため、Webサイトから直接クロールしていると見なすことができ、すべてのサフィックスがjpgフォーマットに変更されます。実際の状況から、元の画像にはフォーマットなどが含まれているためjpg、png、gif、tiff、コードの読み取り時にエラーが発生します(エラー)または警告(警告)なので、最初にデータをクリーンアップする必要があります。警告はプログラムの動作に影響を与えないため、エラーを報告できるエラーに変換する必要があります。コードは次のとおりです。

import warnings
warnings.filterwarnings('error')

実情によると、主に以下の種類のエラーがあります

  • corrupt EXIFそしてPossibly corrupt EXIF、これはEXIF情報の不足が原因です。フィルターで除外し、EXIFヘッダーを削除してください。

    # Refer: https://blog.csdn.net/a19990412/article/details/105940446
    # pip install piexif -i https://pypi.tuna.tsinghua.edu.cn/simple/
    import piexif
    piexif.remove(img_path)
    
  • Palette images with TransparencyPILが呼び出さconvert('RGB')れたときスローされました。おそらくアルファチャネルの変換の問題を意味します。当面は解決されていません。まず、非RGB(チャネル数は3ではありません)を除外します。チャネル図とデュアルチャネル図を作成し、データのこの部分を個別にフィルタリングしてエラーを報告し、数が少ないことを確認して、処理のためにデータセットから直接削除します。

  • image file could not be identified because WEBP、これは、一部のデータが元々webp形式であり、PILの読み取りに問題があり、conda環境にのみ存在するように見えるためです。いくつかの解決策があります。

    • PILライブラリ、6.x、7.xのアップグレードは問題ありませんが、新しい問題が発生します。問題を回避するには、バージョン5.4.1を使用してください。
    pip install Pillow==5.4.1
    
    • webpライブラリをインストールし、PILを更新して解決します。実際の測定はありませんが、
  • 最初の調査では不明なエラーは検出されませんでした。幸い、履歴ログを保存するために画面が使用されました。直接除外された不明なエラーはごくわずかでした。エラーメッセージは次のとおりです。

    Image size (117762898 pixels) exceeds limit of 89478485 pixels
    Metadata Warning, tag 296 had too many entries
    Image appears to be a malformed MPO file
    

完全なコード:

ステップ1:すべての写真に目を通し、問題のある写真をフィルタリングします

import os
from PIL import Image
import cv2
import warnings
warnings.filterwarnings('error')

root = './train'

f1 = open('pExifError.txt', 'w')
f2 = open('rgbaError.txt', 'w')
f3 = open('ExifError.txt', 'w')
f4 = open('4chImg.txt', 'w')
f5 = open('WebpError.txt', 'w')
f6 = open('UnknownError.txt', 'w')

idx = 0
for r, d, files in os.walk(root):
    if files != []:
        for i in files:
            fp = os.path.join(r, i)
            try:
                img = Image.open(fp)
                if(len(img.split()) != 3):
                    # print('4CH:', fp)
                    f4.write('{}\n'.format(fp))
                    
            except Exception as e:
                print('Error:', str(e))
                print(fp)
                if 'Possibly corrupt EXIF data' in str(e):
                    print('Exif error')
                    f1.write('{}\n'.format(fp))
                elif 'Palette images with Transparency' in str(e):
                    print('rgba error')
                    f2.write('{}\n'.format(fp))
                elif 'Corrupt EXIF data' in str(e):
                    print('pExif error')
                    f3.write('{}\n'.format(fp))
                elif 'image file could not be identified because WEBP' in str(e):
                    print('Webp error')
                    f5.write('{}\n'.format(fp))
                else:
                    print('Unknown error')
                    f6.write('{}\n'.format(fp))
    		
            if idx % 5000 == 0:
                print('='*20, idx)
            
            idx += 1

f1.close()
f2.close()
f3.close()
f4.close()
f5.close()
f6.close()

ステップ2:変換不可能な画像をフィルタリングする

import warnings
from PIL import Image
warnings.filterwarnings('error')

f1 = open('rgbaError.txt', 'w')
f2 = open('rgbaOK.txt', 'w')

with open('4chImg.txt', 'r')as f:
    for i in f.readlines():
        i = i.strip()
        try:
            img = Image.open(i).convert('RGB')
            f2.write('{}\n'.format(i))
            
        except Exception as e:
            print('Error:', str(e))
            print(i)
            f1.write('{}\n'.format(i))

f1.close()
f2.close()

ステップ3:変更して再テストする

import os
import piexif
import warnings
from PIL import Image
warnings.filterwarnings('error')

files = ['ExifError.txt', 'pExifError.txt']

for file in files:
    with open(file, 'r')as f:
        for i in f.readlines():
            i = i.strip()
            print(i.strip())
            piexif.remove(i.strip())
            # try:
            #    img = Image.open(i)
            # except Exception as e:
            #     print('Error:', str(e))
            #     print(i)

2.データセットを分割します

個人的には、txtへのパスを保存してから、データセットにロードします。

from sklearn.model_selection import train_test_split
import os

if __name__ == '__main__':
    root = './train'

    fpath = []
    labels = []
    for d in os.listdir(root):
        fd = os.path.join(root, d)
        label = int(d)
        for i in os.listdir(fd):
            fp = os.path.join(fd, i)
            fpath.append(fp)
            labels.append(label)
            
    print(len(fpath), len(labels))
    
    x_train, x_val, y_train, y_val = train_test_split(fpath, labels, random_state=999, test_size=0.2)
    print(len(x_train), len(x_val))

    with open('train.txt', 'w')as f:
        for fn, l in zip(x_train, y_train):
            f.write('{},{}\n'.format(fn, l))

    with open('val.txt', 'w')as f:
        for fn, l in zip(x_val, y_val):
            f.write('{},{}\n'.format(fn, l))

3.前処理

元のデータのサイズが異なるため、ほとんどが高解像度の画像です。トレーニング中のサイズ変更には時間がかかるため、サイズを小さくして保存してください。Image.thumbnail()フィルタリングの役割を果たすことができます。hwが範囲内にある場合はサイズ変更されず、それを超える場合は比例してスケーリングされます。画質とJPG圧縮の問題については、ブログ1ブログ2を参照してください

import os
from PIL import Image
import cv2
import shutil

root = './train'
save_path = './thumbnail'
for r, d, files in os.walk(root):
    if files != []:
        for i in files:
            fp = os.path.join(r, i)
            label = i.split('_')[0]
            dst = os.path.join(save_path, label)
            if not os.path.exists(dst):
                os.makedirs(dst)
            
            img = Image.open(fp).convert('RGB')
            w, h = img.size
            if max(w, h) > 1080:
                img.thumbnail((1080, 1080), Image.ANTIALIAS)
                img.save(os.path.join(dst, i), quality=95, subsampling=0)
            else:
                shutil.copy(fp, os.path.join(dst, i))

データセットのサイズは、処理前は114G、処理後は86Gです。

Tesla V100 32GB*2次のハードウェア環境、大幅にトレーニング時間を短縮ベースライン、2400S約トレーニング時間エポックの前処理(40分)、1400年代約後処理エポック(23min)を、訓練、精度も小さいサイズの判別をする必要があり調整、影響はないはず結局のところ、トレーニングデータのサイズは224x224です。

4.ベースライン
# ls: labelsmooth
# cat: cat(gmp, gap)
{
    
    
    model: resnet50,
    pool: cat,
    init_lr: 0.01,
    schedule: cos(warm: 5),
    epochs: <60,
    loss: ls 0.2,
    result: 41.497
}
{
    
    
    model: resnext50,
    pool: cat,
    init_lr: 0.01,
    shcedule: step(step: 8, gamma: 0.5),
    epochs: 60,
    loss: ls 0.2,
    result: 42.748
}

_(:з "∠)_ブッダ参加、大物がハイスコアのソリューションを共有するのを待ちます。

おすすめ

転載: blog.csdn.net/u013347145/article/details/109250455