Arcface训练vgg2face数据集——数据制作

简介

  • 本博客是基于开源项目insightface训练vgg2face。训练首先要对数据里面的人脸图片进行检测,接下来进行人脸对齐,保存对齐后的图片,最后生成一个和原来数据文件目录一样的存放图片文件夹和一个lst文件,lst存放图片绝对路径和每张图片对应类别ID。

代码解读

  • 利用 $INSIGHTFACE_ROOT/src/align/align_lfw.py 进行对齐生成lst文件,作者源码中,vgg和lfw数据集处理方法一样。是利用MTCNN进行人脸检测和关键点检测。
from __future__ import absolute_import
from __future__ import division
from __future__ import print_functionfrom scipy import misc
import sys
import os
import argparse
import tensorflow as tf
import numpy as np
#import facenet
import detect_face
import random
from time import sleep
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
import face_image
import face_preprocess
from skimage import transform as trans
import cv2# 添加代码,指定GPU
os.environ["CUDA_VISIBLE_DEVICES"] = "0"def to_rgb(img):w, h = img.shaperet = np.empty((w, h, 3), dtype=np.uint8)ret[:, :, 0] = ret[:, :, 1] = ret[:, :, 2] = imgreturn retdef IOU(Reframe,GTframe):x1 = Reframe[0];y1 = Reframe[1];width1 = Reframe[2]-Reframe[0];height1 = Reframe[3]-Reframe[1];x2 = GTframe[0]y2 = GTframe[1]width2 = GTframe[2]-GTframe[0]height2 = GTframe[3]-GTframe[1]endx = max(x1+width1,x2+width2)startx = min(x1,x2)width = width1+width2-(endx-startx)endy = max(y1+height1,y2+height2)starty = min(y1,y2)height = height1+height2-(endy-starty)if width <=0 or height <= 0:ratio = 0else:Area = width*heightArea1 = width1*height1Area2 = width2*height2ratio = Area*1./(Area1+Area2-Area)return ratiodef main(args):#facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))dataset = face_image.get_dataset('vgg', args.input_dir)print('dataset size', 'vgg', len(dataset))print('Creating networks and loading parameters')with tf.Graph().as_default():gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))# sess = tf.Session()with sess.as_default():pnet, rnet, onet = detect_face.create_mtcnn(sess, None)minsize = 20threshold = [0.6,0.7,0.9]factor = 0.85# Add a random key to the filename to allow alignment using multiple processes#random_key = np.random.randint(0, high=99999)#bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)#output_filename = os.path.join(output_dir, 'faceinsight_align_%s.lst' % args.name)if not os.path.exists(args.output_dir):os.makedirs(args.output_dir)output_filename = os.path.join(args.output_dir, 'lst')# add by jsc,记录未检测的图片fp = open(os.path.join(args.output_dir,"passImage.txt"),'w')with open(output_filename, "w") as text_file:nrof_images_total = 0nrof = np.zeros( (5,), dtype=np.int32)for fimage in dataset:if nrof_images_total%100==0:print("Processing %d, (%s)" % (nrof_images_total, nrof))nrof_images_total += 1#if nrof_images_total<950000:#  continueimage_path = fimage.image_pathif not os.path.exists(image_path):print('image not found (%s)'%image_path)continuefilename = os.path.splitext(os.path.split(image_path)[1])[0]#print(image_path)try:img = misc.imread(image_path)except (IOError, ValueError, IndexError) as e:errorMessage = '{}: {}'.format(image_path, e)print(errorMessage)else:if img.ndim<2:print('Unable to align "%s", img dim error' % image_path)#text_file.write('%s\n' % (output_filename))continueif img.ndim == 2:img = to_rgb(img)img = img[:,:,0:3]_paths = fimage.image_path.split('/')a,b = _paths[-2], _paths[-1]target_dir = os.path.join(args.output_dir, a)if not os.path.exists(target_dir):os.makedirs(target_dir)target_file = os.path.join(target_dir, b)_minsize = minsize_bbox = None_landmark = Nonebounding_boxes, points = detect_face.detect_face(img, _minsize, pnet, rnet, onet, threshold, factor)nrof_faces = bounding_boxes.shape[0]if nrof_faces>0:det = bounding_boxes[:,0:4]img_size = np.asarray(img.shape)[0:2]bindex = 0if nrof_faces>1:bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])img_center = img_size / 2offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])offset_dist_squared = np.sum(np.power(offsets,2.0),0)bindex = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering_bbox = bounding_boxes[bindex, 0:4]_landmark = points[:, bindex].reshape( (2,5) ).T# print('landmark')nrof[0]+=1else:nrof[1]+=1fp.write(image_path+'\n')print('no detect:', image_path,'\n')warped = face_preprocess.preprocess(img, bbox=_bbox, landmark = _landmark, image_size=args.image_size)bgr = warped[...,::-1]#print(bgr.shape)cv2.imwrite(target_file, bgr)oline = '%d\t%s\t%d\n' % (1,target_file, int(fimage.classname))text_file.write(oline)print('Total number of images: %d' % nrof_images_total)fp.close()def parse_arguments(argv):parser = argparse.ArgumentParser()parser.add_argument('--input-dir', type=str, default="/jmu/jmuaia.tanghaom.top/data/User/admin/home/train/faceRecognition/data/vgg2face/train",help='Directory with unaligned images.')parser.add_argument('--output-dir', type=str,default="/jmu/jmuaia.tanghaom.top/data/User/admin/home/train/faceRecognition/data/align", help='Directory with aligned face thumbnails.')parser.add_argument('--image-size', type=str, help='Image size (height, width) in pixels.', default='112,112')#parser.add_argument('--margin', type=int,#    help='Margin for the crop around the bounding box (height, width) in pixels.', default=44)# 设置显存占用比例parser.add_argument('--gpu_memory_fraction', type=float,help='Upper bound on the amount of GPU memory that will be used by the process.', default=0.4)return parser.parse_args(argv)if __name__ == '__main__':main(parse_arguments(sys.argv[1:]))

