[ML][CV] 車牌辨識 / 資料增強 / imgaug === ###### tags: `ML / CV` ###### tags: `ML`, `CV`, `augment` <br> ## 套件 - [[github] aleju/imgaug](https://github.com/aleju/imgaug) <br> ## [Weather](https://github.com/aleju/imgaug/blob/master/imgaug/augmenters/weather.py) - ### 效果圖 | FastSnowyLandscape<br>(lightness_multiplier=2.0) | Clouds | Fog | Snowflakes | | -------- | -------- | -------- | -------- | | ![](https://raw.githubusercontent.com/aleju/imgaug-doc/master/readme_images/augmenter_videos/fastsnowylandscape_lightness_multiplier_2_0.gif) | ![](https://raw.githubusercontent.com/aleju/imgaug-doc/master/readme_images/augmenter_videos/clouds.gif) | ![](https://raw.githubusercontent.com/aleju/imgaug-doc/master/readme_images/augmenter_videos/fog.gif) | ![](https://raw.githubusercontent.com/aleju/imgaug-doc/master/readme_images/augmenter_videos/snowflakes.gif) | <br><br> ## [Weather / FastSnowyLandscape](https://github.com/aleju/imgaug/blob/master/imgaug/augmenters/weather.py) - ### lightness_multiplier=2.0 [![](https://i.imgur.com/bXBNJWu.png)](https://i.imgur.com/bXBNJWu.png) <br> - ### lightness_threshold=50, lightness_multiplier=20.0 [![](https://i.imgur.com/AkG2bOH.png)](https://i.imgur.com/AkG2bOH.png) [![](https://i.imgur.com/pYYND9P.png)](https://i.imgur.com/pYYND9P.png) <br><br> ## [Weather / Clouds](https://github.com/aleju/imgaug/blob/master/imgaug/augmenters/weather.py) - 無參數可控制,效果可能不如預期。在 ```0003-ZT```中,16 張才出現 3~4 張想要的。 ```python iaa.Clouds().augment_image(raw) ``` [![](https://i.imgur.com/QpTxZ10.png)](https://i.imgur.com/QpTxZ10.png) <br> [![](https://i.imgur.com/JoP2Bl9.png)](https://i.imgur.com/JoP2Bl9.png) <br><br> ## [Weather / Fog](https://github.com/aleju/imgaug/blob/master/imgaug/augmenters/weather.py) - 無參數可控制。 ```python iaa.Fog().augment_image(raw) ``` iaa.Clou [![](https://i.imgur.com/MPP4ZqN.png)](https://i.imgur.com/MPP4ZqN.png) <br> [![](https://i.imgur.com/7ml6TGM.png)](https://i.imgur.com/7ml6TGM.png) <br><br> ## [Weather / Snowflake](https://github.com/aleju/imgaug/blob/master/imgaug/augmenters/weather.py) - ### 效果圖 - 原圖 <span style="margin-right:1.8em"></span>![](https://i.imgur.com/5Bqlqa0.jpg) <br> - ```flake_size=(0.4, 0.9), speed=(0.01, 0.05)``` 隨機產生 ![](https://i.imgur.com/weIdmMQ.png) - ### 程式碼 ```python from matplotlib import image as im from matplotlib import pyplot as plt from imgaug import augmenters as iaa # image / aug / augmenters ``` ```python raw = im.imread('tn_2M-7933_lp.jpg') plt.figure(figsize=(6,2)) plt.title('raw') plt.imshow(raw) fig = plt.figure(figsize=(20, 30)) row = 10 col = 3 applier = iaa.Snowflakes(flake_size=(0.4, 0.9), speed=(0.01, 0.05)) for idx in range(row * col): fig.add_subplot(row, col, idx + 1) # the current canvas aug_img = applier.augment_image(raw) plt.title('Snowflakes - ' + str(idx + 1)) plt.imshow(aug_img) ``` ![](https://i.imgur.com/sAjttgd.png) <br> - ### 參數說明 - #### density (雪花密度) - 有效範圍 ```[0.0, 1.0]``` - 建議範圍 ```[0.01, 0.075]``` - #### flake_size (雪花大小) - 有效範圍 ```[0.0, 1.0]``` - 建議範圍 * 影像大小 96x128 搭配 (0.1, 0.4) * 影像大小 192x256 搭配 (0.2, 0.7) * 影像大小 960x1280 搭配(0.7, 0.95) - #### angle (雪花角度) > Angle in degrees of motion blur applied to the snowflakes, > where ``0.0`` is motion blur that points straight upwards. > Recommended to be in the interval ``[-30, 30]``. - 單位 degrees - 建議範圍 ```[-30, 30]``` - #### speed (雪花的下降速度) > This parameter controls the motion blur's kernel size. It follows roughly the form ``kernel_size = image_size * speed``. Hence, values around ``1.0`` denote that the motion blur should "stretch" each snowflake over the whole image. - 有效範圍 ```[0.0, 1.0]``` - 建議範圍 * 影像大小 96x128 搭配 (0.01, 0.05) * 影像大小 192x256 搭配 (0.007, 0.03) * 影像大小 960x1280 搭配 (0.001, 0.03) - 當值為 1 時,雪花軌跡會形成白線條,橫跨整張圖 - ### 案例說明 - 即便參數都是固定一個數值,產生的效果也是隨機的 ```density=0.1, flake_size=0.0001, angle=-6, speed=0.5``` ![](https://i.imgur.com/ONXGzmv.png) <br> - density 超過 0.5 以上的例子,超過 9 成都無法使用。必須將 speed 控制為 0 - density = 0.5 [![](https://i.imgur.com/eB094Ph.png)](https://i.imgur.com/eB094Ph.png) - density = 0.7 [![](https://i.imgur.com/EcB5pHj.png)](https://i.imgur.com/EcB5pHj.png) - density = 1.0 [![](https://i.imgur.com/orCMcU2.png)](https://i.imgur.com/orCMcU2.png) <br> ## 測試實驗 - ### case1 - base - train - dataset/trainset/G4_Archives_7monthes.new - test - dataset/trainset/G4_Archives_7monthes.new - Total: 9292 - Wrong: 9 - Time: 0.07271468539305714 - ACC: 0.999 - dataset/testset/T1_20191210_tn_renew/ - Total: 68 - Wrong: 8 - Time: 0.1030008126707638 - ACC: 0.88 - ### case2 - with snowflakes - train - dataset/trainset/G4_Archives_7monthes.new.Snowflakes - test - dataset/trainset/G4_Archives_7monthes.new.Snowflakes - dataset/testset/T1_20191210_tn_renew/ <!-- from matplotlib import image as im from matplotlib import pyplot as plt import matplotlib._png as png from imgaug import augmenters as iaa # image / aug / augmenters import shutil import random applier_list = [ iaa.Snowflakes(density=0.1, flake_size=0.9, speed=0), iaa.Snowflakes(density=0.2, flake_size=0.7, speed=0), iaa.Snowflakes(density=0.3, flake_size=0.5, speed=0), iaa.Snowflakes(density=0.4, flake_size=0.3, speed=0), iaa.Snowflakes(density=0.5, flake_size=0.1, speed=0), iaa.Snowflakes(density=0.1, flake_size=0.3, speed=0.04), iaa.Snowflakes(density=0.2, flake_size=0.6, speed=0.04), iaa.Snowflakes(density=(0.05, 0.075), flake_size=(0.8, 0.9), speed=(0.03, 0.045)) ] src_dataset_folder = '/tmp/TJ/dataset/trainset/G4_Archives_7monthes.new/' new_dataset_folder = '/tmp/TJ/dataset/trainset/G4_Archives_7monthes.new.Snowflakes/' filename_list = os.listdir(src_dataset_folder) filename_list.sort() row = 5 col = 3 aug_size_per_img = row * col plt_limit = 0 for filename in filename_list: filepath = os.path.join(src_dataset_folder, filename) new_filepath = os.path.join(new_dataset_folder, filename) shutil.copyfile(filepath, new_filepath) #raw_img = im.imread(file_path) raw_img = png.read_png_int(filepath) #plt.title('raw: ' + filename) #plt.imshow(raw_img) #plt.show() #fig = plt.figure(figsize=(20, 12)) for idx in range(aug_size_per_img): #fig.add_subplot(row, col, idx + 1) index = int(random.random() * 100 // 5) if index < len(applier_list): applier = applier_list[index] else: applier = applier_list[-1] aug_img = applier.augment_image(raw_img) tokens = os.path.splitext(filename) new_filename = tokens[0] + '_' + '{:02d}.jpg'.format(idx) new_filepath = os.path.join(new_dataset_folder, new_filename) im.imsave(new_filepath, aug_img) #plt.imshow(aug_img) #plt.title('aug: ' + str(idx)) #plt.show() #plt_limit += 1 #if plt_limit > 5: # break -->