-
Notifications
You must be signed in to change notification settings - Fork 120
/
getBatch_LapSRN.m
112 lines (88 loc) · 3.01 KB
/
getBatch_LapSRN.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
function inputs = getBatch_LapSRN(opts, imdb, batch, mode)
% -------------------------------------------------------------------------
% Description:
% get one batch for training LapSRN
%
% Input:
% - opts : options generated from init_opts()
% - imdb : imdb file generated from make_imdb()
% - batch : array of ID to fetch
% - mode : 'train' or 'val'
%
% Output:
% - inputs: input for dagnn (include LR and HR images)
%
% Citation:
% Deep Laplacian Pyramid Networks for Fast and Accurate Super-Resolution
% Wei-Sheng Lai, Jia-Bin Huang, Narendra Ahuja, and Ming-Hsuan Yang
% IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 2017
%
% Contact:
% Wei-Sheng Lai
% University of California, Merced
% -------------------------------------------------------------------------
%% get images
image_batch = imdb.images.img(batch);
%% crop patches
HR = zeros(opts.patch_size, opts.patch_size, 1, length(batch), 'single');
for i = 1:length(batch)
img = image_batch{i};
ratio = 1;
if( opts.scale_augmentation && strcmp(mode, 'train') )
% randomly resize between 0.5 ~ 1.0
ratio = randi([5, 10]) * 0.1;
end
eps = 1e-3;
% min width/height should be larger than patch size
if size(img, 1) < size(img, 2)
if size(img, 1) * ratio < opts.patch_size
ratio = opts.patch_size / size(img, 1) + eps;
end
else
if size(img, 2) * ratio < opts.patch_size
ratio = opts.patch_size / size(img, 2) + eps;
end
end
img = imresize(img, ratio);
% random crop
H = size(img, 1);
W = size(img, 2);
y = randi(H - opts.patch_size + 1);
x = randi(W - opts.patch_size + 1);
HR(:, :, :, i) = img(y : y + opts.patch_size - 1, x : x + opts.patch_size - 1, :);
end
%% data augmentation
if( opts.data_augmentation && strcmp(mode, 'train') )
% rotate
rotate = rand;
if( rotate < 0.25 )
HR = rot90(HR, 1);
elseif( rotate < 0.5 )
HR = rot90(HR, 2);
elseif( rotate < 0.75 )
HR = rot90(HR, 3);
end
% horizontally flip
if( rand > 0.5 )
HR = fliplr(HR);
end
end % end of data augmentation
%% dagnn input
inputs = {};
inputs{end+1} = 'level1_HR';
inputs{end+1} = HR;
for i = 2 : opts.level
ratio = 1 / 2^(i - 1);
inputs{end+1} = sprintf('level%d_HR', i);
inputs{end+1} = imresize(HR, ratio);
end
inputs{end+1} = 'LR';
inputs{end+1} = imresize(HR, 1 / opts.scale);
%% convert to GPU array
if( opts.gpu > 0 )
for i = 2:2:length(inputs)
inputs{i} = gpuArray(inputs{i});
end
end
end