rakib72642 commited on
Commit
7d8fc87
·
1 Parent(s): 08ec965

preTrained predictions are ready for observation

Browse files
cutler_colab_demo.ipynb CHANGED
The diff for this file is too large to render. See raw diff
 
maskcut/__pycache__/dinoo.cpython-312.pyc ADDED
Binary file (21.4 kB). View file
 
maskcut/__pycache__/maskcut.cpython-312.pyc CHANGED
Binary files a/maskcut/__pycache__/maskcut.cpython-312.pyc and b/maskcut/__pycache__/maskcut.cpython-312.pyc differ
 
maskcut/dino/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (403 Bytes). View file
 
maskcut/dino/__pycache__/dinoUsed.cpython-312.pyc ADDED
Binary file (21.4 kB). View file
 
maskcut/dino/dinoUsed.py ADDED
@@ -0,0 +1,346 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ """
4
+ Copied from Dino repo. https://github.com/facebookresearch/dino
5
+ Mostly copy-paste from timm library.
6
+ https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/vision_transformer.py
7
+ """
8
+ import math
9
+ from functools import partial
10
+
11
+ import torch
12
+ import torch.nn as nn
13
+
14
+ def _no_grad_trunc_normal_(tensor, mean, std, a, b):
15
+ # Cut & paste from PyTorch official master until it's in a few official releases - RW
16
+ # Method based on https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf
17
+ def norm_cdf(x):
18
+ # Computes standard normal cumulative distribution function
19
+ return (1. + math.erf(x / math.sqrt(2.))) / 2.
20
+
21
+ if (mean < a - 2 * std) or (mean > b + 2 * std):
22
+ warnings.warn("mean is more than 2 std from [a, b] in nn.init.trunc_normal_. "
23
+ "The distribution of values may be incorrect.",
24
+ stacklevel=2)
25
+
26
+ with torch.no_grad():
27
+ # Values are generated by using a truncated uniform distribution and
28
+ # then using the inverse CDF for the normal distribution.
29
+ # Get upper and lower cdf values
30
+ l = norm_cdf((a - mean) / std)
31
+ u = norm_cdf((b - mean) / std)
32
+
33
+ # Uniformly fill tensor with values from [l, u], then translate to
34
+ # [2l-1, 2u-1].
35
+ tensor.uniform_(2 * l - 1, 2 * u - 1)
36
+
37
+ # Use inverse cdf transform for normal distribution to get truncated
38
+ # standard normal
39
+ tensor.erfinv_()
40
+
41
+ # Transform to proper mean, std
42
+ tensor.mul_(std * math.sqrt(2.))
43
+ tensor.add_(mean)
44
+
45
+ # Clamp to ensure it's in the proper range
46
+ tensor.clamp_(min=a, max=b)
47
+ return tensor
48
+
49
+
50
+ def trunc_normal_(tensor, mean=0., std=1., a=-2., b=2.):
51
+ # type: (Tensor, float, float, float, float) -> Tensor
52
+ return _no_grad_trunc_normal_(tensor, mean, std, a, b)
53
+
54
+
55
+ def drop_path(x, drop_prob: float = 0., training: bool = False):
56
+ if drop_prob == 0. or not training:
57
+ return x
58
+ keep_prob = 1 - drop_prob
59
+ shape = (x.shape[0],) + (1,) * (x.ndim - 1) # work with diff dim tensors, not just 2D ConvNets
60
+ random_tensor = keep_prob + torch.rand(shape, dtype=x.dtype, device=x.device)
61
+ random_tensor.floor_() # binarize
62
+ output = x.div(keep_prob) * random_tensor
63
+ return output
64
+
65
+
66
+ class DropPath(nn.Module):
67
+ """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
68
+ """
69
+ def __init__(self, drop_prob=None):
70
+ super(DropPath, self).__init__()
71
+ self.drop_prob = drop_prob
72
+
73
+ def forward(self, x):
74
+ return drop_path(x, self.drop_prob, self.training)
75
+
76
+
77
+ class Mlp(nn.Module):
78
+ def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):
79
+ super().__init__()
80
+ out_features = out_features or in_features
81
+ hidden_features = hidden_features or in_features
82
+ self.fc1 = nn.Linear(in_features, hidden_features)
83
+ self.act = act_layer()
84
+ self.fc2 = nn.Linear(hidden_features, out_features)
85
+ self.drop = nn.Dropout(drop)
86
+
87
+ def forward(self, x):
88
+ x = self.fc1(x)
89
+ x = self.act(x)
90
+ x = self.drop(x)
91
+ x = self.fc2(x)
92
+ x = self.drop(x)
93
+ return x
94
+
95
+
96
+ class Attention(nn.Module):
97
+ def __init__(self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.):
98
+ super().__init__()
99
+ self.num_heads = num_heads
100
+ head_dim = dim // num_heads
101
+ self.scale = qk_scale or head_dim ** -0.5
102
+
103
+ self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
104
+ self.attn_drop = nn.Dropout(attn_drop)
105
+ self.proj = nn.Linear(dim, dim)
106
+ self.proj_drop = nn.Dropout(proj_drop)
107
+
108
+ def forward(self, x):
109
+ B, N, C = x.shape
110
+ qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
111
+ q, k, v = qkv[0], qkv[1], qkv[2]
112
+
113
+ attn = (q @ k.transpose(-2, -1)) * self.scale
114
+ attn = attn.softmax(dim=-1)
115
+ attn = self.attn_drop(attn)
116
+
117
+ x = (attn @ v).transpose(1, 2).reshape(B, N, C)
118
+ x = self.proj(x)
119
+ x = self.proj_drop(x)
120
+ return x, attn
121
+
122
+
123
+ class Block(nn.Module):
124
+ def __init__(self, dim, num_heads, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0.,
125
+ drop_path=0., act_layer=nn.GELU, norm_layer=nn.LayerNorm):
126
+ super().__init__()
127
+ self.norm1 = norm_layer(dim)
128
+ self.attn = Attention(
129
+ dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop)
130
+ self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
131
+ self.norm2 = norm_layer(dim)
132
+ mlp_hidden_dim = int(dim * mlp_ratio)
133
+ self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)
134
+
135
+ def forward(self, x, return_attention=False):
136
+ y, attn = self.attn(self.norm1(x))
137
+ if return_attention:
138
+ return attn
139
+ x = x + self.drop_path(y)
140
+ x = x + self.drop_path(self.mlp(self.norm2(x)))
141
+ return x
142
+
143
+
144
+ class PatchEmbed(nn.Module):
145
+ """ Image to Patch Embedding
146
+ """
147
+ def __init__(self, img_size=224, patch_size=16, in_chans=3, embed_dim=768):
148
+ super().__init__()
149
+ num_patches = (img_size // patch_size) * (img_size // patch_size)
150
+ self.img_size = img_size
151
+ self.patch_size = patch_size
152
+ self.num_patches = num_patches
153
+
154
+ self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)
155
+
156
+ def forward(self, x):
157
+ B, C, H, W = x.shape
158
+ x = self.proj(x).flatten(2).transpose(1, 2)
159
+ return x
160
+
161
+
162
+ class VisionTransformer(nn.Module):
163
+ """ Vision Transformer """
164
+ def __init__(self, img_size=[224], patch_size=16, in_chans=3, num_classes=0, embed_dim=768, depth=12,
165
+ num_heads=12, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop_rate=0., attn_drop_rate=0.,
166
+ drop_path_rate=0., norm_layer=nn.LayerNorm, **kwargs):
167
+ super().__init__()
168
+ self.num_features = self.embed_dim = embed_dim
169
+
170
+ self.patch_embed = PatchEmbed(
171
+ img_size=img_size[0], patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim)
172
+ num_patches = self.patch_embed.num_patches
173
+
174
+ self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim))
175
+ self.pos_embed = nn.Parameter(torch.zeros(1, num_patches + 1, embed_dim))
176
+ self.pos_drop = nn.Dropout(p=drop_rate)
177
+
178
+ dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule
179
+ self.blocks = nn.ModuleList([
180
+ Block(
181
+ dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,
182
+ drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[i], norm_layer=norm_layer)
183
+ for i in range(depth)])
184
+ self.norm = norm_layer(embed_dim)
185
+
186
+ # Classifier head
187
+ self.head = nn.Linear(embed_dim, num_classes) if num_classes > 0 else nn.Identity()
188
+
189
+ trunc_normal_(self.pos_embed, std=.02)
190
+ trunc_normal_(self.cls_token, std=.02)
191
+ self.apply(self._init_weights)
192
+
193
+ def _init_weights(self, m):
194
+ if isinstance(m, nn.Linear):
195
+ trunc_normal_(m.weight, std=.02)
196
+ if isinstance(m, nn.Linear) and m.bias is not None:
197
+ nn.init.constant_(m.bias, 0)
198
+ elif isinstance(m, nn.LayerNorm):
199
+ nn.init.constant_(m.bias, 0)
200
+ nn.init.constant_(m.weight, 1.0)
201
+
202
+ def interpolate_pos_encoding(self, x, w, h):
203
+ npatch = x.shape[1] - 1
204
+ N = self.pos_embed.shape[1] - 1
205
+ if npatch == N and w == h:
206
+ return self.pos_embed
207
+ class_pos_embed = self.pos_embed[:, 0]
208
+ patch_pos_embed = self.pos_embed[:, 1:]
209
+ dim = x.shape[-1]
210
+ w0 = w // self.patch_embed.patch_size
211
+ h0 = h // self.patch_embed.patch_size
212
+ # we add a small number to avoid floating point error in the interpolation
213
+ # see discussion at https://github.com/facebookresearch/dino/issues/8
214
+ w0, h0 = w0 + 0.1, h0 + 0.1
215
+ patch_pos_embed = nn.functional.interpolate(
216
+ patch_pos_embed.reshape(1, int(math.sqrt(N)), int(math.sqrt(N)), dim).permute(0, 3, 1, 2),
217
+ scale_factor=(w0 / math.sqrt(N), h0 / math.sqrt(N)),
218
+ mode='bicubic',
219
+ )
220
+ assert int(w0) == patch_pos_embed.shape[-2] and int(h0) == patch_pos_embed.shape[-1]
221
+ patch_pos_embed = patch_pos_embed.permute(0, 2, 3, 1).view(1, -1, dim)
222
+ return torch.cat((class_pos_embed.unsqueeze(0), patch_pos_embed), dim=1)
223
+
224
+ def prepare_tokens(self, x):
225
+ B, nc, w, h = x.shape
226
+ x = self.patch_embed(x) # patch linear embedding
227
+
228
+ # add the [CLS] token to the embed patch tokens
229
+ cls_tokens = self.cls_token.expand(B, -1, -1)
230
+ x = torch.cat((cls_tokens, x), dim=1)
231
+
232
+ # add positional encoding to each token
233
+ x = x + self.interpolate_pos_encoding(x, w, h)
234
+
235
+ return self.pos_drop(x)
236
+
237
+ def forward(self, x):
238
+ x = self.prepare_tokens(x)
239
+ for blk in self.blocks:
240
+ x = blk(x)
241
+ x = self.norm(x)
242
+ return x[:, 0]
243
+
244
+ def get_last_selfattention(self, x):
245
+ x = self.prepare_tokens(x)
246
+ for i, blk in enumerate(self.blocks):
247
+ if i < len(self.blocks) - 1:
248
+ x = blk(x)
249
+ else:
250
+ # return attention of the last block
251
+ return blk(x, return_attention=True)
252
+
253
+ def get_intermediate_layers(self, x, n=1):
254
+ x = self.prepare_tokens(x)
255
+ # we return the output tokens from the `n` last blocks
256
+ output = []
257
+ for i, blk in enumerate(self.blocks):
258
+ x = blk(x)
259
+ if len(self.blocks) - i <= n:
260
+ output.append(self.norm(x))
261
+ return output
262
+
263
+
264
+ def vit_small(patch_size=16, **kwargs):
265
+ model = VisionTransformer(
266
+ patch_size=patch_size, embed_dim=384, depth=12, num_heads=6, mlp_ratio=4,
267
+ qkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), **kwargs)
268
+ return model
269
+
270
+
271
+ def vit_base(patch_size=16, **kwargs):
272
+ model = VisionTransformer(
273
+ patch_size=patch_size, embed_dim=768, depth=12, num_heads=12, mlp_ratio=4,
274
+ qkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), **kwargs)
275
+ return model
276
+
277
+ class ViTFeat(nn.Module):
278
+ """ Vision Transformer """
279
+ def __init__(self, pretrained_pth, feat_dim, vit_arch = 'base', vit_feat = 'k', patch_size=16):
280
+ super().__init__()
281
+ if vit_arch == 'base' :
282
+ self.model = vit_base(patch_size=patch_size, num_classes=0)
283
+
284
+ else :
285
+ self.model = vit_small(patch_size=patch_size, num_classes=0)
286
+
287
+ self.feat_dim = feat_dim
288
+ self.vit_feat = vit_feat
289
+ self.patch_size = patch_size
290
+
291
+ # state_dict = torch.load(pretrained_pth, map_location="cpu")
292
+ state_dict = torch.hub.load_state_dict_from_url(pretrained_pth)
293
+ self.model.load_state_dict(state_dict, strict=True)
294
+ print('Loading weight from {}'.format(pretrained_pth))
295
+
296
+
297
+ def forward(self, img) :
298
+ feat_out = {}
299
+ def hook_fn_forward_qkv(module, input, output):
300
+ feat_out["qkv"] = output
301
+
302
+ self.model._modules["blocks"][-1]._modules["attn"]._modules["qkv"].register_forward_hook(hook_fn_forward_qkv)
303
+
304
+
305
+ # Forward pass in the model
306
+ with torch.no_grad() :
307
+ h, w = img.shape[2], img.shape[3]
308
+ feat_h, feat_w = h // self.patch_size, w // self.patch_size
309
+ attentions = self.model.get_last_selfattention(img)
310
+ bs, nb_head, nb_token = attentions.shape[0], attentions.shape[1], attentions.shape[2]
311
+ qkv = (
312
+ feat_out["qkv"]
313
+ .reshape(bs, nb_token, 3, nb_head, -1)
314
+ .permute(2, 0, 3, 1, 4)
315
+ )
316
+ q, k, v = qkv[0], qkv[1], qkv[2]
317
+
318
+ k = k.transpose(1, 2).reshape(bs, nb_token, -1)
319
+ q = q.transpose(1, 2).reshape(bs, nb_token, -1)
320
+ v = v.transpose(1, 2).reshape(bs, nb_token, -1)
321
+
322
+ # Modality selection
323
+ if self.vit_feat == "k":
324
+ feats = k[:, 1:].transpose(1, 2).reshape(bs, self.feat_dim, feat_h * feat_w)
325
+ elif self.vit_feat == "q":
326
+ feats = q[:, 1:].transpose(1, 2).reshape(bs, self.feat_dim, feat_h * feat_w)
327
+ elif self.vit_feat == "v":
328
+ feats = v[:, 1:].transpose(1, 2).reshape(bs, self.feat_dim, feat_h * feat_w)
329
+ elif self.vit_feat == "kqv":
330
+ k = k[:, 1:].transpose(1, 2).reshape(bs, self.feat_dim, feat_h * feat_w)
331
+ q = q[:, 1:].transpose(1, 2).reshape(bs, self.feat_dim, feat_h * feat_w)
332
+ v = v[:, 1:].transpose(1, 2).reshape(bs, self.feat_dim, feat_h * feat_w)
333
+ feats = torch.cat([k, q, v], dim=1)
334
+ return feats
335
+
336
+
337
+ if __name__ == "__main__":
338
+ vit_arch = 'base'
339
+ vit_feat = 'k'
340
+
341
+ model = ViTFeat(vit_arch, vit_feat)
342
+ img = torch.cuda.FloatTensor(4, 3, 224, 224)
343
+ model.cuda()
344
+ # Forward pass in the model
345
+ feat = model(img)
346
+ print (feat.shape)
maskcut/maskcut.py CHANGED
@@ -24,8 +24,8 @@ import dino
24
  # modfied by Xudong Wang based on third_party/TokenCut
