admin commited on
Commit
82336dd
·
1 Parent(s): 2e12b46
Files changed (6) hide show
  1. .gitattributes +11 -11
  2. .gitignore +6 -0
  3. app.py +191 -0
  4. model.py +142 -0
  5. requirements.txt +5 -0
  6. utils.py +67 -0
.gitattributes CHANGED
@@ -1,35 +1,35 @@
1
  *.7z filter=lfs diff=lfs merge=lfs -text
2
  *.arrow filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
 
4
  *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
  *.ftz filter=lfs diff=lfs merge=lfs -text
7
  *.gz filter=lfs diff=lfs merge=lfs -text
8
  *.h5 filter=lfs diff=lfs merge=lfs -text
9
  *.joblib filter=lfs diff=lfs merge=lfs -text
10
  *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
  *.model filter=lfs diff=lfs merge=lfs -text
13
  *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
  *.onnx filter=lfs diff=lfs merge=lfs -text
17
  *.ot filter=lfs diff=lfs merge=lfs -text
18
  *.parquet filter=lfs diff=lfs merge=lfs -text
19
  *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
  *.pt filter=lfs diff=lfs merge=lfs -text
23
  *.pth filter=lfs diff=lfs merge=lfs -text
24
  *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
  saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
  *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
  *.tflite filter=lfs diff=lfs merge=lfs -text
30
  *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
  *.xz filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
- *.zst filter=lfs diff=lfs merge=lfs -text
35
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
1
  *.7z filter=lfs diff=lfs merge=lfs -text
2
  *.arrow filter=lfs diff=lfs merge=lfs -text
3
  *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bin.* filter=lfs diff=lfs merge=lfs -text
5
  *.bz2 filter=lfs diff=lfs merge=lfs -text
 
6
  *.ftz filter=lfs diff=lfs merge=lfs -text
7
  *.gz filter=lfs diff=lfs merge=lfs -text
8
  *.h5 filter=lfs diff=lfs merge=lfs -text
9
  *.joblib filter=lfs diff=lfs merge=lfs -text
10
  *.lfs.* filter=lfs diff=lfs merge=lfs -text
 
11
  *.model filter=lfs diff=lfs merge=lfs -text
12
  *.msgpack filter=lfs diff=lfs merge=lfs -text
 
 
13
  *.onnx filter=lfs diff=lfs merge=lfs -text
14
  *.ot filter=lfs diff=lfs merge=lfs -text
15
  *.parquet filter=lfs diff=lfs merge=lfs -text
16
  *.pb filter=lfs diff=lfs merge=lfs -text
 
 
17
  *.pt filter=lfs diff=lfs merge=lfs -text
18
  *.pth filter=lfs diff=lfs merge=lfs -text
19
  *.rar filter=lfs diff=lfs merge=lfs -text
 
20
  saved_model/**/* filter=lfs diff=lfs merge=lfs -text
21
  *.tar.* filter=lfs diff=lfs merge=lfs -text
 
22
  *.tflite filter=lfs diff=lfs merge=lfs -text
23
  *.tgz filter=lfs diff=lfs merge=lfs -text
 
24
  *.xz filter=lfs diff=lfs merge=lfs -text
25
  *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *.tfevents* filter=lfs diff=lfs merge=lfs -text
