-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels_counting_network.py
179 lines (150 loc) · 6.13 KB
/
models_counting_network.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
from functools import partial
import copy
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
import open_clip
from models_vit import CrossAttentionBlock
from util.pos_embed import get_2d_sincos_pos_embed
class CountingNetwork(nn.Module):
def __init__(
self,
img_encoder_num_output_tokens=196,
fim_embed_dim=512,
fim_depth=2,
fim_num_heads=16,
mlp_ratio=4.0,
norm_layer=partial(nn.LayerNorm, eps=1e-6),
):
super().__init__()
# --------------------------------------------------------------------------
# Feature interaction module specifics.
self.fim_num_img_tokens = img_encoder_num_output_tokens
# Use a fixed sin-cos embedding.
self.fim_pos_embed = nn.Parameter(
torch.zeros(1, self.fim_num_img_tokens, fim_embed_dim), requires_grad=False
)
self.fim_blocks = nn.ModuleList(
[
CrossAttentionBlock(
fim_embed_dim,
fim_num_heads,
mlp_ratio,
qkv_bias=True,
qk_scale=None,
norm_layer=norm_layer,
)
for _ in range(fim_depth)
]
)
self.fim_norm = norm_layer(fim_embed_dim)
# --------------------------------------------------------------------------
# Density map decoder regresssion module specifics.
self.decode_head0 = nn.Sequential(
nn.Conv2d(fim_embed_dim, 256, kernel_size=3, stride=1, padding=1),
nn.GroupNorm(8, 256),
nn.ReLU(inplace=True),
)
self.decode_head1 = nn.Sequential(
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.GroupNorm(8, 256),
nn.ReLU(inplace=True),
)
self.decode_head2 = nn.Sequential(
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.GroupNorm(8, 256),
nn.ReLU(inplace=True),
)
self.decode_head3 = nn.Sequential(
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
nn.GroupNorm(8, 256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 1, kernel_size=1, stride=1),
)
# --------------------------------------------------------------------------
self.initialize_weights()
# --------------------------------------------------------------------------
# CLIP model specifics (contains image and text encoder modules).
self.clip_model = open_clip.create_model(
"ViT-B-16", pretrained="laion2b_s34b_b88k"
)
# Freeze all the weights of the text encoder.
vis_copy = copy.deepcopy(self.clip_model.visual)
for param in self.clip_model.parameters():
param.requires_grad = False
self.clip_model.visual = vis_copy
def initialize_weights(self):
# Initialize the positional embedding for the feature interaction module.
fim_pos_embed = get_2d_sincos_pos_embed(
self.fim_pos_embed.shape[-1],
int(self.fim_num_img_tokens**0.5),
cls_token=False,
)
self.fim_pos_embed.data.copy_(
torch.from_numpy(fim_pos_embed).float().unsqueeze(0)
)
# Initialize nn.Linear and nn.LayerNorm layers.
self.apply(self._init_weights)
def _init_weights(self, m):
if isinstance(m, nn.Linear):
# We use Xavier uniform weight initialization following the official JAX ViT.
torch.nn.init.xavier_uniform_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.LayerNorm):
nn.init.constant_(m.weight, 1.0)
nn.init.constant_(m.bias, 0)
def forward_img_encoder(self, imgs):
return self.clip_model.encode_image(imgs)
def foward_txt_encoder(self, counting_queries):
return self.clip_model.encode_text(counting_queries)
def forward_fim(self, img_tokens, txt_tokens):
# Add positional embedding to image tokens.
img_tokens = img_tokens + self.fim_pos_embed
# Pass image tokens and counting query tokens through the feature interaction module.
x = img_tokens
for blk in self.fim_blocks:
x = blk(x, txt_tokens)
return self.fim_norm(x)
def forward_decoder(self, fim_output_tokens):
# Reshape the tokens output by the feature interaction module into a square feature map with [fim_embed_dim] channels.
n, hw, c = fim_output_tokens.shape
h = w = int(math.sqrt(hw))
x = fim_output_tokens.transpose(1, 2).reshape(n, c, h, w)
# Upsample output of this map to be N x [fim_embed_dim] x 24 x 24, as it was in CounTR.
x = F.interpolate(x, size=24, mode="bilinear", align_corners=False)
# Pass [x] through the density map regression decoder and upsample output until density map is the size of the input image.
x = F.interpolate(
self.decode_head0(x),
size=x.shape[-1] * 2,
mode="bilinear",
align_corners=False,
)
x = F.interpolate(
self.decode_head1(x),
size=x.shape[-1] * 2,
mode="bilinear",
align_corners=False,
)
x = F.interpolate(
self.decode_head2(x),
size=x.shape[-1] * 2,
mode="bilinear",
align_corners=False,
)
x = F.interpolate(
self.decode_head3(x),
size=x.shape[-1] * 2,
mode="bilinear",
align_corners=False,
)
# Remove the channel dimension from [x], as the density map only has 1 channel.
return x.squeeze(-3)
def forward(self, imgs, counting_queries):
img_tokens = self.forward_img_encoder(imgs)
# Add a token dimension to the CLIP text embeddings.
txt_tokens = self.foward_txt_encoder(counting_queries).unsqueeze(-2)
fim_output_tokens = self.forward_fim(img_tokens, txt_tokens)
pred = self.forward_decoder(fim_output_tokens)
return pred