Spaces:
Paused
Paused
Upload models/anchor_gen.py
Browse files- models/anchor_gen.py +107 -0
models/anchor_gen.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from torch.autograd import Function
|
| 5 |
+
from models import basic, clusterkit
|
| 6 |
+
import pdb
|
| 7 |
+
|
| 8 |
+
class AnchorAnalysis:
|
| 9 |
+
def __init__(self, mode, colorLabeler):
|
| 10 |
+
## anchor generating mode: 1.random; 2.clustering
|
| 11 |
+
self.mode = mode
|
| 12 |
+
self.colorLabeler = colorLabeler
|
| 13 |
+
|
| 14 |
+
def _detect_correlation(self, data_tensors, color_probs, hint_masks, thres=0.1):
|
| 15 |
+
N,C,H,W = data_tensors.shape
|
| 16 |
+
## (N,C,HW)
|
| 17 |
+
data_vecs = data_tensors.flatten(2)
|
| 18 |
+
prob_vecs = color_probs.flatten(2)
|
| 19 |
+
mask_vecs = hint_masks.flatten(2)
|
| 20 |
+
#anchor_data = torch.masked_select(data_vecs, mask_vecs.bool()).view(N,C,-1)
|
| 21 |
+
#anchor_prob = torch.masked_select(prob_vecs, mask_vecs.bool()).view(N,313,-1)
|
| 22 |
+
#_,_,K = anchor_data.shape
|
| 23 |
+
anchor_mask = torch.matmul(mask_vecs.permute(0,2,1), mask_vecs)
|
| 24 |
+
cosine_sim = True
|
| 25 |
+
## non-similarity matrix
|
| 26 |
+
if cosine_sim:
|
| 27 |
+
norm_data = F.normalize(data_vecs, p=2, dim=1)
|
| 28 |
+
## (N,HW,HW) = (N,HW,C) X (N,C,HW)
|
| 29 |
+
corr_matrix = torch.matmul(norm_data.permute(0,2,1), norm_data)
|
| 30 |
+
## remapping: [-1.0,1.0] to [0.0,1.0], and convert into dis-similarity
|
| 31 |
+
dist_matrix = 1.0 - 0.5*(corr_matrix + 1.0)
|
| 32 |
+
else:
|
| 33 |
+
## (N,HW,HW) = (N,HW,C) X (N,C,HW)
|
| 34 |
+
XtX = torch.matmul(data_vecs.permute(0,2,1), data_vecs)
|
| 35 |
+
diag_vec = torch.diagonal(XtX, dim1=-2, dim2=-1)
|
| 36 |
+
A = diag_vec.unsqueeze(1).repeat(1,H*W,1)
|
| 37 |
+
At = diag_vec.unsqueeze(2).repeat(1,1,H*W)
|
| 38 |
+
dist_matrix = A - 2*XtX + At
|
| 39 |
+
#dist_matrix = dist_matrix + 1e7*torch.eye(K).to(data_tensors.device).repeat(N,1,1)
|
| 40 |
+
## for debug use
|
| 41 |
+
K = 8
|
| 42 |
+
anchor_adj_matrix = torch.masked_select(dist_matrix, anchor_mask.bool()).view(N,K,K)
|
| 43 |
+
## dectect connected nodes
|
| 44 |
+
adj_matrix = torch.where((dist_matrix < thres) & (anchor_mask > 0), torch.ones_like(dist_matrix), torch.zeros_like(dist_matrix))
|
| 45 |
+
adj_matrix = torch.matmul(adj_matrix, adj_matrix)
|
| 46 |
+
adj_matrix = adj_matrix / (1e-7+adj_matrix)
|
| 47 |
+
## merge nodes
|
| 48 |
+
## (N,K,C) = (N,K,K) X (N,K,C)
|
| 49 |
+
anchor_prob = torch.matmul(adj_matrix, prob_vecs.permute(0,2,1)) / torch.sum(adj_matrix, dim=2, keepdim=True)
|
| 50 |
+
updated_prob_vecs = anchor_prob.permute(0,2,1) * mask_vecs + (1-mask_vecs) * prob_vecs
|
| 51 |
+
color_probs = updated_prob_vecs.view(N,313,H,W)
|
| 52 |
+
return color_probs, anchor_adj_matrix
|
| 53 |
+
|
| 54 |
+
def _sample_anchor_colors(self, pred_prob, hint_mask, T=0):
|
| 55 |
+
N,C,H,W = pred_prob.shape
|
| 56 |
+
topk = 10
|
| 57 |
+
assert T < topk
|
| 58 |
+
sorted_probs, batch_indexs = torch.sort(pred_prob, dim=1, descending=True)
|
| 59 |
+
## (N,topk,H,W,1)
|
| 60 |
+
topk_probs = torch.softmax(sorted_probs[:,:topk,:,:], dim=1).unsqueeze(4)
|
| 61 |
+
topk_indexs = batch_indexs[:,:topk,:,:]
|
| 62 |
+
topk_ABs = torch.stack([self.colorLabeler.q_to_ab.index_select(0, q_i.flatten()).reshape(topk,H,W,2)
|
| 63 |
+
for q_i in topk_indexs])
|
| 64 |
+
## (N,topk,H,W,2)
|
| 65 |
+
topk_ABs = topk_ABs / 110.0
|
| 66 |
+
## choose the most distinctive 3 colors for each anchor
|
| 67 |
+
if T == 0:
|
| 68 |
+
sampled_ABs = topk_ABs[:,0,:,:,:]
|
| 69 |
+
elif T == 1:
|
| 70 |
+
sampled_AB0 = topk_ABs[:,[0],:,:,:]
|
| 71 |
+
internal_diff = torch.norm(topk_ABs-sampled_AB0, p=2, dim=4, keepdim=True)
|
| 72 |
+
_, batch_indexs = torch.sort(internal_diff, dim=1, descending=True)
|
| 73 |
+
## (N,1,H,W,2)
|
| 74 |
+
selected_index = batch_indexs[:,[0],:,:,:].expand([-1,-1,-1,-1,2])
|
| 75 |
+
sampled_ABs = torch.gather(topk_ABs, 1, selected_index)
|
| 76 |
+
sampled_ABs = sampled_ABs.squeeze(1)
|
| 77 |
+
else:
|
| 78 |
+
sampled_AB0 = topk_ABs[:,[0],:,:,:]
|
| 79 |
+
internal_diff = torch.norm(topk_ABs-sampled_AB0, p=2, dim=4, keepdim=True)
|
| 80 |
+
_, batch_indexs = torch.sort(internal_diff, dim=1, descending=True)
|
| 81 |
+
selected_index = batch_indexs[:,[0],:,:,:].expand([-1,-1,-1,-1,2])
|
| 82 |
+
sampled_AB1 = torch.gather(topk_ABs, 1, selected_index)
|
| 83 |
+
internal_diff2 = torch.norm(topk_ABs-sampled_AB1, p=2, dim=4, keepdim=True)
|
| 84 |
+
_, batch_indexs = torch.sort(internal_diff+internal_diff2, dim=1, descending=True)
|
| 85 |
+
## (N,1,H,W,2)
|
| 86 |
+
selected_index = batch_indexs[:,[T-2],:,:,:].expand([-1,-1,-1,-1,2])
|
| 87 |
+
sampled_ABs = torch.gather(topk_ABs, 1, selected_index)
|
| 88 |
+
sampled_ABs = sampled_ABs.squeeze(1)
|
| 89 |
+
|
| 90 |
+
return sampled_ABs.permute(0,3,1,2)
|
| 91 |
+
|
| 92 |
+
def __call__(self, data_tensors, n_anchors, spixel_sizes, use_sklearn_kmeans=False):
|
| 93 |
+
N,C,H,W = data_tensors.shape
|
| 94 |
+
if self.mode == 'clustering':
|
| 95 |
+
## clusters map: (N,K,H,W)
|
| 96 |
+
cluster_mask = clusterkit.batch_kmeans_pytorch(data_tensors, n_anchors, 'euclidean', use_sklearn_kmeans)
|
| 97 |
+
#noises = torch.rand(N,1,H,W).to(cluster_mask.device)
|
| 98 |
+
perturb_factors = spixel_sizes
|
| 99 |
+
cluster_prob = cluster_mask + perturb_factors * 0.01
|
| 100 |
+
hint_mask_layers = F.one_hot(torch.argmax(cluster_prob.flatten(2), dim=-1), num_classes=H*W).float()
|
| 101 |
+
hint_mask = torch.sum(hint_mask_layers, dim=1, keepdim=True).view(N,1,H,W)
|
| 102 |
+
else:
|
| 103 |
+
#print('----------hello, random!')
|
| 104 |
+
cluster_mask = torch.zeros(N,n_anchors,H,W).to(data_tensors.device)
|
| 105 |
+
binary_mask = basic.get_random_mask(N, H, W, minNum=n_anchors, maxNum=n_anchors)
|
| 106 |
+
hint_mask = torch.from_numpy(binary_mask).to(data_tensors.device)
|
| 107 |
+
return hint_mask, cluster_mask
|