Visual Tokenizer Pre-training Model. A unified framework for vision-language pre-training that supports: - Contrastive learning (CLIP-style) - Self-supervised learning (DINOv2-style) - Image reconstruction
| 86 | |
| 87 | |
| 88 | class VTP(nn.Module): |
| 89 | """Visual Tokenizer Pre-training Model. |
| 90 | |
| 91 | A unified framework for vision-language pre-training that supports: |
| 92 | - Contrastive learning (CLIP-style) |
| 93 | - Self-supervised learning (DINOv2-style) |
| 94 | - Image reconstruction |
| 95 | """ |
| 96 | |
| 97 | def __init__( |
| 98 | self, |
| 99 | vtp_config: Optional[DictConfig] = None, |
| 100 | config_path: Optional[str] = None, |
| 101 | cli_overrides: Optional[Union[Sequence[str], DictConfig]] = None, |
| 102 | ): |
| 103 | """ |
| 104 | Args: |
| 105 | vtp_config: Pre-constructed configuration (takes priority). |
| 106 | config_path: Path to load YAML from when vtp_config is not provided. |
| 107 | cli_overrides: Additional CLI-style configuration overrides, e.g., ["training.lr=1e-4"]. |
| 108 | """ |
| 109 | super().__init__() |
| 110 | |
| 111 | self.vtp_config = self._load_vtp_config( |
| 112 | vtp_config=vtp_config, |
| 113 | config_path=config_path, |
| 114 | cli_overrides=cli_overrides, |
| 115 | ) |
| 116 | self._init_vision_components() |
| 117 | self._init_text_components() |
| 118 | |
| 119 | def _load_vtp_config( |
| 120 | self, |
| 121 | vtp_config: Optional[DictConfig], |
| 122 | config_path: Optional[str], |
| 123 | cli_overrides: Optional[Union[Sequence[str], DictConfig]], |
| 124 | ) -> DictConfig: |
| 125 | if config_path is not None and not os.path.isabs(config_path): |
| 126 | config_path = os.path.abspath(config_path) |
| 127 | |
| 128 | if vtp_config is not None: |
| 129 | if isinstance(vtp_config, DictConfig): |
| 130 | loaded_config = vtp_config |
| 131 | else: |
| 132 | cfg_dict = self._object_to_config_dict(vtp_config) |
| 133 | loaded_config = OmegaConf.create(cfg_dict) |
| 134 | else: |
| 135 | if config_path is None: |
| 136 | raise ValueError("Either vtp_config or config_path must be provided") |
| 137 | if not os.path.exists(config_path): |
| 138 | raise FileNotFoundError(f"Configuration file not found: {config_path}") |
| 139 | loaded_config = OmegaConf.load(config_path) |
| 140 | |
| 141 | if cli_overrides: |
| 142 | if isinstance(cli_overrides, DictConfig): |
| 143 | overrides_cfg = cli_overrides |
| 144 | else: |
| 145 | if isinstance(cli_overrides, (list, tuple)): |
nothing calls this directly
no outgoing calls
no test coverage detected