Arcface训练vgg2face数据集——数据制作相关推荐

  1. 人脸识别0-02:insightFace-模型训练与训练数据制作-史上最全

    以下链接是个人关于insightFace所有见解,如有错误欢迎大家指出,我会第一时间纠正,如有兴趣可以加QQ:944284742相互讨论技术. 人脸识别0-00:insightFace目录:https ...

  2. Pytorch 实现全连接神经网络/卷积神经网络训练MNIST数据集,并将训练好的模型在制作自己的手写图片数据集上测试

    使用教程 代码下载地址:点我下载 模型在训练过程中会自动显示训练进度,如果您的pytorch是CPU版本的,代码会自动选择CPU训练,如果有cuda,则会选择GPU训练. 项目目录说明: CNN文件夹 ...

  3. 行人检测0-05:LFFD-行人训练数据制作以及训练

    以下链接是个人关于LFFD(行人检测)所有见解,如有错误欢迎大家指出,我会第一时间纠正.有兴趣的朋友可以加微信:17575010159 相互讨论技术.若是帮助到了你什么,一定要记得点赞!因为这是对我最 ...

  4. 语义分割 patches 训练数据制作

    patches 切割 在制作训练数据集,或使用训练好的模型对大尺寸图像进行预测时,需要将图像进行切割成 patches patches 的切割可以分为: 离线切割,将 切割的 patches 保存至本 ...

  5. Fast RCNN 训练自己数据集 (2修改数据读取接口)

    Fast RCNN 训练自己数据集 (2修改数据读取接口) Fast RCNN训练自己的数据集 (2修改读写接口) 转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.co ...

  6. [caffe] 数据制作和训练

    [caffe] 数据制作和训练 在使用caffe时,我们希望使用自己的数据进行训练,以下给出如何制作自己的数据.所有的数据制作都是基于imagenet的. 1.数据准备,我们需要一个train和val ...

  7. 制作用于图像语义分割训练的标签数据【图像分割】【labelme】

    制作用于图像语义分割训练的标签数据 *写在前面 一.使用labelme制作json数据 1.安装labelme 2.利用labelme制作json数据 二.将json数据转化为图像数据 1.单个jso ...

  8. 神经网络学习小记录19——微调VGG分类模型训练自己的数据(猫狗数据集)

    神经网络学习小记录19--微调VGG分类模型训练自己的数据(猫狗数据集) 注意事项 学习前言 什么是VGG16模型 VGG模型的复杂程度 训练前准备 1.数据集处理 2.创建Keras的VGG模型 3 ...

  9. 数据标准化常见问题:对整个数据集数据标准化后再划分训练集、测试集和先对训练级标准化再将规则用于测试集有什么区别(Python实现)

    在数据分析与挖掘.算法建模的都会用到数据标准化.数据的标准化(normalization)是将数据按比例缩放,使之落入一个小的特定区间.在某些比较和评价的指标处理中经常会用到,去除数据的单位限制,将其 ...

  10. 利用matlab将自己的数据制作为标准VOC数据集格式

    在使用各种深度网络的时候,需要根据自己的需求,自己的数据fine-tuning自己的模型,首要的一步就是讲自己的数据制作成标准VOC数据集,本文记录自己利用matlab制作标准VOC数据集的方法. 1 ...

最新文章

  1. 基于EasyNVR摄像机网页无插件直播服务二次开发实现H5播放页面的简单集成方案...
  2. 使用NFS启动Tiny4412开发板根文件系统
  3. [Android]关于Root
  4. Java-Frame
  5. 查询SQL中某表里有多少列包含某字段
  6. T-SQL中REPLACE的用法_字符串替换
  7. matlab snr eb n0,snr ber Eb/N0之间的区别与联系
  8. CSS学习笔记--定位
  9. DataTable判断列是否为空!(实用)
  10. Springboot集成通用Mapper与Pagehelper,实现mybatis+Druid的多数据源配置
  11. 如何从零开始学习平面设计?
  12. 用matlab实现傅里叶变换,matlab实现傅里叶变换
  13. 制作u盘winpe启动盘_微PE工具箱 (WePE),绿色纯净的WinPE启动盘/U盘重装系统工具...
  14. vmware 安装 mac os 修改序列号
  15. 哪些排序是不稳定的?稳定又意味着什么?
  16. spring cache相关注解介绍 @Cacheable、@CachePut、@CacheEvict
  17. 【老生谈算法】matlab实现EKF UKF PF三种算法对比源码——EKF UKF PF算法
  18. Python获取pi值的五种方法
  19. 第一个ASP.net的CRED(创建读取编辑删除)页面
  20. [机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心

热门文章

  1. 服务器的使用:Linux一键搭建KMS激活服务器
  2. 在TMS320F28XXX DSP上实现从flash拷贝整个程序到ram运行的方法探讨
  3. 谷歌浏览器如何在不登录的情况下保存书签
  4. java生成mib文件_Mib浏览器设计(附源码) | 学步园
  5. 电池型号 常见的电池型号有哪些
  6. 测试方案包括哪些内容
  7. 如何用计算机制作思维导向图,mindmaster使用方法,手把手教你制作思维导图
  8. 计算机锁屏如何取消密码,笔记本电脑怎么取消锁屏密码
  9. 转换到coff期间_error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
  10. 大牛教你如何利用积分商城API接口对接积分商城平台