-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
64 lines (54 loc) · 2.03 KB
/
main.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
--
-- Adapted from https://github.com/facebook/fb.resnet.torch/blob/master/main.lua
--
-- Train or test semantic segmentation model
--
require 'torch'
require 'paths'
require 'optim'
require 'nn'
local models = require 'models/init'
local Trainer = require 'train'
local opts = require 'opts'
local checkpoints = require 'checkpoints'
torch.setdefaulttensortype('torch.FloatTensor')
torch.setnumthreads(1)
local opt = opts.parse(arg)
torch.manualSeed(opt.manualSeed)
cutorch.manualSeedAll(opt.manualSeed)
-- resume from checkpoint if exists
local checkpoint, model, optimState = checkpoints.latest(opt)
local model, criterion = models.setup(opt, model)
local DataLoader = opt.nThreads == 1 and require 'dataloader' or require 'dataloader2'
local trainLoader, valLoader = DataLoader.create(opt)
local trainer = Trainer(model, criterion, opt, optimState)
if opt.testOnly then
local _, pixelAccuracy = trainer:test(0, valLoader, {})
print(string.format(' * Results pixel accuracy %.3f', pixelAccuracy))
return
end
local startEpoch = checkpoint and checkpoint.epoch + 1 or 1
local bestPixelAccuracy = checkpoint and checkpoint.bestPixelAccuracy or 0
local trainLog = checkpoint and checkpoint.trainLog or {}
local testLog = checkpoint and checkpoint.testLog or {}
for epoch = startEpoch, opt.nEpochs do
trainer:train(epoch, trainLoader, trainLog)
-- test on validation set after every train epoch
local _, testPixelAccuracy = trainer:test(epoch, valLoader, testLog)
local isBestModel = false
if testPixelAccuracy > bestPixelAccuracy then
isBestModel = true
bestPixelAccuracy = testPixelAccuracy
print(string.format(' * Best model pixel accuracy %.3f', testPixelAccuracy))
end
checkpoint = {
epoch = epoch,
trainLog = trainLog,
testLog = testLog,
bestPixelAccuracy = bestPixelAccuracy,
isBestModel = isBestModel,
}
checkpoints.save(trainer.lightModel, trainer.optimState, checkpoint, opt)
print('')
end
print(string.format(' * Finished pixel accuracy %.3f', bestPixelAccuracy))