-
Notifications
You must be signed in to change notification settings - Fork 4
/
flownet_get_batch.m
97 lines (85 loc) · 3.39 KB
/
flownet_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
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
function inputs = flownet_get_batch(fldb, batch, varargin)
% FLOWNET_GET_BATCH Construct input
%
% Hang Su
opts.multiplier = 64;
opts.interpolation = 'bilinear' ;
opts.averageImage = single([123.6591 116.7663 103.9318]) ;
opts.convertFlowFmt = '';
opts.imageSize = [384 512] ;
opts.mode = 'random' ; % 'random', 'fixed'
opts.subwins = [1 1 1] ;
opts.scaleRange = [0.8 1.2] ;
opts.nSamples = 4 ;
[opts,~] = vl_argparse(opts,varargin);
% initialization
N = get_subwins([],[],opts.subwins);
inputs{1} = 'im1';
inputs{3} = 'im2';
inputs{5} = 'gt';
inputs{2} = zeros([opts.imageSize(1), opts.imageSize(2), 3, numel(batch)*N], 'single');
inputs{4} = zeros([opts.imageSize(1), opts.imageSize(2), 3, numel(batch)*N], 'single');
inputs{6} = zeros([opts.imageSize(1), opts.imageSize(2), 2, numel(batch)*N], 'single');
% Map: id --> <inputid, idx>
loadedImages = containers.Map('KeyType','int32','ValueType','any');
[Y, I] = ismember(batch,fldb.flows.id); assert(all(Y(:)));
[Y, I1] = ismember(fldb.flows.im1,fldb.frames.id); assert(all(Y(:)));
[Y, I2] = ismember(fldb.flows.im2,fldb.frames.id); assert(all(Y(:)));
for i=1:numel(batch),
% load images and flows
idx = I(i);
im1_id = fldb.flows.im1(idx);
im2_id = fldb.flows.im2(idx);
if loadedImages.isKey(im1_id),
im1 = loadedImages(im1_id);
else
imPath = fullfile(fldb.rootDir,fldb.frames.name{I1(idx)});
im1 = imread_255(imPath,3);
loadedImages(im1_id) = im1;
end
if loadedImages.isKey(im2_id),
im2 = loadedImages(im2_id);
else
imPath = fullfile(fldb.rootDir,fldb.frames.name{I2(idx)});
im2 = imread_255(imPath,3);
loadedImages(im2_id) = im2;
end
flo = readFlowFile(fullfile(fldb.rootDir,fldb.flows.name{idx}));
assert(all(size(im1)==size(im2)) && ...
all(size(im1(:,:,1))==size(flo(:,:,1))));
% compute subwins according to translation/scaling/flipping/rotation
% TODO NOT_YET_IMPLEMENTED
if strcmpi(opts.mode,'fixed'),
subwins = get_subwins([size(im1,1) size(im1,2)],opts.imageSize,opts.subwins);
elseif strcmpi(opts.mode,'random'),
subwins = get_subwins([size(im1,1) size(im1,2)],opts.imageSize,opts.scaleRange,opts.nSamples);
end
% obtain actual crops according to subwins
% TODO NOT_YET_IMPLEMENTED
for j=1:size(subwins,2),
if subwins(5,j)~=1,
im1_rs = imresize(im1,round(size(im1(:,:,1))*subwins(5,j)),...
'method',opts.interpolation);
im2_rs = imresize(im2,round(size(im2(:,:,1))*subwins(5,j)),...
'method',opts.interpolation);
flo_rs = imresize(flo,round(size(flo(:,:,1))*subwins(5,j)),...
'method',opts.interpolation);
end
inputs{2}(:,:,:,(i-1)*N+j) = im1_rs(subwins(2,j):subwins(2,j)+subwins(4,j)-1,...
subwins(1,j):subwins(1,j)+subwins(3,j)-1,:);
inputs{4}(:,:,:,(i-1)*N+j) = im2_rs(subwins(2,j):subwins(2,j)+subwins(4,j)-1,...
subwins(1,j):subwins(1,j)+subwins(3,j)-1,:);
flo_rs = flo_rs.*subwins(5,j);
inputs{6}(:,:,:,(i-1)*N+j) = flo_rs(subwins(2,j):subwins(2,j)+subwins(4,j)-1,...
subwins(1,j):subwins(1,j)+subwins(3,j)-1,:);
end
% rgb perturbation
% TODO NOT_YET_IMPLEMENTED
% deduct average pixel values
inputs{2} = bsxfun(@minus,inputs{2},reshape(opts.averageImage,[1 1 3 1]));
inputs{4} = bsxfun(@minus,inputs{4},reshape(opts.averageImage,[1 1 3 1]));
end
if ~isemtpy(opts.convertFlowFmt) && ~strcmpi(opts.convertFlowFmt,'none'),
inputs{6} = flow_convert(inputs{6},'cartesian',opts.convertFlowFmt);
end
end