Update inference.py
Browse files- inference.py +118 -117
inference.py
CHANGED
|
@@ -1,117 +1,118 @@
|
|
| 1 |
-
from pathlib import Path
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
args.
|
| 29 |
-
args.
|
| 30 |
-
args.
|
| 31 |
-
args.
|
| 32 |
-
args.
|
| 33 |
-
args.
|
| 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 |
-
postprocessor
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
import hydra
|
| 3 |
+
import torch
|
| 4 |
+
from omegaconf import DictConfig
|
| 5 |
+
from slider import Beatmap
|
| 6 |
+
from argparse import Namespace
|
| 7 |
+
from torch.serialization import add_safe_globals
|
| 8 |
+
|
| 9 |
+
# Trust custom objects in your checkpoint
|
| 10 |
+
add_safe_globals([Namespace])
|
| 11 |
+
|
| 12 |
+
from osudiffusion import DiT_models
|
| 13 |
+
from osuT5.inference import Preprocessor, Pipeline, Postprocessor, DiffisionPipeline
|
| 14 |
+
from osuT5.tokenizer import Tokenizer
|
| 15 |
+
from osuT5.utils import get_model
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def get_args_from_beatmap(args: DictConfig):
|
| 19 |
+
if args.beatmap_path is None or args.beatmap_path == "":
|
| 20 |
+
return
|
| 21 |
+
|
| 22 |
+
beatmap_path = Path(args.beatmap_path)
|
| 23 |
+
|
| 24 |
+
if not beatmap_path.is_file():
|
| 25 |
+
raise FileNotFoundError(f"Beatmap file {beatmap_path} not found.")
|
| 26 |
+
|
| 27 |
+
beatmap = Beatmap.from_path(beatmap_path)
|
| 28 |
+
args.audio_path = beatmap_path.parent / beatmap.audio_filename
|
| 29 |
+
args.output_path = beatmap_path.parent
|
| 30 |
+
args.bpm = beatmap.bpm_max()
|
| 31 |
+
args.offset = min(tp.offset.total_seconds() * 1000 for tp in beatmap.timing_points)
|
| 32 |
+
args.slider_multiplier = beatmap.slider_multiplier
|
| 33 |
+
args.title = beatmap.title
|
| 34 |
+
args.artist = beatmap.artist
|
| 35 |
+
args.beatmap_id = beatmap.beatmap_id if args.beatmap_id == -1 else args.beatmap_id
|
| 36 |
+
args.diffusion.style_id = beatmap.beatmap_id if args.diffusion.style_id == -1 else args.diffusion.style_id
|
| 37 |
+
args.difficulty = float(beatmap.stars()) if args.difficulty == -1 else args.difficulty
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def find_model(ckpt_path, args: DictConfig, device):
|
| 41 |
+
assert Path(ckpt_path).exists(), f"Could not find DiT checkpoint at {ckpt_path}"
|
| 42 |
+
|
| 43 |
+
# Force full unpickling because we trust the checkpoint
|
| 44 |
+
checkpoint = torch.load(ckpt_path, weights_only=False, map_location=lambda storage, loc: storage)
|
| 45 |
+
if "ema" in checkpoint: # supports checkpoints from train.py
|
| 46 |
+
checkpoint = checkpoint["ema"]
|
| 47 |
+
|
| 48 |
+
model = DiT_models[args.diffusion.model](
|
| 49 |
+
num_classes=args.diffusion.num_classes,
|
| 50 |
+
context_size=19 - 3 + 128,
|
| 51 |
+
).to(device)
|
| 52 |
+
model.load_state_dict(checkpoint)
|
| 53 |
+
model.eval() # important!
|
| 54 |
+
return model
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@hydra.main(config_path="configs", config_name="inference", version_base="1.1")
|
| 58 |
+
def main(args: DictConfig):
|
| 59 |
+
get_args_from_beatmap(args)
|
| 60 |
+
|
| 61 |
+
torch.set_grad_enabled(False)
|
| 62 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 63 |
+
ckpt_path = Path(args.model_path)
|
| 64 |
+
|
| 65 |
+
# Trust the tokenizer checkpoint
|
| 66 |
+
model_state = torch.load(ckpt_path / "pytorch_model.bin", map_location=device)
|
| 67 |
+
tokenizer_state = torch.load(ckpt_path / "custom_checkpoint_0.pkl", weights_only=False)
|
| 68 |
+
|
| 69 |
+
tokenizer = Tokenizer()
|
| 70 |
+
tokenizer.load_state_dict(tokenizer_state)
|
| 71 |
+
|
| 72 |
+
model = get_model(args, tokenizer)
|
| 73 |
+
model.load_state_dict(model_state)
|
| 74 |
+
model.eval()
|
| 75 |
+
model.to(device)
|
| 76 |
+
|
| 77 |
+
preprocessor = Preprocessor(args)
|
| 78 |
+
audio = preprocessor.load(args.audio_path)
|
| 79 |
+
sequences = preprocessor.segment(audio)
|
| 80 |
+
total_duration_ms = len(audio) / 16000 * 1000
|
| 81 |
+
args.total_duration_ms = total_duration_ms
|
| 82 |
+
|
| 83 |
+
generated_maps = []
|
| 84 |
+
generated_positions = []
|
| 85 |
+
diffs = []
|
| 86 |
+
|
| 87 |
+
if args.full_set:
|
| 88 |
+
for i in range(args.set_difficulties):
|
| 89 |
+
diffs.append(3 + i * (7 - 3) / (args.set_difficulties - 1))
|
| 90 |
+
|
| 91 |
+
print(diffs)
|
| 92 |
+
for diff in diffs:
|
| 93 |
+
print(f"Generating difficulty {diff}")
|
| 94 |
+
args.difficulty = diff
|
| 95 |
+
pipeline = Pipeline(args, tokenizer)
|
| 96 |
+
events = pipeline.generate(model, sequences)
|
| 97 |
+
generated_maps.append(events)
|
| 98 |
+
else:
|
| 99 |
+
pipeline = Pipeline(args, tokenizer)
|
| 100 |
+
events = pipeline.generate(model, sequences)
|
| 101 |
+
generated_maps.append(events)
|
| 102 |
+
|
| 103 |
+
if args.generate_positions:
|
| 104 |
+
model = find_model(args.diff_ckpt, args, device)
|
| 105 |
+
refine_model = find_model(args.diff_refine_ckpt, args, device) if len(args.diff_refine_ckpt) > 0 else None
|
| 106 |
+
diffusion_pipeline = DiffisionPipeline(args.diffusion)
|
| 107 |
+
for events in generated_maps:
|
| 108 |
+
events = diffusion_pipeline.generate(model, events, refine_model)
|
| 109 |
+
generated_positions.append(events)
|
| 110 |
+
else:
|
| 111 |
+
generated_positions = generated_maps
|
| 112 |
+
|
| 113 |
+
postprocessor = Postprocessor(args)
|
| 114 |
+
postprocessor.generate(generated_positions)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
main()
|