-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
196 lines (171 loc) · 6.67 KB
/
model.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
from copy import deepcopy
from typing import Any, Callable, Union
from matplotlib import pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models.resnet as resnets
import torchvision.ops.misc as vops
import torchvision.transforms as T
class DummyModule(nn.Module):
def __init__(self, *args, **kwargs):
super().__init__()
def __getitem__(self, _):
return self
def forward(self, x):
return torch.empty(x.size(0), 0, 7, 7, device=x.device, dtype=x.dtype)
class ForegroundBackgroundEncoder(nn.Module):
def __init__(
self,
depth: int = 18,
freeze_at: int = 2,
num_embedding: int = 2048,
pdrop: float = 0.0,
depth_input: bool = False,
normal_input: bool = False,
normalize_output: bool = False,
freeze_bn: bool = True,
large_fc: bool = False,
global_pool: str = 'Max',
bg_input: bool = True,
):
super().__init__()
resnet_fn = getattr(resnets, 'resnet' + str(depth))
norm_layer = vops.FrozenBatchNorm2d if freeze_bn else nn.BatchNorm2d
resnet: resnets.ResNet = resnet_fn(pretrained=True, norm_layer=norm_layer)
resnet = resnet.requires_grad_(False)
self.fg_net = nn.Sequential(
nn.Sequential(
resnet.conv1,
resnet.bn1,
resnet.relu,
resnet.maxpool,
),
resnet.layer1,
resnet.layer2,
resnet.layer3,
resnet.layer4,
)
if bg_input:
self.bg_net = deepcopy(self.fg_net)
else:
self.bg_net = DummyModule()
self.depth_input = depth_input
if self.depth_input:
self.fg_net_depth = deepcopy(self.fg_net)
self.bg_net_depth = deepcopy(self.bg_net)
self.normal_input = normal_input
if self.normal_input:
self.fg_net_normal = deepcopy(self.fg_net)
self.bg_net_normal = deepcopy(self.bg_net)
for i in range(freeze_at, len(self.fg_net)):
self.fg_net[i].requires_grad_()
self.bg_net[i].requires_grad_()
if self.depth_input:
self.fg_net_depth[i].requires_grad_()
self.bg_net_depth[i].requires_grad_()
if self.normal_input:
self.fg_net_normal[i].requires_grad_()
self.bg_net_normal[i].requires_grad_()
last_block: Union[resnets.BasicBlock, resnets.Bottleneck] = resnet.layer4[-1]
if isinstance(last_block, resnets.BasicBlock):
out_channels = last_block.conv2.out_channels
else:
out_channels = last_block.conv3.out_channels
if not large_fc:
global_pool = getattr(nn, 'Adaptive{}Pool2d'.format(global_pool.capitalize()))
self.output = nn.Sequential(
nn.MaxPool2d(2),
nn.Conv2d(out_channels * (1 + int(bg_input)), out_channels * 4, 2), # 3, 3
global_pool((1, 1)),
nn.ReLU(True),
nn.Flatten(),
# nn.Dropout(pdrop),
nn.Linear(out_channels * 4, num_embedding),
)
else:
hiddens = out_channels * 4
self.output = nn.Sequential(
nn.Flatten(),
nn.Linear(7 * 7 * out_channels * 2, hiddens),
nn.ReLU(True),
nn.Linear(hiddens, hiddens),
nn.ReLU(True),
nn.Linear(hiddens, num_embedding),
)
self.image_transform = T.Compose([
T.ToTensor(),
# T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
self.mask_transform = T.Compose([
T.ToTensor(),
T.Lambda(lambda x: x.bool()),
])
self.drop = nn.Dropout2d(p=pdrop)
self.normalize_output = normalize_output
@property
def device(self):
return next(self.parameters()).device
def forward(self, data_list: list[dict]) -> torch.Tensor:
mask = self._preprocess_mask(data_list)
fg_images, bg_images = self._preprocess(data_list, mask)
fg_embeds = self.fg_net(fg_images)
bg_embeds = self.bg_net(bg_images)
for modal in ('depth', 'normal'):
if getattr(self, '{}_input'.format(modal)):
fg_images, bg_images = self._preprocess(data_list, mask, modal)
fg_embeds = fg_embeds + getattr(self, 'fg_net_' + modal)(fg_images)
bg_embeds = bg_embeds + getattr(self, 'bg_net_' + modal)(bg_images)
embeds = self.drop(torch.cat([fg_embeds, bg_embeds], dim=1))
output = self.output(embeds)
if self.normalize_output:
output = F.normalize(output, dim=-1)
return output
def _preprocess_mask(self, data_list):
masks = torch.stack([self.mask_transform(d['mask']) for d in data_list])
return masks.to(self.device)
def _preprocess(self, data_list: list[dict], masks: torch.Tensor, field: str = 'image'):
images = torch.stack([self.image_transform(d[field]) for d in data_list])
images = images.to(self.device)
fg_image = images * masks
bg_image = images * masks.logical_not()
return fg_image, bg_image
ModelType = Union[ForegroundBackgroundEncoder, Callable[[Any], torch.Tensor]]
def build_model(cfg: dict) -> ModelType:
model_cfg = cfg['MODEL']
input_cfg = cfg['INPUT']
model = ForegroundBackgroundEncoder(
depth=model_cfg['DEPTH'],
freeze_at=model_cfg['FREEZE_AT'],
num_embedding=model_cfg['EMBED_DIM'],
pdrop=model_cfg['DROPOUT'],
depth_input=input_cfg['DEPTH'],
normal_input=input_cfg['NORMAL'],
normalize_output=model_cfg['NORMALIZE_EMBED'],
freeze_bn=model_cfg['FREEZE_BN'],
large_fc=model_cfg['LARGE_FC'],
global_pool=model_cfg['GLOBAL_POOL_TYPE'],
bg_input=input_cfg.get('BG_INPUT', True),
)
if torch.has_cuda:
model = model.cuda()
return model
if __name__ == '__main__':
model = ForegroundBackgroundEncoder(depth_input=True) # .cuda()
print(sum(p.numel() for p in model.parameters() if p.requires_grad))
import os
from dataset import CropDataset
crop_data = CropDataset(
'./crops_assoc_no_filter_train_100.pkl',
os.environ['HOME'] + '/Data/Resized400k/tasks/scannet_frames_25k',
use_depth=True,
use_normal=False,
keep_ratio=False,
box_scale=5.,
normalize_depth=True,
)
data = []
# for i in range(50):
data.append(crop_data['scene0000_00', 0])
result = model(data)
from IPython import embed; embed()