-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_configs.py
executable file
·424 lines (340 loc) · 14.7 KB
/
task_configs.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import numpy as np
import random, sys, os, time, glob, math, itertools, json, copy
from collections import defaultdict, namedtuple
from functools import partial
import PIL
from PIL import Image
from scipy import ndimage
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms.functional as TF
import torch.optim as optim
from torchvision import transforms
from utils import *
from models import DataParallelModel
from modules.unet import UNet, UNetOld2, UNetOld
# from modules.percep_nets import Dense1by1Net
# from modules.depth_nets import UNetDepth
# from modules.resnet import ResNetClass
import IPython
from torch.distributions.lowrank_multivariate_normal import LowRankMultivariateNormal
from PIL import ImageFilter
from skimage.filters import gaussian
import pdb
class GaussianBulr(object):
def __init__(self, radius):
self.radius = radius
self.filter = ImageFilter.GaussianBlur(radius)
def __call__(self, im):
return im.filter(self.filter)
def __repr__(self):
return 'GaussianBulr Filter with Radius {:d}'.format(self.radius)
""" Model definitions for launching new transfer jobs between tasks. """
model_types = {
('normal', 'principal_curvature'): lambda : Dense1by1Net(),
('normal', 'depth_zbuffer'): lambda : UNetDepth(),
('normal', 'reshading'): lambda : UNet(downsample=5),
('depth_zbuffer', 'normal'): lambda : UNet(downsample=6, in_channels=1, out_channels=3),
('reshading', 'normal'): lambda : UNet(downsample=4, in_channels=3, out_channels=3),
('sobel_edges', 'principal_curvature'): lambda : UNet(downsample=5, in_channels=1, out_channels=3),
('depth_zbuffer', 'principal_curvature'): lambda : UNet(downsample=4, in_channels=1, out_channels=3),
('principal_curvature', 'depth_zbuffer'): lambda : UNet(downsample=6, in_channels=3, out_channels=1),
('rgb', 'normal'): lambda : UNet(downsample=6),
('rgb', 'keypoints2d'): lambda : UNet(downsample=3, out_channels=1),
}
def get_model(src_task, dest_task):
if isinstance(src_task, str) and isinstance(dest_task, str):
src_task, dest_task = get_task(src_task), get_task(dest_task)
if (src_task.name, dest_task.name) in model_types:
return model_types[(src_task.name, dest_task.name)]()
elif isinstance(src_task, ImageTask) and isinstance(dest_task, ImageTask):
return UNet(downsample=5, in_channels=src_task.shape[0], out_channels=dest_task.shape[0])
elif isinstance(src_task, ImageTask) and isinstance(dest_task, ClassTask):
return ResNet(in_channels=src_task.shape[0], out_channels=dest_task.classes)
elif isinstance(src_task, ImageTask) and isinstance(dest_task, PointInfoTask):
return ResNet(out_channels=dest_task.out_channels)
return None
"""
Abstract task type definitions.
Includes Task, ImageTask, ClassTask, PointInfoTask, and SegmentationTask.
"""
class Task(object):
variances = {
"normal": 1.0,
"principal_curvature": 1.0,
"sobel_edges": 5,
"depth_zbuffer": 0.1,
"reshading": 1.0,
"keypoints2d": 0.3,
"keypoints3d": 0.6,
"edge_occlusion": 0.1,
}
""" General task output space"""
def __init__(self, name,
file_name=None, file_name_alt=None, file_ext="png", file_loader=None,
plot_func=None
):
super().__init__()
self.name = name
self.file_name, self.file_ext = file_name or name, file_ext
self.file_name_alt = file_name_alt or self.file_name
self.file_loader = file_loader or self.file_loader
self.plot_func = plot_func or self.plot_func
self.variance = Task.variances.get(name, 1.0)
self.kind = name
def norm(self, pred, target, batch_mean=True, compute_mse=True):
if batch_mean:
loss = ((pred - target)**2).mean() if compute_mse else ((pred - target).abs()).mean()
else:
loss = ((pred - target)**2).mean(dim=1).mean(dim=1).mean(dim=1) if compute_mse \
else ((pred - target).abs()).mean(dim=1).mean(dim=1).mean(dim=1)
return loss, (loss.mean().detach(),)
def nll(self, pred, target, batch_mean=True, mask=None):
nchannels = pred.size(1)//2
mux, sigma = pred[:,:nchannels], pred[:, nchannels:].exp()+1e-10
lap_dist = torch.distributions.Laplace(loc=mux, scale=sigma)
logprobs = lap_dist.log_prob(target)
if mask is not None: nll = -logprobs*mask
if batch_mean:
nll = (nll).mean()
else:
nll = (nll).mean(dim=1).mean(dim=1).mean(dim=1)
return nll, (nll.detach(),)
def __call__(self, size=256):
task = copy.deepcopy(self)
return task
def plot_func(self, data, name, logger, **kwargs):
### Non-image tasks cannot be easily plotted, default to nothing
pass
def file_loader(self, path, resize=None, seed=0, T=0):
raise NotImplementedError()
def __eq__(self, other):
return self.name == other.name
def __repr__(self):
return self.name
def __hash__(self):
return hash(self.name)
"""
Abstract task type definitions.
Includes Task, ImageTask, ClassTask, PointInfoTask, and SegmentationTask.
"""
class RealityTask(Task):
""" General task output space"""
def __init__(self, name, dataset, tasks=None, use_dataset=True, shuffle=False, batch_size=64):
super().__init__(name=name)
self.tasks = tasks if tasks is not None else \
(dataset.dataset.tasks if hasattr(dataset, "dataset") else dataset.tasks)
self.shape = (1,)
if not use_dataset: return
self.dataset, self.shuffle, self.batch_size = dataset, shuffle, batch_size
loader = torch.utils.data.DataLoader(
self.dataset, batch_size=self.batch_size,
num_workers=8, shuffle=self.shuffle, pin_memory=True
)
self.generator = cycle(loader)
self.step()
self.static = False
@classmethod
def from_dataloader(cls, name, loader, tasks):
reality = cls(name, None, tasks, use_dataset=False)
reality.loader = loader
reality.generator = cycle(loader)
reality.static = False
reality.step()
return reality
@classmethod
def from_static(cls, name, data, tasks):
reality = cls(name, None, tasks, use_dataset=False)
reality.task_data = {task: x.requires_grad_() for task, x in zip(tasks, data)}
reality.static = True
return reality
def norm(self, pred, target, batch_mean=True):
loss = torch.tensor(0.0, device=pred.device)
return loss, (loss.detach(),)
def step(self):
self.task_data = {task: x.requires_grad_() for task, x in zip(self.tasks, next(self.generator))}
def reload(self):
loader = torch.utils.data.DataLoader(
self.dataset, batch_size=self.batch_size,
num_workers=8, shuffle=self.shuffle, pin_memory=True
)
self.generator = cycle(loader)
class ImageTask(Task):
""" Output space for image-style tasks """
def __init__(self, *args, **kwargs):
self.shape = kwargs.pop("shape", (3, 256, 256))
self.mask_val = kwargs.pop("mask_val", -1.0)
self.transform = kwargs.pop("transform", lambda x: x)
self.resize = kwargs.pop("resize", self.shape[1])
self.blur_radius = None
self.image_transform = self.load_image_transform()
super().__init__(*args, **kwargs)
@staticmethod
def build_mask(target, val=0.0, tol=1e-3):
if target.shape[1] == 1:
mask = ((target >= val - tol) & (target <= val + tol))
mask = F.conv2d(mask.float(), torch.ones(1, 1, 5, 5, device=mask.device), padding=2) != 0
return (~mask).expand_as(target)
mask1 = (target[:, 0, :, :] >= val - tol) & (target[:, 0, :, :] <= val + tol)
mask2 = (target[:, 1, :, :] >= val - tol) & (target[:, 1, :, :] <= val + tol)
mask3 = (target[:, 2, :, :] >= val - tol) & (target[:, 2, :, :] <= val + tol)
mask = (mask1 & mask2 & mask3).unsqueeze(1)
mask = F.conv2d(mask.float(), torch.ones(1, 1, 5, 5, device=mask.device), padding=2) != 0
return (~mask).expand_as(target)
def norm(self, pred, target, batch_mean=True, compute_mask=1, compute_mse=True, mask=None):
# breakpoint()
if compute_mask:
if mask is None: mask = ImageTask.build_mask(target, val=self.mask_val)
return super().norm(pred*mask.float(), target*mask.float(), batch_mean=batch_mean, compute_mse=compute_mse)
else:
return super().norm(pred, target, batch_mean=batch_mean, compute_mse=compute_mse)
def nll(self, pred, target, batch_mean=True, compute_mask=1, mask=None):
if compute_mask:
if mask is None: mask = ImageTask.build_mask(target, val=self.mask_val)
return super().nll(pred, target, batch_mean=batch_mean, mask=mask)
else:
return super().nll(pred, target, batch_mean=batch_mean)
def __call__(self, size=256, blur_radius=None):
task = copy.deepcopy(self)
task.shape = (3, size, size)
task.resize = size
task.blur_radius = blur_radius
task.name += "blur" if blur_radius else str(size)
task.base = self
return task
def plot_func(self, data, name, logger, resize=None, nrow=2):
logger.images(data.clamp(min=0, max=1), name, nrow=nrow, resize=resize or self.resize)
def file_loader(self, path, resize=None, crop=None, seed=0, jitter=False):
image_transform = self.load_image_transform(resize=resize, crop=crop, seed=seed, jitter=jitter)
return image_transform(Image.open(open(path, 'rb')))[0:3]
def load_image_transform(self, resize=None, crop=None, seed=0, jitter=False):
size = resize or self.resize
# size=320
random.seed(seed)
jitter_transform = lambda x: x
if jitter: jitter_transform = transforms.ColorJitter(0.4,0.4,0.4,0.1)
crop_transform = lambda x: x
if crop is not None: crop_transform = transforms.CenterCrop(crop)
# blur = [GaussianBulr(self.blur_radius)] if self.blur_radius else []
return transforms.Compose([
crop_transform,
transforms.Resize(size, interpolation=PIL.Image.BILINEAR),
jitter_transform,
transforms.CenterCrop(size),
transforms.ToTensor(),
self.transform]
)
class ImageClassTask(ImageTask):
""" Output space for image-class segmentation tasks """
def __init__(self, *args, **kwargs):
self.classes = kwargs.pop("classes", (3, 256, 256))
super().__init__(*args, **kwargs)
def norm(self, pred, target):
loss = F.kl_div(F.log_softmax(pred, dim=1), F.softmax(target, dim=1))
return loss, (loss.detach(),)
def plot_func(self, data, name, logger, resize=None):
_, idx = torch.max(data, dim=1)
idx = idx.float()/16.0
idx = idx.unsqueeze(1).expand(-1, 3, -1, -1)
logger.images(idx.clamp(min=0, max=1), name, nrow=2, resize=resize or self.resize)
def file_loader(self, path, resize=None):
data = (self.image_transform(Image.open(open(path, 'rb')))*255.0).long()
one_hot = torch.zeros((self.classes, data.shape[1], data.shape[2]))
one_hot = one_hot.scatter_(0, data, 1)
return one_hot
class PointInfoTask(Task):
""" Output space for point-info prediction tasks (what models do we evem use?) """
def __init__(self, *args, **kwargs):
self.point_type = kwargs.pop("point_type", "vanishing_points_gaussian_sphere")
self.out_channels = 9
super().__init__(*args, **kwargs)
def plot_func(self, data, name, logger):
logger.window(name, logger.visdom.text, str(data.data.cpu().numpy()))
def file_loader(self, path, resize=None):
points = json.load(open(path))[self.point_type]
return np.array(points['x'] + points['y'] + points['z'])
"""
Current list of task definitions.
Accessible via tasks.{TASK_NAME} or get_task("{TASK_NAME}")
"""
def clamp_maximum_transform(x, max_val=8000.0):
x = x.unsqueeze(0).float() / max_val
return x[0].clamp(min=0, max=1)
def crop_transform(x, max_val=8000.0):
x = x.unsqueeze(0).float() / max_val
return x[0].clamp(min=0, max=1)
def sobel_transform(x):
image = x.data.cpu().numpy().mean(axis=0)
blur = ndimage.filters.gaussian_filter(image, sigma=2, )
sx = ndimage.sobel(blur, axis=0, mode='constant')
sy = ndimage.sobel(blur, axis=1, mode='constant')
sob = np.hypot(sx, sy)
edge = torch.FloatTensor(sob).unsqueeze(0)
return edge
def blur_transform(x, max_val=4000.0):
if x.shape[0] == 1:
x = x.squeeze(0)
image = x.data.cpu().numpy()
blur = ndimage.filters.gaussian_filter(image, sigma=2, )
norm = torch.FloatTensor(blur).unsqueeze(0)**0.8 / (max_val**0.8)
norm = norm.clamp(min=0, max=1)
if norm.shape[0] != 1:
norm = norm.unsqueeze(0)
return norm
def sparsify(x, max_pct=0.015, mask_val=1.):
x = clamp_maximum_transform(x, max_val=8000.0)
p = random.uniform(0,max_pct)
mask = torch.FloatTensor(x[:1].size()).uniform_() < p
mask = mask.repeat(x.size(0),1,1)
gt_valid_mask = x < mask_val
x = x * mask * gt_valid_mask
return x.float()
def get_task(task_name):
return task_map[task_name]
tasks = [
ImageTask('rgb'),
ImageTask('gray'),
ImageTask('mask_valid', file_name_alt="mask_valid"),
ImageTask('imagenet', mask_val=0.0),
ImageTask('sparse_normal',
file_name_alt="normal",
mask_val=0.502,
transform=partial(sparsify, max_pct=0.015),),
ImageTask('sparse_depth',
file_name_alt="depth_zbuffer",
transform=partial(sparsify, max_pct=0.01, mask_val=1.),),
ImageTask('normal', mask_val=0.502),
ImageTask('principal_curvature', mask_val=0.0),
ImageTask('depth_zbuffer',
shape=(1, 256, 256),
mask_val=1.0,
transform=partial(clamp_maximum_transform, max_val=8000.0),
),
ImageClassTask('segment_semantic',
file_name_alt="segmentsemantic",
shape=(16, 256, 256), classes=16,
),
ImageTask('reshading', mask_val=0.0507),
ImageTask('edge_occlusion',
shape=(1, 256, 256),
transform=partial(blur_transform, max_val=4000.0),
),
ImageTask('sobel_edges',
shape=(1, 256, 256),
file_name='rgb',
transform=sobel_transform,
),
ImageTask('keypoints3d',
shape=(1, 256, 256),
transform=partial(clamp_maximum_transform, max_val=64131.0),
),
ImageTask('keypoints2d',
shape=(1, 256, 256),
transform=partial(blur_transform, max_val=2000.0),
),
]
task_map = {task.name: task for task in tasks}
tasks = namedtuple('TaskMap', task_map.keys())(**task_map)
if __name__ == "__main__":
IPython.embed()