Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
完成训练,保存模型
Browse files Browse the repository at this point in the history
  • Loading branch information
zzaihang committed Dec 11, 2018
1 parent aa84cde commit 1bc3530
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 11 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# FaceAdd
# FaceEmotionClassifier

用Keras做前端,tensorflow做后端训练模型识别人类的情绪。根据情绪选择相应的emoji匹配

## 数据集处理

## 模型训练

## 识别人脸,匹配emoji

识别出人脸并且自动在人脸上添加素材
Binary file added emoji/angry.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added emoji/fear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added emoji/happy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added emoji/neutral.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added emoji/sad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added emoji/surprise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added model/model.h5
Binary file not shown.
1 change: 1 addition & 0 deletions model/model_fit_log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'val_acc': [0.24937004313624095, 0.2881222400515429, 0.32933623689415986, 0.3815072339761113, 0.4042979560809815, 0.4260382283109732, 0.44381911254332307, 0.46415339801646155, 0.4703510522836748, 0.48881068874625083, 0.5023049294546659, 0.5111852710926247, 0.5079490774404377, 0.5233906906040167, 0.5179364316398345, 0.5269016173022143, 0.5335962151654533, 0.5374182366353939, 0.5454662987531691, 0.5498175853568323, 0.5567263133824564, 0.5525689559905255, 0.5511589290448541, 0.5583019881967608, 0.5626371140279333, 0.568075212230341, 0.5708831455468008, 0.5726083074599566, 0.5781878123739469, 0.5776018026643523, 0.5762202389563302, 0.5803291140430705, 0.5840703316768664, 0.5858762974239997, 0.5748910158446965, 0.5842400197322105, 0.5871449176613996, 0.5951162161366044, 0.6007563239301265, 0.5926678598875058, 0.5899124490599376, 0.6034349711104283, 0.5962959521498683, 0.5967282526770024, 0.596275751191389, 0.6012290263713294, 0.5984412940102785, 0.603742025691246, 0.5951687386300714, 0.5992776137208453, 0.593249647510722, 0.6054631474086002, 0.6106305527562235, 0.6023562398918487, 0.6018431355329046, 0.6079559457684723, 0.6116203997534365, 0.6090750789017229, 0.6063011840145789, 0.5970918699392022, 0.6029461078986612, 0.6093659727129396, 0.6093740530950309, 0.6130183061280178, 0.6199755164570264, 0.6209976849885649, 0.6106143919872847, 0.6166463983887547, 0.6151030451090768, 0.6171554625581582, 0.6264438635710169, 0.6199189537689015, 0.6187351775652542, 0.6206744696424809, 0.6248399074163226, 0.6176604865360947, 0.6178867372809483, 0.6151434470302496, 0.6192886838451304, 0.60984271534868, 0.6194987738206238, 0.6085175324288143, 0.6236561312120732, 0.6224885157739329, 0.6251550423798337, 0.6129779042100959, 0.6221733808091576, 0.6258096561294001, 0.6288679786701089, 0.6230016201367903, 0.6199027930016485, 0.6239308642573613, 0.622177421002491, 0.615171728375215, 0.6283063920092853, 0.6294093643735971, 0.6269367669780439, 0.6260398443914973, 0.626072165925883, 0.6282094274013722], 'val_loss': [1.7918231389600305, 1.7439876464244597, 1.6906619235145783, 1.6047460206756732, 1.5391054775807413, 1.4948824447591584, 1.4552244232603497, 1.4136698239586907, 1.3759015969657582, 1.3520662607040965, 1.3133547836699695, 1.2820008036704642, 1.2747466523326747, 1.2461225064316452, 1.2583594747356037, 1.2275687959913082, 1.2027141147497056, 1.1973164520188855, 1.1900215516597288, 1.187303314363327, 1.1681778700637377, 1.1613983487300288, 1.1538233951777526, 1.1579516269542187, 1.147583652881735, 1.139403683849947, 1.1404834502424956, 1.1390817905126775, 1.1336678538445437, 1.1286732734454823, 1.1224069260720768, 1.1189071942184652, 1.12368928290049, 1.1089613050443825, 1.115043700852962, 1.1114695749136498, 1.1044655106497105, 1.0952741010717075, 1.0913373901054486, 1.1127118740115072, 1.117530078021909, 1.108314835018165, 1.121884060279906, 1.114964057492193, 1.0970145476240658, 1.1017921001565172, 1.1282675052773847, 1.1058046686961442, 1.1268161795724088, 1.1274718108610404, 1.1368726998675827, 1.1090983346795218, 1.122384731006111, 1.11498380348435, 1.1041577495501356, 1.122497089396663, 1.0987920202498624, 1.1377048783111838, 1.1413242200104854, 1.1096590855948492, 1.1520718370484584, 1.1299226179657624, 1.1341491768272702, 1.1275146232405324, 1.1335570536579176, 1.1238483322336519, 1.140385496320713, 1.1782971201792305, 1.1998451823642677, 1.1606354495926472, 1.156430333234297, 1.151478749307149, 1.1702539239925207, 1.1940382496987252, 1.1817661656690612, 1.1699137859595246, 1.2096873391970586, 1.2106127318600466, 1.1979970435509666, 1.206415689857984, 1.221534212822276, 1.2134344738799787, 1.1960747327952372, 1.2096961865201614, 1.214998798541125, 1.2408835523110944, 1.229870862409818, 1.2218471123040853, 1.230802626909463, 1.2230135385483218, 1.2264266248777111, 1.2063100468890582, 1.2314744792582397, 1.2737061852022817, 1.2394357905132163, 1.2716865335868928, 1.2507996435220832, 1.2598147904413861, 1.262187662394025, 1.2542485479241265], 'loss': [1.8226229739189148, 1.7811286591965727, 1.7420279865445465, 1.6831640746939387, 1.6338112938805514, 1.576069244101344, 1.5378713009334928, 1.4994595541687292, 1.4531040240158464, 1.4275278854370117, 1.386926871726727, 1.3670518069149695, 1.3323288153272985, 1.3095760393359537, 1.283013053529008, 1.2651510945145574, 1.2449771029950403, 1.2338860144015693, 1.2135504508018493, 1.2009645127447262, 1.1786131615911462, 1.1675150022030418, 1.159316687779451, 1.1457532166014033, 1.1359223434209047, 1.1278269395604306, 1.108045284258043, 1.1001095137000083, 1.0967433562099858, 1.080408758355204, 1.0714365457104256, 1.054444501571875, 1.0507979505256229, 1.0441964957634542, 1.0281824333552139, 1.019324315590133, 1.0072237864136695, 0.9955112745220805, 0.9784026898347605, 0.9753677531988628, 0.9752671936907106, 0.9590184814978844, 0.9548802813651248, 0.9442389466718708, 0.934032071052199, 0.9205672487616539, 0.9068723036875668, 0.9077750115277013, 0.88461630522394, 0.8840005897831676, 0.8766625583734968, 0.8639934892244857, 0.8583625918358309, 0.8474781975115643, 0.8330672860145569, 0.8242842668815737, 0.820948977870239, 0.8105622341652564, 0.7964627140996186, 0.7936669345922484, 0.7772242859472667, 0.7772950882389963, 0.7678384346180425, 0.7596929386258126, 0.7400601275188985, 0.7344985127126583, 0.7241346335400791, 0.7211650965345467, 0.7081072409008395, 0.6984174663244129, 0.6911281848898723, 0.6855693383991491, 0.6683668237924576, 0.664824652844256, 0.6589214611237592, 0.6471770931109906, 0.6456811242472948, 0.6236470258615865, 0.631454617230174, 0.6285202610571684, 0.6201023493724296, 0.5957476995885372, 0.5908199068103517, 0.5908477878415406, 0.5836559672423695, 0.5681218781700801, 0.5730308038834435, 0.5580890439107551, 0.5636389059832396, 0.5474087821574609, 0.5397467738389969, 0.5411034822627552, 0.5276154593258875, 0.5183893829973167, 0.516068944948695, 0.5108523602192926, 0.5127517395973056, 0.5074577761105785, 0.49470332666330213, 0.4926890636980534], 'acc': [0.2458203125, 0.2575561566330693, 0.2906817201684301, 0.3302755889867717, 0.3595201693508251, 0.3901368144564741, 0.39946685483437994, 0.418009330052061, 0.4371006311381072, 0.44765625, 0.4643459171191103, 0.4748520130266974, 0.49245364382367096, 0.49739307696842017, 0.5142106707483324, 0.5151907169936644, 0.5268728683877054, 0.5321259163584394, 0.541015625, 0.5442784899657549, 0.5536477321776317, 0.5569798894157328, 0.5606648633465025, 0.5661923242426568, 0.5690932611668795, 0.5750911442938329, 0.579834568219658, 0.5850390625, 0.5844603865419273, 0.593398408432916, 0.593398408432916, 0.6021796228828731, 0.6039829079979305, 0.6050805598070959, 0.6079814967383286, 0.6148418205315926, 0.6204296875, 0.6240542553258038, 0.6277392292892859, 0.6299737347649539, 0.6324434513122099, 0.6389901603005187, 0.6402054176770223, 0.6449880434566797, 0.6500058802588562, 0.6558203125, 0.6568270022485252, 0.6600807558034961, 0.6705868516807073, 0.6727429534487107, 0.6732525775263323, 0.6756046885786849, 0.679093653178373, 0.6868164177629564, 0.6863671875, 0.6949704025609439, 0.6915206397858804, 0.6973617154309113, 0.7023403504622053, 0.7073189854701332, 0.7130816574916177, 0.7114743815814646, 0.7157865851151349, 0.7187109375, 0.7273119291534306, 0.7256654513836038, 0.7291544160861028, 0.7339762436997311, 0.7362499509789941, 0.7437767062979213, 0.7453447802642824, 0.7490297541786957, 0.7527734375, 0.7566741150392584, 0.7584381982642661, 0.7612215296842392, 0.7628288054939177, 0.7715708181729923, 0.7689442941449821, 0.7677682386982506, 0.7714140107319606, 0.781796875, 0.7826257399489446, 0.7858402916921424, 0.7871339526968658, 0.789446861929267, 0.7896820730148748, 0.7965031949648214, 0.7954839468095783, 0.7990121133624047, 0.80359375, 0.8052060057374898, 0.8069308871518925, 0.8131639813469291, 0.8115175036401908, 0.8139088165101583, 0.8145752479189907, 0.8146928534769826, 0.8172409737809719, 0.825703125]}
1 change: 1 addition & 0 deletions model/model_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"class_name": "Sequential", "backend": "tensorflow", "config": [{"class_name": "Conv2D", "config": {"padding": "same", "kernel_size": [1, 1], "batch_input_shape": [null, 48, 48, 1], "dilation_rate": [1, 1], "data_format": "channels_last", "kernel_constraint": null, "name": "conv2d_1", "filters": 32, "bias_regularizer": null, "use_bias": true, "trainable": true, "bias_initializer": {"class_name": "Zeros", "config": {}}, "activation": "linear", "kernel_initializer": {"class_name": "VarianceScaling", "config": {"distribution": "uniform", "seed": null, "mode": "fan_avg", "scale": 1.0}}, "strides": [1, 1], "kernel_regularizer": null, "bias_constraint": null, "activity_regularizer": null, "dtype": "float32"}}, {"class_name": "Activation", "config": {"trainable": true, "name": "activation_1", "activation": "relu"}}, {"class_name": "Conv2D", "config": {"padding": "same", "kernel_size": [5, 5], "data_format": "channels_last", "dilation_rate": [1, 1], "kernel_constraint": null, "name": "conv2d_2", "filters": 32, "bias_regularizer": null, "use_bias": true, "trainable": true, "bias_initializer": {"class_name": "Zeros", "config": {}}, "activation": "linear", "kernel_initializer": {"class_name": "VarianceScaling", "config": {"distribution": "uniform", "seed": null, "mode": "fan_avg", "scale": 1.0}}, "strides": [1, 1], "kernel_regularizer": null, "activity_regularizer": null, "bias_constraint": null}}, {"class_name": "Activation", "config": {"trainable": true, "name": "activation_2", "activation": "relu"}}, {"class_name": "MaxPooling2D", "config": {"padding": "valid", "trainable": true, "pool_size": [2, 2], "data_format": "channels_last", "strides": [2, 2], "name": "max_pooling2d_1"}}, {"class_name": "Conv2D", "config": {"padding": "same", "kernel_size": [3, 3], "data_format": "channels_last", "dilation_rate": [1, 1], "kernel_constraint": null, "name": "conv2d_3", "filters": 32, "bias_regularizer": null, "use_bias": true, "trainable": true, "bias_initializer": {"class_name": "Zeros", "config": {}}, "activation": "linear", "kernel_initializer": {"class_name": "VarianceScaling", "config": {"distribution": "uniform", "seed": null, "mode": "fan_avg", "scale": 1.0}}, "strides": [1, 1], "kernel_regularizer": null, "activity_regularizer": null, "bias_constraint": null}}, {"class_name": "Activation", "config": {"trainable": true, "name": "activation_3", "activation": "relu"}}, {"class_name": "MaxPooling2D", "config": {"padding": "valid", "trainable": true, "pool_size": [2, 2], "data_format": "channels_last", "strides": [2, 2], "name": "max_pooling2d_2"}}, {"class_name": "Conv2D", "config": {"padding": "same", "kernel_size": [5, 5], "data_format": "channels_last", "dilation_rate": [1, 1], "kernel_constraint": null, "name": "conv2d_4", "filters": 64, "bias_regularizer": null, "use_bias": true, "trainable": true, "bias_initializer": {"class_name": "Zeros", "config": {}}, "activation": "linear", "kernel_initializer": {"class_name": "VarianceScaling", "config": {"distribution": "uniform", "seed": null, "mode": "fan_avg", "scale": 1.0}}, "strides": [1, 1], "kernel_regularizer": null, "activity_regularizer": null, "bias_constraint": null}}, {"class_name": "Activation", "config": {"trainable": true, "name": "activation_4", "activation": "relu"}}, {"class_name": "MaxPooling2D", "config": {"padding": "valid", "trainable": true, "pool_size": [2, 2], "data_format": "channels_last", "strides": [2, 2], "name": "max_pooling2d_3"}}, {"class_name": "Flatten", "config": {"trainable": true, "name": "flatten_1", "data_format": "channels_last"}}, {"class_name": "Dense", "config": {"bias_constraint": null, "kernel_constraint": null, "name": "dense_1", "bias_regularizer": null, "use_bias": true, "trainable": true, "bias_initializer": {"class_name": "Zeros", "config": {}}, "activation": "linear", "kernel_initializer": {"class_name": "VarianceScaling", "config": {"distribution": "uniform", "seed": null, "mode": "fan_avg", "scale": 1.0}}, "kernel_regularizer": null, "units": 2048, "activity_regularizer": null}}, {"class_name": "Activation", "config": {"trainable": true, "name": "activation_5", "activation": "relu"}}, {"class_name": "Dropout", "config": {"seed": null, "noise_shape": null, "name": "dropout_1", "rate": 0.5, "trainable": true}}, {"class_name": "Dense", "config": {"bias_constraint": null, "kernel_constraint": null, "name": "dense_2", "bias_regularizer": null, "use_bias": true, "trainable": true, "bias_initializer": {"class_name": "Zeros", "config": {}}, "activation": "linear", "kernel_initializer": {"class_name": "VarianceScaling", "config": {"distribution": "uniform", "seed": null, "mode": "fan_avg", "scale": 1.0}}, "kernel_regularizer": null, "units": 1024, "activity_regularizer": null}}, {"class_name": "Activation", "config": {"trainable": true, "name": "activation_6", "activation": "relu"}}, {"class_name": "Dropout", "config": {"seed": null, "noise_shape": null, "name": "dropout_2", "rate": 0.5, "trainable": true}}, {"class_name": "Dense", "config": {"bias_constraint": null, "kernel_constraint": null, "name": "dense_3", "bias_regularizer": null, "use_bias": true, "trainable": true, "bias_initializer": {"class_name": "Zeros", "config": {}}, "activation": "linear", "kernel_initializer": {"class_name": "VarianceScaling", "config": {"distribution": "uniform", "seed": null, "mode": "fan_avg", "scale": 1.0}}, "kernel_regularizer": null, "units": 7, "activity_regularizer": null}}, {"class_name": "Activation", "config": {"trainable": true, "name": "activation_7", "activation": "softmax"}}], "keras_version": "2.2.2"}
13 changes: 13 additions & 0 deletions model/model_predict_log
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[3.7913677e-01 5.7567502e-03 2.0879397e-01 ... 3.4839332e-01
1.4710234e-03 5.6282818e-02]
[9.9539198e-04 6.2243471e-06 1.4877088e-02 ... 1.1489792e-03
9.7738248e-01 5.5893990e-03]
[3.3585075e-02 5.8197151e-03 2.0556857e-01 ... 9.5929064e-02
5.1386589e-01 1.3000472e-01]
...
[7.6685068e-03 1.5580928e-04 9.2074364e-01 ... 3.3790086e-04
7.0821524e-02 7.3468371e-05]
[4.2802459e-01 2.3080054e-05 5.7184899e-01 ... 7.6518998e-05
1.8332221e-05 1.2069239e-07]
[1.5060876e-01 2.5098317e-04 6.2613398e-01 ... 1.5618929e-01
3.1598373e-03 5.7471637e-02]]
Binary file added model/model_weight.h5
Binary file not shown.
20 changes: 10 additions & 10 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
num_classes = 7
nb_epoch = 100
img_size = 48
root_path = './data'
data_path = './data'
model_path = './model'


