Spaces:
Runtime error
Runtime error
Update models/query_generator.py
Browse files- 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 |
-
|
| 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 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|