Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
script to generate aesthetic embeddings
  • Loading branch information
vicgalle committed Oct 8, 2022
commit b1a53a2135ee2ae8e3bc8d6d2d95a3e39fab1720
26 changes: 26 additions & 0 deletions scripts/gen_aesthetic_embeddings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import clip
import glob
from PIL import Image
import torch
import tqdm

# Just put your images in a folder inside reference_images/
aesthetic_style = "aivazovsky"
image_paths = glob.glob(f"reference_images/{aesthetic_style}/*")


device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-L/14", device=device)


with torch.no_grad():
embs = []
for path in tqdm.tqdm(image_paths):
image = preprocess(Image.open(path)).unsqueeze(0).to(device)
emb = model.encode_image(image)
embs.append(emb.cpu())

embs = torch.cat(embs, dim=0).mean(dim=0, keepdim=True)

# The generated embedding will be located here
torch.save(embs, f"aesthetic_embeddings/{aesthetic_style}.pt")