Karthika0308 commited on
Commit
fe5e2a5
·
verified ·
1 Parent(s): 28b027c

Update models/query_generator.py

Browse files
Files changed (1) hide show
  1. models/query_generator.py +95 -81
models/query_generator.py CHANGED
@@ -113,84 +113,98 @@ class C_base(nn.Module):
113
  return reference_points
114
 
115
  def forward(
116
- self,
117
- image_embeddings: torch.Tensor,
118
- image_pe: torch.Tensor,
119
- prototype_embeddings: torch.Tensor,
120
- hq_features: torch.Tensor,
121
- hq_prototypes: torch.Tensor,
122
- hq_pos: torch.Tensor,
123
- ) -> Tuple[torch.Tensor, torch.Tensor]:
124
- """
125
-
126
- """
127
- if self.spatial_shapes.device != image_embeddings.device:
128
- self.spatial_shapes = self.spatial_shapes.to(image_embeddings.device)
129
- self.spatial_shapes1 = self.spatial_shapes1.to(image_embeddings.device)
130
- self.spatial_shapes2 = self.spatial_shapes2.to(image_embeddings.device)
131
- self.level_start_index = self.level_start_index.to(image_embeddings.device)
132
- self.level_start_index1 = self.level_start_index1.to(image_embeddings.device)
133
- self.level_start_index2 = self.level_start_index2.to(image_embeddings.device)
134
- self.valid_ratios = self.valid_ratios.to(image_embeddings.device)
135
- self.valid_ratios1 = self.valid_ratios1.to(image_embeddings.device)
136
- self.valid_ratios2 = self.valid_ratios2.to(image_embeddings.device)
137
- self.reference_points1 = self.get_reference_points(self.spatial_shapes1, self.valid_ratios1, device=image_embeddings.device)
138
- self.reference_points2 = self.get_reference_points(self.spatial_shapes2, self.valid_ratios2, device=image_embeddings.device)
139
- self.reference_points = self.get_reference_points(self.spatial_shapes, self.valid_ratios, device=image_embeddings.device)
140
-
141
- b, c, h, w = image_embeddings.shape
142
- image_pe = torch.repeat_interleave(image_pe, image_embeddings.shape[0], dim=0)
143
- image_embeddings = image_embeddings.flatten(2).permute(0, 2, 1)
144
- image_pe = image_pe.flatten(2).permute(0, 2, 1)
145
- src = image_embeddings
146
-
147
-
148
- hq_features_l1_pos = hq_pos[0].flatten(2).permute(0, 2, 1)
149
- hq_features_l2_pos = hq_pos[1].flatten(2).permute(0, 2, 1)
150
-
151
- hq_features_l1 = hq_features[0].flatten(2).permute(0, 2, 1)
152
- hq_features_l2 = hq_features[1].flatten(2).permute(0, 2, 1)
153
-
154
-
155
-
156
-
157
-
158
- for layer in self.prototype_attention:
159
- src = layer(image_f=src,
160
- prototypes=prototype_embeddings)
161
-
162
- for layer in self.prototype_attention_l1:
163
- hq_features_l1 = layer(image_f=hq_features_l1,
164
- prototypes=hq_prototypes[0])
165
-
166
- for layer in self.prototype_attention_l2:
167
- hq_features_l2 = layer(image_f=hq_features_l2,
168
- prototypes=hq_prototypes[1])
169
-
170
-
171
-
172
-
173
-
174
- for layer in self.image_attention:
175
- src = layer((src+image_pe),self.reference_points,src,self.spatial_shapes, self.level_start_index)
176
-
177
-
178
- for layer in self.image_attention_l1:
179
- hq_features_l1 = layer((hq_features_l1 +hq_features_l1_pos), self.reference_points1, hq_features_l1, self.spatial_shapes1, self.level_start_index1)
180
-
181
-
182
-
183
- for layer in self.image_attention_l2:
184
- hq_features_l2 = layer((hq_features_l2+hq_features_l2_pos), self.reference_points2, hq_features_l2, self.spatial_shapes2, self.level_start_index2)
185
-
186
- src = src.transpose(1, 2).reshape(b, c, h, w)
187
- hq_features_l2 = hq_features_l2.transpose(1, 2).view(b, c, h*2, w*2)
188
- hq_features_l1 = hq_features_l1.transpose(1, 2).view(b, c, h*4, w*4)
189
-
190
- src = self.up1(src) + hq_features_l2
191
- src = self.up2(src) + hq_features_l1
192
- src = self.up3(src)
193
-
194
- src_aux = self.up_aux(hq_features_l1)
195
-
196
- return src, src_aux
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  return reference_points
114
 
