facial keypoints CNN

11 篇文章 0 订阅
2 篇文章 0 订阅

自己按照Kaggle的tutorial跑一下facial keypoints,也可以为人脸检测积累更多的数据。

参考Daniel的Blog

CNN是图像分类的重要工具,如果不明白CNN,可以参见stanford的演示

1 环境准备

1 Python环境

CUDA toolkit, 
Python 2.7.x, 
numpy, 
pandas, 数据处理
matplotlib,
scikit-learn,
theano  我准备用Theano自己写CNN,而不是用lasagne

2 数据

数据下载页面: https://www.kaggle.com/c/facial-keypoints-detection/data

输入:The training dataset for the Facial Keypoint Detection challenge consists of 7,049 96x96 gray-scale images.

输出:For each image, we’re supposed learn to find the correct position (the x and y coordinates) of 15 keypoints, such as left_eye_center, right_eye_outer_corner, mouth_center_bottom_lip, and so on.
Y的信息中,并不每种信息都全。An interesting twist with the dataset is that for some of the keypoints we only have about 2,000 labels, while other keypoints have more than 7,000 labels available for training.

尝试一下加载数据:

python kfkd_load.py
output in short: 
X.shape == (2140L, 9216L); X.min == 0.000; X.max == 1.000
y.shape == (2140L, 30L); y.min == -0.920; y.max == 0.996

注意这句代码df = df.dropna(),因为有些数据的标注不全,这个代码就是去掉这些标注不全的数据。但是这样会少掉很多数据,所以我们的模型需要在标注不全的数据中也能运行。

kfkd.py将数据归一化了。Another feature of the load() function is that it scales the intensity values of the image pixels to be in the interval [0, 1], instead of 0 to 255. The target values (x and y coordinates) are scaled to [-1, 1]; before they were between 0 to 95.

2 建立模型

1 一个简单的模型

这个模型是一个隐层的MLP。

python 1_faceial_h2.py

笔记本上跑: 551.859975 sec/epochs. 只跑了一个epoch, cost: 0.13.

kaggle用的Evaluation是RMS,即 rms=1n(yy)2 。我现在的的cost是 cost=1n(yy)2 。需要转换一下: rms=cost48 ,48是因为y在归一化的时候除以48。y其实是30维的数组,不知道kaggle是否考虑了除30这个维数,看起来应该是这样的。 0.1348=17.31 ,这个结果很差。

在不算很强的GPU上( gtx750ti)上,同样跑400epoch,用时37.6s。daniel.nouri的是under a minute. cost:0.00374897。 0.0037548=2.94
参考的daniel.nouri这个模型按照应该是2.7385251505144153。差不多。

2 CNN模型

构造一个如下模型:

InputLayer            (None, 1, 96, 96)       produces    9216 outputs
Conv2DCCLayer         (None, 32, 94, 94)      produces  282752 outputs
MaxPool2DCCLayer      (None, 32, 47, 47)      produces   70688 outputs
Conv2DCCLayer         (None, 64, 46, 46)      produces  135424 outputs
MaxPool2DCCLayer      (None, 64, 23, 23)      produces   33856 outputs
Conv2DCCLayer         (None, 128, 22, 22)     produces   61952 outputs
MaxPool2DCCLayer      (None, 128, 11, 11)     produces   15488 outputs
DenseLayer            (None, 500)             produces     500 outputs
DenseLayer            (None, 500)             produces     500 outputs
DenseLayer            (None, 30)              produces      30 outputs
python 2_faceial_h2.py

结果 0.0548=10.73312629199899 ,比上一个模型好。

在不算很强的GPU上( gtx750ti)上,同样跑1000epoch,用时2207.7s。daniel.nouri的是20minutes. cost: 0.00172055。 0.0017205548=1.979
参考的daniel.nouri这个模型按照应该是1.899。差不多。

3 Data augmentation

直观的想法是将所有training的图片左右反转一下,就可以多一倍的training数据。Rather, we will just perform the horizontal flips with 50% chance while we’re iterating over the data. 这样做看起来效率更高,实现也比较方便。

2层MLP模型3_faceial_h2.py运行结果:cost = 0.00383073,
The code run for 400 epochs, with 0.112929 sec/epochs,Training took 45.2s。

CNN模型运行结果:cost = 0.00171999, The code run for 1000 epochs, with 2.226594 sec/epochs, Training took 2226.6s。

经过数据增强以后,似乎有点不明显的提高。

4 Changing learning rate and momentum over time

我的代码优化是rmsprop,本来就是动态调整momentum的,所以这个实验就不用了。

5 Dropout

dropout是要解决overfit的问题,但是从我的运行结果来看:
(‘Train ‘, 0.0014464521, ‘Test ‘, 0.0017263663),并没有很明显的overfit。不过我也来试试,前面几层加入dropout,后面2层就不用加了。

5_faceial_cnn_h2.pyTrain 0.00563892 Test 0.00243421
The code run for 1000 epochs, with 2.336197 sec/epochs
Training took 2336.2s。结果还不如不用dropout。

6 拼命算

把epochs改为10000。
6_faceial_cnn_h2.py(‘Train ‘, 0.00099971099, ‘Test ‘, 0.0012784186), The code run for 10000 epochs, with 2.651319 sec/epochs, Training took 26513.2s

7 pretrain

It turns out that initializing those nets not at random, but by re-using weights from one of the networks we learned earlier has in fact two big advantages: One is that training converges much faster; maybe four times faster in this case.

8 分成6个模型

由于有些training数据中缺少一些点。

3 小节一下

prefixrmstime
12.9437.6s
21.9792207s
41.972226.6s
53.62336.2s

1 提交一下结果

提交结果的时候,也不是没一个predict的值都需要,要按照IdLookupTable.csv的规定来做。参考了一下daniel的代码。
first effort: RMS = 25.83760,是不是哪里搞错了?
检查一下在测试集上的效果:
这里写图片描述
看起来很准确,应该是数据转换的时候有问题。由于直接抄了Daniel的代码,有些y的字段没有对应上,修改以后重新上传:
这里写图片描述
排名21,RMS=3.21,这算是一个出发点。

4 下面的方向

  • 通过加噪声和小幅度的变换。。。来继续增加训练集?
  • 提交一次结果(和现有的进行比较)
  • crossvalidation得到更公允的结果。
  • 给不同的问题设置不同的权重,统一模型。
  • 先白平衡一下。
  • -
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值