閱讀CariFaceParsing(未完待續。。。。)

閱讀CariFaceParsing(未完待續。。。。)

github

1. 數據集介紹

1.1 Helen_images

我們使用到的helen數據集從dataset鏈接中下載得到,(注意:我們下載的是Resized過的數據版本——341.7M)。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-I8OkFd7O-1579076962687)(assets/1573719835860.png)]

在images文件夾下是我們會使用到的圖片文件,此時下載下來的文件中的labels文件夾裏存放着每張image對應的11個parsing圖片,每一張parsing代表人臉的組件,如左眼、右眼、頭髮等等。但是我們在CariFaceParsing試驗中並不是用到這個parsing文件。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-TZvfvqFo-1579076962688)(assets/1573720142057.png)]

1.2 helen_parsing

import numpy as np
import os
import argparse
from PIL import Image
import cv2
import torchvision.transforms as transforms

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='PyTorch ')
    parser.add_argument('--dataset_path', type=str, default='../datasets/helen/labels_11')
    opt = parser.parse_args()
    osize = [286, 286]
    transform = transforms.Compose([
        transforms.Resize(osize, Image.BICUBIC),
        transforms.RandomCrop(256)
    ])
    namelist = os.listdir(opt.dataset_path)
    for name in namelist:
        parsing_list = os.listdir(os.path.join(opt.dataset_path, name))
        flag = 0
        parsing_list.sort()
        index = 0
        image = Image.open('../datasets/helen/images/' + name + '.jpg').convert('RGB')
        im = image.resize((256, 256), Image.BILINEAR)
        im = np.array(im)
        vis_im = im.copy().astype(np.uint8)
        for parsingname in parsing_list:
            img = np.array(Image.open(os.path.join(opt.dataset_path, name, parsingname)).resize((256, 256), Image.BICUBIC))[np.newaxis, :, :]
            img = np.where(img > 20, index, 0)
            index += 1
            if flag == 0:
                flag = 1
                imgs = img
            else:
                imgs = np.concatenate((imgs, img), axis=0)
        parsing = np.argmax(imgs, axis=0)
        save_path = '../datasets/helen/labels/%s' %(name)
        print(name, np.unique(parsing))

        part_colors = [ [255, 85, 255], [255, 85, 0], [255, 170, 0],
                       [255, 0, 85], [255, 0, 170],
                       [0, 255, 0], [85, 255, 0], [170, 255, 0],
                       [0, 255, 85], [0, 255, 170],
                       [0, 0, 255], [85, 0, 255], [170, 0, 255],
                       [0, 85, 255], [0, 170, 255],
                       [255, 255, 0], [255, 255, 85], [255, 255, 170],
                       [255, 0, 255], [255, 85, 255], [255, 170, 255],
                       [0, 255, 255], [85, 255, 255], [170, 255, 255]]
        parsing_anno = parsing
        vis_parsing_anno = parsing_anno.copy().astype(np.uint8)
        vis_parsing_anno = cv2.resize(vis_parsing_anno, None, fx=1, fy=1, interpolation=cv2.INTER_NEAREST)
        vis_parsing_anno_color = np.zeros((vis_parsing_anno.shape[0], vis_parsing_anno.shape[1], 3)) + 255
        num_of_class = np.max(vis_parsing_anno)
        for pi in np.unique(parsing):
            index = np.where(vis_parsing_anno == pi)
            vis_parsing_anno_color[index[0], index[1], :] = part_colors[pi]
        vis_parsing_anno_color = vis_parsing_anno_color.astype(np.uint8)
        vis_im = cv2.addWeighted(cv2.cvtColor(vis_im, cv2.COLOR_RGB2BGR), 0.4, vis_parsing_anno_color, 0.6, 0)
        cv2.imwrite(save_path + '.png', vis_parsing_anno)
        # cv2.imwrite(save_path+'.jpg', vis_im, [int(cv2.IMWRITE_JPEG_QUALITY), 100])  # 保存附上顏色的圖片
    print('end')