25
  sys.path.append('../')
26
  sys.path.append('../third_party')
27
- from token_cut.unsupervised_saliency_detection import utils, metric
28
- from token_cut.unsupervised_saliency_detection.object_discovery import detect_box
29
  # bilateral_solver codes are modfied based on https://github.com/poolio/bilateral_solver/blob/master/notebooks/bilateral_solver.ipynb
30
  # from TokenCut.unsupervised_saliency_detection.bilateral_solver import BilateralSolver, BilateralGrid
31
  # crf codes are are modfied based on https://github.com/lucasb-eyer/pydensecrf/blob/master/pydensecrf/tests/test_dcrf.py
 
24
  # modfied by Xudong Wang based on third_party/TokenCut
25
  sys.path.append('../')
26
  sys.path.append('../third_party')
27
+ from unsupervised_saliency_detection import utils, metric
28
+ from unsupervised_saliency_detection.object_discovery import detect_box
29
  # bilateral_solver codes are modfied based on https://github.com/poolio/bilateral_solver/blob/master/notebooks/bilateral_solver.ipynb
30
  # from TokenCut.unsupervised_saliency_detection.bilateral_solver import BilateralSolver, BilateralGrid
31
  # crf codes are are modfied based on https://github.com/lucasb-eyer/pydensecrf/blob/master/pydensecrf/tests/test_dcrf.py
maskcut/unsupervised_saliency_detection/__pycache__/metric.cpython-312.pyc CHANGED
Binary files a/maskcut/unsupervised_saliency_detection/__pycache__/metric.cpython-312.pyc and b/maskcut/unsupervised_saliency_detection/__pycache__/metric.cpython-312.pyc differ
 
maskcut/unsupervised_saliency_detection/__pycache__/object_discovery.cpython-312.pyc ADDED
Binary file (4.88 kB). View file
 
maskcut/unsupervised_saliency_detection/__pycache__/utils.cpython-312.pyc ADDED
Binary file (817 Bytes). View file
 
maskcut_colab_demo.ipynb CHANGED
The diff for this file is too large to render. See raw diff
 
third_party/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (201 Bytes). View file