基于Retinanet的人脸检测

本文详细介绍如何使用Keras RetinaNet进行人脸检测器的训练,包括安装配置、数据准备、模型训练及评估等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

project address: https://github.com/zhongqianli/keras-retinanet

Trainning a face detector by transfer learning based on keras-retinanet.

Install keras-retinanet

You’d better use ubuntu system rather than windows system.

  1. Clone keras-retinanet.
  2. In the repository, execute pip install . --user.
    Note that due to inconsistencies with how tensorflow should be installed,
    this package does not define a dependency on tensorflow as it will try to install that (which at least on Arch Linux results in an incorrect installation).
    Please make sure tensorflow is installed as per your systems requirements.
  3. Alternatively, you can run the code directly from the cloned repository, however you need to run python setup.py build_ext --inplace to compile Cython code first.
  4. Optionally, install pycocotools if you want to train / test on the MS COCO dataset by running pip install --user git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI.

tested configure:

keras-retinanet-0.4.1  
keras-2.2.2  
tensorflow-1.4.0+  
Pillow-5.2.0, lower version maybe has a bug in keras-retinanet  
Python2.7+ or Python3.6+
OpenCV2.4+ or OpenCV3.0+

Testing

  1. Dowload pretrained model face_detector.resnet50_retinanet.inference.h5
  2. Move to keras-retinanet/model/
  3. Execute run.evaluate.image.sh to detect face in a image. For evaluating thousands of images, you should use run.evaluate.csv.sh. If you have a webcam, you can also execute run.evaluate.video.sh to detect face.

bugs:

File "keras_retinanet/bin/../../keras_retinanet/utils/anchors.py", line 20, in <module>
    from ..utils.compute_overlap import compute_overlap
ImportError: No module named compute_overlap

add the following texts to anchos.py before “from …utils.compute_overlap import compute_overlap”

import pyximport
pyximport.install()

from ..utils.compute_overlap import compute_overlap

result:

在这里插入图片描述

Training

1. Save annotations to csv files.

(1)training annotations
training csv file: images.train.csv
format: image_path,left,top,right,bottom,class_name
If one image has more than one face, each face’s annotation use one line.

for example(009.jpg has two faces):

/opt/images/008.jpg,45,62,194,211,face
/opt/images/009.jpg,45,62,194,211,face
/opt/images/009.jpg,xx,xx,xxx,xxx,face
/opt/images/010.jpg,45,62,194,211,face

(2)classes file
classes csv file: classes.csv
for example:

face,0

(3)optionally, validation file
validation csv file: images.val.csv
for example:

/opt/images/008.jpg,45,62,194,211,face
/opt/images/009.jpg,45,62,194,211,face

2. execute train.py

use keras_retinanet/bin/train.py to train face detector, but train.py is hard to use because some arguements should place in right place.Otherwise, it will not work.
You can use run.train.sh to train face detector. The script shows the right way of using train.py. Before transfer learning, you should download ResNet-50-model.keras.h5, and move it to ~/.keras/models.

transfer learning with frozen backbone

python keras_retinanet/bin/train.py --gpu 0 --snapshot-path ./snapshots --backbone resnet50 --batch-size 1 --epochs 2000 --steps 1000 --tensorboard-dir=./log --freeze-backbone --weights ~/.keras/models/ResNet-50-model.keras.h5 csv images.train.csv classes.csv

transfer learning without frozen backbone

python keras_retinanet/bin/train.py --gpu 0 --snapshot-path ./snapshots --backbone resnet50 --batch-size 1 --epochs 2000 --steps 1000 --tensorboard-dir=./log --weights ~/.keras/models/ResNet-50-model.keras.h5 csv images.train.csv classes.csv

result
在这里插入图片描述

3. Converting a training model to inference model

The training procedure of keras-retinanet works with training models. These are stripped down versions compared to the inference model and only contains the layers necessary for training (regression and classification values). If you wish to do inference on a model (perform object detection on an image), you need to convert the trained model to an inference model. This is done as follows:

# Running directly from the repository:
keras_retinanet/bin/convert_model.py /path/to/training/model.h5 /path/to/save/inference/model.h5

# Using the installed script:
retinanet-convert-model /path/to/training/model.h5 /path/to/save/inference/model.h5

4. Evaluate the trained inference model

run.evaluate.csv.sh

python keras_retinanet/bin/evaluate.py csv images.val.csv classes.csv ./model/face_detector.resnet50_retinanet.inference.h5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值