forked from guyyariv/AudioToken
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
205 lines (173 loc) · 9.4 KB
/
inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import argparse
import logging
import os
import torch
import torch.utils.checkpoint
from torch.utils.data import Dataset
import datasets
import diffusers
import transformers
from accelerate import Accelerator
from accelerate.logging import get_logger
from accelerate.utils import set_seed
from diffusers import StableDiffusionPipeline
from diffusers.utils import check_min_version
# TODO: remove and import from diffusers.utils when the new version of diffusers is released
from transformers import CLIPTokenizer
from data.dataloader import AudioTokenVGGSound
from modules.AudioToken.AudioToken import AudioTokenWrapper
# Will error if the minimal version of diffusers is not installed. Remove at your own risks.
check_min_version("0.12.0")
logger = get_logger(__name__)
def parse_args():
parser = argparse.ArgumentParser(description="Simple example of a training script.")
parser.add_argument("--learned_embeds", type=str, default='output/embedder_learned_embeds.bin',
help="Path to pretrained embedder")
parser.add_argument("--learned_embeds_lora", type=str, default='output/lora_layers_learned_embeds.bin',
help="Path to pretrained embedder")
parser.add_argument("--pretrained_model_name_or_path", type=str, default='stabilityai/stable-diffusion-2',
help="Path to pretrained model or model identifier from huggingface.co/models.")
parser.add_argument("--revision", type=str, default=None, required=False,
help="Revision of pretrained model identifier from huggingface.co/models.")
parser.add_argument("--tokenizer_name", type=str, default=None,
help="Pretrained tokenizer name or path if not the same as model_name")
parser.add_argument("--train_data_dir", type=str,
help="A folder containing the training data.")
parser.add_argument("--placeholder_token", type=str, default="<*>",
help="A token to use as a placeholder for the audio.",)
parser.add_argument("--output_dir", type=str, default="output",
help="The output directory where the model predictions and checkpoints will be written.")
parser.add_argument("--seed", type=int, default=None,
help="A seed for reproducible training.")
parser.add_argument("--resolution", type=int, default=768,
help="The resolution for input images, all the images in the train/validation"
" dataset will be resized to this resolution")
parser.add_argument("--dataloader_num_workers", type=int, default=0,
help="Number of subprocesses to use for data loading."
" 0 means that the data will be loaded in the main process.")
parser.add_argument("--logging_dir", type=str, default="logs",
help="[TensorBoard](https://www.tensorflow.org/tensorboard) log directory."
" Will default to *output_dir/runs/**CURRENT_DATETIME_HOSTNAME***.")
parser.add_argument("--mixed_precision", type=str, default="no", choices=["no", "fp16", "bf16"],
help="Whether to use mixed precision. Choose between fp16 and bf16 (bfloat16)."
" Bf16 requires PyTorch >= 1.10. and an Nvidia Ampere GPU.")
parser.add_argument("--allow_tf32", action="store_true",
help="Whether or not to allow TF32 on Ampere GPUs. Can be used to speed up training."
" For more information, see"
" https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices")
parser.add_argument("--report_to", type=str, default="tensorboard",
help='The integration to report the results and logs to. Supported platforms are `"tensorboard"`'
' (default), `"wandb"` and `"comet_ml"`. Use `"all"` to report to all integrations.')
parser.add_argument("--num_inference_steps", type=int, default=50)
parser.add_argument("--data_set", type=str, default='test', choices=['train', 'test'],
help="Whether use train or test set")
parser.add_argument("--generation_steps", type=int, default=50)
parser.add_argument("--run_name", type=str, default='AudioToken',
help="Insert run name")
parser.add_argument("--set_size", type=str, default='full')
parser.add_argument("--prompt", type=str, default='a photo of <*>, 4k, high resolution')
parser.add_argument("--input_length", type=int, default=10,
help="Select the number of seconds of audio you want in each test-sample.")
parser.add_argument("--lora", type=bool, default=True,
help="Whether load Lora layers or not")
args = parser.parse_args()
env_local_rank = int(os.environ.get("LOCAL_RANK", -1))
if env_local_rank != -1 and env_local_rank != args.local_rank:
args.local_rank = env_local_rank
if args.train_data_dir is None:
raise ValueError("You must specify a train data directory.")
return args
def inference(args):
logging_dir = os.path.join(args.output_dir, args.logging_dir)
folder_path = "output/"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
folder_path = "output/imgs/"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
folder_path = f"output/imgs/{args.run_name}/"
if not os.path.exists(folder_path):
os.makedirs(folder_path)
accelerator = Accelerator(
mixed_precision=args.mixed_precision,
log_with=args.report_to,
logging_dir=logging_dir,
)
# Make one log on every process with the configuration for debugging.
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO,
)
logger.info(accelerator.state, main_process_only=False)
if accelerator.is_local_main_process:
datasets.utils.logging.set_verbosity_warning()
transformers.utils.logging.set_verbosity_warning()
diffusers.utils.logging.set_verbosity_info()
else:
datasets.utils.logging.set_verbosity_error()
transformers.utils.logging.set_verbosity_error()
diffusers.utils.logging.set_verbosity_error()
# If passed along, set the training seed now.
if args.seed is not None:
set_seed(args.seed)
# Load tokenizer
if args.tokenizer_name:
tokenizer = CLIPTokenizer.from_pretrained(args.tokenizer_name)
elif args.pretrained_model_name_or_path:
tokenizer = CLIPTokenizer.from_pretrained(args.pretrained_model_name_or_path, subfolder="tokenizer")
weight_dtype = torch.float32
if accelerator.mixed_precision == "fp16":
weight_dtype = torch.float16
elif accelerator.mixed_precision == "bf16":
weight_dtype = torch.bfloat16
model = AudioTokenWrapper(args, accelerator).to(weight_dtype).eval()
# Add the placeholder token in tokenizer
num_added_tokens = tokenizer.add_tokens(args.placeholder_token)
if num_added_tokens == 0:
raise ValueError(
f"The tokenizer already contains the token {args.placeholder_token}. Please pass a different"
" `placeholder_token` that is not already in the tokenizer."
)
# Enable TF32 for faster training on Ampere GPUs,
# cf https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices
if args.allow_tf32:
torch.backends.cuda.matmul.allow_tf32 = True
test_dataset = AudioTokenVGGSound(
args=args,
tokenizer=tokenizer,
logger=logger,
size=args.resolution,
)
test_dataloader = torch.utils.data.DataLoader(
test_dataset, batch_size=1, shuffle=True, num_workers=args.dataloader_num_workers
)
# Prepare everything with our `accelerator`.
model, test_dataloader = accelerator.prepare(
model, test_dataloader
)
placeholder_token_id = tokenizer.convert_tokens_to_ids(args.placeholder_token)
# Resize the token embeddings as we are adding new special tokens to the tokenizer
accelerator.unwrap_model(model).text_encoder.resize_token_embeddings(len(tokenizer))
prompt = args.prompt
for step, batch in enumerate(test_dataloader):
if step >= args.generation_steps:
break
# Audio's feature extraction
audio_values = batch["audio_values"].to(accelerator.device).to(dtype=weight_dtype)
aud_features = accelerator.unwrap_model(model).aud_encoder.extract_features(audio_values)[1]
audio_token = accelerator.unwrap_model(model).embedder(aud_features)
token_embeds = model.text_encoder.get_input_embeddings().weight.data
token_embeds[placeholder_token_id] = audio_token.clone()
pipeline = StableDiffusionPipeline.from_pretrained(
args.pretrained_model_name_or_path,
tokenizer=tokenizer,
text_encoder=accelerator.unwrap_model(model).text_encoder,
vae=accelerator.unwrap_model(model).vae,
unet=accelerator.unwrap_model(model).unet,
).to(accelerator.device)
image = pipeline(prompt, num_inference_steps=args.num_inference_steps, guidance_scale=7.5).images[0]
image.save(f'output/imgs/{args.run_name}/{batch["full_name"][0]}.png')
if __name__ == "__main__":
args = parse_args()
inference(args)