28
+ *.db* filter=lfs diff=lfs merge=lfs -text
29
+ *.ark* filter=lfs diff=lfs merge=lfs -text
30
+ **/*ckpt*data* filter=lfs diff=lfs merge=lfs -text
31
+ **/*ckpt*.meta filter=lfs diff=lfs merge=lfs -text
32
+ **/*ckpt*.index filter=lfs diff=lfs merge=lfs -text
33
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
34
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
35
+ *.wav filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ *.pt
2
+ __pycache__/*
3
+ tmp/*
4
+ flagged/*
5
+ test.py
6
+ rename.sh
app.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import random
4
+ import shutil
5
+ import librosa
6
+ import warnings
7
+ import numpy as np
8
+ import gradio as gr
9
+ import librosa.display
10
+ import matplotlib.pyplot as plt
11
+ from utils import get_modelist, find_wav_files, embed_img, TEMP_DIR
12
+ from collections import Counter
13
+ from model import EvalNet
14
+
15
+
16
+ TRANSLATE = {
17
+ "m_bel": "男声美声唱法 (Bel Canto, Male)",
18
+ "f_bel": "女声美声唱法 (Bel Canto, Female)",
19
+ "m_folk": "男声民族唱法 (Folk Singing, Male)",
20
+ "f_folk": "女声民族唱法 (Folk Singing, Female)",
21
+ }
22
+ CLASSES = list(TRANSLATE.keys())
23
+ SAMPLE_RATE = 22050
24
+
25
+
26
+ def wav2mel(audio_path: str, width=1.6, topdb=40):
27
+ os.makedirs(TEMP_DIR, exist_ok=True)
28
+ try:
29
+ y, sr = librosa.load(audio_path, sr=SAMPLE_RATE)
30
+ non_silents = librosa.effects.split(y, top_db=topdb)
31
+ non_silent = np.concatenate([y[start:end] for start, end in non_silents])
32
+ mel_spec = librosa.feature.melspectrogram(y=non_silent, sr=sr)
33
+ log_mel_spec = librosa.power_to_db(mel_spec, ref=np.max)
34
+ dur = librosa.get_duration(y=non_silent, sr=sr)
35
+ total_frames = log_mel_spec.shape[1]
36
+ step = int(width * total_frames / dur)
37
+ count = int(total_frames / step)
38
+ begin = int(0.5 * (total_frames - count * step))
39
+ end = begin + step * count
40
+ for i in range(begin, end, step):
41
+ librosa.display.specshow(log_mel_spec[:, i : i + step])
42
+ plt.axis("off")
43
+ plt.savefig(
44
+ f"{TEMP_DIR}/mel_{round(dur, 2)}_{i}.jpg",
45
+ bbox_inches="tight",
46
+ pad_inches=0.0,
47
+ )
48
+ plt.close()
49
+
50
+ except Exception as e:
51
+ print(f"Error converting {audio_path} : {e}")
52
+
53
+
54
+ def wav2cqt(audio_path: str, width=1.6, topdb=40):
55
+ os.makedirs(TEMP_DIR, exist_ok=True)
56
+ try:
57
+ y, sr = librosa.load(audio_path, sr=SAMPLE_RATE)
58
+ non_silents = librosa.effects.split(y, top_db=topdb)
59
+ non_silent = np.concatenate([y[start:end] for start, end in non_silents])
60
+ cqt_spec = librosa.cqt(y=non_silent, sr=sr)
61
+ log_cqt_spec = librosa.power_to_db(np.abs(cqt_spec) ** 2, ref=np.max)
62
+ dur = librosa.get_duration(y=non_silent, sr=sr)
63
+ total_frames = log_cqt_spec.shape[1]
64
+ step = int(width * total_frames / dur)
65
+ count = int(total_frames / step)
66
+ begin = int(0.5 * (total_frames - count * step))
67
+ end = begin + step * count
68
+ for i in range(begin, end, step):
69
+ librosa.display.specshow(log_cqt_spec[:, i : i + step])
70
+ plt.axis("off")
71
+ plt.savefig(
72
+ f"{TEMP_DIR}/cqt_{round(dur, 2)}_{i}.jpg",
73
+ bbox_inches="tight",
74
+ pad_inches=0.0,
75
+ )
76
+ plt.close()
77
+
78
+ except Exception as e:
79
+ print(f"Error converting {audio_path} : {e}")
80
+
81
+
82
+ def wav2chroma(audio_path: str, width=1.6, topdb=40):
83
+ os.makedirs(TEMP_DIR, exist_ok=True)
84
+ try:
85
+ y, sr = librosa.load(audio_path, sr=SAMPLE_RATE)
86
+ non_silents = librosa.effects.split(y, top_db=topdb)
87
+ non_silent = np.concatenate([y[start:end] for start, end in non_silents])
88
+ chroma_spec = librosa.feature.chroma_stft(y=non_silent, sr=sr)
89
+ log_chroma_spec = librosa.power_to_db(np.abs(chroma_spec) ** 2, ref=np.max)
90
+ dur = librosa.get_duration(y=non_silent, sr=sr)
91
+ total_frames = log_chroma_spec.shape[1]
92
+ step = int(width * total_frames / dur)
93
+ count = int(total_frames / step)
94
+ begin = int(0.5 * (total_frames - count * step))
95
+ end = begin + step * count
96
+ for i in range(begin, end, step):
97
+ librosa.display.specshow(log_chroma_spec[:, i : i + step])
98
+ plt.axis("off")
99
+ plt.savefig(
100
+ f"{TEMP_DIR}/chroma_{round(dur, 2)}_{i}.jpg",
101
+ bbox_inches="tight",
102
+ pad_inches=0.0,
103
+ )
104
+ plt.close()
105
+
106
+ except Exception as e:
107
+ print(f"Error converting {audio_path} : {e}")
108
+
109
+
110
+ def most_common_element(input_list: list):
111
+ counter = Counter(input_list)
112
+ mce, _ = counter.most_common(1)[0]
113
+ return mce
114
+
115
+
116
+ def infer(wav_path: str, log_name: str, folder_path=TEMP_DIR):
117
+ if os.path.exists(folder_path):
118
+ shutil.rmtree(folder_path)
119
+
120
+ if not wav_path:
121
+ return None, "请输入音频 Please input an audio!"
122
+
123
+ try:
124
+ model = EvalNet(log_name, len(TRANSLATE)).model
125
+ except Exception as e:
126
+ return None, f"{e}"
127
+
128
+ spec = log_name.split("_")[-3]
129
+ eval("wav2%s" % spec)(wav_path)
130
+ outputs = []
131
+ all_files = os.listdir(folder_path)
132
+ for file_name in all_files:
133
+ if file_name.lower().endswith(".jpg"):
134
+ file_path = os.path.join(folder_path, file_name)
135
+ input = embed_img(file_path)
136
+ output: torch.Tensor = model(input)
137
+ pred_id = torch.max(output.data, 1)[1]
138
+ outputs.append(int(pred_id))
139
+
140
+ max_count_item = most_common_element(outputs)
141
+ shutil.rmtree(folder_path)
142
+ return os.path.basename(wav_path), TRANSLATE[CLASSES[max_count_item]]
143
+
144
+
145
+ if __name__ == "__main__":
146
+ warnings.filterwarnings("ignore")
147
+ models = get_modelist()
148
+ examples = []
149
+ example_wavs = find_wav_files()
150
+ model_num = len(models)
151
+ for wav in example_wavs:
152
+ examples.append([wav, models[random.randint(0, model_num - 1)]])
153
+
154
+ with gr.Blocks() as demo:
155
+ gr.Interface(
156
+ fn=infer,
157
+ inputs=[
158
+ gr.Audio(label="上传录音 Upload a recording", type="filepath"),
159
+ gr.Dropdown(
160
+ choices=models, label="选择模型 Select a model", value=models[0]
161
+ ),
162
+ ],
163
+ outputs=[
164
+ gr.Textbox(label="音频文件名 Audio filename", show_copy_button=True),
165
+ gr.Textbox(
166
+ label="唱法识别 Singing method recognition", show_copy_button=True
167
+ ),
168
+ ],
169
+ examples=examples,
170
+ cache_examples=False,
171
+ allow_flagging="never",
172
+ title="建议录音时长保持在 5s 左右, 过长会影响识别效率<br>It is recommended to keep the recording length around 5s, too long will affect the recognition efficiency.",
173
+ )
174
+
175
+ gr.Markdown(
176
+ """
177
+ # 引用 Cite
178
+ ```bibtex
179
+ @dataset{zhaorui_liu_2021_5676893,
180
+ author = {Monan Zhou, Shenyang Xu, Zhaorui Liu, Zhaowen Wang, Feng Yu, Wei Li and Baoqiang Han},
181
+ title = {CCMusic: an Open and Diverse Database for Chinese and General Music Information Retrieval Research},
182
+ month = {mar},
183
+ year = {2024},
184
+ publisher = {HuggingFace},
185
+ version = {1.2},
186
+ url = {https://huggingface.co/ccmusic-database}
187
+ }
188
+ ```"""
189
+ )
190
+
191
+ demo.launch()
model.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torchvision.models as models
4
+ from modelscope.msdatasets import MsDataset
5
+ from utils import MODEL_DIR
6
+
7
+
8
+ class EvalNet:
9
+ model: nn.Module = None
10
+ m_type = "squeezenet"
11
+ input_size = 224
12
+ output_size = 512
13
+
14
+ def __init__(self, log_name: str, cls_num: int):
15
+ saved_model_path = f"{MODEL_DIR}/{log_name}/save.pt"
16
+ m_ver = "_".join(log_name.split("_")[:-3])
17
+ self.m_type, self.input_size = self._model_info(m_ver)
18
+
19
+ if not hasattr(models, m_ver):
20
+ raise Exception("Unsupported model.")
21
+
22
+ self.model = eval("models.%s()" % m_ver)
23
+ linear_output = self._set_outsize()
24
+ self._set_classifier(cls_num, linear_output)
25
+ checkpoint = torch.load(saved_model_path, map_location="cpu")
26
+ if torch.cuda.is_available():
27
+ checkpoint = torch.load(saved_model_path)
28
+
29
+ self.model.load_state_dict(checkpoint, False)
30
+ self.model.eval()
31
+
32
+ def _get_backbone(self, ver: str, backbone_list: list):
33
+ for bb in backbone_list:
34
+ if ver == bb["ver"]:
35
+ return bb
36
+
37
+ print("Backbone name not found, using default option - alexnet.")
38
+ return backbone_list[0]
39
+
40
+ def _model_info(self, m_ver: str):
41
+ backbone_list = MsDataset.load("monetjoe/cv_backbones", split="v1")
42
+ backbone = self._get_backbone(m_ver, backbone_list)
43
+ m_type = str(backbone["type"])
44
+ input_size = int(backbone["input_size"])
45
+ return m_type, input_size
46
+
47
+ def _classifier(self, cls_num: int, output_size: int, linear_output: bool):
48
+ q = (1.0 * output_size / cls_num) ** 0.25
49
+ l1 = int(q * cls_num)
50
+ l2 = int(q * l1)
51
+ l3 = int(q * l2)
52
+ if linear_output:
53
+ return torch.nn.Sequential(
54
+ nn.Dropout(),
55
+ nn.Linear(output_size, l3),
56
+ nn.ReLU(inplace=True),
57
+ nn.Dropout(),
58
+ nn.Linear(l3, l2),
59
+ nn.ReLU(inplace=True),
60
+ nn.Dropout(),
61
+ nn.Linear(l2, l1),
62
+ nn.ReLU(inplace=True),
63
+ nn.Linear(l1, cls_num),
64
+ )
65
+
66
+ else:
67
+ return torch.nn.Sequential(
68
+ nn.Dropout(),
69
+ nn.Conv2d(output_size, l3, kernel_size=(1, 1), stride=(1, 1)),
70
+ nn.ReLU(inplace=True),
71
+ nn.AdaptiveAvgPool2d(output_size=(1, 1)),
72
+ nn.Flatten(),
73
+ nn.Linear(l3, l2),
74
+ nn.ReLU(inplace=True),
75
+ nn.Dropout(),
76
+ nn.Linear(l2, l1),
77
+ nn.ReLU(inplace=True),
78
+ nn.Linear(l1, cls_num),
79
+ )
80
+
81
+ def _set_outsize(self):
82
+ for name, module in self.model.named_modules():
83
+ if (
84
+ str(name).__contains__("classifier")
85
+ or str(name).__eq__("fc")
86
+ or str(name).__contains__("head")
87
+ or hasattr(module, "classifier")
88
+ ):
89
+ if isinstance(module, torch.nn.Linear):
90
+ self.output_size = module.in_features
91
+ return True
92
+
93
+ if isinstance(module, torch.nn.Conv2d):
94
+ self.output_size = module.in_channels
95
+ return False
96
+
97
+ return False
98
+
99
+ def _set_classifier(self, cls_num: int, linear_output: bool):
100
+ if self.m_type == "convnext":
101
+ del self.model.classifier[2]
102
+ self.model.classifier = nn.Sequential(
103
+ *list(self.model.classifier)
104
+ + list(self._classifier(cls_num, self.output_size, linear_output))
105
+ )
106
+ return
107
+
108
+ elif self.m_type == "maxvit":
109
+ del self.model.classifier[5]
110
+ self.model.classifier = nn.Sequential(
111
+ *list(self.model.classifier)
112
+ + list(self._classifier(cls_num, self.output_size, linear_output))
113
+ )
114
+ return
115
+
116
+ if hasattr(self.model, "classifier"):
117
+ self.model.classifier = self._classifier(
118
+ cls_num, self.output_size, linear_output
119
+ )
120
+ return
121
+
122
+ elif hasattr(self.model, "fc"):
123
+ self.model.fc = self._classifier(cls_num, self.output_size, linear_output)
124
+ return
125
+
126
+ elif hasattr(self.model, "head"):
127
+ self.model.head = self._classifier(cls_num, self.output_size, linear_output)
128
+ return
129
+
130
+ self.model.heads.head = self._classifier(
131
+ cls_num, self.output_size, linear_output
132
+ )
133
+
134
+ def forward(self, x: torch.Tensor):
135
+ if torch.cuda.is_available():
136
+ x = x.cuda()
137
+ self.model = self.model.cuda()
138
+
139
+ if self.m_type == "googlenet":
140
+ return self.model(x)[0]
141
+ else:
142
+ return self.model(x)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ librosa
2
+ torch
3
+ matplotlib
4
+ torchvision
5
+ pillow
utils.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import torchvision.transforms as transforms
4
+ from modelscope import snapshot_download
5
+ from PIL import Image
6
+
7
+ MODEL_DIR = snapshot_download(
8
+ f"ccmusic-database/bel_canto",
9
+ cache_dir=f"{os.getcwd()}/__pycache__",
10
+ )
11
+ TEMP_DIR = f"{os.getcwd()}/flagged"
12
+
13
+
14
+ def toCUDA(x):
15
+ if hasattr(x, "cuda"):
16
+ if torch.cuda.is_available():
17
+ return x.cuda()
18
+
19
+ return x
20
+
21
+
22
+ def find_wav_files(folder_path=f"{MODEL_DIR}/examples"):
23
+ wav_files = []
24
+ for root, _, files in os.walk(folder_path):
25
+ for file in files:
26
+ if file.endswith(".wav"):
27
+ file_path = os.path.join(root, file)
28
+ wav_files.append(file_path)
29
+
30
+ return wav_files
31
+
32
+
33
+ def get_modelist(model_dir=MODEL_DIR):
34
+ try:
35
+ entries = os.listdir(model_dir)
36
+ except OSError as e:
37
+ print(f"无法访问 {model_dir}: {e}")
38
+ return
39
+
40
+ # 遍历所有条目
41
+ output = []
42
+ for entry in entries:
43
+ # 获取完整路径
44
+ full_path = os.path.join(model_dir, entry)
45
+ # 跳过'.git'文件夹
46
+ if entry == ".git" or entry == "examples":
47
+ print(f"跳过 .git 或 examples 文件夹: {full_path}")
48
+ continue
49
+
50
+ # 检查条目是文件还是目录
51
+ if os.path.isdir(full_path):
52
+ # 打印目录路径
53
+ output.append(os.path.basename(full_path))
54
+
55
+ return output
56
+
57
+
58
+ def embed_img(img_path: str, input_size=224):
59
+ transform = transforms.Compose(
60
+ [
61
+ transforms.Resize([input_size, input_size]),
62
+ transforms.ToTensor(),
63
+ transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
64
+ ]
65
+ )
66
+ img = Image.open(img_path).convert("RGB")
67
+ return transform(img).unsqueeze(0)