-
Notifications
You must be signed in to change notification settings - Fork 0
/
motcnt_conv.py
186 lines (144 loc) · 6.58 KB
/
motcnt_conv.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import sys
import Data
import Data.Helpers.encoding as enc
import Model
import numpy as np
import tensorflow as tf
################################################################################
#################################### SACRED ####################################
################################################################################
from sacred import Experiment
from sacred.observers import MongoObserver
ex = Experiment('LipR.MotCnt')
@ex.config
def cfg():
#### DATA
AllSpeakers = 's1-s2-s3-s4-s5-s6-s7-s8_s9'
(SourceSpeakers,TargetSpeakers) = AllSpeakers.split('_')
WordsPerSpeaker = -1
### DATA PROCESSING
VideoNorm = 'MV'
AddChannel = True
DownSample = False
### TRAINING DATA
TruncateRemainder = False
Shuffle = 1
### NET SPECS
MotSpec = '*FLATFEAT!2-1_CONV16r!5_*MP!2-2_CONV32r!5_*MP!2-2_CONV64r!7_*MP!2-2_*UNDOFLAT_*CONVLSTM!64-7_*MASKSEQ'
#
CntSpec = 'CONV16r!3_CONV16r!3_*MP!2-2_CONV32r!3_CONV32r!3_*MP!2-2_CONV64r!3_CONV64r!3_CONV64r!3_*MP!2-2'
#
TrgSpec = '*CONCAT!3_CONV64r!3_CONV32r!3_CONV64r!3_*FLATFEAT!3_FC64r'
#
# NET TRAINING
MaxEpochs = 200
BatchSize = 64
LearnRate = 0.0001
InitStd = 0.1
EarlyStoppingCondition = 'SOURCEVALID'
EarlyStoppingValue = 'ACCURACY'
EarlyStoppingPatience = 10
DBPath = None
Variant = ''
Collection = 'CONV' + Variant
OutDir = 'Outdir/MotCnt'
TensorboardDir = OutDir + '/tensorboard'
ModelDir = OutDir + '/model'
# Prepare MongoDB batch exp
if DBPath != None:
ex.observers.append(MongoObserver.create(url=DBPath, db_name='LipR_MotCnt_Valid', collection=Collection))
################################################################################
#################################### SCRIPT ####################################
################################################################################
@ex.automain
def main(
# Speakers
SourceSpeakers, TargetSpeakers, WordsPerSpeaker,
# Data
VideoNorm, AddChannel, DownSample, TruncateRemainder, Shuffle, InitStd,
# NN settings
MotSpec, CntSpec, TrgSpec,
# Training settings
BatchSize, LearnRate, MaxEpochs, EarlyStoppingCondition, EarlyStoppingValue, EarlyStoppingPatience,
# Extra settings
OutDir, ModelDir, TensorboardDir, DBPath, _config
):
print('Config directory is:',_config)
###########################################################################
# Prepare output directory
try:
os.makedirs(OutDir)
except OSError as e:
print('Error %s when making output dir - ignoring' % str(e))
if TensorboardDir is not None:
TensorboardDir = TensorboardDir + '%d' % _config['seed']
if ModelDir is not None:
ModelDir = ModelDir + '%d' % _config['seed']
if DBPath != None:
LogPath = OutDir + '/Logs/%d.txt' % _config['seed']
try: os.makedirs(os.path.dirname(LogPath))
except OSError as exc: pass
sys.stdout = open(LogPath, 'w+')
# Data Loader
data_loader = Data.Loader((Data.DomainType.SOURCE, SourceSpeakers),
(Data.DomainType.TARGET, TargetSpeakers))
# Load data
train_data, _ = data_loader.load_data(Data.SetType.TRAIN, WordsPerSpeaker, VideoNorm, True, AddChannel, DownSample)
valid_data, _ = data_loader.load_data(Data.SetType.VALID, WordsPerSpeaker, VideoNorm, True, AddChannel, DownSample)
# Create source & target datasets for all domain types
train_source_set = Data.Set(train_data[Data.DomainType.SOURCE], BatchSize, TruncateRemainder, Shuffle)
valid_source_set = Data.Set(valid_data[Data.DomainType.SOURCE], BatchSize, TruncateRemainder, Shuffle)
valid_target_set = Data.Set(valid_data[Data.DomainType.TARGET], BatchSize, TruncateRemainder, Shuffle)
# Memory cleanup
del data_loader, train_data, valid_data
# Adding classification layers
TrgSpec += '_FC{0}i_*PREDICT!sce'.format(enc.word_classes_count())
# Model Builder
builder = Model.Builder(InitStd)
# Adding placeholders for data
builder.add_placeholder(train_source_set.data_dtype, train_source_set.data_shape, 'MotFrames')
builder.add_placeholder(train_source_set.data_dtype, (None,) + feature_size, 'CntFrame')
builder.add_placeholder(train_source_set.target_dtype, train_source_set.target_shape, 'TrgWords')
seq_lens = builder.add_placeholder(tf.int32, [None], 'SeqLengths')
# Create network
mot = builder.add_specification('MOT', MotSpec, 'MotFrames', None)
mot.layers['CONVLSTM-8'].extra_params['SequenceLengthsTensor'] = seq_lens
mot.layers['MASKSEQ-9'].extra_params['MaskIndicesTensor'] = seq_lens - 1
builder.add_specification('CNT', CntSpec, 'CntFrame', None)
trg = builder.add_specification('TRG', TrgSpec, ['MOT-MASKSEQ-9/Output', 'CNT-MP-9/Output'], 'TrgWords')
builder.build_model()
# Setup Optimizer
optimizer = tf.train.AdamOptimizer(LearnRate)
# Feed Builder
def feed_builder(epoch, batch, training):
batch_size = batch.data.shape[0]
keys = builder.placeholders.values()
values = [batch.data,
batch.data[np.arange(batch_size),batch.data_lengths-1],
batch.data_targets,
batch.data_lengths]
return dict(zip(keys, values))
# Training
stopping_type = Model.StoppingType[EarlyStoppingCondition]
stopping_value = Model.StoppingValue[EarlyStoppingValue]
trainer = Model.Trainer(epochs=MaxEpochs,
optimizer=optimizer,
accuracy=trg.accuracy,
eval_losses={'Wrd': trg.loss},
tensorboard_path=TensorboardDir,
model_path=ModelDir)
trainer.init_session()
best_e, best_v = trainer.train(train_sets=[train_source_set],
valid_sets=[valid_source_set, valid_target_set],
batched_valid=True,
stopping_type=stopping_type,
stopping_value=stopping_value,
stopping_patience=EarlyStoppingPatience,
feed_builder=feed_builder)
test_result = trainer.test(test_sets=[valid_source_set, valid_target_set],
feed_builder=feed_builder,
batched=True)
if DBPath != None:
test_result = list(test_result[Data.SetType.VALID].values())
return best_e, list(test_result[0]), list(test_result[1])