-
Notifications
You must be signed in to change notification settings - Fork 2
/
imdb_get_batch.m
67 lines (57 loc) · 1.86 KB
/
imdb_get_batch.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
function imo = imdb_get_batch(images, varargin)
% CNN_IMAGENET_GET_BATCH Load, preprocess, and pack images for CNN evaluation
opts.imageSize = [227, 227] ;
opts.border = [29, 29] ;
opts.keepAspect = true ;
opts.numAugments = 1 ;
opts.transformation = 'none' ;
opts.averageImage = [] ;
opts.rgbVariance = zeros(0,3,'single') ;
opts.interpolation = 'bilinear' ;
opts.numThreads = 1 ;
opts.prefetch = false ;
opts.useSRCNN = false ;% added for SRCNN
opts.useDistill = 0 ;% added for Distilling multi-dataset
opts.noise_ratio = 0;
opts.numEpochs = 0;
opts = vl_argparse(opts, varargin);
im = cell(numel(images),1);
for i=1:numel(images)
im{i} = single(imread(images{i})) ;
end
if (~isempty(opts.rgbVariance) && isempty(opts.averageImage))
opts.averageImage = zeros(1,1,3) ;
end
if numel(opts.averageImage) == 3
opts.averageImage = reshape(opts.averageImage, 1,1,3) ;
end
imo = zeros(opts.imageSize(1), opts.imageSize(2), 3, ...
numel(images)*opts.numAugments, 'single') ;
for i=1:numel(images)
% acquire image
imt = im{i} ;
if size(imt,3) == 1
imt = cat(3, imt, imt, imt) ;
end
% resize
w_ori = size(imt,2) ;
h_ori = size(imt,1) ;
factor = [(opts.imageSize(1)+opts.border(1))/h_ori ...
(opts.imageSize(2)+opts.border(2))/w_ori];
if opts.keepAspect
factor = max(factor) ;
imt = imresize(imt, 'scale', factor) ; % use bicubic default
else
imt = imresize(imt, opts.imageSize(1:2)) ; % use bicubic default
end
% crop & flip
if (~isempty(opts.averageImage) && ~opts.useSRCNN)
offset = opts.averageImage ;
if ~isempty(opts.rgbVariance)
offset = bsxfun(@plus, offset, reshape(opts.rgbVariance * randn(3,1), 1,1,3)) ;
end
imo(:,:,:,i) = bsxfun(@minus, imt, offset) ;
else
imo(:,:,:,i) = imt ;
end
end