It is based on PLOT.
| 264 | |
| 265 | @TRAINER_REGISTRY.register() |
| 266 | class PLOTPP(TrainerX): |
| 267 | """ |
| 268 | It is based on PLOT. |
| 269 | """ |
| 270 | |
| 271 | def check_cfg(self, cfg): |
| 272 | assert cfg.TRAINER.PLOTPP.PREC in ["fp16", "fp32", "amp"] |
| 273 | |
| 274 | def build_model(self): |
| 275 | cfg = self.cfg |
| 276 | classnames = self.dm.dataset.classnames |
| 277 | name_tp_update = cfg.TRAINER.PLOTPP.MODEL_UPD |
| 278 | |
| 279 | print(f"Loading CLIP (backbone: {cfg.MODEL.BACKBONE.NAME})") |
| 280 | clip_model = load_clip_to_cpu(cfg) |
| 281 | |
| 282 | if cfg.TRAINER.PLOTPP.PREC == "fp32" or cfg.TRAINER.PLOTPP.PREC == "amp": |
| 283 | # CLIP's default precision is fp16 |
| 284 | clip_model.float() |
| 285 | |
| 286 | print("Building custom CLIP") |
| 287 | self.model = CustomCLIP(cfg, classnames, clip_model) |
| 288 | |
| 289 | print("Turning off gradients in both the image and the text encoder") |
| 290 | for name, param in self.model.named_parameters(): |
| 291 | |
| 292 | if "prompt_learner" not in name: |
| 293 | param.requires_grad_(False) |
| 294 | else: |
| 295 | if name_tp_update == "vision" and name_tp_update not in name: |
| 296 | param.requires_grad_(False) |
| 297 | # Double check |
| 298 | enabled = set() |
| 299 | for name, param in self.model.named_parameters(): |
| 300 | if param.requires_grad: |
| 301 | enabled.add(name) |
| 302 | |
| 303 | print(f"Parameters to be updated: {enabled}") |
| 304 | |
| 305 | # if cfg.MODEL.INIT_WEIGHTS: |
| 306 | # load_pretrained_weights(self.model.prompt_learner, cfg.MODEL.INIT_WEIGHTS) |
| 307 | |
| 308 | if cfg.TRAINER.PLOTPP.PRETRAIN_DIR: |
| 309 | load_pretrained_weights(self.model, cfg.TRAINER.PLOTPP.PRETRAIN_DIR) |
| 310 | |
| 311 | device_count = torch.cuda.device_count() |
| 312 | if cfg.DATASET.NAME == 'ImageNet': |
| 313 | self.device = torch.device("cuda:0") |
| 314 | device1 = torch.device("cuda") |
| 315 | self.model.to(self.device) |
| 316 | self.model.text_encoder.to(device1) |
| 317 | self.model.text_encoder=nn.DataParallel(self.model.text_encoder) |
| 318 | elif device_count > 1: |
| 319 | print(f"Multiple GPUs detected (n_gpus={device_count}), use all of them!") |
| 320 | self.device = torch.device("cuda") |
| 321 | self.model.to(self.device) |
| 322 | self.model = nn.DataParallel(self.model) |
| 323 | else: |
nothing calls this directly
no outgoing calls
no test coverage detected