-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathmain.lua
executable file
·92 lines (67 loc) · 2.43 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
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
-- usage example: DATA_ROOT=/path/to/data/ which_direction=BtoA name=expt1 th train.lua
--
-- code derived from https://github.com/soumith/dcgan.torch
--
require 'torch'
require 'nn'
require 'optim'
--util = paths.dofile('../util/util.lua')
require 'image'
--require 'models'
local opts = require 'opts'
local opt = opts.parse(arg)
package.path = '../util/lua/?.lua;' .. package.path
require 'ginit' (opt)
opts.init(opt)
----------------------------------------------------
-- Make directories
require 'paths'
paths.mkdir('../gen')
paths.mkdir('../checkpoint')
----------------------------------------------------
local models = require 'models/init'
local DataLoader = require('models/' .. opt.loaderType .. '-dataloader')
local checkpoints = require 'checkpoints'
----------------------------------------------------
-- Load previous checkpoint, if it exists
print('=> Checking checkpoints')
local checkpoint, optimState = checkpoints.load(opt)
-- Create model
print('=> Setting up model')
local model, criterion = models.setup(opt, checkpoint)
-- Data loading
print('=> Setting up data loader')
local trainLoader, valLoader = DataLoader.create(opt)
-- The trainer handles the training loop and evaluation on validation set
print('=> Loading trainer')
local Trainer = require('models/' .. opt.trainerType .. '-train')
local trainer = Trainer(model, criterion, opt, optimState)
if opt.valOnly then
local loss = trainer:test(0, valLoader)
print(string.format(' * Results Err %1.4f', loss))
return
end
local loggerLoss = optim.Logger(paths.concat(opt.resume, 'loss.log'))
loggerLoss:setNames{'Training', 'Test'}
--local startEpoch = checkpoint and checkpoint.epoch + 1 or math.max(1, opt.epochNumber)
local startEpoch = checkpoint and checkpoint.epoch + 1 or 1
local bestLoss = math.huge
for epoch = startEpoch, opt.nEpochs do
-- Train for a single epoch
local trainLoss = trainer:train(epoch, trainLoader)
--checkpoints.save(epoch, model, trainer.optimState, true, opt)
-- Run model on validation set
local testLoss = trainer:test(epoch, valLoader)
--testLoss = 0
local bestModel = false
if testLoss < bestLoss then
bestModel = true
bestLoss = testLoss
print(' * Best model ', testLoss)
end
checkpoints.save(epoch, model, trainer.optimState, bestModel, opt)
loggerLoss:add({trainLoss, testLoss})
end
print(string.format(' * Finished Err %1.4f', bestLoss))
loggerLoss:style({'+-', '+-'})
loggerLoss:plot()