| import torch |
| import torch.nn as nn |
| from transformers import CLIPVisionModel |
|
|
|
|
| class CreativeScorer(nn.Module): |
| def __init__(self): |
| super().__init__() |
| |
| self.clip = CLIPVisionModel.from_pretrained( |
| "openai/clip-vit-base-patch32", |
| use_safetensors=True, |
| ) |
| for param in self.clip.parameters(): |
| param.requires_grad = False |
|
|
| |
| assert not any(p.requires_grad for p in self.clip.parameters()) |
|
|
| |
| self.projection = nn.Sequential( |
| nn.Linear(768, 256), |
| nn.ReLU(), |
| nn.Dropout(0.2), |
| ) |
| self.ctr_head = nn.Linear(256, 1) |
| self.fatigue_head = nn.Linear(256, 2) |
|
|
| def forward(self, pixel_values=None, embedding=None): |
| if embedding is not None: |
| |
| pass |
| else: |
| with torch.no_grad(): |
| clip_out = self.clip(pixel_values=pixel_values) |
| embedding = clip_out.pooler_output |
|
|
| shared = self.projection(embedding) |
| ctr_logit = self.ctr_head(shared) |
| ctr_score = torch.sigmoid(ctr_logit) |
| weibull_params = self.fatigue_head(shared) |
|
|
| return { |
| "ctr_score": ctr_score, |
| "weibull_params": weibull_params, |
| "shared_repr": shared, |
| } |
|
|