【人脸颜值打分预测】华南理工数据集+ResNet50多阶段训练

【人脸颜值打分预测】华南理工数据集+ResNet50多阶段训练_第1张图片

主要采用fine-tune方法,因为数据集较小,采用迁移学习能提高特征提取准确度

但是top层需要我们自己设计,所以top的权重是正态初始化的,不一定符合最优的一个权重,所以一开始不能直接全图fine-tune,因为top层的差距会不断向后传导,影响预训练权重,反而会打扰效果。

采集的方案是:

1.对其他层冻结,只对top层训练,直到loss接近0.2,左右,初步提升top层的预测能力

2.解冻其它层,联合训练,全图反向传播,提升全网络权重优化

# -*- coding: utf-8 -*-
"""
Created on Thu Sep 27 10:09:50 2018

@author: Administrator
"""

from keras.applications.xception import Xception
from keras.applications.resnet50 import ResNet50
from keras.models import Model,Sequential
from keras.layers import Dense,Flatten,Dropout
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
from keras import backend as K
from keras.callbacks import ModelCheckpoint,EarlyStopping
import pandas as pd
from PIL import Image
from keras.optimizers import adam,SGD

def r_square(y_true, y_pred):
    SSR = K.sum(K.square(y_pred-y_true),axis=-1)
    SST = K.sum(K.square(y_true-K.mean(y_true)),axis=-1)
    return 1-SSR/(SST+1e-6)

resnet = ResNet50(include_top=False, pooling='avg',input_shape=(200,200,3),weights='imagenet')
finalmodel = Sequential()
finalmodel.add(resnet)
finalmodel.add(Dense(1))
finalmodel.layers[0].trainable = False
#vgg16 = ResNet50(include_top=False,input_shape=(200,200,3),weights='imagenet')
#x = vgg16.output
#fla = Flatten()(x)
##dp = Dropout(0.5)(fla)
#out = Dense(1)(fla)
#finalmodel = Model(input=vgg16.input,output=out)    
finalmodel.compile(loss='mse',optimizer='adam',metrics=[r_square])
finalmodel.load_weights('facr_beauty_predict_score.h5')

print(finalmodel.summary())
for i in finalmodel.layers[:-5]:
    i.trainable=False
#finalmodel.load_weights('resume_predict_score.h5')
scorefile = pd.read_table('train_test_files//All_labels.txt',sep=' ')
#print(scorefile['imgname'])
import os
path = r'Images'
filenamelist = os.listdir(path)
numlist = list()
imglist = []
for filename in filenamelist[:2500]:
#    print()
    try:
#        print(filename)
        imglist.append(np.array(Image.open(path+'\\'+filename).convert('RGB').resize((200,200),Image.LANCZOS))/255.0)
        #aaaa=np.array(Image.open(path+'\\'+filename).convert('RGB').resize((120,120),Image.LANCZOS))/255.0
        score = scorefile[scorefile['imgname']==filename].values[0][1]
    
        numlist.append(score)
    except:
        continue
numlist = np.array(numlist)
imglist = np.array(imglist)

mp = ModelCheckpoint(filepath='facr_beauty_predict_score.h5',save_best_only=True,mode='max', monitor='r_square', verbose=1)
el = EarlyStopping( monitor='r_square',patience=8, verbose=1, mode='max')
cllist = [mp,el]
his = finalmodel.fit(x=imglist,
            y=numlist,
            batch_size=30,
            epochs=2000,
            verbose=1,
            callbacks=cllist,
            validation_split=0.15,
            shuffle=True)

print((finalmodel.predict(imglist[-50:-1])+1)*1e2)

【人脸颜值打分预测】华南理工数据集+ResNet50多阶段训练_第2张图片

要点:

不能采用MSE做模型好坏的评价,需要用R—Square指标来衡量,关于这个指标,之前的文章我有介绍过。

需要对网络做再次开发,更换metrics指标。

 

你可能感兴趣的:(【人脸颜值打分预测】华南理工数据集+ResNet50多阶段训练)