CelebAMask数据集是2020年MaskGAN这篇文章一起提出的,这篇文章主要讲了利用用户修改后的语义分割信息来引导图像编辑。
港中文和商汤合作的,还是蛮不错的一篇文章。
文章链接:MaskGAN: Towards Diverse and Interactive Facial Image Manipulation
本篇博客只讲文章对于CelebAMask数据集补充的部分(因为博主目前更关心这部分哈哈哈啊哈!)
感兴趣可以去看看CelebA-HQ和CelebA的提出
CelebA-HQ[17]Tero Karras, Timo Aila, Samuli Laine, and Jaakko Lehtinen.Progressive growing of gans for improved quality, stability,and variation. arXiv preprint arXiv:1710.10196, 2017.
CelebA[27] Tero Karras, Timo Aila, Samuli Laine, and Jaakko Lehtinen.Progressive growing of gans for improved quality, stability,and variation. arXiv preprint arXiv:1710.10196, 2017.
g_mask.py
import os
import cv2
import numpy as np
from utils import make_folder
label_list = ['skin', 'nose', 'eye_g', 'l_eye', 'r_eye', 'l_brow', 'r_brow', 'l_ear', 'r_ear', 'mouth', 'u_lip', 'l_lip', 'hair', 'hat', 'ear_r', 'neck_l', 'neck', 'cloth']
# 输入数据集的名称一定要检查一下,这个名称错了不会报错,但是输出的mask里面全部都是空值
folder_base = 'CelebAMask-HQ-mask-anno'
folder_save = 'CelebAMaskHQ-mask'
img_num = 30000 # 数据集一共有30000张图片
make_folder(folder_save)
for k in range(img_num):
folder_num = k // 2000 # 该图片的分割组件存放的目录,一共有15个目录,每个目录存了2000张分割结果(包含一张图片的面部各个组件分开的分割结果)
im_base = np.zeros((512, 512))
for idx, label in enumerate(label_list):
filename = os.path.join(folder_base, str(folder_num), str(k).rjust(5, '0') + '_' + label + '.png')
if (os.path.exists(filename)):
print (label, idx+1)
im = cv2.imread(filename)
im = im[:, :, 0] # 取出图像第一个通道的值(分割图像只有一个通道,但是是部分的组件)
im_base[im != 0] = (idx + 1) # 将该部分的值赋予一个idx+1的数值,实现分割,后期填充上颜色就变成我们看到的最终分割结果了
filename_save = os.path.join(folder_save, str(k) + '.png')
print (filename_save)
cv2.imwrite(filename_save, im_base) # 保存图片
g_color.py
import os
from PIL import Image
import numpy as np
from utils import make_folder
color_list = [[0, 0, 0], [204, 0, 0], [76, 153, 0], [204, 204, 0], [51, 51, 255], [204, 0, 204], [0, 255, 255], [255, 204, 204], [102, 51, 0], [255, 0, 0], [102, 204, 0], [255, 255, 0], [0, 0, 153], [0, 0, 204], [255, 51, 153], [0, 204, 204], [0, 51, 0], [255, 153, 51], [0, 204, 0]]
folder_base = 'CelebAMaskHQ-mask'
folder_save = 'CelebAMask-HQ-mask-color'
img_num = 30000
make_folder(folder_save)
for k in range(img_num):
filename = os.path.join(folder_base, str(k) + '.png')
im_base = np.zeros((512, 512, 3))
if (os.path.exists(filename)):
print(filename)
im = Image.open(filename)
im = np.array(im)
for idx, color in enumerate(color_list):
im_base[im == idx] = color # 将标签idx(单通道数据)和三通道的颜色对应上,给数据一个色彩填充
filename_save = os.path.join(folder_save, str(k) + '.png')
result = Image.fromarray((im_base).astype(np.uint8))
print (filename_save)
result.save(filename_save)
import os
import shutil
import pandas as pd
from shutil import copyfile
from utils import make_folder
#### source data path
# s_label = 'CelebAMask-HQ-label'
s_label = 'CelebAMask-HQ-mask'
s_img = 'CelebA-HQ-img'
#### destination training data path
d_train_label = 'train_label'
d_train_img = 'train_img'
#### destination testing data path
d_test_label = 'test_label'
d_test_img = 'test_img'
#### val data path
d_val_label = 'val_label'
d_val_img = 'val_img'
#### make folder
make_folder(d_train_label)
make_folder(d_train_img)
make_folder(d_test_label)
make_folder(d_test_img)
make_folder(d_val_label)
make_folder(d_val_img)
#### calculate data counts in destination folder
train_count = 0
test_count = 0
val_count = 0
image_list = pd.read_csv('CelebA-HQ-to-CelebA-mapping.txt', delim_whitespace=True, header=None)
f_train = open('train_list.txt', 'w')
f_val = open('val_list.txt', 'w')
f_test = open('test_list.txt', 'w')
for idx, x in enumerate(image_list.loc[:, 1]):
print (idx, x)
if x >= 162771 and x < 182638:
copyfile(os.path.join(s_label, str(idx)+'.png'), os.path.join(d_val_label, str(val_count)+'.png'))
copyfile(os.path.join(s_img, str(idx)+'.jpg'), os.path.join(d_val_img, str(val_count)+'.jpg'))
val_count += 1
elif x >= 182638:
copyfile(os.path.join(s_label, str(idx)+'.png'), os.path.join(d_test_label, str(test_count)+'.png'))
copyfile(os.path.join(s_img, str(idx)+'.jpg'), os.path.join(d_test_img, str(test_count)+'.jpg'))
test_count += 1
else:
copyfile(os.path.join(s_label, str(idx)+'.png'), os.path.join(d_train_label, str(train_count)+'.png'))
copyfile(os.path.join(s_img, str(idx)+'.jpg'), os.path.join(d_train_img, str(train_count)+'.jpg'))
train_count += 1
print (train_count + test_count + val_count)
#### close the file
f_train.close()
f_val.close()
f_test.close()