-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartImage_Model.py
159 lines (121 loc) · 7.08 KB
/
PartImage_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
import torch
import math
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from backbone import Get_vit_PartImage, conv_ln, ArcMarginProduct_PartImage, load_DINO
class UnsupervisedPart_PartImage(nn.Module):
def __init__(self, num_part, d_model, pretrain,
cfg):
super(UnsupervisedPart_PartImage, self).__init__()
self.num_part = num_part
self.d_model = d_model
self.background = cfg.MODEL.BACKGROUND
self.Reconstruct_Block = nn.Sequential(
conv_ln(64, 64, 1),
conv_ln(64, 64, 1),
nn.Upsample(scale_factor=2.0),
conv_ln(64, 64, 1),
conv_ln(64, 64, 1),
nn.Upsample(scale_factor=2.0),
conv_ln(64, 64, 1),
nn.Conv2d(64, 3, (1, 1), (1, 1))
)
self.compress_layer = nn.Sequential(
nn.Conv2d(384, 64, (1, 1), (1, 1)),
)
self.logit_scale = torch.nn.Parameter(torch.ones([]) * np.log(1 / 0.07))
self.ArcMarginProduct = ArcMarginProduct_PartImage(d_model, (self.num_part-self.background) *110, self.num_part-self.background)
self.apply(self._init_weights)
self.logit_scale = nn.Parameter(torch.ones([]) * np.log(1 / 0.07))
x_map = torch.arange(cfg.MODEL.IMG_SIZE // 8, dtype=torch.float32).cuda() / (cfg.MODEL.IMG_SIZE // 8) * 2.0 - 1.0
self.x_map = x_map.unsqueeze(0).repeat(cfg.MODEL.IMG_SIZE // 8, 1)
self.x_map = self.x_map.unsqueeze(0).unsqueeze(0)
y_map = torch.arange(cfg.MODEL.IMG_SIZE // 8, dtype=torch.float32).cuda() / (cfg.MODEL.IMG_SIZE // 8) * 2.0 - 1.0
self.y_map = y_map.unsqueeze(1).repeat(1, cfg.MODEL.IMG_SIZE // 8)
self.y_map = self.y_map.unsqueeze(0).unsqueeze(0)
self.backbone1 = load_DINO("small", pretrain, 8)
self.Transformer = Get_vit_PartImage(num_part, 128, self.num_part-self.background, 110, 16, 256, 6, 8, 64)
def _reset_parameters(self):
for p in self.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
def Concentration_loss(self, pred, epsilon=1.5):
pred_full = pred
pred = pred[:, :self.num_part-self.background, :, :]
SumWeight = torch.sum(pred, dim=(2, 3), keepdim=True) + 1e-8
SumWeight_full = torch.sum(pred_full, dim=(2, 3), keepdim=True) + 1e-8
X_sum = torch.sum(pred * self.x_map, dim=(2, 3), keepdim=True)
Y_sum = torch.sum(pred * self.y_map, dim=(2, 3), keepdim=True)
# [B, N, 1, 1]
X_coord = X_sum / SumWeight
Y_coord = Y_sum / SumWeight
X_var = torch.square(self.x_map - X_coord)
Y_var = torch.square(self.y_map - Y_coord)
X_var = torch.sum(X_var * pred / SumWeight, dim=(1, 2, 3))
Y_var = torch.sum(Y_var * pred / SumWeight, dim=(1, 2, 3))
size_constrain = 1 / (1 + SumWeight_full / epsilon)
size_constrain = torch.sum(size_constrain, dim=(1, 2, 3))
return torch.mean(X_var + Y_var), torch.mean(size_constrain)
def high_level_loss(self, input_feature, img, vgg, return_img=False):
Bs = img.size(0)
R_img = self.Reconstruct_Block(input_feature)
vgg_features = vgg(torch.cat((R_img, img), dim=0))
loss_weight = [1.0 / 32.0, 1.0 / 16.0, 1.0 / 8.0, 1.0 / 4.0, 1.0]
loss = 0
for i in range(len(vgg_features)):
feature_diff = (vgg_features[i][0:Bs] - vgg_features[i][Bs:])
value = torch.abs(feature_diff).mean()
loss += value * loss_weight[i]
if return_img is False:
return loss
else:
return loss, R_img
def _init_weights(self, m):
if isinstance(m, nn.Linear):
# we use xavier_uniform following official JAX ViT:
torch.nn.init.xavier_uniform_(m.weight)
if isinstance(m, nn.Linear) and m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Conv2d):
torch.nn.init.xavier_uniform_(m.weight)
elif isinstance(m, nn.LayerNorm):
nn.init.constant_(m.bias, 0)
nn.init.constant_(m.weight, 1.0)
def forward(self, input, label, input_pair=None, VGG=None, trainable=False):
Large_input = input
Large_input_pair = input_pair
input = F.interpolate(input, scale_factor=0.5)
Local_Features = self.backbone1(Large_input)
Local_Features = self.compress_layer(Local_Features)
Global_Feature = self.Transformer(input, label)
if trainable:
input_pair = F.interpolate(input_pair, scale_factor=0.5)
Local_Features_pair = self.backbone1(Large_input_pair)
Local_Features_pair = self.compress_layer(Local_Features_pair)
Global_Feature_pair = self.Transformer(input_pair, label)
output_distribution_raw_pair = torch.einsum("bqc,bchw->bqhw", F.normalize(Global_Feature, dim=-1), F.normalize(Local_Features_pair, dim=1))
output_distribution_raw_pair = F.softmax(output_distribution_raw_pair * self.logit_scale, dim=1)
R_img_pair = torch.einsum("bln,bnc->blc", Global_Feature.permute(0, 2, 1), output_distribution_raw_pair.flatten(2))
output_distribution_pair_raw = torch.einsum("bqc,bchw->bqhw", F.normalize(Global_Feature_pair, dim=-1), F.normalize(Local_Features, dim=1))
output_distribution_pair_raw = F.softmax(output_distribution_pair_raw * self.logit_scale, dim=1)
R_img = torch.einsum("bln,bnc->blc", Global_Feature_pair.permute(0, 2, 1), output_distribution_pair_raw.flatten(2))
Bs = R_img_pair.size(0)
R_img_pair = R_img_pair.view(Bs, self.d_model, 32, 32)
R_img = R_img.view(Bs, self.d_model, 32, 32)
Highlevel_loss_raw_pair = self.high_level_loss(R_img_pair, input_pair, VGG)
concentration_loss_raw_pair, area_loss_raw_pair = self.Concentration_loss(output_distribution_raw_pair)
Highlevel_loss_pair_raw = self.high_level_loss(R_img, input, VGG)
concentration_loss_pair_raw, area_loss_pair_raw = self.Concentration_loss(output_distribution_pair_raw)
Highlevel_loss = (Highlevel_loss_raw_pair + Highlevel_loss_pair_raw) / 2.0
Concentration_loss = (concentration_loss_raw_pair + concentration_loss_pair_raw) / 2.0
area_loss = (area_loss_raw_pair + area_loss_pair_raw) / 2.0
semantic_loss = self.ArcMarginProduct(Global_Feature[:, :self.num_part-self.background, :], label) \
+ self.ArcMarginProduct(Global_Feature_pair[:, :self.num_part-self.background, :], label)
semantic_loss = semantic_loss / 2.0
return area_loss, Highlevel_loss, Concentration_loss, semantic_loss
else:
Local_Features = F.interpolate(Local_Features, scale_factor=4.0, mode='bilinear')
output_distribution_raw_raw = torch.einsum("bqc,bchw->bqhw", F.normalize(Global_Feature, dim=-1), F.normalize(Local_Features, dim=1))
output_distribution_raw_raw = F.softmax(output_distribution_raw_raw * self.logit_scale, dim=1)
return output_distribution_raw_raw