Advertisement
ethansmith2000

Untitled

May 11th, 2024
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. from PIL import Image
  2. import torchvision.transforms as T
  3. import torch
  4.  
  5. img = Image.open("test.png").convert("RGB").resize((128,128))
  6. img = T.ToTensor()(img).unsqueeze(0).to("cuda") * 2 - 1
  7. img = img.permute(0,2,3,1).reshape(1,-1,3)
  8.  
  9. steps = 5
  10. step_size = 0.95
  11. do_attention = True
  12.  
  13. with torch.no_grad():
  14.     for i in range(steps):
  15.         q = k = torch.nn.functional.normalize(img, dim=-1)
  16.         output = torch.nn.functional.scaled_dot_product_attention(q, k, img, dropout_p=0.0, is_causal=False, scale=100.0)
  17.         img = img * (1-step_size) + output * step_size
  18.         img = img.clamp(-1, 1)
  19.  
  20. img = img.reshape(1,128,128,3).permute(0,3,1,2)
  21. img = (img + 1) / 2
  22. img = T.ToPILImage()(img[0])
  23.  
Tags: Cluster
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement