代码文件
import os
import numpy as np # linear algebra
np.random.seed(2022)
import pandas as pd # data processing
from sklearn.model_selection import train_test_split
id_lookup_table = "facial-keypoints-detection/IdLookupTable.csv"
sample_Submission = "facial-keypoints-detection/SampleSubmission.csv"
# 读取数据集
train_df = pd.read_csv('training.csv')
idLookupTable = pd.read_csv(id_lookup_table)
sampleSumission = pd.read_csv(sample_Submission)
# 缺失值填充
train_df.fillna(method='ffill', inplace=True)
# 处理训练数据
image_df = train_df['Image']
imageArr = []
for i in range(0, len(image_df)):
img = image_df[i].split()
img = ['0' if x == '' else x for x in img]
imageArr.append(img)
x_train = np.array(imageArr, dtype='float')
x_train = x_train.reshape(-1, 96, 96, 1)
print(x_train.shape)
keypoints_df = train_df.drop('Image', axis=1)
y_train = np.array(keypoints_df, dtype='float')
print(y_train.shape)
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train)
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D, BatchNormalization, Flatten, Dense, Dropout
from keras.layers.advanced_activations import LeakyReLU
# 构建模型
model = Sequential()
model.add(Convolution2D(32, (3, 3), padding='same', use_bias=False, input_shape=(96, 96, 1)))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(Convolution2D(32, (3, 3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
########## Begin ##########
# 卷积层,卷积核数为 64,卷积核大小为 (3, 3),padding 为 same,use_bias 设置为 False
model.add(Convolution2D(64, (3, 3), padding='same', use_bias=False))
# 激活函数层
model.add(LeakyReLU(alpha=0.1))
# 批标准化层
model.add(BatchNormalization())
########## End ##########
model.add(Convolution2D(64, (3, 3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(96, (3, 3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(Convolution2D(96, (3, 3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(128, (3, 3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(Convolution2D(128, (3, 3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(256, (3, 3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(Convolution2D(256, (3, 3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(512, (3, 3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(Convolution2D(512, (3, 3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(Flatten())
########## Begin ##########
# 全连接层,神经元数为 512 个,激活函数为 relu
model.add(Dense(512, activation='relu'))
# 随机将 10% 的神经元的失去活性
model.add(Dropout(0.1))
# 全连接层,神经元数为 30 个
model.add(Dense(30))
########## End ##########
model.summary()
# 加载模型
model.load_weights('model_weights.h5')
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae', 'acc'])
# model.fit(x_train, y_train, batch_size=256, epochs=45, validation_split=2.0)
# model.save_weights('model_weights.h5')
########## Begin ##########
# 评估模型
score = model.evaluate(x_test, y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
########## End ##########
题目描述
任务描述
本关任务:使用 CNN 完成人脸定位案例。
相关知识
为了完成本关任务,你需要掌握:
- 算法介绍
- 程序和编程
- 人脸定位的应用
算法介绍
利用卷积神经网络和计算机视觉技术进行人脸关键点检测,对人脸的重要区域(如眼睛、嘴角和鼻子)进行标记,这些区域与各种任务(如人脸滤波、情感识别和姿势识别)相关。
它需要预测特定面部的面部关键点坐标,例如鼻尖、眼睛中心等。为了识别面部关键点,我们使用基于卷积神经网络的模型使用自动编码器。
卷积神经网络 (CNN) 具有深层结构,允许它们提取高级信息并在识别每个重要点时提供更好的精度。卷积网络旨在同时预测所有点。
图 1-1
程序和编程
导入 NumPy 和 pandas 库:
import numpy as np # linear algebra
import pandas as pd # data processing
import os
for directoryname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(directoryname, filename))
你写入当前目录的任何输出都会被存储。
下一步是为训练和测试数据的每个文件设置路径,
train_zip_path = "../input/facial-keypoints-detection/training.zip"
test_zip_path = "../input/facial-keypoints-detection/test.zip"
id_lookup_table = "../input/facial-keypoints-detection/IdLookupTable.csv"
sample_Submission = "../input/facial-keypoints-detection/SampleSubmission.csv"
让我们使用 zip 文件解压 zip 文件,然后加载数据集。
import zipfile
with zipfile.ZipFile(train_zip_path,'r') as zip_ref:
zip_ref.extractall('')
with zipfile.ZipFile(test_zip_path,'r') as zip_ref:
zip_ref.extractall('')
train_df = pd.read_csv('training.csv')
test_df = pd.read_csv('test.csv')
idLookupTable = pd.read_csv(id_lookup_table)
sampleSumission = pd.read_csv(sample_Submission)
加载数据集后,我们可以使用 pandas 的库查看数据框,并列出数据集的头部。
train_df.info()
图 1-2
填充 Nan 值并分离和重塑输入值(x_train)
train_df.fillna(method='ffill',inplace=True)
在训练数据集中分离和重塑输入值(x_train):
使用图像创建一个数组,keypoints:关键点将是我们数据集的一个样本。我们的数据集将接受一个可选的参数转换,允许我们对样本执行任何必要的处理。
image_df = train_df['Image']
imageArr = []
for i in range(0,len(image_df)):
img = image_df[i].split()
img = ['0' if x == '' else x for x in img]
imageArr.append(img)
x_train = np.array(imageArr,dtype='float')
x_train = x_train.reshape(-1,96,96,1)
print(x_train.shape)
创建一个以图片为输入输出关键点的CNN:
输入图片大小为 224 * 224 px(由 transform 决定),输出类分数为 136,即 136/2 = 68。(我们想要的 68 个关键点)和分离目标值。
keypoints_df = train_df.drop('Image',axis = 1)
y_train = np.array(keypoints_df,dtype='float')
print(y_train.shape)
def visualizeWithNoKeypoints(index):
plt.imshow(x_train[index].reshape(96,96),cmap='gray')
def visualizeWithKeypoints(index):
plt.imshow(x_train[index].reshape(96,96),cmap='gray')
for i in range(1,31,2):
plt.plot(y_train[0][i-1],y_train[0][i],'ro')
在我们编写了可视化函数之后,接下来,我们可以使用函数调用来可视化每个图像。
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,4))
fig.subplots_adjust(left=0,right=1,bottom=0,top=1,hspace=0.05,wspace=0.05)
plt.subplot(1,2,1)
visualizeWithNoKeypoints(1)
plt.subplot(1,2,2)
visualizeWithKeypoints(1)
图 1-3
数据已经预处理完毕。现在是创建训练模型的时候了。
为了创建我们的 CNN 模型,我们将使用 Keras 框架。
from keras.models import Sequential, Model
from keras.layers import Activation, Convolution2D,MaxPooling2D,BatchNormalization, Flatten, Dense, Dropout
from keras.layers.advanced_activations import LeakyReLU
最初只从一两个 epoch 开始,以快速评估你的模型是如何训练的,并确定你是否应该改变其结构或超参数。
在训练时跟踪模型的损失如何随时间变化:它是否首先快速减少,然后减速?
在训练多个时期并创建最终模型之前,使用这些初始观察对模型进行更改并决定最佳架构。
model = Sequential()
model.add(Convolution2D(32,(3,3),padding='same',use_bias=False, input_shape=(96,96,1)))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
model.add(Convolution2D(32,(3,3),padding='same',use_bias = False))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Convolution2D(64,(3,3),padding='same',use_bias = False))
model.add(LeakyReLU(alpha=0.1))
model.add(BatchNormalization())
model.add(Convolution2D(64, (3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(96, (3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
model.add(Convolution2D(96, (3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(128, (3,3),padding='same', use_bias=False))
# model.add(BatchNormalization())
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
model.add(Convolution2D(128, (3,3),padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(256, (3,3),padding='same',use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
model.add(Convolution2D(256, (3,3),padding='same',use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(512, (3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
model.add(Convolution2D(512, (3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(30))
model.summary()
图 1-4
下一步是配置模型:
model.compile(optimizer='adam',loss='mean_squared_error',metrics=['mae','acc'])
model.fit(x_train,y_train,batch_size=256,epochs=45,validation_split=2.0)
训练输出示例:
图 1-5
在整个训练数据集中执行了总共 50 次迭代。
已经学习了如何简单地使用 CNN 来训练深度学习模型。
现在是时候使用数据收集对模型进行测试了。
我们必须首先准备我们的测试集。
x_test:分离和重塑输入测试值。
image_df = test_df['Image']
keypoints_df = test_df.drop('Image', axis=1)
imageArr = []
for i in range(0, len(image_df)):
img = image_df[i].split()
img = ['0' if x == '' else x for x in img]
imageArr.append(img)
x_test = np.array(imageArr, dtype='float')
x_test = x_test.reshape(-1, 96, 96, 1)
print(x_test.shape)
我们知道要在测试数据集中分离目标值 (y_test)。
y_test = np.array(keypoints_df, dtype='float')
print(y_test.shape)
现在,是时候预测训练模型的结果了:
pred = model.predict(x_test)
idLookupTable.head()
图 1-6
人脸定位的应用
该技术的适用性是多种多样的。以下只是当今企业界中一些更有趣的面部识别应用。
脸部视频特效
用有趣的东西覆盖人脸照片的面部过滤器越来越受欢迎。它们通常出现在社交媒体平台上。此过滤器应用程序中使用了面部关键点(或“关键点”)检测技术。
可以评估一个人的面部表情(以及情绪)的面部重要区域被称为面部关键点。它们对于开发实时应用程序以检测困倦、评估生物特征和读取人们的情绪至关重要。
图 1-7
虚拟现实和增强现实 (AR)
几个流行的智能手机应用程序中使用了面部关键点识别。Instagram、Snapchat 和 LINE 上的面部过滤器就是一些众所周知的例子。AR 软件可以通过识别用户面部关键点,实时将图片滤镜正确叠加在用户面部上。
图 1-8
编程要求
根据提示,在右侧编辑器补充代码,使用 CNN 构建人脸定位模型并使用模型预测测试数据的类别。
测试说明
平台会对你编写的代码进行测试:
预期输出:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 96, 96, 32) 288
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU) (None, 96, 96, 32) 0
_________________________________________________________________
batch_normalization_1 (Batch (None, 96, 96, 32) 128
_________________________________________________________________
conv2d_2 (Conv2D) (None, 96, 96, 32) 9216
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU) (None, 96, 96, 32) 0
_________________________________________________________________
batch_normalization_2 (Batch (None, 96, 96, 32) 128
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 48, 48, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 48, 48, 64) 18432
_________________________________________________________________
leaky_re_lu_3 (LeakyReLU) (None, 48, 48, 64) 0
_________________________________________________________________
batch_normalization_3 (Batch (None, 48, 48, 64) 256
_________________________________________________________________
conv2d_4 (Conv2D) (None, 48, 48, 64) 36864
_________________________________________________________________
leaky_re_lu_4 (LeakyReLU) (None, 48, 48, 64) 0
_________________________________________________________________
batch_normalization_4 (Batch (None, 48, 48, 64) 256
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 24, 24, 64) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 24, 24, 96) 55296
_________________________________________________________________
leaky_re_lu_5 (LeakyReLU) (None, 24, 24, 96) 0
_________________________________________________________________
batch_normalization_5 (Batch (None, 24, 24, 96) 384
_________________________________________________________________
conv2d_6 (Conv2D) (None, 24, 24, 96) 82944
_________________________________________________________________
leaky_re_lu_6 (LeakyReLU) (None, 24, 24, 96) 0
_________________________________________________________________
batch_normalization_6 (Batch (None, 24, 24, 96) 384
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 12, 12, 96) 0
_________________________________________________________________
conv2d_7 (Conv2D) (None, 12, 12, 128) 110592
_________________________________________________________________
leaky_re_lu_7 (LeakyReLU) (None, 12, 12, 128) 0
_________________________________________________________________
batch_normalization_7 (Batch (None, 12, 12, 128) 512
_________________________________________________________________
conv2d_8 (Conv2D) (None, 12, 12, 128) 147456
_________________________________________________________________
leaky_re_lu_8 (LeakyReLU) (None, 12, 12, 128) 0
_________________________________________________________________
batch_normalization_8 (Batch (None, 12, 12, 128) 512
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 6, 6, 128) 0
_________________________________________________________________
conv2d_9 (Conv2D) (None, 6, 6, 256) 294912
_________________________________________________________________
leaky_re_lu_9 (LeakyReLU) (None, 6, 6, 256) 0
_________________________________________________________________
batch_normalization_9 (Batch (None, 6, 6, 256) 1024
_________________________________________________________________
conv2d_10 (Conv2D) (None, 6, 6, 256) 589824
_________________________________________________________________
leaky_re_lu_10 (LeakyReLU) (None, 6, 6, 256) 0
_________________________________________________________________
batch_normalization_10 (Batc (None, 6, 6, 256) 1024
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 3, 3, 256) 0
_________________________________________________________________
conv2d_11 (Conv2D) (None, 3, 3, 512) 1179648
_________________________________________________________________
leaky_re_lu_11 (LeakyReLU) (None, 3, 3, 512) 0
_________________________________________________________________
batch_normalization_11 (Batc (None, 3, 3, 512) 2048
_________________________________________________________________
conv2d_12 (Conv2D) (None, 3, 3, 512) 2359296
_________________________________________________________________
leaky_re_lu_12 (LeakyReLU) (None, 3, 3, 512) 0
_________________________________________________________________
batch_normalization_12 (Batc (None, 3, 3, 512) 2048
_________________________________________________________________
flatten_1 (Flatten) (None, 4608) 0
_________________________________________________________________
dense_1 (Dense) (None, 512) 2359808
_________________________________________________________________
dropout_1 (Dropout) (None, 512) 0
_________________________________________________________________
dense_2 (Dense) (None, 30) 15390
=================================================================
Total params: 7,268,670
Trainable params: 7,264,318
Non-trainable params: 4,352
_________________________________________________________________
Test score: 1.7251816006864396
Test accuracy: 0.8401128589532217
开始你的任务吧,祝你成功!