-
Notifications
You must be signed in to change notification settings - Fork 822
/
core.py
234 lines (198 loc) · 9.21 KB
/
core.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author: kerlomz <[email protected]>
import sys
from config import RecurrentNetwork, RESIZE_MAP, CNNNetwork, Optimizer
from network.CNN import *
from network.MobileNet import MobileNetV2
from network.DenseNet import DenseNet
from network.GRU import GRU, BiGRU, GRUcuDNN
from network.LSTM import LSTM, BiLSTM, BiLSTMcuDNN, LSTMcuDNN
from network.ResNet import ResNet50, ResNetTiny
from network.utils import NetworkUtils
from optimizer.AdaBound import AdaBoundOptimizer
from optimizer.RAdam import RAdamOptimizer
from loss import *
from encoder import *
from decoder import *
from fc import *
import tensorflow as tf
tf.compat.v1.disable_v2_behavior()
tf.compat.v1.disable_eager_execution()
class NeuralNetwork(object):
"""
神经网络构建类
"""
def __init__(self, model_conf: ModelConfig, mode: RunMode, backbone: CNNNetwork, recurrent: RecurrentNetwork):
"""
:param model_conf: 模型配置
:param mode: 运行模式 (Trains/Validation/Predict)
:param backbone:
:param recurrent:
"""
self.model_conf = model_conf
self.decoder = Decoder(self.model_conf)
self.mode = mode
self.network = backbone
self.recurrent = recurrent
self.inputs = tf.keras.Input(dtype=tf.float32, shape=self.input_shape, name='input')
self.labels = tf.keras.Input(dtype=tf.int32, shape=[None], sparse=True, name='labels')
self.utils = NetworkUtils(mode)
self.merged_summary = None
self.optimizer = None
self.dataset_size = None
@property
def input_shape(self):
"""
:return: tuple/list 类型,输入的 Shape
"""
return RESIZE_MAP[self.model_conf.loss_func](*self.model_conf.resize) + [self.model_conf.image_channel]
def build_graph(self):
"""
在当前Session中构建网络计算图
"""
self._build_model()
def build_train_op(self, dataset_size=None):
self.dataset_size = dataset_size
self._build_train_op()
self.merged_summary = tf.compat.v1.summary.merge_all()
def _build_model(self):
"""选择采用哪种卷积网络"""
if self.network == CNNNetwork.CNN3:
x = CNN3(model_conf=self.model_conf, inputs=self.inputs, utils=self.utils).build()
elif self.network == CNNNetwork.CNN5:
x = CNN5(model_conf=self.model_conf, inputs=self.inputs, utils=self.utils).build()
elif self.network == CNNNetwork.CNNX:
x = CNNX(model_conf=self.model_conf, inputs=self.inputs, utils=self.utils).build()
elif self.network == CNNNetwork.ResNetTiny:
x = ResNetTiny(model_conf=self.model_conf, inputs=self.inputs, utils=self.utils).build()
elif self.network == CNNNetwork.ResNet50:
x = ResNet50(model_conf=self.model_conf, inputs=self.inputs, utils=self.utils).build()
elif self.network == CNNNetwork.DenseNet:
x = DenseNet(model_conf=self.model_conf, inputs=self.inputs, utils=self.utils).build()
elif self.network == CNNNetwork.MobileNetV2:
x = MobileNetV2(model_conf=self.model_conf, inputs=self.inputs, utils=self.utils).build()
else:
raise ValueError('This cnn neural network is not supported at this time.')
"""选择采用哪种循环网络"""
# time_major = True: [max_time_step, batch_size, num_classes]
tf.compat.v1.logging.info("CNN Output: {}".format(x.get_shape()))
self.seq_len = tf.compat.v1.fill([tf.shape(x)[0]], tf.shape(x)[1], name="seq_len")
if self.recurrent == RecurrentNetwork.NoRecurrent:
self.recurrent_network_builder = None
elif self.recurrent == RecurrentNetwork.LSTM:
self.recurrent_network_builder = LSTM(model_conf=self.model_conf, inputs=x, utils=self.utils)
elif self.recurrent == RecurrentNetwork.BiLSTM:
self.recurrent_network_builder = BiLSTM(model_conf=self.model_conf, inputs=x, utils=self.utils)
elif self.recurrent == RecurrentNetwork.GRU:
self.recurrent_network_builder = GRU(model_conf=self.model_conf, inputs=x, utils=self.utils)
elif self.recurrent == RecurrentNetwork.BiGRU:
self.recurrent_network_builder = BiGRU(model_conf=self.model_conf, inputs=x, utils=self.utils)
elif self.recurrent == RecurrentNetwork.LSTMcuDNN:
self.recurrent_network_builder = LSTMcuDNN(model_conf=self.model_conf, inputs=x, utils=self.utils)
elif self.recurrent == RecurrentNetwork.BiLSTMcuDNN:
self.recurrent_network_builder = BiLSTMcuDNN(model_conf=self.model_conf, inputs=x, utils=self.utils)
elif self.recurrent == RecurrentNetwork.GRUcuDNN:
self.recurrent_network_builder = GRUcuDNN(model_conf=self.model_conf, inputs=x, utils=self.utils)
else:
raise ValueError('This recurrent neural network is not supported at this time.')
logits = self.recurrent_network_builder.build() if self.recurrent_network_builder else x
if self.recurrent_network_builder and self.model_conf.loss_func != LossFunction.CTC:
raise ValueError('CTC loss must use recurrent neural network.')
"""输出层,根据Loss函数区分"""
with tf.keras.backend.name_scope('output'):
if self.model_conf.loss_func == LossFunction.CTC:
self.outputs = FullConnectedRNN(model_conf=self.model_conf, outputs=logits).build()
elif self.model_conf.loss_func == LossFunction.CrossEntropy:
self.outputs = FullConnectedCNN(model_conf=self.model_conf, outputs=logits).build()
return self.outputs
@property
def decay_steps(self):
if not self.dataset_size:
return 10000
return 10000
# epoch_step = int(self.dataset_size / self.model_conf.batch_size)
# return int(epoch_step / 4)
def _build_train_op(self):
"""构建训练操作符"""
# 步数
self.global_step = tf.compat.v1.train.get_or_create_global_step()
# Loss函数
if self.model_conf.loss_func == LossFunction.CTC:
self.loss = Loss.ctc(
labels=self.labels,
logits=self.outputs,
sequence_length=self.seq_len
)
elif self.model_conf.loss_func == LossFunction.CrossEntropy:
self.loss = Loss.cross_entropy(
labels=self.labels,
logits=self.outputs
)
self.cost = tf.reduce_mean(self.loss)
tf.compat.v1.summary.scalar('cost', self.cost)
# 学习率 指数衰减法
self.lrn_rate = tf.compat.v1.train.exponential_decay(
self.model_conf.trains_learning_rate,
self.global_step,
staircase=True,
decay_steps=self.decay_steps,
decay_rate=0.98,
)
tf.compat.v1.summary.scalar('learning_rate', self.lrn_rate)
if self.model_conf.neu_optimizer == Optimizer.AdaBound:
self.optimizer = AdaBoundOptimizer(
learning_rate=self.lrn_rate,
final_lr=0.001,
beta1=0.9,
beta2=0.999,
amsbound=True
)
elif self.model_conf.neu_optimizer == Optimizer.Adam:
self.optimizer = tf.compat.v1.train.AdamOptimizer(
learning_rate=self.lrn_rate
)
elif self.model_conf.neu_optimizer == Optimizer.RAdam:
self.optimizer = RAdamOptimizer(
learning_rate=self.lrn_rate,
warmup_proportion=0.1,
min_lr=1e-6
)
elif self.model_conf.neu_optimizer == Optimizer.Momentum:
self.optimizer = tf.compat.v1.train.MomentumOptimizer(
learning_rate=self.lrn_rate,
use_nesterov=True,
momentum=0.9,
)
elif self.model_conf.neu_optimizer == Optimizer.SGD:
self.optimizer = tf.compat.v1.train.GradientDescentOptimizer(
learning_rate=self.lrn_rate,
)
elif self.model_conf.neu_optimizer == Optimizer.AdaGrad:
self.optimizer = tf.compat.v1.train.AdagradOptimizer(
learning_rate=self.lrn_rate,
)
elif self.model_conf.neu_optimizer == Optimizer.RMSProp:
self.optimizer = tf.compat.v1.train.RMSPropOptimizer(
learning_rate=self.lrn_rate,
)
# BN 操作符更新(moving_mean, moving_variance)
update_ops = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.UPDATE_OPS)
# 将 train_op 和 update_ops 融合
with tf.control_dependencies(update_ops):
self.train_op = self.optimizer.minimize(
loss=self.cost,
global_step=self.global_step,
)
# 转录层-Loss函数
if self.model_conf.loss_func == LossFunction.CTC:
self.dense_decoded = self.decoder.ctc(
inputs=self.outputs,
sequence_length=self.seq_len
)
elif self.model_conf.loss_func == LossFunction.CrossEntropy:
self.dense_decoded = self.decoder.cross_entropy(
inputs=self.outputs
)
if __name__ == '__main__':
pass