-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcnRun_satellite.m
169 lines (140 loc) · 4.65 KB
/
fcnRun_satellite.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
function info = fcnRun_satellite(varargin)
run matconvnet/matlab/vl_setupnn;
addpath matconvnet/examples;
% experiment and data paths
opts.expDir = 'data/modelzoo/satellite_UgandaSST_test';
opts.dataDir = 'data/satellite/UgandaSST_jpgs';
opts.modelPath = 'data/fcn8s-satellite/net-epoch-3.mat';
opts.modelFamily = 'matconvnet';
[opts, varargin] = vl_argparse(opts, varargin);
% experiment setup
opts.imdbPath = fullfile(opts.expDir, 'imdb.mat') ;
opts.vocEdition = '11' ;
opts.vocAdditionalSegmentations = true ;
opts.vocAdditionalSegmentationsMergeMode = 2 ;
opts.gpus = [1] ;
opts = vl_argparse(opts, varargin) ;
resPath = fullfile(opts.expDir, 'results.mat') ;
if exist(resPath)
info = load(resPath) ;
return ;
end
if ~isempty(opts.gpus)
gpuDevice(opts.gpus(1))
end
% -------------------------------------------------------------------------
% Setup data
% -------------------------------------------------------------------------
% Get PASCAL VOC 11/12 segmentation dataset plus Berkeley's additional
% segmentations
if exist(opts.imdbPath)
imdb = load(opts.imdbPath) ;
% run specific variables
imdb.paths.imagesToRun = 'data/satellite/UgandaSST_jpgs/%s.jpg';
imdb.paths.imageDir = 'data/satellite/UgandaSST_jpgs';
imdb.images.run = dir(imdb.paths.imageDir);
imdb.images.run = imdb.images.run(3:end);
else
disp('IMDB.MAT NOT IN CORRECT MODELZOO DIRECTORY')
end
% Get validation subset
val = find(imdb.images.set == 2 & imdb.images.segmentation) ;
disp('loggins')
% -------------------------------------------------------------------------
% Setup model
% -------------------------------------------------------------------------
switch opts.modelFamily
case 'matconvnet'
net = load(opts.modelPath) ;
net = dagnn.DagNN.loadobj(net.net) ;
net.mode = 'test' ;
for name = {'objective', 'accuracy'}
net.removeLayer(name) ;
end
net.meta.normalization.averageImage = reshape(net.meta.normalization.rgbMean,1,1,3) ;
predVar = net.getVarIndex('prediction') ;
inputVar = 'input' ;
imageNeedsToBeMultiple = true ;
case 'ModelZoo'
net = dagnn.DagNN.loadobj(load(opts.modelPath)) ;
net.mode = 'test' ;
predVar = net.getVarIndex('upscore') ;
inputVar = 'data' ;
imageNeedsToBeMultiple = false ;
case 'TVG'
net = dagnn.DagNN.loadobj(load(opts.modelPath)) ;
net.mode = 'test' ;
predVar = net.getVarIndex('coarse') ;
inputVar = 'data' ;
imageNeedsToBeMultiple = false ;
end
if ~isempty(opts.gpus)
gpuDevice(opts.gpus(1)) ;
net.move('gpu') ;
end
net.mode = 'test' ;
% -------------------------------------------------------------------------
% Train
% -------------------------------------------------------------------------
disp(size(imdb.images.run,1))
fileExt = '.jpg';
for i = 1:size(imdb.images.run,1)
[~, resultname, ~] = fileparts(imdb.images.run(i).name);
resultpath = fullfile(opts.expDir, [resultname '.png']);
if ~exist(resultpath,'file')
name = regexprep(imdb.images.run(i).name,fileExt,'') ;
rgbPath = sprintf(imdb.paths.imagesToRun, name) ;
% Load an image and gt segmentation
rgb = vl_imreadjpeg({rgbPath}) ;
rgb = rgb{1} ;
% Subtract the mean (color)
im = bsxfun(@minus, single(rgb), net.meta.normalization.averageImage) ;
% Soome networks requires the image to be a multiple of 32 pixels
if imageNeedsToBeMultiple
sz = [size(im,1), size(im,2)] ;
sz_ = round(sz / 32)*32 ;
im_ = imresize(im, sz_) ;
else
im_ = im ;
end
if ~isempty(opts.gpus)
im_ = gpuArray(im_) ;
end
net.eval({inputVar, im_}) ;
scores_ = gather(net.vars(predVar).value) ;
[~,pred_] = max(scores_,[],3) ;
if imageNeedsToBeMultiple
pred = imresize(pred_, sz, 'method', 'nearest') ;
else
pred = pred_ ;
end
% Plots
if mod(i - 1,1) == 0 || i == numel(val)
% Print segmentation
% figure(100) ;clf ;
% displayImage(rgb/255, '', pred) ;
% drawnow ;
disp(name);
% Save segmentation
imPath = fullfile(opts.expDir, [name '.png']) ;
imwrite(pred,labelColors(),imPath,'png');
end
end
end
disp('done!')
% -------------------------------------------------------------------------
function cmap = labelColors()
% -------------------------------------------------------------------------
N=21;
cmap = zeros(N,3);
for i=1:N
id = i-1; r=0;g=0;b=0;
for j=0:7
r = bitor(r, bitshift(bitget(id,1),7 - j));
g = bitor(g, bitshift(bitget(id,2),7 - j));
b = bitor(b, bitshift(bitget(id,3),7 - j));
id = bitshift(id,-3);
end
cmap(i,1)=r; cmap(i,2)=g; cmap(i,3)=b;
end
cmap = cmap / 255;