Python影像辨識筆記(三十六):Arbitrary Style Transfer in Real-time with Adaptive Instance Normalization
Jul 30, 2021
2021/08/10更新:DOMAIN GENERALIZATION WITH MIXSTYLE
這篇論文提出了更精簡的方法,不須使用特別的GAN模型也能達到Mix Style的效果,而且能輕易的加在CNN模型上
論文
[1703.06868] Arbitrary Style Transfer in Real-time with Adaptive Instance Normalization (arxiv.org)
* Given a content input and a style input, AdaIN simply adjusts the mean and variance of the content input to match those of the style input.- This approach can transfer arbitrary new styles in real-time.
# 訓練
git clone https://github.com/irasin/Pytorch_AdaIN
cd Pytorch_AdaIN# 將風格圖片(style)放入到style資料夾、想進行風格轉換的圖片(content)放入到content資料夾,並透過以下指令即可進行訓練。python train.py
如果圖片數量很多的話,可以重寫dataset.py的程式碼,使其可支援自定義的DataLoader(例如從json格式的檔案讀取圖片路徑資料,只不過train.py也要跟著修改部分的程式),修改過的dataset.py如下:
train.py主要修改的是將自己的json檔案透過pandas讀取後,與PreprocessDataset搭配在一起,傳給PyTorch的DataLoader進行讀取即可
預設是訓練20個epoch,我自己使用某個PCB瑕疵檢測的資料集(100多萬張圖片),大約13小時的時間可完成訓練。在Testing上確實能將目標的瑕疵風格(style)轉移到content上,從肉眼觀察很像是將Style image的對比度transfer到content image上。在視覺上,並不會影響到原有瑕疵的狀態。
# 測試(在content和style各放入1個圖片路徑)(也可使用for迴圈,將多張圖片一次轉換成特定的風格)
python test.py -c content_image_path -s style_image_path
這邊提供我自己修改過可以使用多張圖片一次完成風格遷移的程式碼