Download src/utils/edit_directions.py from multimodalart/pix2pix-zero: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/multimodalart/pix2pix-zero/resolve/main/src/utils/edit_directions.py
- Command line
-
hf download hf://spaces/multimodalart/pix2pix-zero/src/utils/edit_directions.py
-
curl -L -o edit_directions.py https://huggingface.co/spaces/multimodalart/pix2pix-zero/resolve/main/src/utils/edit_directions.py
1.21 kB
| import os | |
| import torch | |
| """ | |
| This function takes in a task name and returns the direction in the embedding space that transforms class A to class B for the given task. | |
| Parameters: | |
| task_name (str): name of the task for which direction is to be constructed. | |
| Returns: | |
| torch.Tensor: A tensor representing the direction in the embedding space that transforms class A to class B. | |
| Examples: | |
| construct_direction("cat2dog") | |
| """ | |
| def construct_direction(task_name): | |
| if task_name=="cat2dog": | |
| emb_dir = f"assets/embeddings_sd_1.4" | |
| embs_a = torch.load(os.path.join(emb_dir, f"cat.pt")) | |
| embs_b = torch.load(os.path.join(emb_dir, f"dog.pt")) | |
| return (embs_b.mean(0)-embs_a.mean(0)).unsqueeze(0) | |
| elif task_name=="dog2cat": | |
| emb_dir = f"assets/embeddings_sd_1.4" | |
| embs_a = torch.load(os.path.join(emb_dir, f"dog.pt")) | |
| embs_b = torch.load(os.path.join(emb_dir, f"cat.pt")) | |
| return (embs_b.mean(0)-embs_a.mean(0)).unsqueeze(0) | |
| else: | |
| raise NotImplementedError | |
| def construct_direction_prompts(source_prompt_embeddings, target_prompt_embeddings): | |
| return((target_prompt_embeddings.mean(0)-source_prompt_embeddings.mean(0)).unsqueeze(0)) |