当前位置: 首页 > 编程日记 > 正文

Facial keypoints detection Kaggle 竞赛系列

3.2# Facial keypoints detection

  • 作者:Stu. Rui
  • QQ: 1026163725
  • 原文链接:http://blog.csdn.net/i_love_home/article/details/51051888

该题主要任务是检測面部关键点位置

Detect the location of keypoints on face images

问题表述

在本问题中。要求计算面部关键点的位置,即关键点在图片中的百分比坐标。
因此该问题的机理就是 [0, 1] 范围内的数值拟合,当然了,这也是一个多输出的拟合的问题。

给定图片与其相应的 30 个标签的百分比位置,标签信息例如以下:

123
left_eye_center_xleft_eye_center_yright_eye_center_x
right_eye_center_yleft_eye_inner_corner_xleft_eye_inner_corner_y
left_eye_outer_corner_xleft_eye_outer_corner_yright_eye_inner_corner_x
right_eye_inner_corner_yright_eye_outer_corner_xright_eye_outer_corner_y
left_eyebrow_inner_end_xleft_eyebrow_inner_end_yleft_eyebrow_outer_end_x
left_eyebrow_outer_end_yright_eyebrow_inner_end_xright_eyebrow_inner_end_y
right_eyebrow_outer_end_xright_eyebrow_outer_end_ynose_tip_x
nose_tip_ymouth_left_corner_xmouth_left_corner_y
mouth_right_corner_xmouth_right_corner_ymouth_center_top_lip_x
mouth_center_top_lip_ymouth_center_bottom_lip_xmouth_center_bottom_lip_y

当中标签完整的图片有 2140 张,当中,图片的大小为 96*96 pixels。

求解方案

求解过程例如以下:
Step 1. 选择拟合器 SVR/KernelRidge 以及相应的 kernel
Step 2. 交叉验证实验选择超參数,超參数的选择通过枚举的方法
Step 3. 选定超參数后,用全部训练集训练拟合器
Step 4. 对測试集做预測。并输出结果

实验结果

结果First idea:

Using 30 fitter to fit 30 labels, then I got 3.48060 RMSE

Second idea
Using 1 fitter to fit 30 labels, then I got 3.43998 RMSE[Better]
Third idea
Adding symmetrical training data, then resulting in abnormal result, such as position was greater then 96.
So, I can see that the result of fitting is only cover [0,96](or [0,1])

备注

超參数选择 gamma

for G in G_para:scores = list()for i in range(3):X1, X2, y1, y2 = train_test_split(train_X, train_y, test_size=0.3, random_state=42)clf = KernelRidge(kernel='rbf', gamma=G, alpha=1e-2)pred = clf.fit(X1, y1).predict(X2)sco = calbais(pred, y2)scores.append(sco)print('G:', G, 'Score:', scores)

30 个拟合器超參数调试的方法与结果例如以下:

拟合器 KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2)
0.7:0.3 训练集划分拟合误差:
[0] 0.7792    [10] 0.9744    [20] 1.0985
[1] 0.6383    [11] 0.7451    [21] 1.2300
[2] 0.7714    [12] 0.9513    [22] 1.2636
[3] 0.6482    [13] 0.9299    [23] 1.1784
[4] 0.7355    [14] 1.0870    [24] 1.2469
[5] 0.6005    [15] 1.1898    [25] 1.2440
[6] 0.9636    [16] 0.9012    [26] 0.9444
[7] 0.7063    [17] 0.9462    [27] 1.3718
[8] 0.7214    [18] 1.1349    [28] 0.9961
[9] 0.6089    [19] 1.1669    [29] 1.5076

pandas usage:

数据统计:DataFrame.count()
数据去缺失项:DataFrame.dropna()
字符串切割:Series = Series.apply(lambda im: numpy.fromstring(im, sep=' '))

值得注意的地方:

镜像图片,似乎对本问题採用 kernel ridge 拟合器 的求解没有帮助。

Conclusion

The 30 fitter is replaced by the only 1 fitter. The grade is better.

源代码