115
  def forward(
116
+ self,
117
+ image_embeddings: torch.Tensor,
118
+ image_pe: torch.Tensor,
119
+ prototype_embeddings: torch.Tensor,
120
+ hq_features: torch.Tensor,
121
+ hq_prototypes: torch.Tensor,
122
+ hq_pos: torch.Tensor,
123
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
124
+
125
+ # ALWAYS ensure tensors are on correct device
126
+ device = image_embeddings.device
127
+
128
+ self.spatial_shapes = self.spatial_shapes.to(device)
129
+ self.spatial_shapes1 = self.spatial_shapes1.to(device)
130
+ self.spatial_shapes2 = self.spatial_shapes2.to(device)
131
+
132
+ self.level_start_index = self.level_start_index.to(device)
133
+ self.level_start_index1 = self.level_start_index1.to(device)
134
+ self.level_start_index2 = self.level_start_index2.to(device)
135
+
136
+ self.valid_ratios = self.valid_ratios.to(device)
137
+ self.valid_ratios1 = self.valid_ratios1.to(device)
138
+ self.valid_ratios2 = self.valid_ratios2.to(device)
139
+
140
+ # 🔥 ALWAYS create reference points (not inside condition)
141
+ self.reference_points = self.get_reference_points(
142
+ self.spatial_shapes, self.valid_ratios, device=device
143
+ )
144
+ self.reference_points1 = self.get_reference_points(
145
+ self.spatial_shapes1, self.valid_ratios1, device=device
146
+ )
147
+ self.reference_points2 = self.get_reference_points(
148
+ self.spatial_shapes2, self.valid_ratios2, device=device
149
+ )
150
+
151
+ b, c, h, w = image_embeddings.shape
152
+
153
+ image_pe = torch.repeat_interleave(image_pe, b, dim=0)
154
+
155
+ image_embeddings = image_embeddings.flatten(2).permute(0, 2, 1)
156
+ image_pe = image_pe.flatten(2).permute(0, 2, 1)
157
+
158
+ src = image_embeddings
159
+
160
+ hq_features_l1_pos = hq_pos[0].flatten(2).permute(0, 2, 1)
161
+ hq_features_l2_pos = hq_pos[1].flatten(2).permute(0, 2, 1)
162
+
163
+ hq_features_l1 = hq_features[0].flatten(2).permute(0, 2, 1)
164
+ hq_features_l2 = hq_features[1].flatten(2).permute(0, 2, 1)
165
+
166
+ # Prototype attention
167
+ for layer in self.prototype_attention:
168
+ src = layer(image_f=src, prototypes=prototype_embeddings)
169
+
170
+ for layer in self.prototype_attention_l1:
171
+ hq_features_l1 = layer(image_f=hq_features_l1, prototypes=hq_prototypes[0])
172
+
173
+ for layer in self.prototype_attention_l2:
174
+ hq_features_l2 = layer(image_f=hq_features_l2, prototypes=hq_prototypes[1])
175
+
176
+ # Image attention
177
+ for layer in self.image_attention:
178
+ src = layer(src + image_pe, self.reference_points, src, self.spatial_shapes, self.level_start_index)
179
+
180
+ for layer in self.image_attention_l1:
181
+ hq_features_l1 = layer(
182
+ hq_features_l1 + hq_features_l1_pos,
183
+ self.reference_points1,
184
+ hq_features_l1,
185
+ self.spatial_shapes1,
186
+ self.level_start_index1,
187
+ )
188
+
189
+ for layer in self.image_attention_l2:
190
+ hq_features_l2 = layer(
191
+ hq_features_l2 + hq_features_l2_pos,
192
+ self.reference_points2,
193
+ hq_features_l2,
194
+ self.spatial_shapes2,
195
+ self.level_start_index2,
196
+ )
197
+
198
+ # reshape back
199
+ src = src.transpose(1, 2).reshape(b, c, h, w)
200
+ hq_features_l2 = hq_features_l2.transpose(1, 2).view(b, c, h * 2, w * 2)
201
+ hq_features_l1 = hq_features_l1.transpose(1, 2).view(b, c, h * 4, w * 4)
202
+
203
+ # Upsampling
204
+ src = self.up1(src) + hq_features_l2
205
+ src = self.up2(src) + hq_features_l1
206
+ src = self.up3(src)
207
+
208
+ src_aux = self.up_aux(hq_features_l1)
209
+
210
+ return src, src_aux