-
Notifications
You must be signed in to change notification settings - Fork 73
/
Network.lua
217 lines (182 loc) · 7.06 KB
/
Network.lua
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
require 'optim'
require 'nnx'
require 'gnuplot'
require 'lfs'
require 'xlua'
require 'UtilsMultiGPU'
require 'Loader'
require 'nngraph'
require 'Mapper'
require 'ModelEvaluator'
local suffix = '_' .. os.date('%Y%m%d_%H%M%S')
local threads = require 'threads'
local Network = {}
--Training parameters
seed = 10
torch.setdefaulttensortype('torch.FloatTensor')
torch.manualSeed(seed)
function Network:init(opt)
self.fileName = opt.saveFileName
self.nGPU = opt.nGPU
self.gpu = self.nGPU > 0
if not self.gpu then
require 'rnn'
else
require 'cutorch'
require 'cunn'
require 'cudnn'
require 'BatchBRNNReLU'
cutorch.manualSeedAll(seed)
end
self.trainingSetLMDBPath = opt.trainingSetLMDBPath
self.validationSetLMDBPath = opt.validationSetLMDBPath
self.logsTrainPath = opt.logsTrainPath or nil
self.logsValidationPath = opt.logsValidationPath or nil
self.modelTrainingPath = opt.modelTrainingPath or nil
self.permuteBatch = opt.permuteBatch or false
self:makeDirectories({ self.logsTrainPath, self.logsValidationPath, self.modelTrainingPath })
self.mapper = Mapper(opt.dictionaryPath)
self.tester = ModelEvaluator(self.gpu, self.validationSetLMDBPath, self.mapper,
opt.validationBatchSize, self.logsValidationPath)
self.loadModel = opt.loadModel
self.epochSave = opt.epochSave or false -- Saves model every number of iterations.
self.maxNorm = opt.maxNorm or 400 -- value chosen by Baidu for english speech.
-- setting model saving/loading
if self.loadModel then
assert(opt.loadPath, "loadPath hasn't been given to load model.")
self:loadNetwork(opt.loadPath, opt.modelName)
else
assert(opt.modelName, "Must have given a model to train.")
self:prepSpeechModel(opt.modelName, opt)
end
-- setting online loading
self.indexer = indexer(opt.trainingSetLMDBPath, opt.batchSize)
self.pool = threads.Threads(1, function() require 'Loader' end)
self.logger = optim.Logger(self.logsTrainPath .. 'train' .. suffix .. '.log')
self.logger:setNames { 'loss', 'WER', 'CER' }
self.logger:style { '-', '-', '-' }
end
function Network:prepSpeechModel(modelName, opt)
local model = require(modelName)
self.model = model[1](opt)
self.calSize = model[2]
end
function Network:testNetwork(epoch)
self.model:evaluate()
local wer, cer = self.tester:runEvaluation(self.model, true, epoch or 1) -- details in log
self.model:zeroGradParameters()
self.model:training()
return wer, cer
end
function Network:trainNetwork(epochs, optimizerParams)
self.model:training()
local lossHistory = {}
local validationHistory = {}
local criterion = nn.CTCCriterion(true)
local x, gradParameters = self.model:getParameters()
print("Number of parameters: ", gradParameters:size(1))
-- inputs (preallocate)
local inputs = torch.Tensor()
local sizes = torch.Tensor()
if self.gpu then
criterion = criterion:cuda()
inputs = inputs:cuda()
sizes = sizes:cuda()
end
-- def loading buf and loader
local loader = Loader(self.trainingSetLMDBPath, self.mapper)
local specBuf, labelBuf, sizesBuf
-- load first batch
local inds = self.indexer:nextIndices()
self.pool:addjob(function()
return loader:nextBatch(inds)
end,
function(spect, label, sizes)
specBuf = spect
labelBuf = label
sizesBuf = sizes
end)
-- define the feval
local function feval(x_new)
self.pool:synchronize() -- wait previous loading
local inputsCPU, sizes, targets = specBuf, sizesBuf, labelBuf -- move buf to training data
inds = self.indexer:nextIndices() -- load next batch whilst training
self.pool:addjob(function()
return loader:nextBatch(inds)
end,
function(spect, label, sizes)
specBuf = spect
labelBuf = label
sizesBuf = sizes
end)
inputs:resize(inputsCPU:size()):copy(inputsCPU) -- transfer over to GPU
sizes = self.calSize(sizes)
local predictions = self.model:forward(inputs)
local loss = criterion:forward(predictions, targets, sizes)
if loss == math.huge or loss == -math.huge then loss = 0 print("Recieved an inf cost!") end
self.model:zeroGradParameters()
local gradOutput = criterion:backward(predictions, targets)
self.model:backward(inputs, gradOutput)
local norm = gradParameters:norm()
if norm > self.maxNorm then
gradParameters:mul(self.maxNorm / norm)
end
return loss, gradParameters
end
-- training
local currentLoss
local startTime = os.time()
for i = 1, epochs do
local averageLoss = 0
for j = 1, self.indexer.nbOfBatches do
currentLoss = 0
local _, fs = optim.sgd(feval, x, optimizerParams)
if self.gpu then cutorch.synchronize() end
currentLoss = currentLoss + fs[1]
xlua.progress(j, self.indexer.nbOfBatches)
averageLoss = averageLoss + currentLoss
end
if self.permuteBatch then self.indexer:permuteBatchOrder() end
averageLoss = averageLoss / self.indexer.nbOfBatches -- Calculate the average loss at this epoch.
-- anneal learningRate
optimizerParams.learningRate = optimizerParams.learningRate / (optimizerParams.learningRateAnnealing or 1)
-- Update validation error rates
local wer, cer = self:testNetwork(i)
print(string.format("Training Epoch: %d Average Loss: %f Average Validation WER: %.2f Average Validation CER: %.2f",
i, averageLoss, 100 * wer, 100 * cer))
table.insert(lossHistory, averageLoss) -- Add the average loss value to the logger.
table.insert(validationHistory, 100 * wer)
self.logger:add { averageLoss, 100 * wer, 100 * cer }
-- periodically save the model
if self.epochSave then
print("Saving model..")
self:saveNetwork(self.modelTrainingPath .. 'model_epoch_' .. i .. suffix .. '_' .. self.fileName)
end
end
local endTime = os.time()
local secondsTaken = endTime - startTime
local minutesTaken = secondsTaken / 60
print("Minutes taken to train: ", minutesTaken)
print("Saving model..")
self:saveNetwork(self.modelTrainingPath .. 'final_model_' .. suffix .. '_' .. self.fileName)
return lossHistory, validationHistory, minutesTaken
end
function Network:createLossGraph()
self.logger:plot()
end
function Network:saveNetwork(saveName)
self.model:clearState()
saveDataParallel(saveName, self.model)
end
--Loads the model into Network.
function Network:loadNetwork(saveName, modelName)
self.model = loadDataParallel(saveName, self.nGPU)
local model = require(modelName)
self.calSize = model[2]
end
function Network:makeDirectories(folderPaths)
for index, folderPath in ipairs(folderPaths) do
if (folderPath ~= nil) then os.execute("mkdir -p " .. folderPath) end
end
end
return Network