0


Keras深度学习实战——基于VGG19模型实现性别分类

Keras深度学习实战——基于VGG19模型实现性别分类

0. 前言

在《迁移学习》中,我们了解了利用迁移学习,只需要少量样本即可训练得到性能较好的模型;并基于迁移学习利用预训练的

  1. VGG16

模型进行了性别分类的实战,进一步加深对迁移学习工作原理的理解。

1. VGG19 架构简介

本文,我们将介绍另一种常用的网络模型架构——

  1. VGG19

,并使用预训练的

  1. VGG19

模型进行性别分类实战。

  1. VGG19

  1. VGG16

的改进版本,具有更多的卷积和池化操作,

  1. VGG19

模型的体系结构如下:

  1. Model: "vgg19"
  2. _________________________________________________________________
  3. Layer (type) Output Shape Param # =================================================================
  4. input_1 (InputLayer)[(None, 256, 256, 3)]0
  5. _________________________________________________________________
  6. block1_conv1 (Conv2D)(None, 256, 256, 64)1792
  7. _________________________________________________________________
  8. block1_conv2 (Conv2D)(None, 256, 256, 64)36928
  9. _________________________________________________________________
  10. block1_pool (MaxPooling2D)(None, 128, 128, 64)0
  11. _________________________________________________________________
  12. block2_conv1 (Conv2D)(None, 128, 128, 128)73856
  13. _________________________________________________________________
  14. block2_conv2 (Conv2D)(None, 128, 128, 128)147584
  15. _________________________________________________________________
  16. block2_pool (MaxPooling2D)(None, 64, 64, 128)0
  17. _________________________________________________________________
  18. block3_conv1 (Conv2D)(None, 64, 64, 256)295168
  19. _________________________________________________________________
  20. block3_conv2 (Conv2D)(None, 64, 64, 256)590080
  21. _________________________________________________________________
  22. block3_conv3 (Conv2D)(None, 64, 64, 256)590080
  23. _________________________________________________________________
  24. block3_conv4 (Conv2D)(None, 64, 64, 256)590080
  25. _________________________________________________________________
  26. block3_pool (MaxPooling2D)(None, 32, 32, 256)0
  27. _________________________________________________________________
  28. block4_conv1 (Conv2D)(None, 32, 32, 512)1180160
  29. _________________________________________________________________
  30. block4_conv2 (Conv2D)(None, 32, 32, 512)2359808
  31. _________________________________________________________________
  32. block4_conv3 (Conv2D)(None, 32, 32, 512)2359808
  33. _________________________________________________________________
  34. block4_conv4 (Conv2D)(None, 32, 32, 512)2359808
  35. _________________________________________________________________
  36. block4_pool (MaxPooling2D)(None, 16, 16, 512)0
  37. _________________________________________________________________
  38. block5_conv1 (Conv2D)(None, 16, 16, 512)2359808
  39. _________________________________________________________________
  40. block5_conv2 (Conv2D)(None, 16, 16, 512)2359808
  41. _________________________________________________________________
  42. block5_conv3 (Conv2D)(None, 16, 16, 512)2359808
  43. _________________________________________________________________
  44. block5_conv4 (Conv2D)(None, 16, 16, 512)2359808
  45. _________________________________________________________________
  46. block5_pool (MaxPooling2D)(None, 8, 8, 512)0=================================================================
  47. Total params: 20,024,384
  48. Trainable params: 20,024,384
  49. Non-trainable params: 0
  50. _________________________________________________________________

可以看到,上示的体系结构中具有更多的网络层以及更多的参数量。需要注意的是,

  1. VGG16

  1. VGG19

体系结构中的

  1. 16

  1. 19

代表这些网络中的网络层数。

VGG19架构

将每个图像通过

  1. VGG19

网络后,提取到

  1. 8 x 8 x 512

输出后,该输出将成为微调模型的输入。接下来,创建输入和输出数据集,然后构建、编译和拟合模型的过程与使用基于预训练的 VGG16 模型进行性别分类的过程相同。

2. 使用预训练 VGG19 模型进行性别分类

在本节中,我们基于迁移学习使用预训练的

  1. VGG19

模型进行性别分类。

2.1 构建输入与输出数据

首先,准备输入和输出数据,我们重用在《卷积神经网络进行性别分类》中使用的数据集以及数据加载代码:

  1. from keras.applications import VGG19
  2. from keras.applications.vgg19 import preprocess_input
  3. from glob import glob
  4. from skimage import io
  5. import cv2
  6. import numpy as np
  7. model = VGG19(include_top=False, weights='imagenet', input_shape=(256,256,3))
  8. x =[]
  9. y =[]for i in glob('man_woman/a_resized/*.jpg')[:800]:try:
  10. image = io.imread(i)
  11. x.append(image)
  12. y.append(0)except:continuefor i in glob('man_woman/b_resized/*.jpg')[:800]:try:
  13. image = io.imread(i)
  14. x.append(image)
  15. y.append(1)except:continue
  16. x_vgg19 =[]for i inrange(len(x)):
  17. img = x[i]
  18. img = preprocess_input(img.reshape((1,256,256,3)))
  19. img_feature = model.predict(img)
  20. x_vgg19.append(img_feature)

