Example1/训练模型.py

87 lines
3.2 KiB
Python

import numpy as np
import cv2
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D
from keras.optimizers import Adam
from keras.layers import MaxPooling2D
from keras.models import Sequential
# 照片路径
train_dir = 'D:\\my\\college\\31\\machinelearning\\archive\\train'
val_dir = 'D:\\my\\college\\31\\machinelearning\\archive\\test'
# 把灰度值从0-255映射到0-1
train_datagen = ImageDataGenerator(rescale=1./255)
val_datagen = ImageDataGenerator(rescale=1./255)
# 图像增强
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(48,48),
batch_size=64,
color_mode="grayscale",
class_mode='categorical')
validation_generator = val_datagen.flow_from_directory(
val_dir,
target_size=(48,48),
batch_size=64,
color_mode="grayscale",
class_mode='categorical')
# 第2个
emotion_model = Sequential()
emotion_model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(48,48,1)))
emotion_model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Dropout(0.25))
emotion_model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Dropout(0.25))
emotion_model.add(Flatten())
emotion_model.add(Dense(1024, activation='relu'))
emotion_model.add(Dropout(0.5))
emotion_model.add(Dense(7, activation='softmax'))
# 第1个
# # 第一层
# emotion_model = Sequential()
# emotion_model.add(Conv2D(input_shape=(48, 48, 1), filters=32, kernel_size=3, padding='same', activation='relu'))
# emotion_model.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
# emotion_model.add(MaxPooling2D(pool_size=2, strides=2))
#
# # 第二层
# emotion_model.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
# emotion_model.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
# emotion_model.add(MaxPooling2D(pool_size=2, strides=2))
#
# # 第三层
# emotion_model.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
# emotion_model.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
# emotion_model.add(MaxPooling2D(pool_size=2, strides=2))
#
# emotion_model.add(Flatten())
#
# # 全连接层
# emotion_model.add(Dense(64, activation='relu'))
# emotion_model.add(Dropout(0.5))
# emotion_model.add(Dense(7, activation='softmax'))
cv2.ocl.setUseOpenCL(False)
emotion_dict = {0: "Angry", 1: "Disgusted", 2: "Fearful", 3: "Happy", 4: "Neutral", 5: "Sad", 6: "Surprised"}
emotion_model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
emotion_model_info = emotion_model.fit(
train_generator,
steps_per_epoch=28709 // 64,
epochs=20,
validation_data=validation_generator,
validation_steps=7178 // 64)
emotion_model.save_weights('emotion_model_3.h5')