上篇我们介绍了celebA数据集
CelebA Datasets——Readme
今天我们就使用这个数据集进行对我们的GAN模型进行训练
首先引入一个库
代码语言:javascript复制mtcnn
是一个人脸识别的深度学习的库,传入一张人脸好骗,mtcnn库可以给我们返回四个坐标,用这四个坐标就可以组成一个矩形框也就是对应的人脸位置
安装方式:
代码语言:javascript复制pip install mtcnn
教程中的用法:
下面是一个完整的实例,准备数据集
代码语言:javascript复制# example of extracting and resizing faces into a new dataset
from os import listdir
from numpy import asarray
from numpy import savez_compressed
from PIL import Image
from mtcnn.mtcnn import MTCNN
然后加载图像
代码语言:javascript复制# load an image as an rgb numpy array
def load_image(filename):
# load image from file
image = Image.open(filename)
# convert to RGB, if needed
image = image.convert('RGB')
# convert to array
pixels = asarray(image)
return pixels
然后是提取面部:
代码语言:javascript复制# extract the face from a loaded image and resize
def extract_face(model, pixels, required_size=(80, 80)):
# detect face in the image
faces = model.detect_faces(pixels)
# skip cases where we could not detect a face
if len(faces) == 0:
return None
# extract details of the face
x1, y1, width, height = faces[0]['box']
# force detected pixel values to be positive (bug fix)
x1, y1 = abs(x1), abs(y1)
# convert into coordinates
x2, y2 = x1 width, y1 height
# retrieve face pixels
face_pixels = pixels[y1:y2, x1:x2]
# resize pixels to the model size
image = Image.fromarray(face_pixels)
image = image.resize(required_size)
face_array = asarray(image)
return face_array
然后加载脸部的头像数据:
代码语言:javascript复制# load images and extract faces for all images in a directory
def load_faces(directory, n_faces):
# prepare model
model = MTCNN()
faces = list()
# enumerate files
for filename in listdir(directory):
# load the image
pixels = load_image(directory filename)
# get face
face = extract_face(model, pixels)
if face is None:
continue
# store
faces.append(face)
print(len(faces), face.shape)
# stop once we have enough
if len(faces) >= n_faces:
break
return asarray(faces)
# directory that contains all images
directory = 'img_align_celeba/'
# load and extract all faces
all_faces = load_faces(directory, 50000)
print('Loaded: ', all_faces.shape)
# save in compressed format
savez_compressed('img_align_celeba.npz', all_faces)
上面这这一步会把数据压缩存储在一个npz的文件里,全是以numpy的格式保存的。