-
Notifications
You must be signed in to change notification settings - Fork 123
/
fcnTrain.m
107 lines (91 loc) · 3.42 KB
/
fcnTrain.m
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
function fcnTrain(varargin)
%FNCTRAIN Train FCN model using MatConvNet
run matconvnet/matlab/vl_setupnn ;
addpath matconvnet/examples ;
% experiment and data paths
opts.expDir = 'data/fcn32s-voc11' ;
opts.dataDir = 'data/voc11' ;
opts.modelType = 'fcn32s' ;
opts.sourceModelPath = 'data/models/imagenet-vgg-verydeep-16.mat' ;
[opts, varargin] = vl_argparse(opts, varargin) ;
% experiment setup
opts.imdbPath = fullfile(opts.expDir, 'imdb.mat') ;
opts.imdbStatsPath = fullfile(opts.expDir, 'imdbStats.mat') ;
opts.vocEdition = '11' ;
opts.vocAdditionalSegmentations = true ;
opts.numFetchThreads = 1 ; % not used yet
% training options (SGD)
opts.train = struct([]) ;
[opts, varargin] = vl_argparse(opts, varargin) ;
trainOpts.batchSize = 20 ;
trainOpts.numSubBatches = 10 ;
trainOpts.continue = true ;
trainOpts.gpus = [] ;
trainOpts.prefetch = true ;
trainOpts.expDir = opts.expDir ;
trainOpts.learningRate = 0.0001 * ones(1,50) ;
trainOpts.numEpochs = numel(trainOpts.learningRate) ;
% -------------------------------------------------------------------------
% Setup data
% -------------------------------------------------------------------------
% Get PASCAL VOC 12 segmentation dataset plus Berkeley's additional
% segmentations
if exist(opts.imdbPath)
imdb = load(opts.imdbPath) ;
else
imdb = vocSetup('dataDir', opts.dataDir, ...
'edition', opts.vocEdition, ...
'includeTest', false, ...
'includeSegmentation', true, ...
'includeDetection', false) ;
if opts.vocAdditionalSegmentations
imdb = vocSetupAdditionalSegmentations(imdb, 'dataDir', opts.dataDir) ;
end
mkdir(opts.expDir) ;
save(opts.imdbPath, '-struct', 'imdb') ;
end
% Get training and test/validation subsets
train = find(imdb.images.set == 1 & imdb.images.segmentation) ;
val = find(imdb.images.set == 2 & imdb.images.segmentation) ;
% Get dataset statistics
if exist(opts.imdbStatsPath)
stats = load(opts.imdbStatsPath) ;
else
stats = getDatasetStatistics(imdb) ;
save(opts.imdbStatsPath, '-struct', 'stats') ;
end
% -------------------------------------------------------------------------
% Setup model
% -------------------------------------------------------------------------
% Get initial model from VGG-VD-16
net = fcnInitializeModel('sourceModelPath', opts.sourceModelPath) ;
if any(strcmp(opts.modelType, {'fcn16s', 'fcn8s'}))
% upgrade model to FCN16s
net = fcnInitializeModel16s(net) ;
end
if strcmp(opts.modelType, 'fcn8s')
% upgrade model fto FCN8s
net = fcnInitializeModel8s(net) ;
end
net.meta.normalization.rgbMean = stats.rgbMean ;
net.meta.classes = imdb.classes.name ;
% -------------------------------------------------------------------------
% Train
% -------------------------------------------------------------------------
% Setup data fetching options
bopts.numThreads = opts.numFetchThreads ;
bopts.labelStride = 1 ;
bopts.labelOffset = 1 ;
bopts.classWeights = ones(1,21,'single') ;
bopts.rgbMean = stats.rgbMean ;
bopts.useGpu = numel(opts.train.gpus) > 0 ;
% Launch SGD
info = cnn_train_dag(net, imdb, getBatchWrapper(bopts), ...
trainOpts, ....
'train', train, ...
'val', val, ...
opts.train) ;
% -------------------------------------------------------------------------
function fn = getBatchWrapper(opts)
% -------------------------------------------------------------------------
fn = @(imdb,batch) getBatch(imdb,batch,opts,'prefetch',nargout==0) ;