class Model:
Expand Down Expand Up @@ -58,19 +59,19 @@ def train_model(self):
rescale=1. / 255)
# 以文件分类名划分label
train_generator = train_datagen.flow_from_directory(
root_path + '/train',
data_path + '/train',
target_size=(img_size, img_size),
color_mode='grayscale',
batch_size=batch_siz,
class_mode='categorical')
val_generator = val_datagen.flow_from_directory(
root_path + '/val',
data_path + '/val',
target_size=(img_size, img_size),
color_mode='grayscale',
batch_size=batch_siz,
class_mode='categorical')
eval_generator = eval_datagen.flow_from_directory(
root_path + '/test',
data_path + '/test',
target_size=(img_size, img_size),
color_mode='grayscale',
batch_size=batch_siz,
Expand All @@ -90,20 +91,19 @@ def train_model(self):
history_predict = self.model.predict_generator(
eval_generator,
steps=2000)
with open(root_path + '/model_fit_log', 'w') as f:
with open(model_path + '/model_fit_log', 'w') as f:
f.write(str(history_fit.history))
with open(root_path + '/model_predict_log', 'w') as f:
with open(model_path + '/model_predict_log', 'w') as f:
f.write(str(history_predict))
# print("%s: %.2f%%" % (self.model.metrics_names[1], history_eval[1] * 100))
print('model trained')

def save_model(self):
model_json = self.model.to_json()
with open(root_path + "/model_json.json", "w") as json_file:
with open(model_path + "/model_json.json", "w") as json_file:
json_file.write(model_json)
self.model.save_weights(root_path + '/model_weight.h5')
self.model.save(root_path + '/model.h5')
print('model saved')
self.model.save_weights(model_path + '/model_weight.h5')
self.model.save(model_path + '/model.h5')


if __name__ == '__main__':
Expand Down

0 comments on commit 1bc3530

Please sign in to comment.