import pandas as pd
import numpy as np
import csv as csv
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.svm import SVR
from sklearn.kernel_ridge import KernelRidge
from sklearn.cross_validation import cross_val_score, train_test_splittrain_file = 'training.csv'         # 训练集数据
test_file = 'test.csv'              # 測试集数据 1783 张图片
test_type = 'IdLookupTable.csv'     # 測试集样表 行号, 图编号, 标签名pd.set_option('chained_assignment',None)# csv 数据读取,返回 df (pandas)
def csvFileRead(filename):print('Loading', filename)df = pd.read_csv(filename, header=0, encoding='GBK')print('Loaded')# 缺失项数据删除if 'train' in filename:df = df.dropna()''' 数据查看print('\n数据表尺寸: ', df.values.shape)print('类别统计:\n')print(df.count(), '\n') '''return df# 结果存储
def csvSave(filename, ids, predicted):with open(filename, 'w') as mycsv:mywriter = csv.writer(mycsv)mywriter.writerow(['RowId','Location'])mywriter.writerows(zip(ids, predicted))# 训练集数据预处理
def preTrain():print('-----------------Training reading...-----------------')df = csvFileRead(train_file)print('Image: str -> narray')df.Image = df.Image.apply(lambda im: np.fromstring(im, sep=' '))print('Image transfered.\n')# problem: 7049*9046 MemoryError -> df.dropna()X = np.vstack(df.Image.values) / 255.X.astype(np.float32)y = df[df.columns[:-1]].valuesy = (y-48)/48.y = y.astype(np.float32)'''# 增加人工镜像图片print('增加人工镜像图片...')X, y = imageSym(X, y)'''X, y = shuffle(X, y, random_state=42)yd = dict()for i in range(len(df.columns[:-1].values)):yd[df.columns[i]] = ireturn X, y, yd# 预測集数据预处理
def preTest():print('-----------------Test reading...-----------------')df = csvFileRead(test_file)print('Image: str -> narray')df.Image = df.Image.apply(lambda im: np.fromstring(im, sep=' '))print('Image transfered.\n')# 測试集图像X = np.vstack(df.Image.values) / 255.X.astype(np.float32)# 预測内容:行号, 图编号, 标签名df = csvFileRead(test_type)RowId = df.RowId.valuesImageId = df.ImageId.values - 1FeatureName = df.FeatureName.valuesreturn RowId, ImageId, FeatureName, X# 人工特征:镜像图片
def imageSym(X, y):nX = np.zeros(X.shape)ny = np.zeros(y.shape)for i in range(X.shape[0]):temp = X[i,:].reshape(96, 96)temp = temp[:,::-1]nX[i,:] = temp.reshape(-1)ny[i,0::2] = -y[i,0::2]ny[i,1::2] = y[i,1::2]X = np.vstack((X, nX))y = np.vstack((y, ny))return X, y      # 30 个拟合器进行拟合
def modelfit(train_X, train_y, test_X, yd, ImageId, FeatureName):#There are fitting codes.# 30 个拟合器相应 1 个位置n_clf = 30clfs = [KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2),KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2), KernelRidge(kernel='rbf', gamma=2e-4, alpha=1e-2)]print('-----------------開始训练...------------------')# 超參数C_para = np.logspace(-2, 4, 7)      # SVR.CG_para = np.logspace(-4, -3, 6)     # kernel = 'rbf'.gammaA_para = np.logspace(-3, 1, 5)      # KernelRidge.alpha# 训练for i in range(n_clf):print('Training', i, 'clf...')clfs[i].fit(train_X, train_y[:,i])# 打印训练误差predict = np.zeros([train_y.shape[0], 30]).astype(np.float32)for i in range(n_clf):predict[:,i] = clfs[i].predict(train_X)print(calbais(predict, train_y))print()print('-----------------開始预測...------------------')# 预測pred = np.zeros([test_X.shape[0], 30]).astype(np.float32)for i in range(n_clf):pred[:,i] = clfs[i].predict(test_X)predicted = np.zeros(len(FeatureName))for i in range(len(FeatureName)):if i % 500 == 0:print('i =', i)else:passimageID = ImageId[i]clfID = yd[FeatureName[i]]predicted[i] = pred[imageID, clfID]predicted = predicted*48.+48.return predicted# 单一拟合器,同一时候对 30 个标签做拟合
def modelfitOne(train_X, train_y, test_X, yd, ImageId, FeatureName):n_clf = 1# 拟合器clf = KernelRidge(kernel='rbf', gamma=6e-4, alpha=2e-2)# 训练print('-----------------開始训练...------------------')clf.fit(train_X, train_y)# 预測print('-----------------開始预測...------------------')pred = clf.predict(test_X)predicted = np.zeros(len(FeatureName))for i in range(len(FeatureName)):if i % 500 == 0:print('i =', i)else:passimageID = ImageId[i]clfID = yd[FeatureName[i]]predicted[i] = pred[imageID, clfID]predicted = predicted*48.+48.return predicted# 均方根计算方法
def calbais(pred, y2):y_diff = pred - y2y_diff = y_diff.reshape(-1)sco = np.linalg.norm(y_diff)/(len(y2)**0.5)return sco# 參数选择的调试函数# 超參数调试 X-y
def testfit(clf, train_X, train_y):scores = list()for i in range(3):X1, X2, y1, y2 = train_test_split(train_X, train_y, test_size=0.3, random_state=42)pred = clf.fit(X1, y1).predict(X2)sco = calbais(pred, y2)scores.append(sco)print(scores)# 測试图
def plotface(x, y):img = x.reshape(96, 96)plt.imshow(img, cmap='gray')y = y * 48 + 48plt.scatter(y[0::2], y[1::2], marker='x', s=20)plt.show()# 训练集数据读取
df = csvFileRead(train_file)
train_X, train_y, yd = preTrain()# 測试集数据读取
RowId, ImageId, FeatureName, test_X = preTest()# 1) 数据拟合: 30 个拟合器
predicted = modelfit(train_X, train_y, test_X, yd, ImageId, FeatureName)# 2) 数据拟合: 1 个拟合器
predicted = modelfitOne(train_X, train_y, test_X, yd, ImageId, FeatureName)# 结果存储
csvSave('KernelRidge.csv', np.linspace(1, len(predicted), len(predicted)).astype(int), predicted)

