前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于CelebA数据集的GAN模型

基于CelebA数据集的GAN模型

作者头像
Tom2Code
发布2023-02-14 11:26:38
1.2K0
发布2023-02-14 11:26:38
举报
文章被收录于专栏:Tom

上篇我们介绍了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的格式保存的。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-12-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Tom的小院 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
人脸识别
腾讯云神图·人脸识别(Face Recognition)基于腾讯优图强大的面部分析技术,提供包括人脸检测与分析、比对、搜索、验证、五官定位、活体检测等多种功能,为开发者和企业提供高性能高可用的人脸识别服务。 可应用于在线娱乐、在线身份认证等多种应用场景,充分满足各行业客户的人脸属性识别及用户身份确认等需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档