-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
265 lines (203 loc) · 10.9 KB
/
utils.py
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import os
import torch
import shutil
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import math
from Data import CustomDataLoader
import cv2
cmap = plt.cm.viridis
def rgb2grayscale(rgb):
return rgb[:, :, 0] * 0.2989 + rgb[:, :, 1] * 0.587 + rgb[:, :, 2] * 0.114
class DenseToSparse:
def __init__(self):
pass
def dense_to_sparse(self, rgb, depth):
pass
def __repr__(self):
pass
class UniformSampling(DenseToSparse):
name = "uar"
def __init__(self, num_samples, max_depth=np.inf):
DenseToSparse.__init__(self)
self.num_samples = num_samples
self.max_depth = max_depth
def __repr__(self):
return "%s{ns=%d,md=%f}" % (self.name, self.num_samples, self.max_depth)
def dense_to_sparse(self, rgb, depth):
"""
Samples pixels with `num_samples`/#pixels probability in `depth`.
Only pixels with a maximum depth of `max_depth` are considered.
If no `max_depth` is given, samples in all pixels
"""
mask_keep = depth > 0
if self.max_depth is not np.inf:
mask_keep = np.bitwise_and(mask_keep, depth <= self.max_depth)
n_keep = np.count_nonzero(mask_keep)
if n_keep == 0:
return mask_keep
else:
prob = float(self.num_samples) / n_keep
return np.bitwise_and(mask_keep, np.random.uniform(0, 1, depth.shape) < prob)
class SimulatedStereo(DenseToSparse):
name = "sim_stereo"
def __init__(self, num_samples, max_depth=np.inf, dilate_kernel=3, dilate_iterations=1):
DenseToSparse.__init__(self)
self.num_samples = num_samples
self.max_depth = max_depth
self.dilate_kernel = dilate_kernel
self.dilate_iterations = dilate_iterations
def __repr__(self):
return "%s{ns=%d,md=%f,dil=%d.%d}" % \
(self.name, self.num_samples, self.max_depth, self.dilate_kernel, self.dilate_iterations)
# We do not use cv2.Canny, since that applies non max suppression
# So we simply do
# RGB to intensitities
# Smooth with gaussian
# Take simple sobel gradients
# Threshold the edge gradient
# Dilatate
def dense_to_sparse(self, rgb, depth):
gray = rgb2grayscale(rgb)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
gx = cv2.Sobel(blurred, cv2.CV_64F, 1, 0, ksize=5)
gy = cv2.Sobel(blurred, cv2.CV_64F, 0, 1, ksize=5)
depth_mask = np.bitwise_and(depth != 0.0, depth <= self.max_depth)
edge_fraction = float(self.num_samples) / np.size(depth)
mag = cv2.magnitude(gx, gy)
min_mag = np.percentile(mag[depth_mask], 100 * (1.0 - edge_fraction))
mag_mask = mag >= min_mag
if self.dilate_iterations >= 0:
kernel = np.ones((self.dilate_kernel, self.dilate_kernel), dtype=np.uint8)
cv2.dilate(mag_mask.astype(np.uint8), kernel, iterations=self.dilate_iterations)
mask = np.bitwise_and(mag_mask, depth_mask)
return mask
def parse_command():
#############
model_names = ['resnet18', 'resnet50']
loss_names = ['l1', 'l2']
# from dataloaders.dense_to_sparse import UniformSampling, SimulatedStereo
sparsifier_names = [x.name for x in [UniformSampling, SimulatedStereo]]
from models import Decoder
decoder_names = Decoder.names
################
data_names = ['nyudepthv2']
modality_names = CustomDataLoader.modality_names
import argparse
parser = argparse.ArgumentParser(description='FastDepth')
parser.add_argument('--data', metavar='DATA', default='nyudepthv2',
choices=data_names,
help='dataset: ' + ' | '.join(data_names) + ' (default: nyudepthv2)')
# parser.add_argument('--modality', '-m', metavar='MODALITY', default='rgb', choices=modality_names,
# help='modality: ' + ' | '.join(modality_names) + ' (default: rgb)')
parser.add_argument('-j', '--workers', default=16, type=int, metavar='N',
help='number of data loading workers (default: 16)')
# parser.add_argument('--print-freq', '-p', default=50, type=int,
# metavar='N', help='print frequency (default: 50)')
parser.add_argument('-e', '--evaluate', default='', type=str, metavar='PATH',)
parser.add_argument('-t', '--train', default='', type=str, )
parser.add_argument('--gpu', default='0', type=str, metavar='N', help="gpu id")
# args = parser.parse_args()
# return args
#####
# parser = argparse.ArgumentParser(description='Sparse-to-Dense')
parser.add_argument('--arch', '-a', metavar='ARCH', default='MobileNet', choices=model_names,
help='model architecture: ' + ' | '.join(model_names) + ' (default: MobileNet)')
# parser.add_argument('--data', metavar='DATA', default='nyudepthv2',
# choices=data_names,
# help='dataset: ' + ' | '.join(data_names) + ' (default: nyudepthv2)')
parser.add_argument('--modality', '-m', metavar='MODALITY', default='rgb', choices=modality_names,
help='modality: ' + ' | '.join(modality_names) + ' (default: rgb)')
parser.add_argument('-s', '--num-samples', default=0, type=int, metavar='N',
help='number of sparse depth samples (default: 0)')
parser.add_argument('--max-depth', default=-1.0, type=float, metavar='D',
help='cut-off depth of sparsifier, negative values means infinity (default: inf [m])')
parser.add_argument('--sparsifier', metavar='SPARSIFIER', default=UniformSampling.name, choices=sparsifier_names,
help='sparsifier: ' + ' | '.join(sparsifier_names) + ' (default: ' + UniformSampling.name + ')')
parser.add_argument('--decoder', '-d', metavar='DECODER', default='deconv2', choices=decoder_names,
help='decoder: ' + ' | '.join(decoder_names) + ' (default: deconv2)')
# parser.add_argument('-j', '--workers', default=10, type=int, metavar='N',
# help='number of data loading workers (default: 10)')
parser.add_argument('--epochs', default=15, type=int, metavar='N',
help='number of total epochs to run (default: 15)')
parser.add_argument('-c', '--criterion', metavar='LOSS', default='l1', choices=loss_names,
help='loss function: ' + ' | '.join(loss_names) + ' (default: l1)')
parser.add_argument('-b', '--batch-size', default=8, type=int, help='mini-batch size (default: 8)')
parser.add_argument('--lr', '--learning-rate', default=0.01, type=float,
metavar='LR', help='initial learning rate (default 0.01)')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum')
parser.add_argument('--weight-decay', '--wd', default=1e-4, type=float,
metavar='W', help='weight decay (default: 1e-4)')
parser.add_argument('--print-freq', '-p', default=10, type=int,
metavar='N', help='print frequency (default: 10)')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
help='path to latest checkpoint (default: none)')
parser.add_argument('--no-pretrain', dest='pretrained', action='store_false',
help='not to use ImageNet pre-trained weights')
parser.set_defaults(pretrained=True)
args = parser.parse_args()
if args.modality == 'rgb' and args.num_samples != 0:
print("number of samples is forced to be 0 when input modality is rgb")
args.num_samples = 0
if args.modality == 'rgb' and args.max_depth != 0.0:
print("max depth is forced to be 0.0 when input modality is rgb/rgbd")
args.max_depth = 0.0
return args
def save_checkpoint(state, is_best, epoch, output_directory):
checkpoint_filename = os.path.join(output_directory, 'checkpoint-' + str(epoch) + '.pth.tar')
torch.save(state, checkpoint_filename)
if is_best:
best_filename = os.path.join(output_directory, 'model_best.pth.tar')
shutil.copyfile(checkpoint_filename, best_filename)
if epoch > 0:
prev_checkpoint_filename = os.path.join(output_directory, 'checkpoint-' + str(epoch-1) + '.pth.tar')
if os.path.exists(prev_checkpoint_filename):
os.remove(prev_checkpoint_filename)
def adjust_learning_rate(optimizer, epoch, lr_init):
"""Sets the learning rate to the initial LR decayed by 10 every 5 epochs"""
lr = lr_init * (0.1 ** (epoch // 5))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def get_output_directory(args):
output_directory = os.path.join('results',
'{}.sparsifier={}.samples={}.modality={}.arch={}.decoder={}.criterion={}.lr={}.bs={}.pretrained={}'.
format(args.data, args.sparsifier, args.num_samples, args.modality, \
args.arch, args.decoder, args.criterion, args.lr, args.batch_size, \
args.pretrained))
return output_directory
def colored_depthmap(depth, d_min=None, d_max=None):
if d_min is None:
d_min = np.min(depth)
if d_max is None:
d_max = np.max(depth)
depth_relative = (depth - d_min) / (d_max - d_min)
return 255 * cmap(depth_relative)[:,:,:3] # H, W, C
def merge_into_row(input, depth_target, depth_pred):
rgb = 255 * np.transpose(np.squeeze(input.cpu().numpy()), (1,2,0)) # H, W, C
depth_target_cpu = np.squeeze(depth_target.cpu().numpy())
depth_pred_cpu = np.squeeze(depth_pred.data.cpu().numpy())
d_min = min(np.min(depth_target_cpu), np.min(depth_pred_cpu))
d_max = max(np.max(depth_target_cpu), np.max(depth_pred_cpu))
depth_target_col = colored_depthmap(depth_target_cpu, d_min, d_max)
depth_pred_col = colored_depthmap(depth_pred_cpu, d_min, d_max)
img_merge = np.hstack([rgb, depth_target_col, depth_pred_col])
return img_merge
def merge_into_row_with_gt(input, depth_input, depth_target, depth_pred):
rgb = 255 * np.transpose(np.squeeze(input.cpu().numpy()), (1,2,0)) # H, W, C
depth_input_cpu = np.squeeze(depth_input.cpu().numpy())
depth_target_cpu = np.squeeze(depth_target.cpu().numpy())
depth_pred_cpu = np.squeeze(depth_pred.data.cpu().numpy())
d_min = min(np.min(depth_input_cpu), np.min(depth_target_cpu), np.min(depth_pred_cpu))
d_max = max(np.max(depth_input_cpu), np.max(depth_target_cpu), np.max(depth_pred_cpu))
depth_input_col = colored_depthmap(depth_input_cpu, d_min, d_max)
depth_target_col = colored_depthmap(depth_target_cpu, d_min, d_max)
depth_pred_col = colored_depthmap(depth_pred_cpu, d_min, d_max)
img_merge = np.hstack([rgb, depth_input_col, depth_target_col, depth_pred_col])
return img_merge
def add_row(img_merge, row):
return np.vstack([img_merge, row])
def save_image(img_merge, filename):
img_merge = Image.fromarray(img_merge.astype('uint8'))
img_merge.save(filename)