-
Notifications
You must be signed in to change notification settings - Fork 68
/
main_training.m
182 lines (116 loc) · 5.07 KB
/
main_training.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
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
%% training code
% Author: Mahmoud Afifi
% Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
% Please cite our paper:
% Mahmoud Afifi, Konstantinos G. Derpanis, Björn Ommer, and Michael S
% Brown. Learning Multi-Scale Photo Exposure Correction, In CVPR 2021.
%%
clc
clear;
close all;
lR = 10^-4; % initial learning rate
chnls = 16; % number of channels of 1st layere of the encoder for the highest pyramid level
convvfilter = 3; % conv kernel size
encoderDecoderDepth = 3; % numbere of layers (i.e., levels) for the highest pyramid level
trainingImgsNum = 0; %if 0, then load all training images
withDiscriminator = 1; % include discriminator loss term?
for ps = [128, 256, 512] % for each patch size, do
% please, update training/validation directories accordingly
In_Tr_datasetDir = fullfile('exposure_dataset','training',sprintf('INPUT_IMAGES_P_%d',ps)); % input training patches with size ps size
GT_Tr_datasetDir = fullfile('exposure_dataset','training',sprintf('GT_IMAGES_P_%d',ps)); % ground truth training patches with size ps
In_Vl_datasetDir = fullfile('exposure_dataset','validation',sprintf('INPUT_IMAGES_P_%d',ps)); % validation
GT_Vl_datasetDir = fullfile('exposure_dataset','validation',sprintf('GT_IMAGES_P_%d',ps));
patchSize = [ps, ps, 12]; % 3 color channels x 4 pyramid levels
switch ps
case 128
dropRate = 20; % drop learning rate
checkpoint_period = 10; % bkup every checkpoint_period
epochs = 40; % number of epochs
miniBatch = 32; % mini-batch size
chkpoint = ''; % start training from scratch -- no chkpoint
if withDiscriminator == 1
chkpoint_d = '';
end
validationImgsNum = 2000; % number of validation patches
vlFreq = 5612 *2; % every vlFreq iterations, do validation
case 256
dropRate = 10;
checkpoint_period = 5;
epochs = 30;
miniBatch = 8;
chkpoint = sprintf('model_%d.mat',ps/2);
if withDiscriminator == 1
chkpoint_d = '';
end
validationImgsNum = 1000;
vlFreq = 13230 *2;
case 512
dropRate = 5;
checkpoint_period = 5;
epochs = 20;
miniBatch = 4;
chkpoint = sprintf('model_%d.mat',ps/2);
if withDiscriminator == 1
chkpoint_d = sprintf('D_model_%d.mat',ps/2);
end
validationImgsNum = 500;
vlFreq = 17378 *2;
otherwise
error('wrong ps value');
end
checkpoint_dir = sprintf('%dx%d_reports_and_backup_%s',ps,ps,date);
GPUDevice = 1;
modelName = sprintf('model_%d.mat',ps);
if withDiscriminator == 1
D_modelName = sprintf('D_model_%d.mat',ps);
end
fprintf('Preparing training data ...\n');
[Trdata,Vldata] = getTr_Vl_data(In_Tr_datasetDir, GT_Tr_datasetDir, ...
In_Vl_datasetDir, GT_Vl_datasetDir, trainingImgsNum, ...
validationImgsNum, patchSize(1:2),...
miniBatch);
options = get_trainingOptions(epochs,miniBatch,lR,...
checkpoint_dir,Vldata,GPUDevice, checkpoint_period, ...
vlFreq, dropRate);
if strcmp(chkpoint,'')
fprintf('Creating the generator model ...\n');
net = create_generator(patchSize, encoderDecoderDepth, chnls, convvfilter);
else
fprintf('Loading the generator model ...\n');
load(chkpoint);
inLayer = imageInputLayer(patchSize,'Name','InputLayer',...
'Normalization','none');
net = layerGraph(net);
net=replaceLayer(net,'InputLayer',inLayer);
net = dlnetwork(net);
end
%define/load the discriminator
if withDiscriminator == 1
if strcmp(chkpoint_d,'')
fprintf('Creating the discriminator model ...\n');
[D] = createDiscriminator();
else
fprintf('Loading the discriminator model ...\n');
load(chkpoint_d);
end
end
fprintf('Starting training ...\n');
if withDiscriminator == 1
switch ps
case 128
[net, D] = train_network(Trdata,net,[], options);
case 256
[net, D] = train_network(Trdata,net,D, options,15);
case 512
[net, D] = train_network(Trdata,net,D, options,5);
end
else
[net, ~] = train_network(Trdata,net,[], options);
end
disp('Done!');
disp('Saving model!');
save(modelName,'net','-v7.3');
if withDiscriminator == 1
save(D_modelName,'D','-v7.3');
end
end