注:保存下來的png文件爲只有1-11 label數字的圖片,而jpg圖片爲在每一個臉部部位附上相應顏色後的圖片。在stap two 中我們使用的是png圖片。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-ccH74TZL-1579076962688)(assets/1576465897786.png)][外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-hPRNGkU8-1579076962689)(assets/1576465927408.png)]在這裏插入圖片描述

1.3 helen_landmarks

download

該鏈接是在CariFaceParsing的github作者提供的,下載下來後我們能在下圖路徑下找到我們需要用到的helen數據集的landmarks文件。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-qZZM1D3q-1579076962689)(assets/1573733358450.png)]

1.3 WebCaricature

對於論文中使用到的WebCaricature數據集也並不是直接使用的,在download中下載下來的文件的路徑CariFaceParsing_data\adaptation\datasets\landmark_webcaricature中我們可以發現作者將WebCaricature部分圖片根據聚類中心劃分了A-I這9類。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-3iIJP1Il-1579076962689)(assets/1573734036218.png)]

由於在作者提供的鏈接裏沒有提供根據聚類好的分類的圖片數據,所以我們通過提供的landmark_webcaricature裏面的文件名稱讀取出相應的圖片文件。並將讀取出來的圖片存放的文件夾命名爲face_webcaricature.

import os
import argparse
import shutil

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='PyTorch ')
    parser.add_argument('--landmark_path', type=str, default='../datasets/landmark_webcaricature')
    parser.add_argument('--webcaricature_dataset', type=str, default='/home/lingna/workspaces/experiments_on_face_photo2drawing-master/datasets/WebCaricature/frontalization_dataset_v005/OriginalImages')
    opt = parser.parse_args()
    path_list = os.listdir(opt.landmark_path)
    for path in path_list:
        if os.path.isdir(os.path.join(opt.landmark_path, path)):
            names = os.listdir(os.path.join(opt.landmark_path, path))
            for name in names:
                p_or_c_name, id = name.split('_')[0], name.split('_')[1].replace('npy', 'jpg')
                original_path = os.path.join(opt.webcaricature_dataset, p_or_c_name, id)
                target_path = os.path.join('../datasets/face_webcaricature', path)
                if not os.path.exists(target_path):
                    os.makedirs(target_path)
                shutil.copy(original_path, target_path)
                os.rename(target_path+'/'+id, target_path+'/'+p_or_c_name+'_'+id)

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-2OamwEaY-1579076962690)(assets/1573734528906.png)]

2. CariFaceParsing實驗部分

2.1 框架結構

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-NAsKpera-1579076962690)(assets/1573735059944.png)]

簡單介紹一下論文所要做的工作,在目前的很多工作都是針對真實人臉照片來生成人臉parsing圖,但卻有很少可以通過一張漫畫圖去生成漫畫的parsing圖。所以本文所做的任務就是去通過Shape Adaptation 和 Texture Adaptation 這兩個模塊去生成漫畫和漫畫parsing,這樣便可以通過漫畫和漫畫的parsing圖去訓練我們的face parsing Network。

下面介紹上圖框架中標註的文字分別所代表的含義。①landmarks Maps、Ground Truth分別是一張Input Photo 的landmark和parsing圖,其中landmark Maps是one-hot形式的,我們把不同的ground truth landmarks連接成線或區域,如嘴,鼻子。然後我們在landmark maps中把每條連接的線或區域看作一個單一的通道,表示照片的面部佈局。②Shape Selector:爲了更好地編碼漫畫形狀誇張的多樣性,我們利用一個one-hot vector作爲conditional input,以生成不同的形狀。那麼它是怎麼得到的呢?其實在訓練之前,我們首先利用landmark positions對caricature 數據集進行聚類,並利用聚類中心作爲形狀集。我們從上面1.2節可以發現作者將caricature數據集聚類成了9類(A-I),所以也就是說Shape Selector中的每一位代表不同的漫畫聚類。(style Selector和此相類似,只是style selector是根據漫畫圖片風格聚類得到的不同的漫畫風格分類)③Synthetic Ground Truth:表示parsing圖的ground truth圖經過形變得到的圖片。④shape Adaptation 和textture adaptation 分別用來做學習變形參數,以及風格遷移的。我們將得到的形變後的漫畫和形變後的parsing圖去訓練FaceParsingNetwork。