转载于:https://www.cnblogs.com/llguanli/p/7247347.html

相关文章:

Error:java: 无效的源发行版: 11

Error:java: 无效的源发行版: 111.问题描述2.原因查找3.解决办法3.1 打开IDEA的File—Project Structure设置3.2 修改Project SDK为自己想要切换的版本3.3 修改project languang level1.问题描述 在我的电脑中同时安装了JDK8和JDK11,之前本来调试好了的&#xff0c…

今天看论坛,有这样一句话,深有同感,还是家里好

就像孟宣后来对这个城市的评价:“这里的人活的才像人……就像那么发达国家的小城市,不用背负那么大的生存压力。在北京,如果你每天生活要30个馒头,那么你要挣到200到300个。而在这里,只需要30个馒头就可以了……”转载…

面对别人强行关机你怎么办与 定时关机

面对这个图你的第一感觉是什么?肯定是有人.....那你怎么办呢?让它继续下去!不能绝对不能!以前比较幸运的打开了几个记事本没有保存逃过了一关,可是屡试不爽呐!直到我同学出现这种情况时,幸亏我眼快,呵呵 所以问他一下!知道了这个玩意出来的命令是在运行里敲入shutdown -s如果…

iOS开发实战-基于SpriteKit的FlappyBird小游戏

写在前面 最近一直在忙自己的维P恩的事情 公司项目也是一团乱 于是...随手找了个游戏项目改了改就上线了,就当充数了. SpriteKit简介 SpriteKit是iOS 7之后苹果推出的2D游戏框架。它支持2D游戏中各种功能,如物理引擎,地图编辑,粒子&#xff0…

2018年12月14日 函数 总结

