forked from KinWaiCheuk/demucs_lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
85 lines (65 loc) · 2.81 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
import pathlib
from pathlib import Path
import hydra
import pytorch_lightning as pl
import torch
import torchaudio
from hydra import compose, initialize
from hydra.utils import to_absolute_path
from torch.utils.data import DataLoader, Dataset
from torchaudio.functional import resample
from demucs.demucs import Demucs
from demucs.hdemucs import HDemucs
from demucs.states import get_quantizer
@hydra.main(config_path="conf", config_name="infer_config")
def main(args):
class InferDataset(Dataset):
def __init__(self, audio_folder_path, audio_ext, sampling_rate):
audiofolder = Path(audio_folder_path) # path of folder
self.audio_path_list = list(
audiofolder.glob(f"*.{audio_ext}")
) # path of audio
self.sample_rate = sampling_rate
self.audio_name = []
for i in self.audio_path_list:
path = pathlib.PurePath(i)
self.audio_name.append(path.name)
def __len__(self):
return len(self.audio_path_list)
def __getitem__(self, idx):
try:
waveform, rate = torchaudio.load(self.audio_path_list[idx])
# return (torch.Tensor, int)
if rate != self.sample_rate:
waveform = resample(waveform, rate, self.sample_rate)
# resample(waveform: torch.Tensor, orig_freq: int, new_freq: int)
# return waveform tensor at the new frequency of dimension
except:
waveform = torch.tensor([[]])
rate = 0
print(f"{self.audio_path_list[idx].name} is corrupted")
audio_name = self.audio_name[idx]
return waveform, audio_name
if args.checkpoint == None:
raise ValueError("Please enter the path for your model checkpoint")
if args.infer_audio_folder_path == None:
raise ValueError("Please enter the path for your inference audio folder")
inference_set = InferDataset(
to_absolute_path(args.infer_audio_folder_path),
args.infer_audio_ext,
args.infer_samplerate,
)
inference_loader = DataLoader(inference_set, args.dataloader.inference.num_workers)
if args.model == "Demucs":
model = Demucs.load_from_checkpoint(to_absolute_path(args.checkpoint))
# call with pretrained model
elif args.model == "HDemucs":
model = HDemucs.load_from_checkpoint(to_absolute_path(args.checkpoint))
else:
print("Invalid model, please choose Demucs or HDemucs")
quantizer = get_quantizer(model, args.quant, model.optimizers)
model.quantizer = quantizer # can use as self.quantizer in class Demucs
trainer = pl.Trainer(**args.trainer)
trainer.predict(model, dataloaders=inference_loader)
if __name__ == "__main__":
main()