1. 程式人生 > >【人臉顏值打分預測】華南理工資料集+ResNet50多階段訓練

【人臉顏值打分預測】華南理工資料集+ResNet50多階段訓練

主要採用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)

要點:

不能採用MSE做模型好壞的評價,需要用R—Square指標來衡量,關於這個指標,之前的文章我有介紹過。

需要對網路做再次開發,更換metrics指標。