map() 处理序列中每个元素,得到迭代器,该迭代器 元素个数和位置与原来一致 filter() 遍历序列中的每个元素,判断每个元素得到布尔值,如果是true则留下来 people[{name:"abc","age":100},{"name":&…

UML类图新手入门级介绍

UML类图新手入门级介绍 看了大话设计模式,觉得很生动形象,比较适合于我这种初学者理解面向对象,所以就记录了一下。 举一个简单的例子,来看这样一副图,其中就包括了UML类图中的基本图示法。 首先,看动物矩形…

SQL中获取刚插入记录时对应的自增列的值

--创建数据库和表create database MyDataBaseuse MyDataBasecreate table mytable(id int identity(1,1),name varchar(20))--执行这个SQL,就能查出来刚插入记录对应的自增列的值insert into mytable values(李四)select identity转载于:https://www.cnblogs.com/bnjbl/archive…

SQL Server开发人员应聘常被问的问题妙解汇总

目前在职场中很难找到非常合格的数据库开发人员。我的一个同事曾经说过:“SQL开发是一门语言,它很容易学,但是很难掌握。” 在面试应聘的SQL Server数据库开发人员时,我运用了一套标准的基准技术问题。下面这些问题是我觉得能够真正有助于淘汰…

little w and Soda(思维题)

链接:https://ac.nowcoder.com/acm/contest/297/A 来源:牛客网 时间限制:C/C 1秒,其他语言2秒 空间限制:C/C 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 不知道你听没听说过这样一个脑筋急…

[导入]实时数据库的经典书

有个朋友给我来了一封邮件,在邮件中,他这样写到:“国外的实时数据库来势汹汹,价格一路上扬;想当初eDNA 2003年刚到中国时也就是二、三十万左右,现在报价已经百万以前了。心里也总个一个结,难道这…

关于CSS(3)

盒子模型 盒子 盒子关系(标准文档流) 行内元素。 只可以设置左右外边距。 上下内边距会影响相邻的圆块状元素呢 垂直margin会合并(margin坍陷)元素嵌套的时候,设置子元素的上margin会被父元素抢走, 解决方案:设置父元素…

jMonkey Engine SDK3 中文乱码问题

1. 升级到了jMonkey Engine SDK 3之后出现了一些方框,乱码问题 官方推荐初学者使用jME3 SDK来开发游戏。官方下载地址为: https://github.com/jMonkeyEngine/sdk/releases 2. 问题分析和解决办法 在jME3.1.0之后SDK就有一个bug,菜单上的中文…

第四天上午 休闲假日

第四天晚上要离开沙巴,赶往吉隆坡了,所以这天的活动安排非常简单。 睡了一个舒服觉,起床吃早饭,我胃口还是不好,吃不下Magellan的美味早餐。早餐后我们来到酒店的游泳池旁休息,晒晒太阳、吹吹海风、看看风景…

expect--自动批量分发公钥脚本

1.在使用之前,先安装epel源,yum install expect -y2.写分发脚本,后缀为exp #!/usr/bin/expect set host_ip [lindex $argv 0] spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $host_ip expect {-timeout 60"(yes/no)?" { send "…

java报错MalformedURLException: unknown protocol: c

java报错:MalformedURLException: unknown protocol: c 1. 报错情况: 部分代码: //打开图片path"C:/Users/MyUser/image.jpg" openPictrues(path);public void openPictures(String path,String picName) throws IOException {F…

3.commonjs模块

1.首先建一个math.js exports.add function(a, b){return a b; } exports.sub function(a, b){return a - b; } exports.mul function(a, b){return a * b; } 2.然后建一个app.js 引人math.js var math require(./math); console.log(math);//{ add: [Function], sub: [Fu…

推荐一个关于.NET平台数据结构和算法的好项目

http://www.codeplex.com/NGenerics这是一个类库,它提供了标准的.NET框架没有实现的通用的数据结构和算法。值得大家研究。转载于:https://www.cnblogs.com/didasoft/archive/2007/07/05/806758.html

JSF和Struts的区别概述

据说JSF的主要负责人就是struts的主要作者,所以二者的相似点还是有很多的。 都采用taglib来处理表示层:在jsp页面中,二者都是采用一套标记库来处理页面的表示和model层的交互。 二者都采用了bean来作为和jsp页面对应的model层。该model层保存…

This和Super关键字的对比

this和Super关键字this和Super关键字的对比Super关键字的用法如下:1. super关键字代表了父类空间的引用;2. super关键字的作用:3. super关键字调用父类构造方法要注意的事项:this关键字的用法如下:1.了解没有 this 关键…

SQL Server 2005下的分页SQL

其实基本上有三种方法:1、使用SQL Server 2005中新增的ROW_NUMBER几种写法分别如下: 1SELECTTOP20*FROM(SELECT2ROW_NUMBER() OVER(ORDERBYNamec) ASRowNumber,3*4FROM5dbo.mem_member) _myResults6WHERE7RowNumber >1000081SELECT*FROM(SELECT2ROW_N…

Oozie 配合 sqoop hive 实现数据分析输出到 mysql

文件/RDBMS -> flume/sqoop -> HDFS -> Hive -> HDFS -> Sqoop -> RDBMS 其中,本文实现了 使用 sqoop 从 RDBMS 中读取数据(非Oozie实现,具体错误将在本文最后说明)从 Hive 处理数据存储到 HDFS使用 sqoop 将 HDFS 存储到 RDBMS 中 1.…

关于eclipse的注释和反注释的快捷键

使用eclipse那么久了额,对注释和反注释的快捷键一直很模糊,现在记下来,方便查看。 注释和反注释有两种方式。如对下面这段代码片段(①)进行注释: private String value; private String count; public voi…

DNN和IBatis.Net几乎同时发布新版本

DotNetNuke发布了最新的版本4.5.0,确实让人期待了很久,据说这个版本在性能上有很大的提升。 IBatis.NET几乎在同一时间也发布了新版本DataMapper 1.6.1,也有不少的改进。 项目中使用到的这两个东西几乎同时发布新版本,振奋人心啊&…

Unity 2D物体移动

一,设置 二,脚本 1,PlayerController using System.Collections; using System.Collections.Generic; using UnityEngine;public class PlayerController : MonoBehaviour {private Rigidbody2D m_rg;public float MoveSpeed;public float J…

朱敏:40岁创业如何成就绝代明星?(五)

来源 中国企业家 东方元素是网讯内涵里不可忽视的一部分 如果有机会拜访网讯的美国总部,你会发现这是 一家带着醒目美国特色IT公司,很难说出它与其他 硅谷公司的不同。但在你视野所不能及的地方,朱敏 与苏布拉在驾驭它的方式中输入…

print、printf、println在Java中的使用

print、printf、println在Java中的使用 文章目录print、printf、println在Java中的使用一、println在JAVA中常常使用System.out.pirntf();的输出格式。二、print在JAVA中常常使用System.out.pirnt();的输出格式。三、printf在JAVA中常常使用System.out.printf();的格…

(转) SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

springboot采纳了建立生产就绪spring应用程序的观点。 Spring Boot优先于配置的惯例,旨在让您尽快启动和运行。在一般情况下,我们不需要做太多的配置就能够让spring boot正常运行。在一些特殊的情况下,我们需要做修改一些配置,或者…

iexpress全力打造“免检”***

IExpress小档案出身:Microsoft功能:专用于制作各种 CAB 压缩与自解压缩包的工具。由于是Windows自带的程序,所以制作出来的安装包具有很好的兼容性。它可以帮助***传播者制造不被杀毒软件查杀的自解压包,而且一般情况下还可伪装成某个系统软件的补丁(如I…

java 稀疏数组和二维数组转换,并保存稀疏数组到文件后可以读取

稀疏数组和二维数组转换 稀疏数组:当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组 稀疏数组的处理方法: 记录数组一共有多少行,有多少个不同的值把具有不同值得元素的行列及值记录在…

springboot redis配置

1、引入maven依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 2、redis连接配置 spring:redis:host: 10.220.1.41port: 6379timeout: 10000passwor…