将输入和输出转换为其相应的数组,并创建训练和测试数据集:

  1. x_vgg19 = np.array(x_vgg19)
  2. x_vgg19 = x_vgg19.reshape(x_vgg19.shape[0], x_vgg19.shape[2], x_vgg19.shape[3], x_vgg19.shape[4])
  3. y = np.array(y)from sklearn.model_selection import train_test_split
  4. x_train, x_test, y_train, y_test = train_test_split(x_vgg19, y, test_size=0.2)

2.2 模型构建与训练

构建微调模型:

  1. from keras.models import Sequential
  2. from keras.layers import Conv2D, MaxPooling2D, Flatten, Dropout, Dense
  3. model_fine_tuning = Sequential()
  4. model_fine_tuning.add(Conv2D(512,
  5. kernel_size=(3,3),
  6. activation='relu',
  7. input_shape=(x_train.shape[1], x_train.shape[2], x_train.shape[3])))
  8. model_fine_tuning.add(MaxPooling2D(pool_size=(2,2)))
  9. model_fine_tuning.add(Flatten())
  10. model_fine_tuning.add(Dense(1024, activation='relu'))
  11. model_fine_tuning.add(Dropout(0.6))
  12. model_fine_tuning.add(Dense(1, activation='sigmoid'))
  13. model_fine_tuning.summary()

该模型架构的简要信息输入如下:

  1. Model: "sequential"
  2. _________________________________________________________________
  3. Layer (type) Output Shape Param # =================================================================
  4. conv2d (Conv2D)(None, 6, 6, 512)2359808
  5. _________________________________________________________________
  6. max_pooling2d (MaxPooling2D)(None, 3, 3, 512)0
  7. _________________________________________________________________
  8. flatten (Flatten)(None, 4608)0
  9. _________________________________________________________________
  10. dense (Dense)(None, 1024)4719616
  11. _________________________________________________________________
  12. dropout (Dropout)(None, 1024)0
  13. _________________________________________________________________
  14. dense_1 (Dense)(None, 1)1025=================================================================
  15. Total params: 7,080,449
  16. Trainable params: 7,080,449
  17. Non-trainable params: 0
  18. _________________________________________________________________

接下来,编译并拟合模型:

  1. model_fine_tuning.compile(loss='binary_crossentropy',optimizer='adam',metrics=['acc'])
  2. history = model_fine_tuning.fit(x_train, y_train,
  3. batch_size=32,
  4. epochs=20,
  5. verbose=1,
  6. validation_data =(x_test, y_test))

最后,我们绘制在训练期间,模型在训练和测试数据集的损失和准确率的变化。可以看到,当我们使用

  1. VGG19

架构时,能够在测试数据集上达到约

  1. 95

的准确率,结果与使用

  1. VGG16

架构时的性能相似:

模型训练过程监测

2.3 模型错误分类示例

一些错误分类的图像示例如下:

  1. x = np.array(x)from sklearn.model_selection import train_test_split
  2. x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
  3. x_test_vgg19 =[]for i inrange(len(x_test)):
  4. img = x_test[i]
  5. img = preprocess_input(img.reshape((1,256,256,3)))
  6. img_feature = model.predict(img)
  7. x_test_vgg19.append(img_feature)
  8. x_test_vgg19 = np.array(x_test_vgg19)
  9. x_test_vgg19 = x_test_vgg19.reshape(x_test_vgg19.shape[0], x_test_vgg19.shape[2], x_test_vgg19.shape[3], x_test_vgg19.shape[4])
  10. y_pred = model_fine_tuning.predict(x_test_vgg19)
  11. wrong = np.argsort(np.abs(y_pred.flatten()-y_test))print(wrong)
  12. y_test_char = np.where(y_test==0,'M','F')
  13. y_pred_char = np.where(y_pred>0.5,'F','M')
  14. plt.subplot(221)
  15. plt.imshow(x_test[wrong[-1]])
  16. plt.title('Actual: '+str(y_test_char[wrong[-1]])+', '+'Predicted: '+str((y_pred_char[wrong[-1]][0])))
  17. plt.subplot(222)
  18. plt.imshow(x_test[wrong[-2]])
  19. plt.title('Actual: '+str(y_test_char[wrong[-2]])+', '+'Predicted: '+str((y_pred_char[wrong[-2]][0])))
  20. plt.subplot(223)
  21. plt.imshow(x_test[wrong[-3]])
  22. plt.title('Actual: '+str(y_test_char[wrong[-3]])+', '+'Predicted: '+str((y_pred_char[wrong[-3]][0])))
  23. plt.subplot(224)
  24. plt.imshow(x_test[wrong[-4]])
  25. plt.title('Actual: '+str(y_test_char[wrong[-4]])+', '+'Predicted: '+str((y_pred_char[wrong[-4]][0])))
  26. plt.show()

错误分类示例

从图中,可以看出,

  1. VGG19

类似于

  1. VGG16

除了由于人物在图像中占据的空间较小造成错误分类外,倾向于根据头发来判断人物究竟是男性还是女性。

相关链接

Keras深度学习实战(7)——卷积神经网络详解与实现
Keras深度学习实战(9)——卷积神经网络的局限性
Keras深度学习实战(10)——迁移学习
Keras深度学习实战——使用卷积神经网络实现性别分类


本文转载自: https://blog.csdn.net/LOVEmy134611/article/details/125509499
版权归原作者 盼小辉丶 所有, 如有侵权,请联系我们删除。

“Keras深度学习实战——基于VGG19模型实现性别分类”的评论:

还没有评论