Shape Adaptation

​ 本論文所提出的Shape Adaptation模塊主要是受到Spatial Transformer Networks(STN)的啓發。我們的方法還包含可微圖像扭曲操作(differentiable image warping operation ),以對照片執行合理的形狀誇張以捕獲形狀域偏移。由於沒有配對的訓練數據可用,所以我們以cycleGAN的方式訓練Shape Adaptation模塊,其中STN被插入到圖像轉換網絡中。

​ Shape Adaptation預測在照片上的空間扭曲的參數以生成變形的參數。根據這種變形方式,我們將ground truth labels也做相應的形變。

Texture Adaptation

在shape Adaptation之後,我們可以得到形變後的人臉照片和形變後的parsing圖,在texture中我們主要是爲了將形變後的人臉照片進行漫畫的風格遷移。在該結構中我們使用conditional information(style Selector)去得到多風格的texture generation。(該部分模型的輸入爲形變後的人臉照片和style Selector。)

在訓練這個模型的時候,我們使用的是content loss和style loss[1]。對於content loss 我們將生成的漫畫和形變的照片用VGG模型提取出Conv4-2層的特徵。目的是加強生成的漫畫,並保存變形照片的結構。對於style loss我們用Batch Normalization (BN)的統計量代替Gram matrices的統計量來計算特徵。然後,以生成的漫畫與參考漫畫圖像的特徵差異爲特徵,得出style loss。

2.2 訓練shape adapatation模型

2.2.1 step one

Our method has two parts, adaptation and segmentation. For adaptation, go to the adaptation directory. Please put the Webcaricature dataset to “CariFaceParsing/adaptation/datasets/face_webcaricature”. And link “trainA” and “val” to “photo”, link “trainB”, “trainC”, “trainD”, “trainE”, “trainF”, “trainG”, “trainH”, “trainI” to “caricature”. And download the provided “landmark_webcaricature” and put it to “CariFaceParsing/adaptation/datasets/”

python train.py --dataroot ./datasets/landmark_webcaricature/ --name shape_adaptation --model shape_adaptation --input_nc 20 --output_nc 11 --dataset_mode star --batch_size 8 

注意:直接使用該行命令去運行作者代碼的時候要注意圖片的大小,因爲默認的–resize_or_crop參數爲scale_width,所以當數據集圖片大小不一的時候便會報錯。所以解決辦法一個是讓輸入圖片大小保持一致,或者將–resize_or_crop參數scale_width修改爲resize_and_crop。

該部分實驗的含義作用:通過cyclegan的方式學習訓練shape adaptation模型。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-Hot2XDjq-1579076962691)(assets/1573821449264.png)]

2.2.2 step two

Then put Helen dataset to “CariFaceParsing/adaptation/datasets/helen” and it should have three subfolder, “images”, “labels”, “landmark”.

generate_shape_adaptation.py --dataroot ./datasets/helen/landmark/ --name shape_adaptation --model shape_adaptation --input_nc 20 --output_nc 11 --dataset_mode helen

此時我們會在helen_shape_adaptation文件夾中得到變形後的helen圖片,以及變形後的helen的parsing圖片,並且每一張helen圖在經過這個代碼運行之後會得到B-I這幾種的變形方式。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-sKC0ebXs-1579076962691)(assets/1576467638111.png)]

2.2.3 step three

[1]Yijun Li, Chen Fang, Jimei Yang, Zhaowen Wang, Xin Lu, and Ming-Hsuan Yang, “Diversified texture synthesis with feed-forward networks,” in CVPR, 2017. 3

發佈了17 篇原創文章 · 獲贊 58 · 訪問量 8945
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章