-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_lstm.py
86 lines (75 loc) · 2.53 KB
/
evaluate_lstm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
import logging
import numpy as np
seed = 23
np.random.seed(seed=seed)
from preprocessing import load_au_file
from preprocessing import print_model_architecture
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau, CSVLogger
from keras.regularizers import l2
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Input
from keras.models import Model
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
num_feats = 3
num_types_au = 6 # activity unit types.
latent_dim = 100
model_name = 'lstm_stateful'
max_num_epochs = 20
au_feats = Input(
batch_shape=(1, 1, num_feats),
dtype='float32',
name='au_feats')
x = LSTM(
latent_dim,
stateful=True,
name='lstm1')(au_feats)
# Softmax over each activity unit type.
x = Dense(
num_types_au,
activation='tanh',
name='dense1')(x)
x = Dense(
num_types_au,
activation='softmax',
name='dense_output')(x)
model = Model(inputs=[au_feats], outputs=[x])
# This is the compilation for a classification model.
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
# This is the compilation for a regression model.
# model.compile(optimizer='rmsprop', loss='mean_squared_error', metrics=['accuracy'])
print_model_architecture(model, model_name + '.summary.txt')
callbacks = [
CSVLogger(filename=model_name + '.log.csv')]
# TODO: Split into training and validation in a meaningful way. (e.g. using a different file).
sessions = ['P01_T1.au']
# feats_and_labels = load_au_files()
for i in range(max_num_epochs):
# for (Feats, Labels) in feats_and_labels:
for session in sessions:
# Feats is a matrix N x F with N rows (activity units) and F columns (features of AUs).
# Labels is a matrix N x C where C is the number of classes.
Feats, Labels = load_au_file('P01_T1.au')
Feats = np.expand_dims(Feats, axis=1)
model.fit(
Feats,
Labels,
epochs=1,
verbose=1,
batch_size=1,
shuffle=False,
callbacks=callbacks)
model.reset_states()
logging.info('Start model evaluation: {0}'.format(
model.metrics_names))
Feats, Labels = load_au_file('P01_T1.au')
Feats = np.expand_dims(Feats, axis=1)
evaluation = model.evaluate(
Feats,
Labels,
batch_size=1,
verbose=1)
logging.info('Results:')
for metric_name, result in zip(model.metrics_names, evaluation):
logging.info('{0}: {1}'.format(metric_name, result))