MCPcopy Create free account
hub / github.com/Francis-Rings/FlashPortrait / CLIPModel

Class CLIPModel

wan/models/wan_image_encoder.py:503–553  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

501
502
503class CLIPModel(ModelMixin, ConfigMixin, FromOriginalModelMixin):
504
505 def __init__(self):
506 super(CLIPModel, self).__init__()
507 # init model
508 self.model, self.transforms = clip_xlm_roberta_vit_h_14(
509 pretrained=False,
510 return_transforms=True,
511 return_tokenizer=False)
512
513 def forward(self, videos):
514 # preprocess
515 size = (self.model.image_size,) * 2
516 videos = torch.cat([
517 F.interpolate(
518 u.transpose(0, 1),
519 size=size,
520 mode='bicubic',
521 align_corners=False) for u in videos
522 ])
523 videos = self.transforms.transforms[-1](videos.mul_(0.5).add_(0.5))
524
525 # forward
526 with torch.cuda.amp.autocast(dtype=self.dtype):
527 out = self.model.visual(videos, use_31_block=True)
528 return out
529
530 @classmethod
531 def from_pretrained(cls, pretrained_model_path, transformer_additional_kwargs={}):
532 def filter_kwargs(cls, kwargs):
533 import inspect
534 sig = inspect.signature(cls.__init__)
535 valid_params = set(sig.parameters.keys()) - {'self', 'cls'}
536 filtered_kwargs = {k: v for k, v in kwargs.items() if k in valid_params}
537 return filtered_kwargs
538
539 model = cls(**filter_kwargs(cls, transformer_additional_kwargs))
540 if pretrained_model_path.endswith(".safetensors"):
541 from safetensors.torch import load_file, safe_open
542 state_dict = load_file(pretrained_model_path)
543 else:
544 state_dict = torch.load(pretrained_model_path, map_location="cpu")
545 tmp_state_dict = {}
546 for key in state_dict:
547 tmp_state_dict["model." + key] = state_dict[key]
548 state_dict = tmp_state_dict
549 m, u = model.load_state_dict(state_dict)
550
551 print(f"### missing keys: {len(m)}; \n### unexpected keys: {len(u)};")
552 print(m, u)
553 return model

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected