| 155 | |
| 156 | |
| 157 | class ImageHead(nn.Module): |
| 158 | def __init__( |
| 159 | self, |
| 160 | config: str, |
| 161 | model_weights: str, |
| 162 | save_directory: str, |
| 163 | embed_dim: int = 1024, |
| 164 | n_head: int = 4, |
| 165 | model_embed_dim: int = 1024, |
| 166 | dropout: float = 0.1, |
| 167 | freeze_backbone: bool = True, |
| 168 | ): |
| 169 | """ |
| 170 | Cross-attention image module that takes token outputs from the AstroDINO model and passes them through a |
| 171 | cross-attention mechanism and MLP to get the final embedding. |
| 172 | |
| 173 | Args: |
| 174 | save_directory (str): Path to the directory containing the AstroDINO model. |
| 175 | config (str): Path to the configuration file of the AstroDINO model. |
| 176 | model_weights (str): Path to the weights of the AstroDINO model. |
| 177 | embed_dim (int): Dimension of the AstroCLIP embedding. |
| 178 | n_head (int): Number of heads in the multihead attention. |
| 179 | model_embed_dim (int): Dimension of the AstroDINO embedding. |
| 180 | dropout (float): Dropout rate for MLP layers. |
| 181 | freeze_backbone (bool): Whether to freeze the backbone of the AstroDINO model. |
| 182 | """ |
| 183 | super().__init__() |
| 184 | |
| 185 | # Define DINO config |
| 186 | class config: |
| 187 | output_dir = save_directory |
| 188 | config_file = config |
| 189 | pretrained_weights = model_weights |
| 190 | opts = [] |
| 191 | |
| 192 | # Define DINO model |
| 193 | sys.stdout = open(os.devnull, "w") # Redirect stdout to null |
| 194 | self.backbone, _ = setup_and_build_model(config()) |
| 195 | sys.stdout = sys.__stdout__ # Reset stdout |
| 196 | |
| 197 | # Freeze backbone if necessary |
| 198 | self.freeze_backbone = freeze_backbone |
| 199 | if self.freeze_backbone: |
| 200 | for param in self.backbone.parameters(): |
| 201 | param.requires_grad = False |
| 202 | |
| 203 | # Set up cross-attention |
| 204 | self.cross_attention = CrossAttentionHead( |
| 205 | embed_dim=embed_dim, |
| 206 | n_head=n_head, |
| 207 | model_embed_dim=model_embed_dim, |
| 208 | dropout=dropout, |
| 209 | ) |
| 210 | |
| 211 | # Set up MLP |
| 212 | self.mlp = MLP( |
| 213 | in_features=embed_dim, |
| 214 | hidden_features=4 * embed_dim, |
nothing calls this directly
no outgoing calls
no test coverage detected