deffit( self, model: "pl.LightningModule", train_dataloaders: Optional[Union[TRAIN_DATALOADERS, LightningDataModule]] = None, val_dataloaders: Optional[EVAL_DATALOADERS] = None, datamodule: Optional[LightningDataModule] = None, train_dataloader=None, # TODO: remove with 1.6 ckpt_path: Optional[str] = None, ) -> None: r""" Runs the full optimization routine. Args: model: Model to fit. train_dataloaders: A collection of :class:`torch.utils.data.DataLoader` or a :class:`~pytorch_lightning.core.datamodule.LightningDataModule` specifying training samples. In the case of multiple dataloaders, please see this :ref:`page <multiple-training-dataloaders>`. val_dataloaders: A :class:`torch.utils.data.DataLoader` or a sequence of them specifying validation samples. ckpt_path: Path/URL of the checkpoint from which training is resumed. If there is no checkpoint file at the path, an exception is raised. If resuming from mid-epoch checkpoint, training will start from the beginning of the next epoch. datamodule: An instance of :class:`~pytorch_lightning.core.datamodule.LightningDataModule`. """ if train_dataloader isnotNone: rank_zero_deprecation( "`trainer.fit(train_dataloader)` is deprecated in v1.4 and will be removed in v1.6." " Use `trainer.fit(train_dataloaders)` instead. HINT: added 's'" ) train_dataloaders = train_dataloader self._call_and_handle_interrupt( self._fit_impl, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path )
deftest_dataloader(self): # test dataset is the same as the evaluation dataset return wds.WebLoader(self.val_dataset, batch_size=self.batch_size, num_workers=self.num_workers, shuffle=False)
defconfigure_optimizers(self): lr = self.learning_rate if'all'inself.trained_param_keys: params = list(self.model.parameters()) # all model parameters else: names = [] params = [] for name, param inself.model.named_parameters(): flag = False for k inself.trained_param_keys: if k in name: names += [name] params += [param] flag = True if flag: break print(names)
for embedder inself.conditioner.embedders: # all conditioners are not trainable if embedder.is_trainable: params = params + list(embedder.parameters())
N = batch['video'].shape[0] # B x C x N_frames x H x W,这里 N 即 batch size ifnot infer: # 训练时,cond_aug 是随机的 cond_aug = ((-3.0) + (0.5) * torch.randn((N,))).exp().cuda().half() else: # 推理时,cond_aug 固定 cond_aug = torch.full((N, ), 0.02).cuda().half() batch['cond_aug'] = cond_aug batch['cond_frames'] = ( image + rearrange(cond_aug, 'b -> b 1 1 1') * torch.randn_like(image) ).half()
# for dataset without indicator ifnot'image_only_indicator'in batch: batch['image_only_indicator'] = torch.zeros((N, self.num_samples)).cuda().half() return batch
CLIP 编码第一帧作为
condition
1 2 3 4 5 6
defforward(self, vid): # 这里 vid 代表 batch['cond_frames_without_noise'],即第一帧图像 vid = self.open_clip(vid) vid = rearrange(vid, "(b t) d -> b t d", t=self.n_cond_frames) vid = repeat(vid, "b t d -> (b s) t d", s=self.n_copies) return vid