|
import torch |
|
|
|
from enhance_a_video.globals import get_enhance_weight |
|
|
|
|
|
def enhance_score(query_image, key_image, head_dim, num_frames): |
|
scale = head_dim**-0.5 |
|
query_image = query_image * scale |
|
attn_temp = query_image @ key_image.transpose(-2, -1) |
|
attn_temp = attn_temp.to(torch.float32) |
|
attn_temp = attn_temp.softmax(dim=-1) |
|
|
|
|
|
attn_temp = attn_temp.reshape(-1, num_frames, num_frames) |
|
|
|
|
|
diag_mask = torch.eye(num_frames, device=attn_temp.device).bool() |
|
diag_mask = diag_mask.unsqueeze(0).expand(attn_temp.shape[0], -1, -1) |
|
|
|
|
|
attn_wo_diag = attn_temp.masked_fill(diag_mask, 0) |
|
|
|
|
|
|
|
num_off_diag = num_frames * num_frames - num_frames |
|
mean_scores = attn_wo_diag.sum(dim=(1, 2)) / num_off_diag |
|
|
|
enhance_scores = mean_scores.mean() * (num_frames + get_enhance_weight()) |
|
enhance_scores = enhance_scores.clamp(min=1) |
|
return enhance_scores |
|
|