feat: initial commit
Browse files- .gitattributes +32 -0
- .gitignore +383 -0
- app-update.py +249 -0
- app.py +177 -0
- config.py +126 -0
- hubert_base.pt +3 -0
- infer_pack/attentions.py +417 -0
- infer_pack/commons.py +166 -0
- infer_pack/models.py +982 -0
- infer_pack/models_onnx.py +760 -0
- infer_pack/models_onnx_moess.py +849 -0
- infer_pack/modules.py +522 -0
- infer_pack/transforms.py +209 -0
- requirements-gcolab.txt +49 -0
- requirements.txt +46 -0
- vc_infer_pipeline.py +320 -0
- weights/nilou-jp/added_IVF218_Flat_nprobe_5.index +3 -0
- weights/nilou-jp/cover.png +0 -0
- weights/nilou-jp/nilou-jp.pth +3 -0
.gitattributes
CHANGED
@@ -32,3 +32,35 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
weights/alice/added_IVF141_Flat_nprobe_4.index filter=lfs diff=lfs merge=lfs -text
|
36 |
+
weights/ayaka-jp/added_IVF1830_Flat_nprobe_9.index filter=lfs diff=lfs merge=lfs -text
|
37 |
+
weights/nilou-zh/added_IVF1939_Flat_nprobe_9.index filter=lfs diff=lfs merge=lfs -text
|
38 |
+
weights/teio/added_IVF3421_Flat_nprobe_11.index filter=lfs diff=lfs merge=lfs -text
|
39 |
+
weights/ayaka-jp/added_IVF415_Flat_nprobe_6.index filter=lfs diff=lfs merge=lfs -text
|
40 |
+
weights/klee-jp/added_IVF282_Flat_nprobe_5.index filter=lfs diff=lfs merge=lfs -text
|
41 |
+
weights/klee-jp/cover.png filter=lfs diff=lfs merge=lfs -text
|
42 |
+
weights/nahida-jp/added_IVF265_Flat_nprobe_5.index filter=lfs diff=lfs merge=lfs -text
|
43 |
+
weights/nahida-jp/cover.png filter=lfs diff=lfs merge=lfs -text
|
44 |
+
weights/hutao-jp/added_IVF265_Flat_nprobe_5.index filter=lfs diff=lfs merge=lfs -text
|
45 |
+
weights/hutao-jp/cover.png filter=lfs diff=lfs merge=lfs -text
|
46 |
+
weights/raiden-jp/added_IVF783_Flat_nprobe_7.index filter=lfs diff=lfs merge=lfs -text
|
47 |
+
weights/raiden-jp/cover.png filter=lfs diff=lfs merge=lfs -text
|
48 |
+
weights/kazuha-jp/added_IVF677_Flat_nprobe_7.index filter=lfs diff=lfs merge=lfs -text
|
49 |
+
weights/kazuha-jp/cover.png filter=lfs diff=lfs merge=lfs -text
|
50 |
+
weights/childe-jp/added_IVF428_Flat_nprobe_6.index filter=lfs diff=lfs merge=lfs -text
|
51 |
+
weights/childe-jp/cover.png filter=lfs diff=lfs merge=lfs -text
|
52 |
+
weights/noah/added_IVF467_Flat_nprobe_6.index filter=lfs diff=lfs merge=lfs -text
|
53 |
+
weights/noah/cover.jpeg filter=lfs diff=lfs merge=lfs -text
|
54 |
+
weights/rie/added_IVF325_Flat_nprobe_5.index filter=lfs diff=lfs merge=lfs -text
|
55 |
+
weights/rie/cover.png filter=lfs diff=lfs merge=lfs -text
|
56 |
+
weights/wanderer-jp/added_IVF128_Flat_nprobe_4.index filter=lfs diff=lfs merge=lfs -text
|
57 |
+
weights/wanderer-jp/cover.png filter=lfs diff=lfs merge=lfs -text
|
58 |
+
weights/xiao-jp/cover.png filter=lfs diff=lfs merge=lfs -text
|
59 |
+
weights/xiao-jp/added_IVF233_Flat_nprobe_5.index filter=lfs diff=lfs merge=lfs -text
|
60 |
+
weights/anji/added_IVF198_Flat_nprobe_4.index filter=lfs diff=lfs merge=lfs -text
|
61 |
+
weights/anji/cover.png filter=lfs diff=lfs merge=lfs -text
|
62 |
+
weights/ariana/added_IVF133_Flat_nprobe_4.index filter=lfs diff=lfs merge=lfs -text
|
63 |
+
weights/ariana/cover.png filter=lfs diff=lfs merge=lfs -text
|
64 |
+
weights/once/added_IVF229_Flat_nprobe_5.index filter=lfs diff=lfs merge=lfs -text
|
65 |
+
weights/once/cover.png filter=lfs diff=lfs merge=lfs -text
|
66 |
+
*.index filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,383 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## Ignore Visual Studio temporary files, build results, and
|
2 |
+
## files generated by popular Visual Studio add-ons.
|
3 |
+
##
|
4 |
+
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
|
5 |
+
|
6 |
+
# User-specific files
|
7 |
+
*.rsuser
|
8 |
+
*.suo
|
9 |
+
*.user
|
10 |
+
*.userosscache
|
11 |
+
*.sln.docstates
|
12 |
+
load.py
|
13 |
+
|
14 |
+
# User-specific files (MonoDevelop/Xamarin Studio)
|
15 |
+
*.userprefs
|
16 |
+
|
17 |
+
# Mono auto generated files
|
18 |
+
mono_crash.*
|
19 |
+
|
20 |
+
# Build results
|
21 |
+
[Dd]ebug/
|
22 |
+
[Dd]ebugPublic/
|
23 |
+
[Rr]elease/
|
24 |
+
[Rr]eleases/
|
25 |
+
x64/
|
26 |
+
x86/
|
27 |
+
[Ww][Ii][Nn]32/
|
28 |
+
[Aa][Rr][Mm]/
|
29 |
+
[Aa][Rr][Mm]64/
|
30 |
+
bld/
|
31 |
+
[Bb]in/
|
32 |
+
[Oo]bj/
|
33 |
+
[Oo]ut/
|
34 |
+
[Ll]og/
|
35 |
+
[Ll]ogs/
|
36 |
+
infer_pack\__pycache__
|
37 |
+
# Visual Studio 2015/2017 cache/options directory
|
38 |
+
.vs/
|
39 |
+
# Uncomment if you have tasks that create the project's static files in wwwroot
|
40 |
+
#wwwroot/
|
41 |
+
|
42 |
+
# Visual Studio 2017 auto generated files
|
43 |
+
Generated\ Files/
|
44 |
+
|
45 |
+
# MSTest test Results
|
46 |
+
[Tt]est[Rr]esult*/
|
47 |
+
[Bb]uild[Ll]og.*
|
48 |
+
|
49 |
+
# NUnit
|
50 |
+
*.VisualState.xml
|
51 |
+
TestResult.xml
|
52 |
+
nunit-*.xml
|
53 |
+
|
54 |
+
# Build Results of an ATL Project
|
55 |
+
[Dd]ebugPS/
|
56 |
+
[Rr]eleasePS/
|
57 |
+
dlldata.c
|
58 |
+
|
59 |
+
# Benchmark Results
|
60 |
+
BenchmarkDotNet.Artifacts/
|
61 |
+
|
62 |
+
# .NET Core
|
63 |
+
project.lock.json
|
64 |
+
project.fragment.lock.json
|
65 |
+
artifacts/
|
66 |
+
|
67 |
+
# ASP.NET Scaffolding
|
68 |
+
ScaffoldingReadMe.txt
|
69 |
+
|
70 |
+
# StyleCop
|
71 |
+
StyleCopReport.xml
|
72 |
+
|
73 |
+
# Files built by Visual Studio
|
74 |
+
*_i.c
|
75 |
+
*_p.c
|
76 |
+
*_h.h
|
77 |
+
*.ilk
|
78 |
+
*.meta
|
79 |
+
*.obj
|
80 |
+
*.iobj
|
81 |
+
*.pch
|
82 |
+
*.pdb
|
83 |
+
*.ipdb
|
84 |
+
*.pgc
|
85 |
+
*.pgd
|
86 |
+
*.rsp
|
87 |
+
*.sbr
|
88 |
+
*.tlb
|
89 |
+
*.tli
|
90 |
+
*.tlh
|
91 |
+
*.tmp
|
92 |
+
*.tmp_proj
|
93 |
+
*_wpftmp.csproj
|
94 |
+
*.log
|
95 |
+
*.vspscc
|
96 |
+
*.vssscc
|
97 |
+
.builds
|
98 |
+
*.pidb
|
99 |
+
*.svclog
|
100 |
+
*.scc
|
101 |
+
|
102 |
+
# Chutzpah Test files
|
103 |
+
_Chutzpah*
|
104 |
+
|
105 |
+
# Visual C++ cache files
|
106 |
+
ipch/
|
107 |
+
*.aps
|
108 |
+
*.ncb
|
109 |
+
*.opendb
|
110 |
+
*.opensdf
|
111 |
+
*.sdf
|
112 |
+
*.cachefile
|
113 |
+
*.VC.db
|
114 |
+
*.VC.VC.opendb
|
115 |
+
|
116 |
+
# Visual Studio profiler
|
117 |
+
*.psess
|
118 |
+
*.vsp
|
119 |
+
*.vspx
|
120 |
+
*.sap
|
121 |
+
|
122 |
+
# Visual Studio Trace Files
|
123 |
+
*.e2e
|
124 |
+
|
125 |
+
# TFS 2012 Local Workspace
|
126 |
+
$tf/
|
127 |
+
|
128 |
+
# Guidance Automation Toolkit
|
129 |
+
*.gpState
|
130 |
+
|
131 |
+
# ReSharper is a .NET coding add-in
|
132 |
+
_ReSharper*/
|
133 |
+
*.[Rr]e[Ss]harper
|
134 |
+
*.DotSettings.user
|
135 |
+
|
136 |
+
# TeamCity is a build add-in
|
137 |
+
_TeamCity*
|
138 |
+
|
139 |
+
# DotCover is a Code Coverage Tool
|
140 |
+
*.dotCover
|
141 |
+
|
142 |
+
# AxoCover is a Code Coverage Tool
|
143 |
+
.axoCover/*
|
144 |
+
!.axoCover/settings.json
|
145 |
+
|
146 |
+
# Coverlet is a free, cross platform Code Coverage Tool
|
147 |
+
coverage*.json
|
148 |
+
coverage*.xml
|
149 |
+
coverage*.info
|
150 |
+
|
151 |
+
# Visual Studio code coverage results
|
152 |
+
*.coverage
|
153 |
+
*.coveragexml
|
154 |
+
|
155 |
+
# NCrunch
|
156 |
+
_NCrunch_*
|
157 |
+
.*crunch*.local.xml
|
158 |
+
nCrunchTemp_*
|
159 |
+
|
160 |
+
# MightyMoose
|
161 |
+
*.mm.*
|
162 |
+
AutoTest.Net/
|
163 |
+
|
164 |
+
# Web workbench (sass)
|
165 |
+
.sass-cache/
|
166 |
+
|
167 |
+
# Installshield output folder
|
168 |
+
[Ee]xpress/
|
169 |
+
|
170 |
+
# DocProject is a documentation generator add-in
|
171 |
+
DocProject/buildhelp/
|
172 |
+
DocProject/Help/*.HxT
|
173 |
+
DocProject/Help/*.HxC
|
174 |
+
DocProject/Help/*.hhc
|
175 |
+
DocProject/Help/*.hhk
|
176 |
+
DocProject/Help/*.hhp
|
177 |
+
DocProject/Help/Html2
|
178 |
+
DocProject/Help/html
|
179 |
+
|
180 |
+
# Click-Once directory
|
181 |
+
publish/
|
182 |
+
|
183 |
+
# Publish Web Output
|
184 |
+
*.[Pp]ublish.xml
|
185 |
+
*.azurePubxml
|
186 |
+
# Note: Comment the next line if you want to checkin your web deploy settings,
|
187 |
+
# but database connection strings (with potential passwords) will be unencrypted
|
188 |
+
*.pubxml
|
189 |
+
*.publishproj
|
190 |
+
|
191 |
+
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
192 |
+
# checkin your Azure Web App publish settings, but sensitive information contained
|
193 |
+
# in these scripts will be unencrypted
|
194 |
+
PublishScripts/
|
195 |
+
|
196 |
+
# NuGet Packages
|
197 |
+
*.nupkg
|
198 |
+
# NuGet Symbol Packages
|
199 |
+
*.snupkg
|
200 |
+
# The packages folder can be ignored because of Package Restore
|
201 |
+
**/[Pp]ackages/*
|
202 |
+
# except build/, which is used as an MSBuild target.
|
203 |
+
!**/[Pp]ackages/build/
|
204 |
+
# Uncomment if necessary however generally it will be regenerated when needed
|
205 |
+
#!**/[Pp]ackages/repositories.config
|
206 |
+
# NuGet v3's project.json files produces more ignorable files
|
207 |
+
*.nuget.props
|
208 |
+
*.nuget.targets
|
209 |
+
|
210 |
+
# Microsoft Azure Build Output
|
211 |
+
csx/
|
212 |
+
*.build.csdef
|
213 |
+
|
214 |
+
# Microsoft Azure Emulator
|
215 |
+
ecf/
|
216 |
+
rcf/
|
217 |
+
|
218 |
+
# Windows Store app package directories and files
|
219 |
+
AppPackages/
|
220 |
+
BundleArtifacts/
|
221 |
+
Package.StoreAssociation.xml
|
222 |
+
_pkginfo.txt
|
223 |
+
*.appx
|
224 |
+
*.appxbundle
|
225 |
+
*.appxupload
|
226 |
+
|
227 |
+
# Visual Studio cache files
|
228 |
+
# files ending in .cache can be ignored
|
229 |
+
*.[Cc]ache
|
230 |
+
# but keep track of directories ending in .cache
|
231 |
+
!?*.[Cc]ache/
|
232 |
+
|
233 |
+
# Others
|
234 |
+
ClientBin/
|
235 |
+
~$*
|
236 |
+
*~
|
237 |
+
*.dbmdl
|
238 |
+
*.dbproj.schemaview
|
239 |
+
*.jfm
|
240 |
+
*.pfx
|
241 |
+
*.publishsettings
|
242 |
+
orleans.codegen.cs
|
243 |
+
|
244 |
+
# Including strong name files can present a security risk
|
245 |
+
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
|
246 |
+
#*.snk
|
247 |
+
|
248 |
+
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
249 |
+
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
250 |
+
#bower_components/
|
251 |
+
|
252 |
+
# RIA/Silverlight projects
|
253 |
+
Generated_Code/
|
254 |
+
|
255 |
+
# Backup & report files from converting an old project file
|
256 |
+
# to a newer Visual Studio version. Backup files are not needed,
|
257 |
+
# because we have git ;-)
|
258 |
+
_UpgradeReport_Files/
|
259 |
+
Backup*/
|
260 |
+
UpgradeLog*.XML
|
261 |
+
UpgradeLog*.htm
|
262 |
+
ServiceFabricBackup/
|
263 |
+
*.rptproj.bak
|
264 |
+
|
265 |
+
# SQL Server files
|
266 |
+
*.mdf
|
267 |
+
*.ldf
|
268 |
+
*.ndf
|
269 |
+
|
270 |
+
# Business Intelligence projects
|
271 |
+
*.rdl.data
|
272 |
+
*.bim.layout
|
273 |
+
*.bim_*.settings
|
274 |
+
*.rptproj.rsuser
|
275 |
+
*- [Bb]ackup.rdl
|
276 |
+
*- [Bb]ackup ([0-9]).rdl
|
277 |
+
*- [Bb]ackup ([0-9][0-9]).rdl
|
278 |
+
|
279 |
+
# Microsoft Fakes
|
280 |
+
FakesAssemblies/
|
281 |
+
|
282 |
+
# GhostDoc plugin setting file
|
283 |
+
*.GhostDoc.xml
|
284 |
+
|
285 |
+
# Node.js Tools for Visual Studio
|
286 |
+
.ntvs_analysis.dat
|
287 |
+
node_modules/
|
288 |
+
|
289 |
+
# Visual Studio 6 build log
|
290 |
+
*.plg
|
291 |
+
|
292 |
+
# Visual Studio 6 workspace options file
|
293 |
+
*.opt
|
294 |
+
|
295 |
+
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
|
296 |
+
*.vbw
|
297 |
+
|
298 |
+
# Visual Studio LightSwitch build output
|
299 |
+
**/*.HTMLClient/GeneratedArtifacts
|
300 |
+
**/*.DesktopClient/GeneratedArtifacts
|
301 |
+
**/*.DesktopClient/ModelManifest.xml
|
302 |
+
**/*.Server/GeneratedArtifacts
|
303 |
+
**/*.Server/ModelManifest.xml
|
304 |
+
_Pvt_Extensions
|
305 |
+
|
306 |
+
# Paket dependency manager
|
307 |
+
.paket/paket.exe
|
308 |
+
paket-files/
|
309 |
+
|
310 |
+
# FAKE - F# Make
|
311 |
+
.fake/
|
312 |
+
|
313 |
+
# CodeRush personal settings
|
314 |
+
.cr/personal
|
315 |
+
|
316 |
+
# Python Tools for Visual Studio (PTVS)
|
317 |
+
__pycache__/
|
318 |
+
|
319 |
+
|
320 |
+
# Cake - Uncomment if you are using it
|
321 |
+
# tools/**
|
322 |
+
# !tools/packages.config
|
323 |
+
|
324 |
+
# Tabs Studio
|
325 |
+
*.tss
|
326 |
+
|
327 |
+
# Telerik's JustMock configuration file
|
328 |
+
*.jmconfig
|
329 |
+
|
330 |
+
# BizTalk build output
|
331 |
+
*.btp.cs
|
332 |
+
*.btm.cs
|
333 |
+
*.odx.cs
|
334 |
+
*.xsd.cs
|
335 |
+
|
336 |
+
# OpenCover UI analysis results
|
337 |
+
OpenCover/
|
338 |
+
|
339 |
+
# Azure Stream Analytics local run output
|
340 |
+
ASALocalRun/
|
341 |
+
|
342 |
+
# MSBuild Binary and Structured Log
|
343 |
+
*.binlog
|
344 |
+
|
345 |
+
# NVidia Nsight GPU debugger configuration file
|
346 |
+
*.nvuser
|
347 |
+
|
348 |
+
# MFractors (Xamarin productivity tool) working folder
|
349 |
+
.mfractor/
|
350 |
+
|
351 |
+
# Local History for Visual Studio
|
352 |
+
.localhistory/
|
353 |
+
|
354 |
+
# BeatPulse healthcheck temp database
|
355 |
+
healthchecksdb
|
356 |
+
|
357 |
+
# Backup folder for Package Reference Convert tool in Visual Studio 2017
|
358 |
+
MigrationBackup/
|
359 |
+
|
360 |
+
# Ionide (cross platform F# VS Code tools) working folder
|
361 |
+
.ionide/
|
362 |
+
|
363 |
+
# Fody - auto-generated XML schema
|
364 |
+
FodyWeavers.xsd
|
365 |
+
|
366 |
+
# build
|
367 |
+
build
|
368 |
+
monotonic_align/core.c
|
369 |
+
*.o
|
370 |
+
*.so
|
371 |
+
*.dll
|
372 |
+
|
373 |
+
# data
|
374 |
+
/config.json
|
375 |
+
/*.pth
|
376 |
+
*.wav
|
377 |
+
/monotonic_align/monotonic_align
|
378 |
+
/resources
|
379 |
+
/MoeGoe.spec
|
380 |
+
/dist/MoeGoe
|
381 |
+
/dist
|
382 |
+
|
383 |
+
.idea
|
app-update.py
ADDED
@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os
|
2 |
+
import json
|
3 |
+
import traceback
|
4 |
+
import logging
|
5 |
+
import gradio as gr
|
6 |
+
import numpy as np
|
7 |
+
import librosa
|
8 |
+
import torch
|
9 |
+
import asyncio
|
10 |
+
import edge_tts
|
11 |
+
import yt_dlp
|
12 |
+
import ffmpeg
|
13 |
+
import subprocess
|
14 |
+
import sys
|
15 |
+
import io
|
16 |
+
import wave
|
17 |
+
from datetime import datetime
|
18 |
+
from fairseq import checkpoint_utils
|
19 |
+
from infer_pack.models import SynthesizerTrnMs256NSFsid, SynthesizerTrnMs256NSFsid_nono
|
20 |
+
from vc_infer_pipeline import VC
|
21 |
+
from config import Config
|
22 |
+
config = Config()
|
23 |
+
logging.getLogger("numba").setLevel(logging.WARNING)
|
24 |
+
limitation = os.getenv("SYSTEM") == "spaces" # limit audio length in huggingface spaces
|
25 |
+
|
26 |
+
def create_vc_fn(tgt_sr, net_g, vc, if_f0, file_index):
|
27 |
+
def vc_fn(
|
28 |
+
input_audio,
|
29 |
+
f0_up_key,
|
30 |
+
f0_method,
|
31 |
+
index_rate,
|
32 |
+
tts_mode,
|
33 |
+
tts_text,
|
34 |
+
tts_voice
|
35 |
+
):
|
36 |
+
try:
|
37 |
+
if tts_mode:
|
38 |
+
if len(tts_text) > 100 and limitation:
|
39 |
+
return "Text is too long", None
|
40 |
+
if tts_text is None or tts_voice is None:
|
41 |
+
return "You need to enter text and select a voice", None
|
42 |
+
asyncio.run(edge_tts.Communicate(tts_text, "-".join(tts_voice.split('-')[:-1])).save("tts.mp3"))
|
43 |
+
audio, sr = librosa.load("tts.mp3", sr=16000, mono=True)
|
44 |
+
else:
|
45 |
+
if config.files:
|
46 |
+
audio, sr = librosa.load(input_audio, sr=16000, mono=True)
|
47 |
+
else:
|
48 |
+
if input_audio is None:
|
49 |
+
return "You need to upload an audio", None
|
50 |
+
sampling_rate, audio = input_audio
|
51 |
+
duration = audio.shape[0] / sampling_rate
|
52 |
+
if duration > 20 and limitation:
|
53 |
+
return "Please upload an audio file that is less than 20 seconds. If you need to generate a longer audio file, please use Colab.", None
|
54 |
+
audio = (audio / np.iinfo(audio.dtype).max).astype(np.float32)
|
55 |
+
if len(audio.shape) > 1:
|
56 |
+
audio = librosa.to_mono(audio.transpose(1, 0))
|
57 |
+
if sampling_rate != 16000:
|
58 |
+
audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
|
59 |
+
times = [0, 0, 0]
|
60 |
+
f0_up_key = int(f0_up_key)
|
61 |
+
audio_opt = vc.pipeline(
|
62 |
+
hubert_model,
|
63 |
+
net_g,
|
64 |
+
0,
|
65 |
+
audio,
|
66 |
+
times,
|
67 |
+
f0_up_key,
|
68 |
+
f0_method,
|
69 |
+
file_index,
|
70 |
+
index_rate,
|
71 |
+
if_f0,
|
72 |
+
f0_file=None,
|
73 |
+
)
|
74 |
+
print(
|
75 |
+
f"[{datetime.now().strftime('%Y-%m-%d %H:%M')}]: npy: {times[0]}, f0: {times[1]}s, infer: {times[2]}s"
|
76 |
+
)
|
77 |
+
return "Success", (tgt_sr, audio_opt)
|
78 |
+
except:
|
79 |
+
info = traceback.format_exc()
|
80 |
+
print(info)
|
81 |
+
return info, (None, None)
|
82 |
+
return vc_fn
|
83 |
+
|
84 |
+
def cut_vocal_and_inst(yt_url):
|
85 |
+
if yt_url != "":
|
86 |
+
if not os.path.exists("/content/youtube_audio"):
|
87 |
+
os.mkdir("/content/youtube_audio")
|
88 |
+
ydl_opts = {
|
89 |
+
'format': 'bestaudio/best',
|
90 |
+
'postprocessors': [{
|
91 |
+
'key': 'FFmpegExtractAudio',
|
92 |
+
'preferredcodec': 'wav',
|
93 |
+
}],
|
94 |
+
"outtmpl": '/content/youtube_audio/audio',
|
95 |
+
}
|
96 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
97 |
+
ydl.download([yt_url])
|
98 |
+
yt_audio_path = "/content/youtube_audio/audio.wav"
|
99 |
+
command = f"demucs --two-stems=vocals {yt_audio_path}"
|
100 |
+
result = subprocess.run(command.split(), stdout=subprocess.PIPE)
|
101 |
+
print(result.stdout.decode())
|
102 |
+
return ("/content/rvc-models/separated/htdemucs/audio/vocals.wav", "/content/rvc-models/separated/htdemucs/audio/no_vocals.wav", yt_audio_path, "/content/rvc-models/separated/htdemucs/audio/vocals.wav")
|
103 |
+
|
104 |
+
def combine_vocal_and_inst(audio_data, audio_volume):
|
105 |
+
print(audio_data)
|
106 |
+
if not os.path.exists("/content/result"):
|
107 |
+
os.mkdir("/content/result")
|
108 |
+
vocal_path = "/content/result/output.wav"
|
109 |
+
inst_path = "/content/rvc-models/separated/htdemucs/audio/no_vocals.wav"
|
110 |
+
output_path = "/content/result/combine.mp3"
|
111 |
+
with wave.open(vocal_path, "w") as wave_file:
|
112 |
+
wave_file.setnchannels(1)
|
113 |
+
wave_file.setsampwidth(2)
|
114 |
+
wave_file.setframerate(audio_data[0])
|
115 |
+
wave_file.writeframes(audio_data[1].tobytes())
|
116 |
+
command = f'ffmpeg -y -i {inst_path} -i {vocal_path} -filter_complex [1:a]volume={audio_volume}dB[v];[0:a][v]amix=inputs=2:duration=longest -b:a 320k -c:a libmp3lame {output_path}'
|
117 |
+
result = subprocess.run(command.split(), stdout=subprocess.PIPE)
|
118 |
+
return output_path
|
119 |
+
|
120 |
+
def load_hubert():
|
121 |
+
global hubert_model
|
122 |
+
models, _, _ = checkpoint_utils.load_model_ensemble_and_task(
|
123 |
+
["hubert_base.pt"],
|
124 |
+
suffix="",
|
125 |
+
)
|
126 |
+
hubert_model = models[0]
|
127 |
+
hubert_model = hubert_model.to(config.device)
|
128 |
+
if config.is_half:
|
129 |
+
hubert_model = hubert_model.half()
|
130 |
+
else:
|
131 |
+
hubert_model = hubert_model.float()
|
132 |
+
hubert_model.eval()
|
133 |
+
|
134 |
+
def change_to_tts_mode(tts_mode):
|
135 |
+
if tts_mode:
|
136 |
+
return gr.Audio.update(visible=False), gr.Textbox.update(visible=True), gr.Dropdown.update(visible=True)
|
137 |
+
else:
|
138 |
+
return gr.Audio.update(visible=True), gr.Textbox.update(visible=False), gr.Dropdown.update(visible=False)
|
139 |
+
|
140 |
+
def change_audio_to_path_mode(audio_mode):
|
141 |
+
if audio_mode:
|
142 |
+
return gr.Audio.update(visible=False), gr.Textbox.update(visible=True)
|
143 |
+
else:
|
144 |
+
return gr.Audio.update(visible=True), gr.Textbox.update(visible=False)
|
145 |
+
|
146 |
+
if __name__ == '__main__':
|
147 |
+
load_hubert()
|
148 |
+
models = []
|
149 |
+
tts_voice_list = asyncio.get_event_loop().run_until_complete(edge_tts.list_voices())
|
150 |
+
voices = [f"{v['ShortName']}-{v['Gender']}" for v in tts_voice_list]
|
151 |
+
folder_path = "weights"
|
152 |
+
for name in os.listdir(folder_path):
|
153 |
+
print("check folder: " + name)
|
154 |
+
if name.startswith("."): break
|
155 |
+
cover_path = glob.glob(f"{folder_path}/{name}/*.png") + glob.glob(f"{folder_path}/{name}/*.jpg")
|
156 |
+
index_path = glob.glob(f"{folder_path}/{name}/*.index")
|
157 |
+
checkpoint_path = glob.glob(f"{folder_path}/{name}/*.pth")
|
158 |
+
title = name
|
159 |
+
cover = cover_path[0]
|
160 |
+
index = index_path[0]
|
161 |
+
cpt = torch.load(checkpoint_path[0], map_location="cpu")
|
162 |
+
tgt_sr = cpt["config"][-1]
|
163 |
+
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
|
164 |
+
if_f0 = cpt.get("f0", 1)
|
165 |
+
if if_f0 == 1:
|
166 |
+
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=config.is_half)
|
167 |
+
else:
|
168 |
+
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
|
169 |
+
del net_g.enc_q
|
170 |
+
print(net_g.load_state_dict(cpt["weight"], strict=False)) # 不加这一行清不干净, 真奇葩
|
171 |
+
net_g.eval().to(config.device)
|
172 |
+
if config.is_half:
|
173 |
+
net_g = net_g.half()
|
174 |
+
else:
|
175 |
+
net_g = net_g.float()
|
176 |
+
vc = VC(tgt_sr, config)
|
177 |
+
models.append((name, title, author, cover, create_vc_fn(tgt_sr, net_g, vc, if_f0, index)))
|
178 |
+
with gr.Blocks() as app:
|
179 |
+
gr.Markdown(
|
180 |
+
"# <center> RVC Models (Latest Update)\n"
|
181 |
+
"## <center> The input audio should be clean and pure voice without background music.\n"
|
182 |
+
"### <center> More feature will be added soon... \n"
|
183 |
+
"<center> [![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1hx6kKvIuv5XNY1Gai2PEuZhpO5z6xpVh?usp=sharing)\n\n"
|
184 |
+
"<center> [![Original Repo](https://badgen.net/badge/icon/github?icon=github&label=Original%20Repo)](https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI)"
|
185 |
+
)
|
186 |
+
with gr.Tabs():
|
187 |
+
for (name, title, author, cover, vc_fn) in models:
|
188 |
+
with gr.TabItem(name):
|
189 |
+
with gr.Row():
|
190 |
+
gr.Markdown(
|
191 |
+
'<div align="center">'
|
192 |
+
f'<div>{title}</div>\n'+
|
193 |
+
(f'<div>Model author: {author}</div>' if author else "")+
|
194 |
+
(f'<img style="width:auto;height:300px;" src="file/{cover}">' if cover else "")+
|
195 |
+
'</div>'
|
196 |
+
)
|
197 |
+
with gr.Row():
|
198 |
+
if config.files:
|
199 |
+
with gr.Column():
|
200 |
+
vc_youtube = gr.Textbox(label="Youtube URL")
|
201 |
+
vc_convert = gr.Button("Convert", variant="primary")
|
202 |
+
vc_vocal_preview = gr.Audio(label="Vocal Preview")
|
203 |
+
vc_inst_preview = gr.Audio(label="Instrumental Preview")
|
204 |
+
vc_audio_preview = gr.Audio(label="Audio Preview")
|
205 |
+
with gr.Column():
|
206 |
+
vc_audio_mode = gr.Checkbox(label="Use audio path", value=False)
|
207 |
+
if config.files:
|
208 |
+
vc_input = gr.Textbox(label="Input audio path")
|
209 |
+
else:
|
210 |
+
vc_input = gr.Audio(label="Input audio"+' (less than 20 seconds)' if limitation else '')
|
211 |
+
vc_transpose = gr.Number(label="Transpose", value=0)
|
212 |
+
vc_f0method = gr.Radio(
|
213 |
+
label="Pitch extraction algorithm, PM is fast but Harvest is better for low frequencies",
|
214 |
+
choices=["pm", "harvest"],
|
215 |
+
value="pm",
|
216 |
+
interactive=True,
|
217 |
+
)
|
218 |
+
vc_index_ratio = gr.Slider(
|
219 |
+
minimum=0,
|
220 |
+
maximum=1,
|
221 |
+
label="Retrieval feature ratio",
|
222 |
+
value=0.6,
|
223 |
+
interactive=True,
|
224 |
+
)
|
225 |
+
tts_mode = gr.Checkbox(label="tts (use edge-tts as input)", value=False)
|
226 |
+
tts_text = gr.Textbox(visible=False,label="TTS text (100 words limitation)" if limitation else "TTS text")
|
227 |
+
tts_voice = gr.Dropdown(label="Edge-tts speaker", choices=voices, visible=False, allow_custom_value=False, value="en-US-AnaNeural-Female")
|
228 |
+
vc_submit = gr.Button("Generate", variant="primary")
|
229 |
+
vc_output1 = gr.Textbox(label="Output Message")
|
230 |
+
vc_output2 = gr.Audio(label="Output Audio")
|
231 |
+
if config.files:
|
232 |
+
with gr.Column():
|
233 |
+
vc_volume = gr.Slider(
|
234 |
+
minimum=0,
|
235 |
+
maximum=10,
|
236 |
+
label="Vocal volume",
|
237 |
+
value=5,
|
238 |
+
interactive=True,
|
239 |
+
step=1
|
240 |
+
)
|
241 |
+
vc_outputCombine = gr.Audio(label="Output Combined Audio")
|
242 |
+
vc_combine = gr.Button("Combine",variant="primary")
|
243 |
+
vc_submit.click(vc_fn, [vc_input, vc_transpose, vc_f0method, vc_index_ratio, tts_mode, tts_text, tts_voice], [vc_output1, vc_output2])
|
244 |
+
tts_mode.change(change_to_tts_mode, [tts_mode], [vc_input, tts_text, tts_voice])
|
245 |
+
vc_audio_input(change_audio_to_path_mode, [vc_audio_mode])
|
246 |
+
if config.files:
|
247 |
+
vc_convert.click(cut_vocal_and_inst, vc_youtube, [vc_vocal_preview, vc_inst_preview, vc_audio_preview, vc_input])
|
248 |
+
vc_combine.click(combine_vocal_and_inst, [vc_output2, vc_volume], vc_outputCombine)
|
249 |
+
app.queue(concurrency_count=1, max_size=20, api_open=config.api).launch(share=config.share)
|
app.py
ADDED
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import glob
|
3 |
+
import json
|
4 |
+
import argparse
|
5 |
+
import traceback
|
6 |
+
import logging
|
7 |
+
import gradio as gr
|
8 |
+
import numpy as np
|
9 |
+
import librosa
|
10 |
+
import torch
|
11 |
+
import asyncio
|
12 |
+
import edge_tts
|
13 |
+
from datetime import datetime
|
14 |
+
from fairseq import checkpoint_utils
|
15 |
+
from infer_pack.models import SynthesizerTrnMs256NSFsid, SynthesizerTrnMs256NSFsid_nono
|
16 |
+
from vc_infer_pipeline import VC
|
17 |
+
from config import Config
|
18 |
+
config = Config()
|
19 |
+
logging.getLogger("numba").setLevel(logging.WARNING)
|
20 |
+
limitation = os.getenv("SYSTEM") == "spaces" # limit audio length in huggingface spaces
|
21 |
+
|
22 |
+
def create_vc_fn(tgt_sr, net_g, vc, if_f0, file_index):
|
23 |
+
def vc_fn(
|
24 |
+
input_audio,
|
25 |
+
f0_up_key,
|
26 |
+
f0_method,
|
27 |
+
index_rate,
|
28 |
+
tts_mode,
|
29 |
+
tts_text,
|
30 |
+
tts_voice
|
31 |
+
):
|
32 |
+
try:
|
33 |
+
if tts_mode:
|
34 |
+
if len(tts_text) > 100 and limitation:
|
35 |
+
return "Text is too long", None
|
36 |
+
if tts_text is None or tts_voice is None:
|
37 |
+
return "You need to enter text and select a voice", None
|
38 |
+
asyncio.run(edge_tts.Communicate(tts_text, "-".join(tts_voice.split('-')[:-1])).save("tts.mp3"))
|
39 |
+
audio, sr = librosa.load("tts.mp3", sr=16000, mono=True)
|
40 |
+
else:
|
41 |
+
if config.files:
|
42 |
+
audio, sr = librosa.load(input_audio, sr=16000, mono=True)
|
43 |
+
else:
|
44 |
+
if input_audio is None:
|
45 |
+
return "You need to upload an audio", None
|
46 |
+
sampling_rate, audio = input_audio
|
47 |
+
duration = audio.shape[0] / sampling_rate
|
48 |
+
if duration > 20 and limitation:
|
49 |
+
return "Please upload an audio file that is less than 20 seconds. If you need to generate a longer audio file, please use Colab.", None
|
50 |
+
audio = (audio / np.iinfo(audio.dtype).max).astype(np.float32)
|
51 |
+
if len(audio.shape) > 1:
|
52 |
+
audio = librosa.to_mono(audio.transpose(1, 0))
|
53 |
+
if sampling_rate != 16000:
|
54 |
+
audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
|
55 |
+
times = [0, 0, 0]
|
56 |
+
f0_up_key = int(f0_up_key)
|
57 |
+
audio_opt = vc.pipeline(
|
58 |
+
hubert_model,
|
59 |
+
net_g,
|
60 |
+
0,
|
61 |
+
audio,
|
62 |
+
times,
|
63 |
+
f0_up_key,
|
64 |
+
f0_method,
|
65 |
+
file_index,
|
66 |
+
index_rate,
|
67 |
+
if_f0,
|
68 |
+
f0_file=None,
|
69 |
+
)
|
70 |
+
print(
|
71 |
+
f"[{datetime.now().strftime('%Y-%m-%d %H:%M')}]: npy: {times[0]}, f0: {times[1]}s, infer: {times[2]}s"
|
72 |
+
)
|
73 |
+
return "Success", (tgt_sr, audio_opt)
|
74 |
+
except:
|
75 |
+
info = traceback.format_exc()
|
76 |
+
print(info)
|
77 |
+
return info, (None, None)
|
78 |
+
return vc_fn
|
79 |
+
|
80 |
+
def load_hubert():
|
81 |
+
global hubert_model
|
82 |
+
models, _, _ = checkpoint_utils.load_model_ensemble_and_task(
|
83 |
+
["hubert_base.pt"],
|
84 |
+
suffix="",
|
85 |
+
)
|
86 |
+
hubert_model = models[0]
|
87 |
+
hubert_model = hubert_model.to(device)
|
88 |
+
if is_half:
|
89 |
+
hubert_model = hubert_model.half()
|
90 |
+
else:
|
91 |
+
hubert_model = hubert_model.float()
|
92 |
+
hubert_model.eval()
|
93 |
+
|
94 |
+
def change_to_tts_mode(tts_mode):
|
95 |
+
if tts_mode:
|
96 |
+
return gr.Audio.update(visible=False), gr.Textbox.update(visible=True), gr.Dropdown.update(visible=True)
|
97 |
+
else:
|
98 |
+
return gr.Audio.update(visible=True), gr.Textbox.update(visible=False), gr.Dropdown.update(visible=False)
|
99 |
+
|
100 |
+
if __name__ == '__main__':
|
101 |
+
load_hubert()
|
102 |
+
models = []
|
103 |
+
tts_voice_list = asyncio.get_event_loop().run_until_complete(edge_tts.list_voices())
|
104 |
+
voices = [f"{v['ShortName']}-{v['Gender']}" for v in tts_voice_list]
|
105 |
+
folder_path = "weights"
|
106 |
+
for name in os.listdir(folder_path):
|
107 |
+
print("check folder: " + name)
|
108 |
+
if name.startswith("."): break
|
109 |
+
cover_path = glob.glob(f"{folder_path}/{name}/*.png") + glob.glob(f"{folder_path}/{name}/*.jpg")
|
110 |
+
index_path = glob.glob(f"{folder_path}/{name}/*.index")
|
111 |
+
checkpoint_path = glob.glob(f"{folder_path}/{name}/*.pth")
|
112 |
+
title = name
|
113 |
+
cover = cover_path[0]
|
114 |
+
index = index_path[0]
|
115 |
+
cpt = torch.load(checkpoint_path[0], map_location="cpu")
|
116 |
+
tgt_sr = cpt["config"][-1]
|
117 |
+
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
|
118 |
+
if_f0 = cpt.get("f0", 1)
|
119 |
+
if if_f0 == 1:
|
120 |
+
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=config.is_half)
|
121 |
+
else:
|
122 |
+
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
|
123 |
+
del net_g.enc_q
|
124 |
+
print(net_g.load_state_dict(cpt["weight"], strict=False)) # 不加这一行清不干净, 真奇葩
|
125 |
+
net_g.eval().to(config.device)
|
126 |
+
if config.is_half:
|
127 |
+
net_g = net_g.half()
|
128 |
+
else:
|
129 |
+
net_g = net_g.float()
|
130 |
+
vc = VC(tgt_sr, config)
|
131 |
+
models.append((name, title, author, cover, create_vc_fn(tgt_sr, net_g, vc, if_f0, index)))
|
132 |
+
with gr.Blocks() as app:
|
133 |
+
gr.Markdown(
|
134 |
+
"# <center> RVC Models (Latest Update)\n"
|
135 |
+
"## <center> The input audio should be clean and pure voice without background music.\n"
|
136 |
+
"### <center> More feature will be added soon... \n"
|
137 |
+
"<center> [![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1hx6kKvIuv5XNY1Gai2PEuZhpO5z6xpVh?usp=sharing)\n\n"
|
138 |
+
"<center> [![Original Repo](https://badgen.net/badge/icon/github?icon=github&label=Original%20Repo)](https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI)"
|
139 |
+
)
|
140 |
+
with gr.Tabs():
|
141 |
+
for (name, title, author, cover, vc_fn) in models:
|
142 |
+
with gr.TabItem(name):
|
143 |
+
with gr.Row():
|
144 |
+
gr.Markdown(
|
145 |
+
'<div align="center">'
|
146 |
+
f'<div>{title}</div>\n'+
|
147 |
+
(f'<div>Model author: {author}</div>' if author else "")+
|
148 |
+
(f'<img style="width:auto;height:300px;" src="file/{cover}">' if cover else "")+
|
149 |
+
'</div>'
|
150 |
+
)
|
151 |
+
with gr.Row():
|
152 |
+
with gr.Column():
|
153 |
+
vc_input = gr.Audio(label="Input audio"+' (less than 20 seconds)' if limitation else '')
|
154 |
+
vc_transpose = gr.Number(label="Transpose", value=0)
|
155 |
+
vc_f0method = gr.Radio(
|
156 |
+
label="Pitch extraction algorithm, PM is fast but Harvest is better for low frequencies",
|
157 |
+
choices=["pm", "harvest"],
|
158 |
+
value="pm",
|
159 |
+
interactive=True,
|
160 |
+
)
|
161 |
+
vc_index_ratio = gr.Slider(
|
162 |
+
minimum=0,
|
163 |
+
maximum=1,
|
164 |
+
label="Retrieval feature ratio",
|
165 |
+
value=0.6,
|
166 |
+
interactive=True,
|
167 |
+
)
|
168 |
+
tts_mode = gr.Checkbox(label="tts (use edge-tts as input)", value=False)
|
169 |
+
tts_text = gr.Textbox(visible=False,label="TTS text (100 words limitation)" if limitation else "TTS text")
|
170 |
+
tts_voice = gr.Dropdown(label="Edge-tts speaker", choices=voices, visible=False, allow_custom_value=False, value="en-US-AnaNeural-Female")
|
171 |
+
vc_submit = gr.Button("Generate", variant="primary")
|
172 |
+
with gr.Column():
|
173 |
+
vc_output1 = gr.Textbox(label="Output Message")
|
174 |
+
vc_output2 = gr.Audio(label="Output Audio")
|
175 |
+
vc_submit.click(vc_fn, [vc_input, vc_transpose, vc_f0method, vc_index_ratio, tts_mode, tts_text, tts_voice], [vc_output1, vc_output2])
|
176 |
+
tts_mode.change(change_to_tts_mode, [tts_mode], [vc_input, tts_text, tts_voice])
|
177 |
+
app.queue(concurrency_count=1, max_size=20, api_open=config.api).launch(share=config.share)
|
config.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import torch
|
3 |
+
from multiprocessing import cpu_count
|
4 |
+
|
5 |
+
|
6 |
+
class Config:
|
7 |
+
def __init__(self):
|
8 |
+
self.device = "cuda:0"
|
9 |
+
self.is_half = True
|
10 |
+
self.n_cpu = 0
|
11 |
+
self.gpu_name = None
|
12 |
+
self.gpu_mem = None
|
13 |
+
(
|
14 |
+
self.python_cmd,
|
15 |
+
self.listen_port,
|
16 |
+
self.iscolab,
|
17 |
+
self.noparallel,
|
18 |
+
self.noautoopen,
|
19 |
+
self.api,
|
20 |
+
self.share,
|
21 |
+
self.files
|
22 |
+
) = self.arg_parse()
|
23 |
+
self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
|
24 |
+
|
25 |
+
@staticmethod
|
26 |
+
def arg_parse() -> tuple:
|
27 |
+
parser = argparse.ArgumentParser()
|
28 |
+
parser.add_argument("--port", type=int, default=7865, help="Listen port")
|
29 |
+
parser.add_argument(
|
30 |
+
"--pycmd", type=str, default="python", help="Python command"
|
31 |
+
)
|
32 |
+
parser.add_argument("--colab", action="store_true", help="Launch in colab")
|
33 |
+
parser.add_argument(
|
34 |
+
"--noparallel", action="store_true", help="Disable parallel processing"
|
35 |
+
)
|
36 |
+
parser.add_argument(
|
37 |
+
"--noautoopen",
|
38 |
+
action="store_true",
|
39 |
+
help="Do not open in browser automatically",
|
40 |
+
)
|
41 |
+
parser.add_argument('--api', action="store_true", default=False)
|
42 |
+
parser.add_argument("--share", action="store_true", default=False, help="share gradio app")
|
43 |
+
parser.add_argument("--files", action="store_true", default=False, help="load audio from path")
|
44 |
+
cmd_opts = parser.parse_args()
|
45 |
+
|
46 |
+
cmd_opts.port = cmd_opts.port if 0 <= cmd_opts.port <= 65535 else 7865
|
47 |
+
|
48 |
+
return (
|
49 |
+
cmd_opts.pycmd,
|
50 |
+
cmd_opts.port,
|
51 |
+
cmd_opts.colab,
|
52 |
+
cmd_opts.noparallel,
|
53 |
+
cmd_opts.noautoopen,
|
54 |
+
cmd_opts.api,
|
55 |
+
cmd_opts.share,
|
56 |
+
cmd_opts.files,
|
57 |
+
)
|
58 |
+
|
59 |
+
def device_config(self) -> tuple:
|
60 |
+
if torch.cuda.is_available():
|
61 |
+
i_device = int(self.device.split(":")[-1])
|
62 |
+
self.gpu_name = torch.cuda.get_device_name(i_device)
|
63 |
+
if (
|
64 |
+
("16" in self.gpu_name and "V100" not in self.gpu_name.upper())
|
65 |
+
or "P40" in self.gpu_name.upper()
|
66 |
+
or "1060" in self.gpu_name
|
67 |
+
or "1070" in self.gpu_name
|
68 |
+
or "1080" in self.gpu_name
|
69 |
+
):
|
70 |
+
print("16系/10系显卡和P40强制单精度")
|
71 |
+
self.is_half = False
|
72 |
+
for config_file in ["32k.json", "40k.json", "48k.json"]:
|
73 |
+
with open(f"configs/{config_file}", "r") as f:
|
74 |
+
strr = f.read().replace("true", "false")
|
75 |
+
with open(f"configs/{config_file}", "w") as f:
|
76 |
+
f.write(strr)
|
77 |
+
with open("trainset_preprocess_pipeline_print.py", "r") as f:
|
78 |
+
strr = f.read().replace("3.7", "3.0")
|
79 |
+
with open("trainset_preprocess_pipeline_print.py", "w") as f:
|
80 |
+
f.write(strr)
|
81 |
+
else:
|
82 |
+
self.gpu_name = None
|
83 |
+
self.gpu_mem = int(
|
84 |
+
torch.cuda.get_device_properties(i_device).total_memory
|
85 |
+
/ 1024
|
86 |
+
/ 1024
|
87 |
+
/ 1024
|
88 |
+
+ 0.4
|
89 |
+
)
|
90 |
+
if self.gpu_mem <= 4:
|
91 |
+
with open("trainset_preprocess_pipeline_print.py", "r") as f:
|
92 |
+
strr = f.read().replace("3.7", "3.0")
|
93 |
+
with open("trainset_preprocess_pipeline_print.py", "w") as f:
|
94 |
+
f.write(strr)
|
95 |
+
elif torch.backends.mps.is_available():
|
96 |
+
print("没有发现支持的N卡, 使用MPS进行推理")
|
97 |
+
self.device = "mps"
|
98 |
+
self.is_half = False
|
99 |
+
else:
|
100 |
+
print("没有发现支持的N卡, 使用CPU进行推理")
|
101 |
+
self.device = "cpu"
|
102 |
+
self.is_half = False
|
103 |
+
|
104 |
+
if self.n_cpu == 0:
|
105 |
+
self.n_cpu = cpu_count()
|
106 |
+
|
107 |
+
if self.is_half:
|
108 |
+
# 6G显存配置
|
109 |
+
x_pad = 3
|
110 |
+
x_query = 10
|
111 |
+
x_center = 60
|
112 |
+
x_max = 65
|
113 |
+
else:
|
114 |
+
# 5G显存配置
|
115 |
+
x_pad = 1
|
116 |
+
x_query = 6
|
117 |
+
x_center = 38
|
118 |
+
x_max = 41
|
119 |
+
|
120 |
+
if self.gpu_mem != None and self.gpu_mem <= 4:
|
121 |
+
x_pad = 1
|
122 |
+
x_query = 5
|
123 |
+
x_center = 30
|
124 |
+
x_max = 32
|
125 |
+
|
126 |
+
return x_pad, x_query, x_center, x_max
|
hubert_base.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lf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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import copy
|
2 |
+
import math
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
from torch import nn
|
6 |
+
from torch.nn import functional as F
|
7 |
+
|
8 |
+
from infer_pack import commons
|
9 |
+
from infer_pack import modules
|
10 |
+
from infer_pack.modules import LayerNorm
|
11 |
+
|
12 |
+
|
13 |
+
class Encoder(nn.Module):
|
14 |
+
def __init__(
|
15 |
+
self,
|
16 |
+
hidden_channels,
|
17 |
+
filter_channels,
|
18 |
+
n_heads,
|
19 |
+
n_layers,
|
20 |
+
kernel_size=1,
|
21 |
+
p_dropout=0.0,
|
22 |
+
window_size=10,
|
23 |
+
**kwargs
|
24 |
+
):
|
25 |
+
super().__init__()
|
26 |
+
self.hidden_channels = hidden_channels
|
27 |
+
self.filter_channels = filter_channels
|
28 |
+
self.n_heads = n_heads
|
29 |
+
self.n_layers = n_layers
|
30 |
+
self.kernel_size = kernel_size
|
31 |
+
self.p_dropout = p_dropout
|
32 |
+
self.window_size = window_size
|
33 |
+
|
34 |
+
self.drop = nn.Dropout(p_dropout)
|
35 |
+
self.attn_layers = nn.ModuleList()
|
36 |
+
self.norm_layers_1 = nn.ModuleList()
|
37 |
+
self.ffn_layers = nn.ModuleList()
|
38 |
+
self.norm_layers_2 = nn.ModuleList()
|
39 |
+
for i in range(self.n_layers):
|
40 |
+
self.attn_layers.append(
|
41 |
+
MultiHeadAttention(
|
42 |
+
hidden_channels,
|
43 |
+
hidden_channels,
|
44 |
+
n_heads,
|
45 |
+
p_dropout=p_dropout,
|
46 |
+
window_size=window_size,
|
47 |
+
)
|
48 |
+
)
|
49 |
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
50 |
+
self.ffn_layers.append(
|
51 |
+
FFN(
|
52 |
+
hidden_channels,
|
53 |
+
hidden_channels,
|
54 |
+
filter_channels,
|
55 |
+
kernel_size,
|
56 |
+
p_dropout=p_dropout,
|
57 |
+
)
|
58 |
+
)
|
59 |
+
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
60 |
+
|
61 |
+
def forward(self, x, x_mask):
|
62 |
+
attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
63 |
+
x = x * x_mask
|
64 |
+
for i in range(self.n_layers):
|
65 |
+
y = self.attn_layers[i](x, x, attn_mask)
|
66 |
+
y = self.drop(y)
|
67 |
+
x = self.norm_layers_1[i](x + y)
|
68 |
+
|
69 |
+
y = self.ffn_layers[i](x, x_mask)
|
70 |
+
y = self.drop(y)
|
71 |
+
x = self.norm_layers_2[i](x + y)
|
72 |
+
x = x * x_mask
|
73 |
+
return x
|
74 |
+
|
75 |
+
|
76 |
+
class Decoder(nn.Module):
|
77 |
+
def __init__(
|
78 |
+
self,
|
79 |
+
hidden_channels,
|
80 |
+
filter_channels,
|
81 |
+
n_heads,
|
82 |
+
n_layers,
|
83 |
+
kernel_size=1,
|
84 |
+
p_dropout=0.0,
|
85 |
+
proximal_bias=False,
|
86 |
+
proximal_init=True,
|
87 |
+
**kwargs
|
88 |
+
):
|
89 |
+
super().__init__()
|
90 |
+
self.hidden_channels = hidden_channels
|
91 |
+
self.filter_channels = filter_channels
|
92 |
+
self.n_heads = n_heads
|
93 |
+
self.n_layers = n_layers
|
94 |
+
self.kernel_size = kernel_size
|
95 |
+
self.p_dropout = p_dropout
|
96 |
+
self.proximal_bias = proximal_bias
|
97 |
+
self.proximal_init = proximal_init
|
98 |
+
|
99 |
+
self.drop = nn.Dropout(p_dropout)
|
100 |
+
self.self_attn_layers = nn.ModuleList()
|
101 |
+
self.norm_layers_0 = nn.ModuleList()
|
102 |
+
self.encdec_attn_layers = nn.ModuleList()
|
103 |
+
self.norm_layers_1 = nn.ModuleList()
|
104 |
+
self.ffn_layers = nn.ModuleList()
|
105 |
+
self.norm_layers_2 = nn.ModuleList()
|
106 |
+
for i in range(self.n_layers):
|
107 |
+
self.self_attn_layers.append(
|
108 |
+
MultiHeadAttention(
|
109 |
+
hidden_channels,
|
110 |
+
hidden_channels,
|
111 |
+
n_heads,
|
112 |
+
p_dropout=p_dropout,
|
113 |
+
proximal_bias=proximal_bias,
|
114 |
+
proximal_init=proximal_init,
|
115 |
+
)
|
116 |
+
)
|
117 |
+
self.norm_layers_0.append(LayerNorm(hidden_channels))
|
118 |
+
self.encdec_attn_layers.append(
|
119 |
+
MultiHeadAttention(
|
120 |
+
hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout
|
121 |
+
)
|
122 |
+
)
|
123 |
+
self.norm_layers_1.append(LayerNorm(hidden_channels))
|
124 |
+
self.ffn_layers.append(
|
125 |
+
FFN(
|
126 |
+
hidden_channels,
|
127 |
+
hidden_channels,
|
128 |
+
filter_channels,
|
129 |
+
kernel_size,
|
130 |
+
p_dropout=p_dropout,
|
131 |
+
causal=True,
|
132 |
+
)
|
133 |
+
)
|
134 |
+
self.norm_layers_2.append(LayerNorm(hidden_channels))
|
135 |
+
|
136 |
+
def forward(self, x, x_mask, h, h_mask):
|
137 |
+
"""
|
138 |
+
x: decoder input
|
139 |
+
h: encoder output
|
140 |
+
"""
|
141 |
+
self_attn_mask = commons.subsequent_mask(x_mask.size(2)).to(
|
142 |
+
device=x.device, dtype=x.dtype
|
143 |
+
)
|
144 |
+
encdec_attn_mask = h_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
|
145 |
+
x = x * x_mask
|
146 |
+
for i in range(self.n_layers):
|
147 |
+
y = self.self_attn_layers[i](x, x, self_attn_mask)
|
148 |
+
y = self.drop(y)
|
149 |
+
x = self.norm_layers_0[i](x + y)
|
150 |
+
|
151 |
+
y = self.encdec_attn_layers[i](x, h, encdec_attn_mask)
|
152 |
+
y = self.drop(y)
|
153 |
+
x = self.norm_layers_1[i](x + y)
|
154 |
+
|
155 |
+
y = self.ffn_layers[i](x, x_mask)
|
156 |
+
y = self.drop(y)
|
157 |
+
x = self.norm_layers_2[i](x + y)
|
158 |
+
x = x * x_mask
|
159 |
+
return x
|
160 |
+
|
161 |
+
|
162 |
+
class MultiHeadAttention(nn.Module):
|
163 |
+
def __init__(
|
164 |
+
self,
|
165 |
+
channels,
|
166 |
+
out_channels,
|
167 |
+
n_heads,
|
168 |
+
p_dropout=0.0,
|
169 |
+
window_size=None,
|
170 |
+
heads_share=True,
|
171 |
+
block_length=None,
|
172 |
+
proximal_bias=False,
|
173 |
+
proximal_init=False,
|
174 |
+
):
|
175 |
+
super().__init__()
|
176 |
+
assert channels % n_heads == 0
|
177 |
+
|
178 |
+
self.channels = channels
|
179 |
+
self.out_channels = out_channels
|
180 |
+
self.n_heads = n_heads
|
181 |
+
self.p_dropout = p_dropout
|
182 |
+
self.window_size = window_size
|
183 |
+
self.heads_share = heads_share
|
184 |
+
self.block_length = block_length
|
185 |
+
self.proximal_bias = proximal_bias
|
186 |
+
self.proximal_init = proximal_init
|
187 |
+
self.attn = None
|
188 |
+
|
189 |
+
self.k_channels = channels // n_heads
|
190 |
+
self.conv_q = nn.Conv1d(channels, channels, 1)
|
191 |
+
self.conv_k = nn.Conv1d(channels, channels, 1)
|
192 |
+
self.conv_v = nn.Conv1d(channels, channels, 1)
|
193 |
+
self.conv_o = nn.Conv1d(channels, out_channels, 1)
|
194 |
+
self.drop = nn.Dropout(p_dropout)
|
195 |
+
|
196 |
+
if window_size is not None:
|
197 |
+
n_heads_rel = 1 if heads_share else n_heads
|
198 |
+
rel_stddev = self.k_channels**-0.5
|
199 |
+
self.emb_rel_k = nn.Parameter(
|
200 |
+
torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
|
201 |
+
* rel_stddev
|
202 |
+
)
|
203 |
+
self.emb_rel_v = nn.Parameter(
|
204 |
+
torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
|
205 |
+
* rel_stddev
|
206 |
+
)
|
207 |
+
|
208 |
+
nn.init.xavier_uniform_(self.conv_q.weight)
|
209 |
+
nn.init.xavier_uniform_(self.conv_k.weight)
|
210 |
+
nn.init.xavier_uniform_(self.conv_v.weight)
|
211 |
+
if proximal_init:
|
212 |
+
with torch.no_grad():
|
213 |
+
self.conv_k.weight.copy_(self.conv_q.weight)
|
214 |
+
self.conv_k.bias.copy_(self.conv_q.bias)
|
215 |
+
|
216 |
+
def forward(self, x, c, attn_mask=None):
|
217 |
+
q = self.conv_q(x)
|
218 |
+
k = self.conv_k(c)
|
219 |
+
v = self.conv_v(c)
|
220 |
+
|
221 |
+
x, self.attn = self.attention(q, k, v, mask=attn_mask)
|
222 |
+
|
223 |
+
x = self.conv_o(x)
|
224 |
+
return x
|
225 |
+
|
226 |
+
def attention(self, query, key, value, mask=None):
|
227 |
+
# reshape [b, d, t] -> [b, n_h, t, d_k]
|
228 |
+
b, d, t_s, t_t = (*key.size(), query.size(2))
|
229 |
+
query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3)
|
230 |
+
key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
231 |
+
value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
|
232 |
+
|
233 |
+
scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
|
234 |
+
if self.window_size is not None:
|
235 |
+
assert (
|
236 |
+
t_s == t_t
|
237 |
+
), "Relative attention is only available for self-attention."
|
238 |
+
key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
|
239 |
+
rel_logits = self._matmul_with_relative_keys(
|
240 |
+
query / math.sqrt(self.k_channels), key_relative_embeddings
|
241 |
+
)
|
242 |
+
scores_local = self._relative_position_to_absolute_position(rel_logits)
|
243 |
+
scores = scores + scores_local
|
244 |
+
if self.proximal_bias:
|
245 |
+
assert t_s == t_t, "Proximal bias is only available for self-attention."
|
246 |
+
scores = scores + self._attention_bias_proximal(t_s).to(
|
247 |
+
device=scores.device, dtype=scores.dtype
|
248 |
+
)
|
249 |
+
if mask is not None:
|
250 |
+
scores = scores.masked_fill(mask == 0, -1e4)
|
251 |
+
if self.block_length is not None:
|
252 |
+
assert (
|
253 |
+
t_s == t_t
|
254 |
+
), "Local attention is only available for self-attention."
|
255 |
+
block_mask = (
|
256 |
+
torch.ones_like(scores)
|
257 |
+
.triu(-self.block_length)
|
258 |
+
.tril(self.block_length)
|
259 |
+
)
|
260 |
+
scores = scores.masked_fill(block_mask == 0, -1e4)
|
261 |
+
p_attn = F.softmax(scores, dim=-1) # [b, n_h, t_t, t_s]
|
262 |
+
p_attn = self.drop(p_attn)
|
263 |
+
output = torch.matmul(p_attn, value)
|
264 |
+
if self.window_size is not None:
|
265 |
+
relative_weights = self._absolute_position_to_relative_position(p_attn)
|
266 |
+
value_relative_embeddings = self._get_relative_embeddings(
|
267 |
+
self.emb_rel_v, t_s
|
268 |
+
)
|
269 |
+
output = output + self._matmul_with_relative_values(
|
270 |
+
relative_weights, value_relative_embeddings
|
271 |
+
)
|
272 |
+
output = (
|
273 |
+
output.transpose(2, 3).contiguous().view(b, d, t_t)
|
274 |
+
) # [b, n_h, t_t, d_k] -> [b, d, t_t]
|
275 |
+
return output, p_attn
|
276 |
+
|
277 |
+
def _matmul_with_relative_values(self, x, y):
|
278 |
+
"""
|
279 |
+
x: [b, h, l, m]
|
280 |
+
y: [h or 1, m, d]
|
281 |
+
ret: [b, h, l, d]
|
282 |
+
"""
|
283 |
+
ret = torch.matmul(x, y.unsqueeze(0))
|
284 |
+
return ret
|
285 |
+
|
286 |
+
def _matmul_with_relative_keys(self, x, y):
|
287 |
+
"""
|
288 |
+
x: [b, h, l, d]
|
289 |
+
y: [h or 1, m, d]
|
290 |
+
ret: [b, h, l, m]
|
291 |
+
"""
|
292 |
+
ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1))
|
293 |
+
return ret
|
294 |
+
|
295 |
+
def _get_relative_embeddings(self, relative_embeddings, length):
|
296 |
+
max_relative_position = 2 * self.window_size + 1
|
297 |
+
# Pad first before slice to avoid using cond ops.
|
298 |
+
pad_length = max(length - (self.window_size + 1), 0)
|
299 |
+
slice_start_position = max((self.window_size + 1) - length, 0)
|
300 |
+
slice_end_position = slice_start_position + 2 * length - 1
|
301 |
+
if pad_length > 0:
|
302 |
+
padded_relative_embeddings = F.pad(
|
303 |
+
relative_embeddings,
|
304 |
+
commons.convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]]),
|
305 |
+
)
|
306 |
+
else:
|
307 |
+
padded_relative_embeddings = relative_embeddings
|
308 |
+
used_relative_embeddings = padded_relative_embeddings[
|
309 |
+
:, slice_start_position:slice_end_position
|
310 |
+
]
|
311 |
+
return used_relative_embeddings
|
312 |
+
|
313 |
+
def _relative_position_to_absolute_position(self, x):
|
314 |
+
"""
|
315 |
+
x: [b, h, l, 2*l-1]
|
316 |
+
ret: [b, h, l, l]
|
317 |
+
"""
|
318 |
+
batch, heads, length, _ = x.size()
|
319 |
+
# Concat columns of pad to shift from relative to absolute indexing.
|
320 |
+
x = F.pad(x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]]))
|
321 |
+
|
322 |
+
# Concat extra elements so to add up to shape (len+1, 2*len-1).
|
323 |
+
x_flat = x.view([batch, heads, length * 2 * length])
|
324 |
+
x_flat = F.pad(
|
325 |
+
x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [0, length - 1]])
|
326 |
+
)
|
327 |
+
|
328 |
+
# Reshape and slice out the padded elements.
|
329 |
+
x_final = x_flat.view([batch, heads, length + 1, 2 * length - 1])[
|
330 |
+
:, :, :length, length - 1 :
|
331 |
+
]
|
332 |
+
return x_final
|
333 |
+
|
334 |
+
def _absolute_position_to_relative_position(self, x):
|
335 |
+
"""
|
336 |
+
x: [b, h, l, l]
|
337 |
+
ret: [b, h, l, 2*l-1]
|
338 |
+
"""
|
339 |
+
batch, heads, length, _ = x.size()
|
340 |
+
# padd along column
|
341 |
+
x = F.pad(
|
342 |
+
x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length - 1]])
|
343 |
+
)
|
344 |
+
x_flat = x.view([batch, heads, length**2 + length * (length - 1)])
|
345 |
+
# add 0's in the beginning that will skew the elements after reshape
|
346 |
+
x_flat = F.pad(x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [length, 0]]))
|
347 |
+
x_final = x_flat.view([batch, heads, length, 2 * length])[:, :, :, 1:]
|
348 |
+
return x_final
|
349 |
+
|
350 |
+
def _attention_bias_proximal(self, length):
|
351 |
+
"""Bias for self-attention to encourage attention to close positions.
|
352 |
+
Args:
|
353 |
+
length: an integer scalar.
|
354 |
+
Returns:
|
355 |
+
a Tensor with shape [1, 1, length, length]
|
356 |
+
"""
|
357 |
+
r = torch.arange(length, dtype=torch.float32)
|
358 |
+
diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1)
|
359 |
+
return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
|
360 |
+
|
361 |
+
|
362 |
+
class FFN(nn.Module):
|
363 |
+
def __init__(
|
364 |
+
self,
|
365 |
+
in_channels,
|
366 |
+
out_channels,
|
367 |
+
filter_channels,
|
368 |
+
kernel_size,
|
369 |
+
p_dropout=0.0,
|
370 |
+
activation=None,
|
371 |
+
causal=False,
|
372 |
+
):
|
373 |
+
super().__init__()
|
374 |
+
self.in_channels = in_channels
|
375 |
+
self.out_channels = out_channels
|
376 |
+
self.filter_channels = filter_channels
|
377 |
+
self.kernel_size = kernel_size
|
378 |
+
self.p_dropout = p_dropout
|
379 |
+
self.activation = activation
|
380 |
+
self.causal = causal
|
381 |
+
|
382 |
+
if causal:
|
383 |
+
self.padding = self._causal_padding
|
384 |
+
else:
|
385 |
+
self.padding = self._same_padding
|
386 |
+
|
387 |
+
self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size)
|
388 |
+
self.conv_2 = nn.Conv1d(filter_channels, out_channels, kernel_size)
|
389 |
+
self.drop = nn.Dropout(p_dropout)
|
390 |
+
|
391 |
+
def forward(self, x, x_mask):
|
392 |
+
x = self.conv_1(self.padding(x * x_mask))
|
393 |
+
if self.activation == "gelu":
|
394 |
+
x = x * torch.sigmoid(1.702 * x)
|
395 |
+
else:
|
396 |
+
x = torch.relu(x)
|
397 |
+
x = self.drop(x)
|
398 |
+
x = self.conv_2(self.padding(x * x_mask))
|
399 |
+
return x * x_mask
|
400 |
+
|
401 |
+
def _causal_padding(self, x):
|
402 |
+
if self.kernel_size == 1:
|
403 |
+
return x
|
404 |
+
pad_l = self.kernel_size - 1
|
405 |
+
pad_r = 0
|
406 |
+
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
407 |
+
x = F.pad(x, commons.convert_pad_shape(padding))
|
408 |
+
return x
|
409 |
+
|
410 |
+
def _same_padding(self, x):
|
411 |
+
if self.kernel_size == 1:
|
412 |
+
return x
|
413 |
+
pad_l = (self.kernel_size - 1) // 2
|
414 |
+
pad_r = self.kernel_size // 2
|
415 |
+
padding = [[0, 0], [0, 0], [pad_l, pad_r]]
|
416 |
+
x = F.pad(x, commons.convert_pad_shape(padding))
|
417 |
+
return x
|
infer_pack/commons.py
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from torch import nn
|
5 |
+
from torch.nn import functional as F
|
6 |
+
|
7 |
+
|
8 |
+
def init_weights(m, mean=0.0, std=0.01):
|
9 |
+
classname = m.__class__.__name__
|
10 |
+
if classname.find("Conv") != -1:
|
11 |
+
m.weight.data.normal_(mean, std)
|
12 |
+
|
13 |
+
|
14 |
+
def get_padding(kernel_size, dilation=1):
|
15 |
+
return int((kernel_size * dilation - dilation) / 2)
|
16 |
+
|
17 |
+
|
18 |
+
def convert_pad_shape(pad_shape):
|
19 |
+
l = pad_shape[::-1]
|
20 |
+
pad_shape = [item for sublist in l for item in sublist]
|
21 |
+
return pad_shape
|
22 |
+
|
23 |
+
|
24 |
+
def kl_divergence(m_p, logs_p, m_q, logs_q):
|
25 |
+
"""KL(P||Q)"""
|
26 |
+
kl = (logs_q - logs_p) - 0.5
|
27 |
+
kl += (
|
28 |
+
0.5 * (torch.exp(2.0 * logs_p) + ((m_p - m_q) ** 2)) * torch.exp(-2.0 * logs_q)
|
29 |
+
)
|
30 |
+
return kl
|
31 |
+
|
32 |
+
|
33 |
+
def rand_gumbel(shape):
|
34 |
+
"""Sample from the Gumbel distribution, protect from overflows."""
|
35 |
+
uniform_samples = torch.rand(shape) * 0.99998 + 0.00001
|
36 |
+
return -torch.log(-torch.log(uniform_samples))
|
37 |
+
|
38 |
+
|
39 |
+
def rand_gumbel_like(x):
|
40 |
+
g = rand_gumbel(x.size()).to(dtype=x.dtype, device=x.device)
|
41 |
+
return g
|
42 |
+
|
43 |
+
|
44 |
+
def slice_segments(x, ids_str, segment_size=4):
|
45 |
+
ret = torch.zeros_like(x[:, :, :segment_size])
|
46 |
+
for i in range(x.size(0)):
|
47 |
+
idx_str = ids_str[i]
|
48 |
+
idx_end = idx_str + segment_size
|
49 |
+
ret[i] = x[i, :, idx_str:idx_end]
|
50 |
+
return ret
|
51 |
+
|
52 |
+
|
53 |
+
def slice_segments2(x, ids_str, segment_size=4):
|
54 |
+
ret = torch.zeros_like(x[:, :segment_size])
|
55 |
+
for i in range(x.size(0)):
|
56 |
+
idx_str = ids_str[i]
|
57 |
+
idx_end = idx_str + segment_size
|
58 |
+
ret[i] = x[i, idx_str:idx_end]
|
59 |
+
return ret
|
60 |
+
|
61 |
+
|
62 |
+
def rand_slice_segments(x, x_lengths=None, segment_size=4):
|
63 |
+
b, d, t = x.size()
|
64 |
+
if x_lengths is None:
|
65 |
+
x_lengths = t
|
66 |
+
ids_str_max = x_lengths - segment_size + 1
|
67 |
+
ids_str = (torch.rand([b]).to(device=x.device) * ids_str_max).to(dtype=torch.long)
|
68 |
+
ret = slice_segments(x, ids_str, segment_size)
|
69 |
+
return ret, ids_str
|
70 |
+
|
71 |
+
|
72 |
+
def get_timing_signal_1d(length, channels, min_timescale=1.0, max_timescale=1.0e4):
|
73 |
+
position = torch.arange(length, dtype=torch.float)
|
74 |
+
num_timescales = channels // 2
|
75 |
+
log_timescale_increment = math.log(float(max_timescale) / float(min_timescale)) / (
|
76 |
+
num_timescales - 1
|
77 |
+
)
|
78 |
+
inv_timescales = min_timescale * torch.exp(
|
79 |
+
torch.arange(num_timescales, dtype=torch.float) * -log_timescale_increment
|
80 |
+
)
|
81 |
+
scaled_time = position.unsqueeze(0) * inv_timescales.unsqueeze(1)
|
82 |
+
signal = torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], 0)
|
83 |
+
signal = F.pad(signal, [0, 0, 0, channels % 2])
|
84 |
+
signal = signal.view(1, channels, length)
|
85 |
+
return signal
|
86 |
+
|
87 |
+
|
88 |
+
def add_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4):
|
89 |
+
b, channels, length = x.size()
|
90 |
+
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
91 |
+
return x + signal.to(dtype=x.dtype, device=x.device)
|
92 |
+
|
93 |
+
|
94 |
+
def cat_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4, axis=1):
|
95 |
+
b, channels, length = x.size()
|
96 |
+
signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
|
97 |
+
return torch.cat([x, signal.to(dtype=x.dtype, device=x.device)], axis)
|
98 |
+
|
99 |
+
|
100 |
+
def subsequent_mask(length):
|
101 |
+
mask = torch.tril(torch.ones(length, length)).unsqueeze(0).unsqueeze(0)
|
102 |
+
return mask
|
103 |
+
|
104 |
+
|
105 |
+
@torch.jit.script
|
106 |
+
def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
|
107 |
+
n_channels_int = n_channels[0]
|
108 |
+
in_act = input_a + input_b
|
109 |
+
t_act = torch.tanh(in_act[:, :n_channels_int, :])
|
110 |
+
s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
|
111 |
+
acts = t_act * s_act
|
112 |
+
return acts
|
113 |
+
|
114 |
+
|
115 |
+
def convert_pad_shape(pad_shape):
|
116 |
+
l = pad_shape[::-1]
|
117 |
+
pad_shape = [item for sublist in l for item in sublist]
|
118 |
+
return pad_shape
|
119 |
+
|
120 |
+
|
121 |
+
def shift_1d(x):
|
122 |
+
x = F.pad(x, convert_pad_shape([[0, 0], [0, 0], [1, 0]]))[:, :, :-1]
|
123 |
+
return x
|
124 |
+
|
125 |
+
|
126 |
+
def sequence_mask(length, max_length=None):
|
127 |
+
if max_length is None:
|
128 |
+
max_length = length.max()
|
129 |
+
x = torch.arange(max_length, dtype=length.dtype, device=length.device)
|
130 |
+
return x.unsqueeze(0) < length.unsqueeze(1)
|
131 |
+
|
132 |
+
|
133 |
+
def generate_path(duration, mask):
|
134 |
+
"""
|
135 |
+
duration: [b, 1, t_x]
|
136 |
+
mask: [b, 1, t_y, t_x]
|
137 |
+
"""
|
138 |
+
device = duration.device
|
139 |
+
|
140 |
+
b, _, t_y, t_x = mask.shape
|
141 |
+
cum_duration = torch.cumsum(duration, -1)
|
142 |
+
|
143 |
+
cum_duration_flat = cum_duration.view(b * t_x)
|
144 |
+
path = sequence_mask(cum_duration_flat, t_y).to(mask.dtype)
|
145 |
+
path = path.view(b, t_x, t_y)
|
146 |
+
path = path - F.pad(path, convert_pad_shape([[0, 0], [1, 0], [0, 0]]))[:, :-1]
|
147 |
+
path = path.unsqueeze(1).transpose(2, 3) * mask
|
148 |
+
return path
|
149 |
+
|
150 |
+
|
151 |
+
def clip_grad_value_(parameters, clip_value, norm_type=2):
|
152 |
+
if isinstance(parameters, torch.Tensor):
|
153 |
+
parameters = [parameters]
|
154 |
+
parameters = list(filter(lambda p: p.grad is not None, parameters))
|
155 |
+
norm_type = float(norm_type)
|
156 |
+
if clip_value is not None:
|
157 |
+
clip_value = float(clip_value)
|
158 |
+
|
159 |
+
total_norm = 0
|
160 |
+
for p in parameters:
|
161 |
+
param_norm = p.grad.data.norm(norm_type)
|
162 |
+
total_norm += param_norm.item() ** norm_type
|
163 |
+
if clip_value is not None:
|
164 |
+
p.grad.data.clamp_(min=-clip_value, max=clip_value)
|
165 |
+
total_norm = total_norm ** (1.0 / norm_type)
|
166 |
+
return total_norm
|
infer_pack/models.py
ADDED
@@ -0,0 +1,982 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math, pdb, os
|
2 |
+
from time import time as ttime
|
3 |
+
import torch
|
4 |
+
from torch import nn
|
5 |
+
from torch.nn import functional as F
|
6 |
+
from infer_pack import modules
|
7 |
+
from infer_pack import attentions
|
8 |
+
from infer_pack import commons
|
9 |
+
from infer_pack.commons import init_weights, get_padding
|
10 |
+
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
11 |
+
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
12 |
+
from infer_pack.commons import init_weights
|
13 |
+
import numpy as np
|
14 |
+
from infer_pack import commons
|
15 |
+
|
16 |
+
|
17 |
+
class TextEncoder256(nn.Module):
|
18 |
+
def __init__(
|
19 |
+
self,
|
20 |
+
out_channels,
|
21 |
+
hidden_channels,
|
22 |
+
filter_channels,
|
23 |
+
n_heads,
|
24 |
+
n_layers,
|
25 |
+
kernel_size,
|
26 |
+
p_dropout,
|
27 |
+
f0=True,
|
28 |
+
):
|
29 |
+
super().__init__()
|
30 |
+
self.out_channels = out_channels
|
31 |
+
self.hidden_channels = hidden_channels
|
32 |
+
self.filter_channels = filter_channels
|
33 |
+
self.n_heads = n_heads
|
34 |
+
self.n_layers = n_layers
|
35 |
+
self.kernel_size = kernel_size
|
36 |
+
self.p_dropout = p_dropout
|
37 |
+
self.emb_phone = nn.Linear(256, hidden_channels)
|
38 |
+
self.lrelu = nn.LeakyReLU(0.1, inplace=True)
|
39 |
+
if f0 == True:
|
40 |
+
self.emb_pitch = nn.Embedding(256, hidden_channels) # pitch 256
|
41 |
+
self.encoder = attentions.Encoder(
|
42 |
+
hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout
|
43 |
+
)
|
44 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
45 |
+
|
46 |
+
def forward(self, phone, pitch, lengths):
|
47 |
+
if pitch == None:
|
48 |
+
x = self.emb_phone(phone)
|
49 |
+
else:
|
50 |
+
x = self.emb_phone(phone) + self.emb_pitch(pitch)
|
51 |
+
x = x * math.sqrt(self.hidden_channels) # [b, t, h]
|
52 |
+
x = self.lrelu(x)
|
53 |
+
x = torch.transpose(x, 1, -1) # [b, h, t]
|
54 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(lengths, x.size(2)), 1).to(
|
55 |
+
x.dtype
|
56 |
+
)
|
57 |
+
x = self.encoder(x * x_mask, x_mask)
|
58 |
+
stats = self.proj(x) * x_mask
|
59 |
+
|
60 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
61 |
+
return m, logs, x_mask
|
62 |
+
|
63 |
+
|
64 |
+
class TextEncoder256Sim(nn.Module):
|
65 |
+
def __init__(
|
66 |
+
self,
|
67 |
+
out_channels,
|
68 |
+
hidden_channels,
|
69 |
+
filter_channels,
|
70 |
+
n_heads,
|
71 |
+
n_layers,
|
72 |
+
kernel_size,
|
73 |
+
p_dropout,
|
74 |
+
f0=True,
|
75 |
+
):
|
76 |
+
super().__init__()
|
77 |
+
self.out_channels = out_channels
|
78 |
+
self.hidden_channels = hidden_channels
|
79 |
+
self.filter_channels = filter_channels
|
80 |
+
self.n_heads = n_heads
|
81 |
+
self.n_layers = n_layers
|
82 |
+
self.kernel_size = kernel_size
|
83 |
+
self.p_dropout = p_dropout
|
84 |
+
self.emb_phone = nn.Linear(256, hidden_channels)
|
85 |
+
self.lrelu = nn.LeakyReLU(0.1, inplace=True)
|
86 |
+
if f0 == True:
|
87 |
+
self.emb_pitch = nn.Embedding(256, hidden_channels) # pitch 256
|
88 |
+
self.encoder = attentions.Encoder(
|
89 |
+
hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout
|
90 |
+
)
|
91 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
92 |
+
|
93 |
+
def forward(self, phone, pitch, lengths):
|
94 |
+
if pitch == None:
|
95 |
+
x = self.emb_phone(phone)
|
96 |
+
else:
|
97 |
+
x = self.emb_phone(phone) + self.emb_pitch(pitch)
|
98 |
+
x = x * math.sqrt(self.hidden_channels) # [b, t, h]
|
99 |
+
x = self.lrelu(x)
|
100 |
+
x = torch.transpose(x, 1, -1) # [b, h, t]
|
101 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(lengths, x.size(2)), 1).to(
|
102 |
+
x.dtype
|
103 |
+
)
|
104 |
+
x = self.encoder(x * x_mask, x_mask)
|
105 |
+
x = self.proj(x) * x_mask
|
106 |
+
return x, x_mask
|
107 |
+
|
108 |
+
|
109 |
+
class ResidualCouplingBlock(nn.Module):
|
110 |
+
def __init__(
|
111 |
+
self,
|
112 |
+
channels,
|
113 |
+
hidden_channels,
|
114 |
+
kernel_size,
|
115 |
+
dilation_rate,
|
116 |
+
n_layers,
|
117 |
+
n_flows=4,
|
118 |
+
gin_channels=0,
|
119 |
+
):
|
120 |
+
super().__init__()
|
121 |
+
self.channels = channels
|
122 |
+
self.hidden_channels = hidden_channels
|
123 |
+
self.kernel_size = kernel_size
|
124 |
+
self.dilation_rate = dilation_rate
|
125 |
+
self.n_layers = n_layers
|
126 |
+
self.n_flows = n_flows
|
127 |
+
self.gin_channels = gin_channels
|
128 |
+
|
129 |
+
self.flows = nn.ModuleList()
|
130 |
+
for i in range(n_flows):
|
131 |
+
self.flows.append(
|
132 |
+
modules.ResidualCouplingLayer(
|
133 |
+
channels,
|
134 |
+
hidden_channels,
|
135 |
+
kernel_size,
|
136 |
+
dilation_rate,
|
137 |
+
n_layers,
|
138 |
+
gin_channels=gin_channels,
|
139 |
+
mean_only=True,
|
140 |
+
)
|
141 |
+
)
|
142 |
+
self.flows.append(modules.Flip())
|
143 |
+
|
144 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
145 |
+
if not reverse:
|
146 |
+
for flow in self.flows:
|
147 |
+
x, _ = flow(x, x_mask, g=g, reverse=reverse)
|
148 |
+
else:
|
149 |
+
for flow in reversed(self.flows):
|
150 |
+
x = flow(x, x_mask, g=g, reverse=reverse)
|
151 |
+
return x
|
152 |
+
|
153 |
+
def remove_weight_norm(self):
|
154 |
+
for i in range(self.n_flows):
|
155 |
+
self.flows[i * 2].remove_weight_norm()
|
156 |
+
|
157 |
+
|
158 |
+
class PosteriorEncoder(nn.Module):
|
159 |
+
def __init__(
|
160 |
+
self,
|
161 |
+
in_channels,
|
162 |
+
out_channels,
|
163 |
+
hidden_channels,
|
164 |
+
kernel_size,
|
165 |
+
dilation_rate,
|
166 |
+
n_layers,
|
167 |
+
gin_channels=0,
|
168 |
+
):
|
169 |
+
super().__init__()
|
170 |
+
self.in_channels = in_channels
|
171 |
+
self.out_channels = out_channels
|
172 |
+
self.hidden_channels = hidden_channels
|
173 |
+
self.kernel_size = kernel_size
|
174 |
+
self.dilation_rate = dilation_rate
|
175 |
+
self.n_layers = n_layers
|
176 |
+
self.gin_channels = gin_channels
|
177 |
+
|
178 |
+
self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
179 |
+
self.enc = modules.WN(
|
180 |
+
hidden_channels,
|
181 |
+
kernel_size,
|
182 |
+
dilation_rate,
|
183 |
+
n_layers,
|
184 |
+
gin_channels=gin_channels,
|
185 |
+
)
|
186 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
187 |
+
|
188 |
+
def forward(self, x, x_lengths, g=None):
|
189 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(
|
190 |
+
x.dtype
|
191 |
+
)
|
192 |
+
x = self.pre(x) * x_mask
|
193 |
+
x = self.enc(x, x_mask, g=g)
|
194 |
+
stats = self.proj(x) * x_mask
|
195 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
196 |
+
z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
|
197 |
+
return z, m, logs, x_mask
|
198 |
+
|
199 |
+
def remove_weight_norm(self):
|
200 |
+
self.enc.remove_weight_norm()
|
201 |
+
|
202 |
+
|
203 |
+
class Generator(torch.nn.Module):
|
204 |
+
def __init__(
|
205 |
+
self,
|
206 |
+
initial_channel,
|
207 |
+
resblock,
|
208 |
+
resblock_kernel_sizes,
|
209 |
+
resblock_dilation_sizes,
|
210 |
+
upsample_rates,
|
211 |
+
upsample_initial_channel,
|
212 |
+
upsample_kernel_sizes,
|
213 |
+
gin_channels=0,
|
214 |
+
):
|
215 |
+
super(Generator, self).__init__()
|
216 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
217 |
+
self.num_upsamples = len(upsample_rates)
|
218 |
+
self.conv_pre = Conv1d(
|
219 |
+
initial_channel, upsample_initial_channel, 7, 1, padding=3
|
220 |
+
)
|
221 |
+
resblock = modules.ResBlock1 if resblock == "1" else modules.ResBlock2
|
222 |
+
|
223 |
+
self.ups = nn.ModuleList()
|
224 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
225 |
+
self.ups.append(
|
226 |
+
weight_norm(
|
227 |
+
ConvTranspose1d(
|
228 |
+
upsample_initial_channel // (2**i),
|
229 |
+
upsample_initial_channel // (2 ** (i + 1)),
|
230 |
+
k,
|
231 |
+
u,
|
232 |
+
padding=(k - u) // 2,
|
233 |
+
)
|
234 |
+
)
|
235 |
+
)
|
236 |
+
|
237 |
+
self.resblocks = nn.ModuleList()
|
238 |
+
for i in range(len(self.ups)):
|
239 |
+
ch = upsample_initial_channel // (2 ** (i + 1))
|
240 |
+
for j, (k, d) in enumerate(
|
241 |
+
zip(resblock_kernel_sizes, resblock_dilation_sizes)
|
242 |
+
):
|
243 |
+
self.resblocks.append(resblock(ch, k, d))
|
244 |
+
|
245 |
+
self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
|
246 |
+
self.ups.apply(init_weights)
|
247 |
+
|
248 |
+
if gin_channels != 0:
|
249 |
+
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
250 |
+
|
251 |
+
def forward(self, x, g=None):
|
252 |
+
x = self.conv_pre(x)
|
253 |
+
if g is not None:
|
254 |
+
x = x + self.cond(g)
|
255 |
+
|
256 |
+
for i in range(self.num_upsamples):
|
257 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
258 |
+
x = self.ups[i](x)
|
259 |
+
xs = None
|
260 |
+
for j in range(self.num_kernels):
|
261 |
+
if xs is None:
|
262 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
263 |
+
else:
|
264 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
265 |
+
x = xs / self.num_kernels
|
266 |
+
x = F.leaky_relu(x)
|
267 |
+
x = self.conv_post(x)
|
268 |
+
x = torch.tanh(x)
|
269 |
+
|
270 |
+
return x
|
271 |
+
|
272 |
+
def remove_weight_norm(self):
|
273 |
+
for l in self.ups:
|
274 |
+
remove_weight_norm(l)
|
275 |
+
for l in self.resblocks:
|
276 |
+
l.remove_weight_norm()
|
277 |
+
|
278 |
+
|
279 |
+
class SineGen(torch.nn.Module):
|
280 |
+
"""Definition of sine generator
|
281 |
+
SineGen(samp_rate, harmonic_num = 0,
|
282 |
+
sine_amp = 0.1, noise_std = 0.003,
|
283 |
+
voiced_threshold = 0,
|
284 |
+
flag_for_pulse=False)
|
285 |
+
samp_rate: sampling rate in Hz
|
286 |
+
harmonic_num: number of harmonic overtones (default 0)
|
287 |
+
sine_amp: amplitude of sine-wavefrom (default 0.1)
|
288 |
+
noise_std: std of Gaussian noise (default 0.003)
|
289 |
+
voiced_thoreshold: F0 threshold for U/V classification (default 0)
|
290 |
+
flag_for_pulse: this SinGen is used inside PulseGen (default False)
|
291 |
+
Note: when flag_for_pulse is True, the first time step of a voiced
|
292 |
+
segment is always sin(np.pi) or cos(0)
|
293 |
+
"""
|
294 |
+
|
295 |
+
def __init__(
|
296 |
+
self,
|
297 |
+
samp_rate,
|
298 |
+
harmonic_num=0,
|
299 |
+
sine_amp=0.1,
|
300 |
+
noise_std=0.003,
|
301 |
+
voiced_threshold=0,
|
302 |
+
flag_for_pulse=False,
|
303 |
+
):
|
304 |
+
super(SineGen, self).__init__()
|
305 |
+
self.sine_amp = sine_amp
|
306 |
+
self.noise_std = noise_std
|
307 |
+
self.harmonic_num = harmonic_num
|
308 |
+
self.dim = self.harmonic_num + 1
|
309 |
+
self.sampling_rate = samp_rate
|
310 |
+
self.voiced_threshold = voiced_threshold
|
311 |
+
|
312 |
+
def _f02uv(self, f0):
|
313 |
+
# generate uv signal
|
314 |
+
uv = torch.ones_like(f0)
|
315 |
+
uv = uv * (f0 > self.voiced_threshold)
|
316 |
+
return uv
|
317 |
+
|
318 |
+
def forward(self, f0, upp):
|
319 |
+
"""sine_tensor, uv = forward(f0)
|
320 |
+
input F0: tensor(batchsize=1, length, dim=1)
|
321 |
+
f0 for unvoiced steps should be 0
|
322 |
+
output sine_tensor: tensor(batchsize=1, length, dim)
|
323 |
+
output uv: tensor(batchsize=1, length, 1)
|
324 |
+
"""
|
325 |
+
with torch.no_grad():
|
326 |
+
f0 = f0[:, None].transpose(1, 2)
|
327 |
+
f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim, device=f0.device)
|
328 |
+
# fundamental component
|
329 |
+
f0_buf[:, :, 0] = f0[:, :, 0]
|
330 |
+
for idx in np.arange(self.harmonic_num):
|
331 |
+
f0_buf[:, :, idx + 1] = f0_buf[:, :, 0] * (
|
332 |
+
idx + 2
|
333 |
+
) # idx + 2: the (idx+1)-th overtone, (idx+2)-th harmonic
|
334 |
+
rad_values = (f0_buf / self.sampling_rate) % 1 ###%1意味着n_har的乘积无法后处理优化
|
335 |
+
rand_ini = torch.rand(
|
336 |
+
f0_buf.shape[0], f0_buf.shape[2], device=f0_buf.device
|
337 |
+
)
|
338 |
+
rand_ini[:, 0] = 0
|
339 |
+
rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
|
340 |
+
tmp_over_one = torch.cumsum(rad_values, 1) # % 1 #####%1意味着后面的cumsum无法再优化
|
341 |
+
tmp_over_one *= upp
|
342 |
+
tmp_over_one = F.interpolate(
|
343 |
+
tmp_over_one.transpose(2, 1),
|
344 |
+
scale_factor=upp,
|
345 |
+
mode="linear",
|
346 |
+
align_corners=True,
|
347 |
+
).transpose(2, 1)
|
348 |
+
rad_values = F.interpolate(
|
349 |
+
rad_values.transpose(2, 1), scale_factor=upp, mode="nearest"
|
350 |
+
).transpose(
|
351 |
+
2, 1
|
352 |
+
) #######
|
353 |
+
tmp_over_one %= 1
|
354 |
+
tmp_over_one_idx = (tmp_over_one[:, 1:, :] - tmp_over_one[:, :-1, :]) < 0
|
355 |
+
cumsum_shift = torch.zeros_like(rad_values)
|
356 |
+
cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
|
357 |
+
sine_waves = torch.sin(
|
358 |
+
torch.cumsum(rad_values + cumsum_shift, dim=1) * 2 * np.pi
|
359 |
+
)
|
360 |
+
sine_waves = sine_waves * self.sine_amp
|
361 |
+
uv = self._f02uv(f0)
|
362 |
+
uv = F.interpolate(
|
363 |
+
uv.transpose(2, 1), scale_factor=upp, mode="nearest"
|
364 |
+
).transpose(2, 1)
|
365 |
+
noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
|
366 |
+
noise = noise_amp * torch.randn_like(sine_waves)
|
367 |
+
sine_waves = sine_waves * uv + noise
|
368 |
+
return sine_waves, uv, noise
|
369 |
+
|
370 |
+
|
371 |
+
class SourceModuleHnNSF(torch.nn.Module):
|
372 |
+
"""SourceModule for hn-nsf
|
373 |
+
SourceModule(sampling_rate, harmonic_num=0, sine_amp=0.1,
|
374 |
+
add_noise_std=0.003, voiced_threshod=0)
|
375 |
+
sampling_rate: sampling_rate in Hz
|
376 |
+
harmonic_num: number of harmonic above F0 (default: 0)
|
377 |
+
sine_amp: amplitude of sine source signal (default: 0.1)
|
378 |
+
add_noise_std: std of additive Gaussian noise (default: 0.003)
|
379 |
+
note that amplitude of noise in unvoiced is decided
|
380 |
+
by sine_amp
|
381 |
+
voiced_threshold: threhold to set U/V given F0 (default: 0)
|
382 |
+
Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
|
383 |
+
F0_sampled (batchsize, length, 1)
|
384 |
+
Sine_source (batchsize, length, 1)
|
385 |
+
noise_source (batchsize, length 1)
|
386 |
+
uv (batchsize, length, 1)
|
387 |
+
"""
|
388 |
+
|
389 |
+
def __init__(
|
390 |
+
self,
|
391 |
+
sampling_rate,
|
392 |
+
harmonic_num=0,
|
393 |
+
sine_amp=0.1,
|
394 |
+
add_noise_std=0.003,
|
395 |
+
voiced_threshod=0,
|
396 |
+
is_half=True,
|
397 |
+
):
|
398 |
+
super(SourceModuleHnNSF, self).__init__()
|
399 |
+
|
400 |
+
self.sine_amp = sine_amp
|
401 |
+
self.noise_std = add_noise_std
|
402 |
+
self.is_half = is_half
|
403 |
+
# to produce sine waveforms
|
404 |
+
self.l_sin_gen = SineGen(
|
405 |
+
sampling_rate, harmonic_num, sine_amp, add_noise_std, voiced_threshod
|
406 |
+
)
|
407 |
+
|
408 |
+
# to merge source harmonics into a single excitation
|
409 |
+
self.l_linear = torch.nn.Linear(harmonic_num + 1, 1)
|
410 |
+
self.l_tanh = torch.nn.Tanh()
|
411 |
+
|
412 |
+
def forward(self, x, upp=None):
|
413 |
+
sine_wavs, uv, _ = self.l_sin_gen(x, upp)
|
414 |
+
if self.is_half:
|
415 |
+
sine_wavs = sine_wavs.half()
|
416 |
+
sine_merge = self.l_tanh(self.l_linear(sine_wavs))
|
417 |
+
return sine_merge, None, None # noise, uv
|
418 |
+
|
419 |
+
|
420 |
+
class GeneratorNSF(torch.nn.Module):
|
421 |
+
def __init__(
|
422 |
+
self,
|
423 |
+
initial_channel,
|
424 |
+
resblock,
|
425 |
+
resblock_kernel_sizes,
|
426 |
+
resblock_dilation_sizes,
|
427 |
+
upsample_rates,
|
428 |
+
upsample_initial_channel,
|
429 |
+
upsample_kernel_sizes,
|
430 |
+
gin_channels,
|
431 |
+
sr,
|
432 |
+
is_half=False,
|
433 |
+
):
|
434 |
+
super(GeneratorNSF, self).__init__()
|
435 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
436 |
+
self.num_upsamples = len(upsample_rates)
|
437 |
+
|
438 |
+
self.f0_upsamp = torch.nn.Upsample(scale_factor=np.prod(upsample_rates))
|
439 |
+
self.m_source = SourceModuleHnNSF(
|
440 |
+
sampling_rate=sr, harmonic_num=0, is_half=is_half
|
441 |
+
)
|
442 |
+
self.noise_convs = nn.ModuleList()
|
443 |
+
self.conv_pre = Conv1d(
|
444 |
+
initial_channel, upsample_initial_channel, 7, 1, padding=3
|
445 |
+
)
|
446 |
+
resblock = modules.ResBlock1 if resblock == "1" else modules.ResBlock2
|
447 |
+
|
448 |
+
self.ups = nn.ModuleList()
|
449 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
450 |
+
c_cur = upsample_initial_channel // (2 ** (i + 1))
|
451 |
+
self.ups.append(
|
452 |
+
weight_norm(
|
453 |
+
ConvTranspose1d(
|
454 |
+
upsample_initial_channel // (2**i),
|
455 |
+
upsample_initial_channel // (2 ** (i + 1)),
|
456 |
+
k,
|
457 |
+
u,
|
458 |
+
padding=(k - u) // 2,
|
459 |
+
)
|
460 |
+
)
|
461 |
+
)
|
462 |
+
if i + 1 < len(upsample_rates):
|
463 |
+
stride_f0 = np.prod(upsample_rates[i + 1 :])
|
464 |
+
self.noise_convs.append(
|
465 |
+
Conv1d(
|
466 |
+
1,
|
467 |
+
c_cur,
|
468 |
+
kernel_size=stride_f0 * 2,
|
469 |
+
stride=stride_f0,
|
470 |
+
padding=stride_f0 // 2,
|
471 |
+
)
|
472 |
+
)
|
473 |
+
else:
|
474 |
+
self.noise_convs.append(Conv1d(1, c_cur, kernel_size=1))
|
475 |
+
|
476 |
+
self.resblocks = nn.ModuleList()
|
477 |
+
for i in range(len(self.ups)):
|
478 |
+
ch = upsample_initial_channel // (2 ** (i + 1))
|
479 |
+
for j, (k, d) in enumerate(
|
480 |
+
zip(resblock_kernel_sizes, resblock_dilation_sizes)
|
481 |
+
):
|
482 |
+
self.resblocks.append(resblock(ch, k, d))
|
483 |
+
|
484 |
+
self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
|
485 |
+
self.ups.apply(init_weights)
|
486 |
+
|
487 |
+
if gin_channels != 0:
|
488 |
+
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
489 |
+
|
490 |
+
self.upp = np.prod(upsample_rates)
|
491 |
+
|
492 |
+
def forward(self, x, f0, g=None):
|
493 |
+
har_source, noi_source, uv = self.m_source(f0, self.upp)
|
494 |
+
har_source = har_source.transpose(1, 2)
|
495 |
+
x = self.conv_pre(x)
|
496 |
+
if g is not None:
|
497 |
+
x = x + self.cond(g)
|
498 |
+
|
499 |
+
for i in range(self.num_upsamples):
|
500 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
501 |
+
x = self.ups[i](x)
|
502 |
+
x_source = self.noise_convs[i](har_source)
|
503 |
+
x = x + x_source
|
504 |
+
xs = None
|
505 |
+
for j in range(self.num_kernels):
|
506 |
+
if xs is None:
|
507 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
508 |
+
else:
|
509 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
510 |
+
x = xs / self.num_kernels
|
511 |
+
x = F.leaky_relu(x)
|
512 |
+
x = self.conv_post(x)
|
513 |
+
x = torch.tanh(x)
|
514 |
+
return x
|
515 |
+
|
516 |
+
def remove_weight_norm(self):
|
517 |
+
for l in self.ups:
|
518 |
+
remove_weight_norm(l)
|
519 |
+
for l in self.resblocks:
|
520 |
+
l.remove_weight_norm()
|
521 |
+
|
522 |
+
|
523 |
+
sr2sr = {
|
524 |
+
"32k": 32000,
|
525 |
+
"40k": 40000,
|
526 |
+
"48k": 48000,
|
527 |
+
}
|
528 |
+
|
529 |
+
|
530 |
+
class SynthesizerTrnMs256NSFsid(nn.Module):
|
531 |
+
def __init__(
|
532 |
+
self,
|
533 |
+
spec_channels,
|
534 |
+
segment_size,
|
535 |
+
inter_channels,
|
536 |
+
hidden_channels,
|
537 |
+
filter_channels,
|
538 |
+
n_heads,
|
539 |
+
n_layers,
|
540 |
+
kernel_size,
|
541 |
+
p_dropout,
|
542 |
+
resblock,
|
543 |
+
resblock_kernel_sizes,
|
544 |
+
resblock_dilation_sizes,
|
545 |
+
upsample_rates,
|
546 |
+
upsample_initial_channel,
|
547 |
+
upsample_kernel_sizes,
|
548 |
+
spk_embed_dim,
|
549 |
+
gin_channels,
|
550 |
+
sr,
|
551 |
+
**kwargs
|
552 |
+
):
|
553 |
+
super().__init__()
|
554 |
+
if type(sr) == type("strr"):
|
555 |
+
sr = sr2sr[sr]
|
556 |
+
self.spec_channels = spec_channels
|
557 |
+
self.inter_channels = inter_channels
|
558 |
+
self.hidden_channels = hidden_channels
|
559 |
+
self.filter_channels = filter_channels
|
560 |
+
self.n_heads = n_heads
|
561 |
+
self.n_layers = n_layers
|
562 |
+
self.kernel_size = kernel_size
|
563 |
+
self.p_dropout = p_dropout
|
564 |
+
self.resblock = resblock
|
565 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
566 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
567 |
+
self.upsample_rates = upsample_rates
|
568 |
+
self.upsample_initial_channel = upsample_initial_channel
|
569 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
570 |
+
self.segment_size = segment_size
|
571 |
+
self.gin_channels = gin_channels
|
572 |
+
# self.hop_length = hop_length#
|
573 |
+
self.spk_embed_dim = spk_embed_dim
|
574 |
+
self.enc_p = TextEncoder256(
|
575 |
+
inter_channels,
|
576 |
+
hidden_channels,
|
577 |
+
filter_channels,
|
578 |
+
n_heads,
|
579 |
+
n_layers,
|
580 |
+
kernel_size,
|
581 |
+
p_dropout,
|
582 |
+
)
|
583 |
+
self.dec = GeneratorNSF(
|
584 |
+
inter_channels,
|
585 |
+
resblock,
|
586 |
+
resblock_kernel_sizes,
|
587 |
+
resblock_dilation_sizes,
|
588 |
+
upsample_rates,
|
589 |
+
upsample_initial_channel,
|
590 |
+
upsample_kernel_sizes,
|
591 |
+
gin_channels=gin_channels,
|
592 |
+
sr=sr,
|
593 |
+
is_half=kwargs["is_half"],
|
594 |
+
)
|
595 |
+
self.enc_q = PosteriorEncoder(
|
596 |
+
spec_channels,
|
597 |
+
inter_channels,
|
598 |
+
hidden_channels,
|
599 |
+
5,
|
600 |
+
1,
|
601 |
+
16,
|
602 |
+
gin_channels=gin_channels,
|
603 |
+
)
|
604 |
+
self.flow = ResidualCouplingBlock(
|
605 |
+
inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels
|
606 |
+
)
|
607 |
+
self.emb_g = nn.Embedding(self.spk_embed_dim, gin_channels)
|
608 |
+
print("gin_channels:", gin_channels, "self.spk_embed_dim:", self.spk_embed_dim)
|
609 |
+
|
610 |
+
def remove_weight_norm(self):
|
611 |
+
self.dec.remove_weight_norm()
|
612 |
+
self.flow.remove_weight_norm()
|
613 |
+
self.enc_q.remove_weight_norm()
|
614 |
+
|
615 |
+
def forward(
|
616 |
+
self, phone, phone_lengths, pitch, pitchf, y, y_lengths, ds
|
617 |
+
): # 这里ds是id,[bs,1]
|
618 |
+
# print(1,pitch.shape)#[bs,t]
|
619 |
+
g = self.emb_g(ds).unsqueeze(-1) # [b, 256, 1]##1是t,广播的
|
620 |
+
m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths)
|
621 |
+
z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g)
|
622 |
+
z_p = self.flow(z, y_mask, g=g)
|
623 |
+
z_slice, ids_slice = commons.rand_slice_segments(
|
624 |
+
z, y_lengths, self.segment_size
|
625 |
+
)
|
626 |
+
# print(-1,pitchf.shape,ids_slice,self.segment_size,self.hop_length,self.segment_size//self.hop_length)
|
627 |
+
pitchf = commons.slice_segments2(pitchf, ids_slice, self.segment_size)
|
628 |
+
# print(-2,pitchf.shape,z_slice.shape)
|
629 |
+
o = self.dec(z_slice, pitchf, g=g)
|
630 |
+
return o, ids_slice, x_mask, y_mask, (z, z_p, m_p, logs_p, m_q, logs_q)
|
631 |
+
|
632 |
+
def infer(self, phone, phone_lengths, pitch, nsff0, sid, max_len=None):
|
633 |
+
g = self.emb_g(sid).unsqueeze(-1)
|
634 |
+
m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths)
|
635 |
+
z_p = (m_p + torch.exp(logs_p) * torch.randn_like(m_p) * 0.66666) * x_mask
|
636 |
+
z = self.flow(z_p, x_mask, g=g, reverse=True)
|
637 |
+
o = self.dec((z * x_mask)[:, :, :max_len], nsff0, g=g)
|
638 |
+
return o, x_mask, (z, z_p, m_p, logs_p)
|
639 |
+
|
640 |
+
|
641 |
+
class SynthesizerTrnMs256NSFsid_nono(nn.Module):
|
642 |
+
def __init__(
|
643 |
+
self,
|
644 |
+
spec_channels,
|
645 |
+
segment_size,
|
646 |
+
inter_channels,
|
647 |
+
hidden_channels,
|
648 |
+
filter_channels,
|
649 |
+
n_heads,
|
650 |
+
n_layers,
|
651 |
+
kernel_size,
|
652 |
+
p_dropout,
|
653 |
+
resblock,
|
654 |
+
resblock_kernel_sizes,
|
655 |
+
resblock_dilation_sizes,
|
656 |
+
upsample_rates,
|
657 |
+
upsample_initial_channel,
|
658 |
+
upsample_kernel_sizes,
|
659 |
+
spk_embed_dim,
|
660 |
+
gin_channels,
|
661 |
+
sr=None,
|
662 |
+
**kwargs
|
663 |
+
):
|
664 |
+
super().__init__()
|
665 |
+
self.spec_channels = spec_channels
|
666 |
+
self.inter_channels = inter_channels
|
667 |
+
self.hidden_channels = hidden_channels
|
668 |
+
self.filter_channels = filter_channels
|
669 |
+
self.n_heads = n_heads
|
670 |
+
self.n_layers = n_layers
|
671 |
+
self.kernel_size = kernel_size
|
672 |
+
self.p_dropout = p_dropout
|
673 |
+
self.resblock = resblock
|
674 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
675 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
676 |
+
self.upsample_rates = upsample_rates
|
677 |
+
self.upsample_initial_channel = upsample_initial_channel
|
678 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
679 |
+
self.segment_size = segment_size
|
680 |
+
self.gin_channels = gin_channels
|
681 |
+
# self.hop_length = hop_length#
|
682 |
+
self.spk_embed_dim = spk_embed_dim
|
683 |
+
self.enc_p = TextEncoder256(
|
684 |
+
inter_channels,
|
685 |
+
hidden_channels,
|
686 |
+
filter_channels,
|
687 |
+
n_heads,
|
688 |
+
n_layers,
|
689 |
+
kernel_size,
|
690 |
+
p_dropout,
|
691 |
+
f0=False,
|
692 |
+
)
|
693 |
+
self.dec = Generator(
|
694 |
+
inter_channels,
|
695 |
+
resblock,
|
696 |
+
resblock_kernel_sizes,
|
697 |
+
resblock_dilation_sizes,
|
698 |
+
upsample_rates,
|
699 |
+
upsample_initial_channel,
|
700 |
+
upsample_kernel_sizes,
|
701 |
+
gin_channels=gin_channels,
|
702 |
+
)
|
703 |
+
self.enc_q = PosteriorEncoder(
|
704 |
+
spec_channels,
|
705 |
+
inter_channels,
|
706 |
+
hidden_channels,
|
707 |
+
5,
|
708 |
+
1,
|
709 |
+
16,
|
710 |
+
gin_channels=gin_channels,
|
711 |
+
)
|
712 |
+
self.flow = ResidualCouplingBlock(
|
713 |
+
inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels
|
714 |
+
)
|
715 |
+
self.emb_g = nn.Embedding(self.spk_embed_dim, gin_channels)
|
716 |
+
print("gin_channels:", gin_channels, "self.spk_embed_dim:", self.spk_embed_dim)
|
717 |
+
|
718 |
+
def remove_weight_norm(self):
|
719 |
+
self.dec.remove_weight_norm()
|
720 |
+
self.flow.remove_weight_norm()
|
721 |
+
self.enc_q.remove_weight_norm()
|
722 |
+
|
723 |
+
def forward(self, phone, phone_lengths, y, y_lengths, ds): # 这里ds是id,[bs,1]
|
724 |
+
g = self.emb_g(ds).unsqueeze(-1) # [b, 256, 1]##1是t,广播的
|
725 |
+
m_p, logs_p, x_mask = self.enc_p(phone, None, phone_lengths)
|
726 |
+
z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g)
|
727 |
+
z_p = self.flow(z, y_mask, g=g)
|
728 |
+
z_slice, ids_slice = commons.rand_slice_segments(
|
729 |
+
z, y_lengths, self.segment_size
|
730 |
+
)
|
731 |
+
o = self.dec(z_slice, g=g)
|
732 |
+
return o, ids_slice, x_mask, y_mask, (z, z_p, m_p, logs_p, m_q, logs_q)
|
733 |
+
|
734 |
+
def infer(self, phone, phone_lengths, sid, max_len=None):
|
735 |
+
g = self.emb_g(sid).unsqueeze(-1)
|
736 |
+
m_p, logs_p, x_mask = self.enc_p(phone, None, phone_lengths)
|
737 |
+
z_p = (m_p + torch.exp(logs_p) * torch.randn_like(m_p) * 0.66666) * x_mask
|
738 |
+
z = self.flow(z_p, x_mask, g=g, reverse=True)
|
739 |
+
o = self.dec((z * x_mask)[:, :, :max_len], g=g)
|
740 |
+
return o, x_mask, (z, z_p, m_p, logs_p)
|
741 |
+
|
742 |
+
|
743 |
+
class SynthesizerTrnMs256NSFsid_sim(nn.Module):
|
744 |
+
"""
|
745 |
+
Synthesizer for Training
|
746 |
+
"""
|
747 |
+
|
748 |
+
def __init__(
|
749 |
+
self,
|
750 |
+
spec_channels,
|
751 |
+
segment_size,
|
752 |
+
inter_channels,
|
753 |
+
hidden_channels,
|
754 |
+
filter_channels,
|
755 |
+
n_heads,
|
756 |
+
n_layers,
|
757 |
+
kernel_size,
|
758 |
+
p_dropout,
|
759 |
+
resblock,
|
760 |
+
resblock_kernel_sizes,
|
761 |
+
resblock_dilation_sizes,
|
762 |
+
upsample_rates,
|
763 |
+
upsample_initial_channel,
|
764 |
+
upsample_kernel_sizes,
|
765 |
+
spk_embed_dim,
|
766 |
+
# hop_length,
|
767 |
+
gin_channels=0,
|
768 |
+
use_sdp=True,
|
769 |
+
**kwargs
|
770 |
+
):
|
771 |
+
super().__init__()
|
772 |
+
self.spec_channels = spec_channels
|
773 |
+
self.inter_channels = inter_channels
|
774 |
+
self.hidden_channels = hidden_channels
|
775 |
+
self.filter_channels = filter_channels
|
776 |
+
self.n_heads = n_heads
|
777 |
+
self.n_layers = n_layers
|
778 |
+
self.kernel_size = kernel_size
|
779 |
+
self.p_dropout = p_dropout
|
780 |
+
self.resblock = resblock
|
781 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
782 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
783 |
+
self.upsample_rates = upsample_rates
|
784 |
+
self.upsample_initial_channel = upsample_initial_channel
|
785 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
786 |
+
self.segment_size = segment_size
|
787 |
+
self.gin_channels = gin_channels
|
788 |
+
# self.hop_length = hop_length#
|
789 |
+
self.spk_embed_dim = spk_embed_dim
|
790 |
+
self.enc_p = TextEncoder256Sim(
|
791 |
+
inter_channels,
|
792 |
+
hidden_channels,
|
793 |
+
filter_channels,
|
794 |
+
n_heads,
|
795 |
+
n_layers,
|
796 |
+
kernel_size,
|
797 |
+
p_dropout,
|
798 |
+
)
|
799 |
+
self.dec = GeneratorNSF(
|
800 |
+
inter_channels,
|
801 |
+
resblock,
|
802 |
+
resblock_kernel_sizes,
|
803 |
+
resblock_dilation_sizes,
|
804 |
+
upsample_rates,
|
805 |
+
upsample_initial_channel,
|
806 |
+
upsample_kernel_sizes,
|
807 |
+
gin_channels=gin_channels,
|
808 |
+
is_half=kwargs["is_half"],
|
809 |
+
)
|
810 |
+
|
811 |
+
self.flow = ResidualCouplingBlock(
|
812 |
+
inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels
|
813 |
+
)
|
814 |
+
self.emb_g = nn.Embedding(self.spk_embed_dim, gin_channels)
|
815 |
+
print("gin_channels:", gin_channels, "self.spk_embed_dim:", self.spk_embed_dim)
|
816 |
+
|
817 |
+
def remove_weight_norm(self):
|
818 |
+
self.dec.remove_weight_norm()
|
819 |
+
self.flow.remove_weight_norm()
|
820 |
+
self.enc_q.remove_weight_norm()
|
821 |
+
|
822 |
+
def forward(
|
823 |
+
self, phone, phone_lengths, pitch, pitchf, y_lengths, ds
|
824 |
+
): # y是spec不需要了现在
|
825 |
+
g = self.emb_g(ds).unsqueeze(-1) # [b, 256, 1]##1是t,广播的
|
826 |
+
x, x_mask = self.enc_p(phone, pitch, phone_lengths)
|
827 |
+
x = self.flow(x, x_mask, g=g, reverse=True)
|
828 |
+
z_slice, ids_slice = commons.rand_slice_segments(
|
829 |
+
x, y_lengths, self.segment_size
|
830 |
+
)
|
831 |
+
|
832 |
+
pitchf = commons.slice_segments2(pitchf, ids_slice, self.segment_size)
|
833 |
+
o = self.dec(z_slice, pitchf, g=g)
|
834 |
+
return o, ids_slice
|
835 |
+
|
836 |
+
def infer(
|
837 |
+
self, phone, phone_lengths, pitch, pitchf, ds, max_len=None
|
838 |
+
): # y是spec不需要了现在
|
839 |
+
g = self.emb_g(ds).unsqueeze(-1) # [b, 256, 1]##1是t,广播的
|
840 |
+
x, x_mask = self.enc_p(phone, pitch, phone_lengths)
|
841 |
+
x = self.flow(x, x_mask, g=g, reverse=True)
|
842 |
+
o = self.dec((x * x_mask)[:, :, :max_len], pitchf, g=g)
|
843 |
+
return o, o
|
844 |
+
|
845 |
+
|
846 |
+
class MultiPeriodDiscriminator(torch.nn.Module):
|
847 |
+
def __init__(self, use_spectral_norm=False):
|
848 |
+
super(MultiPeriodDiscriminator, self).__init__()
|
849 |
+
periods = [2, 3, 5, 7, 11, 17]
|
850 |
+
# periods = [3, 5, 7, 11, 17, 23, 37]
|
851 |
+
|
852 |
+
discs = [DiscriminatorS(use_spectral_norm=use_spectral_norm)]
|
853 |
+
discs = discs + [
|
854 |
+
DiscriminatorP(i, use_spectral_norm=use_spectral_norm) for i in periods
|
855 |
+
]
|
856 |
+
self.discriminators = nn.ModuleList(discs)
|
857 |
+
|
858 |
+
def forward(self, y, y_hat):
|
859 |
+
y_d_rs = [] #
|
860 |
+
y_d_gs = []
|
861 |
+
fmap_rs = []
|
862 |
+
fmap_gs = []
|
863 |
+
for i, d in enumerate(self.discriminators):
|
864 |
+
y_d_r, fmap_r = d(y)
|
865 |
+
y_d_g, fmap_g = d(y_hat)
|
866 |
+
# for j in range(len(fmap_r)):
|
867 |
+
# print(i,j,y.shape,y_hat.shape,fmap_r[j].shape,fmap_g[j].shape)
|
868 |
+
y_d_rs.append(y_d_r)
|
869 |
+
y_d_gs.append(y_d_g)
|
870 |
+
fmap_rs.append(fmap_r)
|
871 |
+
fmap_gs.append(fmap_g)
|
872 |
+
|
873 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
874 |
+
|
875 |
+
|
876 |
+
class DiscriminatorS(torch.nn.Module):
|
877 |
+
def __init__(self, use_spectral_norm=False):
|
878 |
+
super(DiscriminatorS, self).__init__()
|
879 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
880 |
+
self.convs = nn.ModuleList(
|
881 |
+
[
|
882 |
+
norm_f(Conv1d(1, 16, 15, 1, padding=7)),
|
883 |
+
norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
|
884 |
+
norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
|
885 |
+
norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
|
886 |
+
norm_f(Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
|
887 |
+
norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
|
888 |
+
]
|
889 |
+
)
|
890 |
+
self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
|
891 |
+
|
892 |
+
def forward(self, x):
|
893 |
+
fmap = []
|
894 |
+
|
895 |
+
for l in self.convs:
|
896 |
+
x = l(x)
|
897 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
898 |
+
fmap.append(x)
|
899 |
+
x = self.conv_post(x)
|
900 |
+
fmap.append(x)
|
901 |
+
x = torch.flatten(x, 1, -1)
|
902 |
+
|
903 |
+
return x, fmap
|
904 |
+
|
905 |
+
|
906 |
+
class DiscriminatorP(torch.nn.Module):
|
907 |
+
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
|
908 |
+
super(DiscriminatorP, self).__init__()
|
909 |
+
self.period = period
|
910 |
+
self.use_spectral_norm = use_spectral_norm
|
911 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
912 |
+
self.convs = nn.ModuleList(
|
913 |
+
[
|
914 |
+
norm_f(
|
915 |
+
Conv2d(
|
916 |
+
1,
|
917 |
+
32,
|
918 |
+
(kernel_size, 1),
|
919 |
+
(stride, 1),
|
920 |
+
padding=(get_padding(kernel_size, 1), 0),
|
921 |
+
)
|
922 |
+
),
|
923 |
+
norm_f(
|
924 |
+
Conv2d(
|
925 |
+
32,
|
926 |
+
128,
|
927 |
+
(kernel_size, 1),
|
928 |
+
(stride, 1),
|
929 |
+
padding=(get_padding(kernel_size, 1), 0),
|
930 |
+
)
|
931 |
+
),
|
932 |
+
norm_f(
|
933 |
+
Conv2d(
|
934 |
+
128,
|
935 |
+
512,
|
936 |
+
(kernel_size, 1),
|
937 |
+
(stride, 1),
|
938 |
+
padding=(get_padding(kernel_size, 1), 0),
|
939 |
+
)
|
940 |
+
),
|
941 |
+
norm_f(
|
942 |
+
Conv2d(
|
943 |
+
512,
|
944 |
+
1024,
|
945 |
+
(kernel_size, 1),
|
946 |
+
(stride, 1),
|
947 |
+
padding=(get_padding(kernel_size, 1), 0),
|
948 |
+
)
|
949 |
+
),
|
950 |
+
norm_f(
|
951 |
+
Conv2d(
|
952 |
+
1024,
|
953 |
+
1024,
|
954 |
+
(kernel_size, 1),
|
955 |
+
1,
|
956 |
+
padding=(get_padding(kernel_size, 1), 0),
|
957 |
+
)
|
958 |
+
),
|
959 |
+
]
|
960 |
+
)
|
961 |
+
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
962 |
+
|
963 |
+
def forward(self, x):
|
964 |
+
fmap = []
|
965 |
+
|
966 |
+
# 1d to 2d
|
967 |
+
b, c, t = x.shape
|
968 |
+
if t % self.period != 0: # pad first
|
969 |
+
n_pad = self.period - (t % self.period)
|
970 |
+
x = F.pad(x, (0, n_pad), "reflect")
|
971 |
+
t = t + n_pad
|
972 |
+
x = x.view(b, c, t // self.period, self.period)
|
973 |
+
|
974 |
+
for l in self.convs:
|
975 |
+
x = l(x)
|
976 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
977 |
+
fmap.append(x)
|
978 |
+
x = self.conv_post(x)
|
979 |
+
fmap.append(x)
|
980 |
+
x = torch.flatten(x, 1, -1)
|
981 |
+
|
982 |
+
return x, fmap
|
infer_pack/models_onnx.py
ADDED
@@ -0,0 +1,760 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math, pdb, os
|
2 |
+
from time import time as ttime
|
3 |
+
import torch
|
4 |
+
from torch import nn
|
5 |
+
from torch.nn import functional as F
|
6 |
+
from infer_pack import modules
|
7 |
+
from infer_pack import attentions
|
8 |
+
from infer_pack import commons
|
9 |
+
from infer_pack.commons import init_weights, get_padding
|
10 |
+
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
11 |
+
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
12 |
+
from infer_pack.commons import init_weights
|
13 |
+
import numpy as np
|
14 |
+
from infer_pack import commons
|
15 |
+
|
16 |
+
|
17 |
+
class TextEncoder256(nn.Module):
|
18 |
+
def __init__(
|
19 |
+
self,
|
20 |
+
out_channels,
|
21 |
+
hidden_channels,
|
22 |
+
filter_channels,
|
23 |
+
n_heads,
|
24 |
+
n_layers,
|
25 |
+
kernel_size,
|
26 |
+
p_dropout,
|
27 |
+
f0=True,
|
28 |
+
):
|
29 |
+
super().__init__()
|
30 |
+
self.out_channels = out_channels
|
31 |
+
self.hidden_channels = hidden_channels
|
32 |
+
self.filter_channels = filter_channels
|
33 |
+
self.n_heads = n_heads
|
34 |
+
self.n_layers = n_layers
|
35 |
+
self.kernel_size = kernel_size
|
36 |
+
self.p_dropout = p_dropout
|
37 |
+
self.emb_phone = nn.Linear(256, hidden_channels)
|
38 |
+
self.lrelu = nn.LeakyReLU(0.1, inplace=True)
|
39 |
+
if f0 == True:
|
40 |
+
self.emb_pitch = nn.Embedding(256, hidden_channels) # pitch 256
|
41 |
+
self.encoder = attentions.Encoder(
|
42 |
+
hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout
|
43 |
+
)
|
44 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
45 |
+
|
46 |
+
def forward(self, phone, pitch, lengths):
|
47 |
+
if pitch == None:
|
48 |
+
x = self.emb_phone(phone)
|
49 |
+
else:
|
50 |
+
x = self.emb_phone(phone) + self.emb_pitch(pitch)
|
51 |
+
x = x * math.sqrt(self.hidden_channels) # [b, t, h]
|
52 |
+
x = self.lrelu(x)
|
53 |
+
x = torch.transpose(x, 1, -1) # [b, h, t]
|
54 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(lengths, x.size(2)), 1).to(
|
55 |
+
x.dtype
|
56 |
+
)
|
57 |
+
x = self.encoder(x * x_mask, x_mask)
|
58 |
+
stats = self.proj(x) * x_mask
|
59 |
+
|
60 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
61 |
+
return m, logs, x_mask
|
62 |
+
|
63 |
+
|
64 |
+
class TextEncoder256Sim(nn.Module):
|
65 |
+
def __init__(
|
66 |
+
self,
|
67 |
+
out_channels,
|
68 |
+
hidden_channels,
|
69 |
+
filter_channels,
|
70 |
+
n_heads,
|
71 |
+
n_layers,
|
72 |
+
kernel_size,
|
73 |
+
p_dropout,
|
74 |
+
f0=True,
|
75 |
+
):
|
76 |
+
super().__init__()
|
77 |
+
self.out_channels = out_channels
|
78 |
+
self.hidden_channels = hidden_channels
|
79 |
+
self.filter_channels = filter_channels
|
80 |
+
self.n_heads = n_heads
|
81 |
+
self.n_layers = n_layers
|
82 |
+
self.kernel_size = kernel_size
|
83 |
+
self.p_dropout = p_dropout
|
84 |
+
self.emb_phone = nn.Linear(256, hidden_channels)
|
85 |
+
self.lrelu = nn.LeakyReLU(0.1, inplace=True)
|
86 |
+
if f0 == True:
|
87 |
+
self.emb_pitch = nn.Embedding(256, hidden_channels) # pitch 256
|
88 |
+
self.encoder = attentions.Encoder(
|
89 |
+
hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout
|
90 |
+
)
|
91 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
92 |
+
|
93 |
+
def forward(self, phone, pitch, lengths):
|
94 |
+
if pitch == None:
|
95 |
+
x = self.emb_phone(phone)
|
96 |
+
else:
|
97 |
+
x = self.emb_phone(phone) + self.emb_pitch(pitch)
|
98 |
+
x = x * math.sqrt(self.hidden_channels) # [b, t, h]
|
99 |
+
x = self.lrelu(x)
|
100 |
+
x = torch.transpose(x, 1, -1) # [b, h, t]
|
101 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(lengths, x.size(2)), 1).to(
|
102 |
+
x.dtype
|
103 |
+
)
|
104 |
+
x = self.encoder(x * x_mask, x_mask)
|
105 |
+
x = self.proj(x) * x_mask
|
106 |
+
return x, x_mask
|
107 |
+
|
108 |
+
|
109 |
+
class ResidualCouplingBlock(nn.Module):
|
110 |
+
def __init__(
|
111 |
+
self,
|
112 |
+
channels,
|
113 |
+
hidden_channels,
|
114 |
+
kernel_size,
|
115 |
+
dilation_rate,
|
116 |
+
n_layers,
|
117 |
+
n_flows=4,
|
118 |
+
gin_channels=0,
|
119 |
+
):
|
120 |
+
super().__init__()
|
121 |
+
self.channels = channels
|
122 |
+
self.hidden_channels = hidden_channels
|
123 |
+
self.kernel_size = kernel_size
|
124 |
+
self.dilation_rate = dilation_rate
|
125 |
+
self.n_layers = n_layers
|
126 |
+
self.n_flows = n_flows
|
127 |
+
self.gin_channels = gin_channels
|
128 |
+
|
129 |
+
self.flows = nn.ModuleList()
|
130 |
+
for i in range(n_flows):
|
131 |
+
self.flows.append(
|
132 |
+
modules.ResidualCouplingLayer(
|
133 |
+
channels,
|
134 |
+
hidden_channels,
|
135 |
+
kernel_size,
|
136 |
+
dilation_rate,
|
137 |
+
n_layers,
|
138 |
+
gin_channels=gin_channels,
|
139 |
+
mean_only=True,
|
140 |
+
)
|
141 |
+
)
|
142 |
+
self.flows.append(modules.Flip())
|
143 |
+
|
144 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
145 |
+
if not reverse:
|
146 |
+
for flow in self.flows:
|
147 |
+
x, _ = flow(x, x_mask, g=g, reverse=reverse)
|
148 |
+
else:
|
149 |
+
for flow in reversed(self.flows):
|
150 |
+
x = flow(x, x_mask, g=g, reverse=reverse)
|
151 |
+
return x
|
152 |
+
|
153 |
+
def remove_weight_norm(self):
|
154 |
+
for i in range(self.n_flows):
|
155 |
+
self.flows[i * 2].remove_weight_norm()
|
156 |
+
|
157 |
+
|
158 |
+
class PosteriorEncoder(nn.Module):
|
159 |
+
def __init__(
|
160 |
+
self,
|
161 |
+
in_channels,
|
162 |
+
out_channels,
|
163 |
+
hidden_channels,
|
164 |
+
kernel_size,
|
165 |
+
dilation_rate,
|
166 |
+
n_layers,
|
167 |
+
gin_channels=0,
|
168 |
+
):
|
169 |
+
super().__init__()
|
170 |
+
self.in_channels = in_channels
|
171 |
+
self.out_channels = out_channels
|
172 |
+
self.hidden_channels = hidden_channels
|
173 |
+
self.kernel_size = kernel_size
|
174 |
+
self.dilation_rate = dilation_rate
|
175 |
+
self.n_layers = n_layers
|
176 |
+
self.gin_channels = gin_channels
|
177 |
+
|
178 |
+
self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
179 |
+
self.enc = modules.WN(
|
180 |
+
hidden_channels,
|
181 |
+
kernel_size,
|
182 |
+
dilation_rate,
|
183 |
+
n_layers,
|
184 |
+
gin_channels=gin_channels,
|
185 |
+
)
|
186 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
187 |
+
|
188 |
+
def forward(self, x, x_lengths, g=None):
|
189 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(
|
190 |
+
x.dtype
|
191 |
+
)
|
192 |
+
x = self.pre(x) * x_mask
|
193 |
+
x = self.enc(x, x_mask, g=g)
|
194 |
+
stats = self.proj(x) * x_mask
|
195 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
196 |
+
z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
|
197 |
+
return z, m, logs, x_mask
|
198 |
+
|
199 |
+
def remove_weight_norm(self):
|
200 |
+
self.enc.remove_weight_norm()
|
201 |
+
|
202 |
+
|
203 |
+
class Generator(torch.nn.Module):
|
204 |
+
def __init__(
|
205 |
+
self,
|
206 |
+
initial_channel,
|
207 |
+
resblock,
|
208 |
+
resblock_kernel_sizes,
|
209 |
+
resblock_dilation_sizes,
|
210 |
+
upsample_rates,
|
211 |
+
upsample_initial_channel,
|
212 |
+
upsample_kernel_sizes,
|
213 |
+
gin_channels=0,
|
214 |
+
):
|
215 |
+
super(Generator, self).__init__()
|
216 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
217 |
+
self.num_upsamples = len(upsample_rates)
|
218 |
+
self.conv_pre = Conv1d(
|
219 |
+
initial_channel, upsample_initial_channel, 7, 1, padding=3
|
220 |
+
)
|
221 |
+
resblock = modules.ResBlock1 if resblock == "1" else modules.ResBlock2
|
222 |
+
|
223 |
+
self.ups = nn.ModuleList()
|
224 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
225 |
+
self.ups.append(
|
226 |
+
weight_norm(
|
227 |
+
ConvTranspose1d(
|
228 |
+
upsample_initial_channel // (2**i),
|
229 |
+
upsample_initial_channel // (2 ** (i + 1)),
|
230 |
+
k,
|
231 |
+
u,
|
232 |
+
padding=(k - u) // 2,
|
233 |
+
)
|
234 |
+
)
|
235 |
+
)
|
236 |
+
|
237 |
+
self.resblocks = nn.ModuleList()
|
238 |
+
for i in range(len(self.ups)):
|
239 |
+
ch = upsample_initial_channel // (2 ** (i + 1))
|
240 |
+
for j, (k, d) in enumerate(
|
241 |
+
zip(resblock_kernel_sizes, resblock_dilation_sizes)
|
242 |
+
):
|
243 |
+
self.resblocks.append(resblock(ch, k, d))
|
244 |
+
|
245 |
+
self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
|
246 |
+
self.ups.apply(init_weights)
|
247 |
+
|
248 |
+
if gin_channels != 0:
|
249 |
+
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
250 |
+
|
251 |
+
def forward(self, x, g=None):
|
252 |
+
x = self.conv_pre(x)
|
253 |
+
if g is not None:
|
254 |
+
x = x + self.cond(g)
|
255 |
+
|
256 |
+
for i in range(self.num_upsamples):
|
257 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
258 |
+
x = self.ups[i](x)
|
259 |
+
xs = None
|
260 |
+
for j in range(self.num_kernels):
|
261 |
+
if xs is None:
|
262 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
263 |
+
else:
|
264 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
265 |
+
x = xs / self.num_kernels
|
266 |
+
x = F.leaky_relu(x)
|
267 |
+
x = self.conv_post(x)
|
268 |
+
x = torch.tanh(x)
|
269 |
+
|
270 |
+
return x
|
271 |
+
|
272 |
+
def remove_weight_norm(self):
|
273 |
+
for l in self.ups:
|
274 |
+
remove_weight_norm(l)
|
275 |
+
for l in self.resblocks:
|
276 |
+
l.remove_weight_norm()
|
277 |
+
|
278 |
+
|
279 |
+
class SineGen(torch.nn.Module):
|
280 |
+
"""Definition of sine generator
|
281 |
+
SineGen(samp_rate, harmonic_num = 0,
|
282 |
+
sine_amp = 0.1, noise_std = 0.003,
|
283 |
+
voiced_threshold = 0,
|
284 |
+
flag_for_pulse=False)
|
285 |
+
samp_rate: sampling rate in Hz
|
286 |
+
harmonic_num: number of harmonic overtones (default 0)
|
287 |
+
sine_amp: amplitude of sine-wavefrom (default 0.1)
|
288 |
+
noise_std: std of Gaussian noise (default 0.003)
|
289 |
+
voiced_thoreshold: F0 threshold for U/V classification (default 0)
|
290 |
+
flag_for_pulse: this SinGen is used inside PulseGen (default False)
|
291 |
+
Note: when flag_for_pulse is True, the first time step of a voiced
|
292 |
+
segment is always sin(np.pi) or cos(0)
|
293 |
+
"""
|
294 |
+
|
295 |
+
def __init__(
|
296 |
+
self,
|
297 |
+
samp_rate,
|
298 |
+
harmonic_num=0,
|
299 |
+
sine_amp=0.1,
|
300 |
+
noise_std=0.003,
|
301 |
+
voiced_threshold=0,
|
302 |
+
flag_for_pulse=False,
|
303 |
+
):
|
304 |
+
super(SineGen, self).__init__()
|
305 |
+
self.sine_amp = sine_amp
|
306 |
+
self.noise_std = noise_std
|
307 |
+
self.harmonic_num = harmonic_num
|
308 |
+
self.dim = self.harmonic_num + 1
|
309 |
+
self.sampling_rate = samp_rate
|
310 |
+
self.voiced_threshold = voiced_threshold
|
311 |
+
|
312 |
+
def _f02uv(self, f0):
|
313 |
+
# generate uv signal
|
314 |
+
uv = torch.ones_like(f0)
|
315 |
+
uv = uv * (f0 > self.voiced_threshold)
|
316 |
+
return uv
|
317 |
+
|
318 |
+
def forward(self, f0, upp):
|
319 |
+
"""sine_tensor, uv = forward(f0)
|
320 |
+
input F0: tensor(batchsize=1, length, dim=1)
|
321 |
+
f0 for unvoiced steps should be 0
|
322 |
+
output sine_tensor: tensor(batchsize=1, length, dim)
|
323 |
+
output uv: tensor(batchsize=1, length, 1)
|
324 |
+
"""
|
325 |
+
with torch.no_grad():
|
326 |
+
f0 = f0[:, None].transpose(1, 2)
|
327 |
+
f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim, device=f0.device)
|
328 |
+
# fundamental component
|
329 |
+
f0_buf[:, :, 0] = f0[:, :, 0]
|
330 |
+
for idx in np.arange(self.harmonic_num):
|
331 |
+
f0_buf[:, :, idx + 1] = f0_buf[:, :, 0] * (
|
332 |
+
idx + 2
|
333 |
+
) # idx + 2: the (idx+1)-th overtone, (idx+2)-th harmonic
|
334 |
+
rad_values = (f0_buf / self.sampling_rate) % 1 ###%1意味着n_har的乘积无法后处理优化
|
335 |
+
rand_ini = torch.rand(
|
336 |
+
f0_buf.shape[0], f0_buf.shape[2], device=f0_buf.device
|
337 |
+
)
|
338 |
+
rand_ini[:, 0] = 0
|
339 |
+
rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
|
340 |
+
tmp_over_one = torch.cumsum(rad_values, 1) # % 1 #####%1意味着后面的cumsum无法再优化
|
341 |
+
tmp_over_one *= upp
|
342 |
+
tmp_over_one = F.interpolate(
|
343 |
+
tmp_over_one.transpose(2, 1),
|
344 |
+
scale_factor=upp,
|
345 |
+
mode="linear",
|
346 |
+
align_corners=True,
|
347 |
+
).transpose(2, 1)
|
348 |
+
rad_values = F.interpolate(
|
349 |
+
rad_values.transpose(2, 1), scale_factor=upp, mode="nearest"
|
350 |
+
).transpose(
|
351 |
+
2, 1
|
352 |
+
) #######
|
353 |
+
tmp_over_one %= 1
|
354 |
+
tmp_over_one_idx = (tmp_over_one[:, 1:, :] - tmp_over_one[:, :-1, :]) < 0
|
355 |
+
cumsum_shift = torch.zeros_like(rad_values)
|
356 |
+
cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
|
357 |
+
sine_waves = torch.sin(
|
358 |
+
torch.cumsum(rad_values + cumsum_shift, dim=1) * 2 * np.pi
|
359 |
+
)
|
360 |
+
sine_waves = sine_waves * self.sine_amp
|
361 |
+
uv = self._f02uv(f0)
|
362 |
+
uv = F.interpolate(
|
363 |
+
uv.transpose(2, 1), scale_factor=upp, mode="nearest"
|
364 |
+
).transpose(2, 1)
|
365 |
+
noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
|
366 |
+
noise = noise_amp * torch.randn_like(sine_waves)
|
367 |
+
sine_waves = sine_waves * uv + noise
|
368 |
+
return sine_waves, uv, noise
|
369 |
+
|
370 |
+
|
371 |
+
class SourceModuleHnNSF(torch.nn.Module):
|
372 |
+
"""SourceModule for hn-nsf
|
373 |
+
SourceModule(sampling_rate, harmonic_num=0, sine_amp=0.1,
|
374 |
+
add_noise_std=0.003, voiced_threshod=0)
|
375 |
+
sampling_rate: sampling_rate in Hz
|
376 |
+
harmonic_num: number of harmonic above F0 (default: 0)
|
377 |
+
sine_amp: amplitude of sine source signal (default: 0.1)
|
378 |
+
add_noise_std: std of additive Gaussian noise (default: 0.003)
|
379 |
+
note that amplitude of noise in unvoiced is decided
|
380 |
+
by sine_amp
|
381 |
+
voiced_threshold: threhold to set U/V given F0 (default: 0)
|
382 |
+
Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
|
383 |
+
F0_sampled (batchsize, length, 1)
|
384 |
+
Sine_source (batchsize, length, 1)
|
385 |
+
noise_source (batchsize, length 1)
|
386 |
+
uv (batchsize, length, 1)
|
387 |
+
"""
|
388 |
+
|
389 |
+
def __init__(
|
390 |
+
self,
|
391 |
+
sampling_rate,
|
392 |
+
harmonic_num=0,
|
393 |
+
sine_amp=0.1,
|
394 |
+
add_noise_std=0.003,
|
395 |
+
voiced_threshod=0,
|
396 |
+
is_half=True,
|
397 |
+
):
|
398 |
+
super(SourceModuleHnNSF, self).__init__()
|
399 |
+
|
400 |
+
self.sine_amp = sine_amp
|
401 |
+
self.noise_std = add_noise_std
|
402 |
+
self.is_half = is_half
|
403 |
+
# to produce sine waveforms
|
404 |
+
self.l_sin_gen = SineGen(
|
405 |
+
sampling_rate, harmonic_num, sine_amp, add_noise_std, voiced_threshod
|
406 |
+
)
|
407 |
+
|
408 |
+
# to merge source harmonics into a single excitation
|
409 |
+
self.l_linear = torch.nn.Linear(harmonic_num + 1, 1)
|
410 |
+
self.l_tanh = torch.nn.Tanh()
|
411 |
+
|
412 |
+
def forward(self, x, upp=None):
|
413 |
+
sine_wavs, uv, _ = self.l_sin_gen(x, upp)
|
414 |
+
if self.is_half:
|
415 |
+
sine_wavs = sine_wavs.half()
|
416 |
+
sine_merge = self.l_tanh(self.l_linear(sine_wavs))
|
417 |
+
return sine_merge, None, None # noise, uv
|
418 |
+
|
419 |
+
|
420 |
+
class GeneratorNSF(torch.nn.Module):
|
421 |
+
def __init__(
|
422 |
+
self,
|
423 |
+
initial_channel,
|
424 |
+
resblock,
|
425 |
+
resblock_kernel_sizes,
|
426 |
+
resblock_dilation_sizes,
|
427 |
+
upsample_rates,
|
428 |
+
upsample_initial_channel,
|
429 |
+
upsample_kernel_sizes,
|
430 |
+
gin_channels,
|
431 |
+
sr,
|
432 |
+
is_half=False,
|
433 |
+
):
|
434 |
+
super(GeneratorNSF, self).__init__()
|
435 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
436 |
+
self.num_upsamples = len(upsample_rates)
|
437 |
+
|
438 |
+
self.f0_upsamp = torch.nn.Upsample(scale_factor=np.prod(upsample_rates))
|
439 |
+
self.m_source = SourceModuleHnNSF(
|
440 |
+
sampling_rate=sr, harmonic_num=0, is_half=is_half
|
441 |
+
)
|
442 |
+
self.noise_convs = nn.ModuleList()
|
443 |
+
self.conv_pre = Conv1d(
|
444 |
+
initial_channel, upsample_initial_channel, 7, 1, padding=3
|
445 |
+
)
|
446 |
+
resblock = modules.ResBlock1 if resblock == "1" else modules.ResBlock2
|
447 |
+
|
448 |
+
self.ups = nn.ModuleList()
|
449 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
450 |
+
c_cur = upsample_initial_channel // (2 ** (i + 1))
|
451 |
+
self.ups.append(
|
452 |
+
weight_norm(
|
453 |
+
ConvTranspose1d(
|
454 |
+
upsample_initial_channel // (2**i),
|
455 |
+
upsample_initial_channel // (2 ** (i + 1)),
|
456 |
+
k,
|
457 |
+
u,
|
458 |
+
padding=(k - u) // 2,
|
459 |
+
)
|
460 |
+
)
|
461 |
+
)
|
462 |
+
if i + 1 < len(upsample_rates):
|
463 |
+
stride_f0 = np.prod(upsample_rates[i + 1 :])
|
464 |
+
self.noise_convs.append(
|
465 |
+
Conv1d(
|
466 |
+
1,
|
467 |
+
c_cur,
|
468 |
+
kernel_size=stride_f0 * 2,
|
469 |
+
stride=stride_f0,
|
470 |
+
padding=stride_f0 // 2,
|
471 |
+
)
|
472 |
+
)
|
473 |
+
else:
|
474 |
+
self.noise_convs.append(Conv1d(1, c_cur, kernel_size=1))
|
475 |
+
|
476 |
+
self.resblocks = nn.ModuleList()
|
477 |
+
for i in range(len(self.ups)):
|
478 |
+
ch = upsample_initial_channel // (2 ** (i + 1))
|
479 |
+
for j, (k, d) in enumerate(
|
480 |
+
zip(resblock_kernel_sizes, resblock_dilation_sizes)
|
481 |
+
):
|
482 |
+
self.resblocks.append(resblock(ch, k, d))
|
483 |
+
|
484 |
+
self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
|
485 |
+
self.ups.apply(init_weights)
|
486 |
+
|
487 |
+
if gin_channels != 0:
|
488 |
+
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
489 |
+
|
490 |
+
self.upp = np.prod(upsample_rates)
|
491 |
+
|
492 |
+
def forward(self, x, f0, g=None):
|
493 |
+
har_source, noi_source, uv = self.m_source(f0, self.upp)
|
494 |
+
har_source = har_source.transpose(1, 2)
|
495 |
+
x = self.conv_pre(x)
|
496 |
+
if g is not None:
|
497 |
+
x = x + self.cond(g)
|
498 |
+
|
499 |
+
for i in range(self.num_upsamples):
|
500 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
501 |
+
x = self.ups[i](x)
|
502 |
+
x_source = self.noise_convs[i](har_source)
|
503 |
+
x = x + x_source
|
504 |
+
xs = None
|
505 |
+
for j in range(self.num_kernels):
|
506 |
+
if xs is None:
|
507 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
508 |
+
else:
|
509 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
510 |
+
x = xs / self.num_kernels
|
511 |
+
x = F.leaky_relu(x)
|
512 |
+
x = self.conv_post(x)
|
513 |
+
x = torch.tanh(x)
|
514 |
+
return x
|
515 |
+
|
516 |
+
def remove_weight_norm(self):
|
517 |
+
for l in self.ups:
|
518 |
+
remove_weight_norm(l)
|
519 |
+
for l in self.resblocks:
|
520 |
+
l.remove_weight_norm()
|
521 |
+
|
522 |
+
|
523 |
+
sr2sr = {
|
524 |
+
"32k": 32000,
|
525 |
+
"40k": 40000,
|
526 |
+
"48k": 48000,
|
527 |
+
}
|
528 |
+
|
529 |
+
|
530 |
+
class SynthesizerTrnMs256NSFsidO(nn.Module):
|
531 |
+
def __init__(
|
532 |
+
self,
|
533 |
+
spec_channels,
|
534 |
+
segment_size,
|
535 |
+
inter_channels,
|
536 |
+
hidden_channels,
|
537 |
+
filter_channels,
|
538 |
+
n_heads,
|
539 |
+
n_layers,
|
540 |
+
kernel_size,
|
541 |
+
p_dropout,
|
542 |
+
resblock,
|
543 |
+
resblock_kernel_sizes,
|
544 |
+
resblock_dilation_sizes,
|
545 |
+
upsample_rates,
|
546 |
+
upsample_initial_channel,
|
547 |
+
upsample_kernel_sizes,
|
548 |
+
spk_embed_dim,
|
549 |
+
gin_channels,
|
550 |
+
sr,
|
551 |
+
**kwargs
|
552 |
+
):
|
553 |
+
super().__init__()
|
554 |
+
if type(sr) == type("strr"):
|
555 |
+
sr = sr2sr[sr]
|
556 |
+
self.spec_channels = spec_channels
|
557 |
+
self.inter_channels = inter_channels
|
558 |
+
self.hidden_channels = hidden_channels
|
559 |
+
self.filter_channels = filter_channels
|
560 |
+
self.n_heads = n_heads
|
561 |
+
self.n_layers = n_layers
|
562 |
+
self.kernel_size = kernel_size
|
563 |
+
self.p_dropout = p_dropout
|
564 |
+
self.resblock = resblock
|
565 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
566 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
567 |
+
self.upsample_rates = upsample_rates
|
568 |
+
self.upsample_initial_channel = upsample_initial_channel
|
569 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
570 |
+
self.segment_size = segment_size
|
571 |
+
self.gin_channels = gin_channels
|
572 |
+
# self.hop_length = hop_length#
|
573 |
+
self.spk_embed_dim = spk_embed_dim
|
574 |
+
self.enc_p = TextEncoder256(
|
575 |
+
inter_channels,
|
576 |
+
hidden_channels,
|
577 |
+
filter_channels,
|
578 |
+
n_heads,
|
579 |
+
n_layers,
|
580 |
+
kernel_size,
|
581 |
+
p_dropout,
|
582 |
+
)
|
583 |
+
self.dec = GeneratorNSF(
|
584 |
+
inter_channels,
|
585 |
+
resblock,
|
586 |
+
resblock_kernel_sizes,
|
587 |
+
resblock_dilation_sizes,
|
588 |
+
upsample_rates,
|
589 |
+
upsample_initial_channel,
|
590 |
+
upsample_kernel_sizes,
|
591 |
+
gin_channels=gin_channels,
|
592 |
+
sr=sr,
|
593 |
+
is_half=kwargs["is_half"],
|
594 |
+
)
|
595 |
+
self.enc_q = PosteriorEncoder(
|
596 |
+
spec_channels,
|
597 |
+
inter_channels,
|
598 |
+
hidden_channels,
|
599 |
+
5,
|
600 |
+
1,
|
601 |
+
16,
|
602 |
+
gin_channels=gin_channels,
|
603 |
+
)
|
604 |
+
self.flow = ResidualCouplingBlock(
|
605 |
+
inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels
|
606 |
+
)
|
607 |
+
self.emb_g = nn.Embedding(self.spk_embed_dim, gin_channels)
|
608 |
+
print("gin_channels:", gin_channels, "self.spk_embed_dim:", self.spk_embed_dim)
|
609 |
+
|
610 |
+
def remove_weight_norm(self):
|
611 |
+
self.dec.remove_weight_norm()
|
612 |
+
self.flow.remove_weight_norm()
|
613 |
+
self.enc_q.remove_weight_norm()
|
614 |
+
|
615 |
+
def forward(self, phone, phone_lengths, pitch, nsff0, sid, max_len=None):
|
616 |
+
g = self.emb_g(sid).unsqueeze(-1)
|
617 |
+
m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths)
|
618 |
+
z_p = (m_p + torch.exp(logs_p) * torch.randn_like(m_p) * 0.66666) * x_mask
|
619 |
+
z = self.flow(z_p, x_mask, g=g, reverse=True)
|
620 |
+
o = self.dec((z * x_mask)[:, :, :max_len], nsff0, g=g)
|
621 |
+
return o
|
622 |
+
|
623 |
+
|
624 |
+
class MultiPeriodDiscriminator(torch.nn.Module):
|
625 |
+
def __init__(self, use_spectral_norm=False):
|
626 |
+
super(MultiPeriodDiscriminator, self).__init__()
|
627 |
+
periods = [2, 3, 5, 7, 11, 17]
|
628 |
+
# periods = [3, 5, 7, 11, 17, 23, 37]
|
629 |
+
|
630 |
+
discs = [DiscriminatorS(use_spectral_norm=use_spectral_norm)]
|
631 |
+
discs = discs + [
|
632 |
+
DiscriminatorP(i, use_spectral_norm=use_spectral_norm) for i in periods
|
633 |
+
]
|
634 |
+
self.discriminators = nn.ModuleList(discs)
|
635 |
+
|
636 |
+
def forward(self, y, y_hat):
|
637 |
+
y_d_rs = [] #
|
638 |
+
y_d_gs = []
|
639 |
+
fmap_rs = []
|
640 |
+
fmap_gs = []
|
641 |
+
for i, d in enumerate(self.discriminators):
|
642 |
+
y_d_r, fmap_r = d(y)
|
643 |
+
y_d_g, fmap_g = d(y_hat)
|
644 |
+
# for j in range(len(fmap_r)):
|
645 |
+
# print(i,j,y.shape,y_hat.shape,fmap_r[j].shape,fmap_g[j].shape)
|
646 |
+
y_d_rs.append(y_d_r)
|
647 |
+
y_d_gs.append(y_d_g)
|
648 |
+
fmap_rs.append(fmap_r)
|
649 |
+
fmap_gs.append(fmap_g)
|
650 |
+
|
651 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
652 |
+
|
653 |
+
|
654 |
+
class DiscriminatorS(torch.nn.Module):
|
655 |
+
def __init__(self, use_spectral_norm=False):
|
656 |
+
super(DiscriminatorS, self).__init__()
|
657 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
658 |
+
self.convs = nn.ModuleList(
|
659 |
+
[
|
660 |
+
norm_f(Conv1d(1, 16, 15, 1, padding=7)),
|
661 |
+
norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
|
662 |
+
norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
|
663 |
+
norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
|
664 |
+
norm_f(Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
|
665 |
+
norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
|
666 |
+
]
|
667 |
+
)
|
668 |
+
self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
|
669 |
+
|
670 |
+
def forward(self, x):
|
671 |
+
fmap = []
|
672 |
+
|
673 |
+
for l in self.convs:
|
674 |
+
x = l(x)
|
675 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
676 |
+
fmap.append(x)
|
677 |
+
x = self.conv_post(x)
|
678 |
+
fmap.append(x)
|
679 |
+
x = torch.flatten(x, 1, -1)
|
680 |
+
|
681 |
+
return x, fmap
|
682 |
+
|
683 |
+
|
684 |
+
class DiscriminatorP(torch.nn.Module):
|
685 |
+
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
|
686 |
+
super(DiscriminatorP, self).__init__()
|
687 |
+
self.period = period
|
688 |
+
self.use_spectral_norm = use_spectral_norm
|
689 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
690 |
+
self.convs = nn.ModuleList(
|
691 |
+
[
|
692 |
+
norm_f(
|
693 |
+
Conv2d(
|
694 |
+
1,
|
695 |
+
32,
|
696 |
+
(kernel_size, 1),
|
697 |
+
(stride, 1),
|
698 |
+
padding=(get_padding(kernel_size, 1), 0),
|
699 |
+
)
|
700 |
+
),
|
701 |
+
norm_f(
|
702 |
+
Conv2d(
|
703 |
+
32,
|
704 |
+
128,
|
705 |
+
(kernel_size, 1),
|
706 |
+
(stride, 1),
|
707 |
+
padding=(get_padding(kernel_size, 1), 0),
|
708 |
+
)
|
709 |
+
),
|
710 |
+
norm_f(
|
711 |
+
Conv2d(
|
712 |
+
128,
|
713 |
+
512,
|
714 |
+
(kernel_size, 1),
|
715 |
+
(stride, 1),
|
716 |
+
padding=(get_padding(kernel_size, 1), 0),
|
717 |
+
)
|
718 |
+
),
|
719 |
+
norm_f(
|
720 |
+
Conv2d(
|
721 |
+
512,
|
722 |
+
1024,
|
723 |
+
(kernel_size, 1),
|
724 |
+
(stride, 1),
|
725 |
+
padding=(get_padding(kernel_size, 1), 0),
|
726 |
+
)
|
727 |
+
),
|
728 |
+
norm_f(
|
729 |
+
Conv2d(
|
730 |
+
1024,
|
731 |
+
1024,
|
732 |
+
(kernel_size, 1),
|
733 |
+
1,
|
734 |
+
padding=(get_padding(kernel_size, 1), 0),
|
735 |
+
)
|
736 |
+
),
|
737 |
+
]
|
738 |
+
)
|
739 |
+
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
740 |
+
|
741 |
+
def forward(self, x):
|
742 |
+
fmap = []
|
743 |
+
|
744 |
+
# 1d to 2d
|
745 |
+
b, c, t = x.shape
|
746 |
+
if t % self.period != 0: # pad first
|
747 |
+
n_pad = self.period - (t % self.period)
|
748 |
+
x = F.pad(x, (0, n_pad), "reflect")
|
749 |
+
t = t + n_pad
|
750 |
+
x = x.view(b, c, t // self.period, self.period)
|
751 |
+
|
752 |
+
for l in self.convs:
|
753 |
+
x = l(x)
|
754 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
755 |
+
fmap.append(x)
|
756 |
+
x = self.conv_post(x)
|
757 |
+
fmap.append(x)
|
758 |
+
x = torch.flatten(x, 1, -1)
|
759 |
+
|
760 |
+
return x, fmap
|
infer_pack/models_onnx_moess.py
ADDED
@@ -0,0 +1,849 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math, pdb, os
|
2 |
+
from time import time as ttime
|
3 |
+
import torch
|
4 |
+
from torch import nn
|
5 |
+
from torch.nn import functional as F
|
6 |
+
from infer_pack import modules
|
7 |
+
from infer_pack import attentions
|
8 |
+
from infer_pack import commons
|
9 |
+
from infer_pack.commons import init_weights, get_padding
|
10 |
+
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
11 |
+
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
12 |
+
from infer_pack.commons import init_weights
|
13 |
+
import numpy as np
|
14 |
+
from infer_pack import commons
|
15 |
+
|
16 |
+
|
17 |
+
class TextEncoder256(nn.Module):
|
18 |
+
def __init__(
|
19 |
+
self,
|
20 |
+
out_channels,
|
21 |
+
hidden_channels,
|
22 |
+
filter_channels,
|
23 |
+
n_heads,
|
24 |
+
n_layers,
|
25 |
+
kernel_size,
|
26 |
+
p_dropout,
|
27 |
+
f0=True,
|
28 |
+
):
|
29 |
+
super().__init__()
|
30 |
+
self.out_channels = out_channels
|
31 |
+
self.hidden_channels = hidden_channels
|
32 |
+
self.filter_channels = filter_channels
|
33 |
+
self.n_heads = n_heads
|
34 |
+
self.n_layers = n_layers
|
35 |
+
self.kernel_size = kernel_size
|
36 |
+
self.p_dropout = p_dropout
|
37 |
+
self.emb_phone = nn.Linear(256, hidden_channels)
|
38 |
+
self.lrelu = nn.LeakyReLU(0.1, inplace=True)
|
39 |
+
if f0 == True:
|
40 |
+
self.emb_pitch = nn.Embedding(256, hidden_channels) # pitch 256
|
41 |
+
self.encoder = attentions.Encoder(
|
42 |
+
hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout
|
43 |
+
)
|
44 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
45 |
+
|
46 |
+
def forward(self, phone, pitch, lengths):
|
47 |
+
if pitch == None:
|
48 |
+
x = self.emb_phone(phone)
|
49 |
+
else:
|
50 |
+
x = self.emb_phone(phone) + self.emb_pitch(pitch)
|
51 |
+
x = x * math.sqrt(self.hidden_channels) # [b, t, h]
|
52 |
+
x = self.lrelu(x)
|
53 |
+
x = torch.transpose(x, 1, -1) # [b, h, t]
|
54 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(lengths, x.size(2)), 1).to(
|
55 |
+
x.dtype
|
56 |
+
)
|
57 |
+
x = self.encoder(x * x_mask, x_mask)
|
58 |
+
stats = self.proj(x) * x_mask
|
59 |
+
|
60 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
61 |
+
return m, logs, x_mask
|
62 |
+
|
63 |
+
|
64 |
+
class TextEncoder256Sim(nn.Module):
|
65 |
+
def __init__(
|
66 |
+
self,
|
67 |
+
out_channels,
|
68 |
+
hidden_channels,
|
69 |
+
filter_channels,
|
70 |
+
n_heads,
|
71 |
+
n_layers,
|
72 |
+
kernel_size,
|
73 |
+
p_dropout,
|
74 |
+
f0=True,
|
75 |
+
):
|
76 |
+
super().__init__()
|
77 |
+
self.out_channels = out_channels
|
78 |
+
self.hidden_channels = hidden_channels
|
79 |
+
self.filter_channels = filter_channels
|
80 |
+
self.n_heads = n_heads
|
81 |
+
self.n_layers = n_layers
|
82 |
+
self.kernel_size = kernel_size
|
83 |
+
self.p_dropout = p_dropout
|
84 |
+
self.emb_phone = nn.Linear(256, hidden_channels)
|
85 |
+
self.lrelu = nn.LeakyReLU(0.1, inplace=True)
|
86 |
+
if f0 == True:
|
87 |
+
self.emb_pitch = nn.Embedding(256, hidden_channels) # pitch 256
|
88 |
+
self.encoder = attentions.Encoder(
|
89 |
+
hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout
|
90 |
+
)
|
91 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
92 |
+
|
93 |
+
def forward(self, phone, pitch, lengths):
|
94 |
+
if pitch == None:
|
95 |
+
x = self.emb_phone(phone)
|
96 |
+
else:
|
97 |
+
x = self.emb_phone(phone) + self.emb_pitch(pitch)
|
98 |
+
x = x * math.sqrt(self.hidden_channels) # [b, t, h]
|
99 |
+
x = self.lrelu(x)
|
100 |
+
x = torch.transpose(x, 1, -1) # [b, h, t]
|
101 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(lengths, x.size(2)), 1).to(
|
102 |
+
x.dtype
|
103 |
+
)
|
104 |
+
x = self.encoder(x * x_mask, x_mask)
|
105 |
+
x = self.proj(x) * x_mask
|
106 |
+
return x, x_mask
|
107 |
+
|
108 |
+
|
109 |
+
class ResidualCouplingBlock(nn.Module):
|
110 |
+
def __init__(
|
111 |
+
self,
|
112 |
+
channels,
|
113 |
+
hidden_channels,
|
114 |
+
kernel_size,
|
115 |
+
dilation_rate,
|
116 |
+
n_layers,
|
117 |
+
n_flows=4,
|
118 |
+
gin_channels=0,
|
119 |
+
):
|
120 |
+
super().__init__()
|
121 |
+
self.channels = channels
|
122 |
+
self.hidden_channels = hidden_channels
|
123 |
+
self.kernel_size = kernel_size
|
124 |
+
self.dilation_rate = dilation_rate
|
125 |
+
self.n_layers = n_layers
|
126 |
+
self.n_flows = n_flows
|
127 |
+
self.gin_channels = gin_channels
|
128 |
+
|
129 |
+
self.flows = nn.ModuleList()
|
130 |
+
for i in range(n_flows):
|
131 |
+
self.flows.append(
|
132 |
+
modules.ResidualCouplingLayer(
|
133 |
+
channels,
|
134 |
+
hidden_channels,
|
135 |
+
kernel_size,
|
136 |
+
dilation_rate,
|
137 |
+
n_layers,
|
138 |
+
gin_channels=gin_channels,
|
139 |
+
mean_only=True,
|
140 |
+
)
|
141 |
+
)
|
142 |
+
self.flows.append(modules.Flip())
|
143 |
+
|
144 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
145 |
+
if not reverse:
|
146 |
+
for flow in self.flows:
|
147 |
+
x, _ = flow(x, x_mask, g=g, reverse=reverse)
|
148 |
+
else:
|
149 |
+
for flow in reversed(self.flows):
|
150 |
+
x = flow(x, x_mask, g=g, reverse=reverse)
|
151 |
+
return x
|
152 |
+
|
153 |
+
def remove_weight_norm(self):
|
154 |
+
for i in range(self.n_flows):
|
155 |
+
self.flows[i * 2].remove_weight_norm()
|
156 |
+
|
157 |
+
|
158 |
+
class PosteriorEncoder(nn.Module):
|
159 |
+
def __init__(
|
160 |
+
self,
|
161 |
+
in_channels,
|
162 |
+
out_channels,
|
163 |
+
hidden_channels,
|
164 |
+
kernel_size,
|
165 |
+
dilation_rate,
|
166 |
+
n_layers,
|
167 |
+
gin_channels=0,
|
168 |
+
):
|
169 |
+
super().__init__()
|
170 |
+
self.in_channels = in_channels
|
171 |
+
self.out_channels = out_channels
|
172 |
+
self.hidden_channels = hidden_channels
|
173 |
+
self.kernel_size = kernel_size
|
174 |
+
self.dilation_rate = dilation_rate
|
175 |
+
self.n_layers = n_layers
|
176 |
+
self.gin_channels = gin_channels
|
177 |
+
|
178 |
+
self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
179 |
+
self.enc = modules.WN(
|
180 |
+
hidden_channels,
|
181 |
+
kernel_size,
|
182 |
+
dilation_rate,
|
183 |
+
n_layers,
|
184 |
+
gin_channels=gin_channels,
|
185 |
+
)
|
186 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
187 |
+
|
188 |
+
def forward(self, x, x_lengths, g=None):
|
189 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(
|
190 |
+
x.dtype
|
191 |
+
)
|
192 |
+
x = self.pre(x) * x_mask
|
193 |
+
x = self.enc(x, x_mask, g=g)
|
194 |
+
stats = self.proj(x) * x_mask
|
195 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
196 |
+
z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
|
197 |
+
return z, m, logs, x_mask
|
198 |
+
|
199 |
+
def remove_weight_norm(self):
|
200 |
+
self.enc.remove_weight_norm()
|
201 |
+
|
202 |
+
|
203 |
+
class Generator(torch.nn.Module):
|
204 |
+
def __init__(
|
205 |
+
self,
|
206 |
+
initial_channel,
|
207 |
+
resblock,
|
208 |
+
resblock_kernel_sizes,
|
209 |
+
resblock_dilation_sizes,
|
210 |
+
upsample_rates,
|
211 |
+
upsample_initial_channel,
|
212 |
+
upsample_kernel_sizes,
|
213 |
+
gin_channels=0,
|
214 |
+
):
|
215 |
+
super(Generator, self).__init__()
|
216 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
217 |
+
self.num_upsamples = len(upsample_rates)
|
218 |
+
self.conv_pre = Conv1d(
|
219 |
+
initial_channel, upsample_initial_channel, 7, 1, padding=3
|
220 |
+
)
|
221 |
+
resblock = modules.ResBlock1 if resblock == "1" else modules.ResBlock2
|
222 |
+
|
223 |
+
self.ups = nn.ModuleList()
|
224 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
225 |
+
self.ups.append(
|
226 |
+
weight_norm(
|
227 |
+
ConvTranspose1d(
|
228 |
+
upsample_initial_channel // (2**i),
|
229 |
+
upsample_initial_channel // (2 ** (i + 1)),
|
230 |
+
k,
|
231 |
+
u,
|
232 |
+
padding=(k - u) // 2,
|
233 |
+
)
|
234 |
+
)
|
235 |
+
)
|
236 |
+
|
237 |
+
self.resblocks = nn.ModuleList()
|
238 |
+
for i in range(len(self.ups)):
|
239 |
+
ch = upsample_initial_channel // (2 ** (i + 1))
|
240 |
+
for j, (k, d) in enumerate(
|
241 |
+
zip(resblock_kernel_sizes, resblock_dilation_sizes)
|
242 |
+
):
|
243 |
+
self.resblocks.append(resblock(ch, k, d))
|
244 |
+
|
245 |
+
self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
|
246 |
+
self.ups.apply(init_weights)
|
247 |
+
|
248 |
+
if gin_channels != 0:
|
249 |
+
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
250 |
+
|
251 |
+
def forward(self, x, g=None):
|
252 |
+
x = self.conv_pre(x)
|
253 |
+
if g is not None:
|
254 |
+
x = x + self.cond(g)
|
255 |
+
|
256 |
+
for i in range(self.num_upsamples):
|
257 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
258 |
+
x = self.ups[i](x)
|
259 |
+
xs = None
|
260 |
+
for j in range(self.num_kernels):
|
261 |
+
if xs is None:
|
262 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
263 |
+
else:
|
264 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
265 |
+
x = xs / self.num_kernels
|
266 |
+
x = F.leaky_relu(x)
|
267 |
+
x = self.conv_post(x)
|
268 |
+
x = torch.tanh(x)
|
269 |
+
|
270 |
+
return x
|
271 |
+
|
272 |
+
def remove_weight_norm(self):
|
273 |
+
for l in self.ups:
|
274 |
+
remove_weight_norm(l)
|
275 |
+
for l in self.resblocks:
|
276 |
+
l.remove_weight_norm()
|
277 |
+
|
278 |
+
|
279 |
+
class SineGen(torch.nn.Module):
|
280 |
+
"""Definition of sine generator
|
281 |
+
SineGen(samp_rate, harmonic_num = 0,
|
282 |
+
sine_amp = 0.1, noise_std = 0.003,
|
283 |
+
voiced_threshold = 0,
|
284 |
+
flag_for_pulse=False)
|
285 |
+
samp_rate: sampling rate in Hz
|
286 |
+
harmonic_num: number of harmonic overtones (default 0)
|
287 |
+
sine_amp: amplitude of sine-wavefrom (default 0.1)
|
288 |
+
noise_std: std of Gaussian noise (default 0.003)
|
289 |
+
voiced_thoreshold: F0 threshold for U/V classification (default 0)
|
290 |
+
flag_for_pulse: this SinGen is used inside PulseGen (default False)
|
291 |
+
Note: when flag_for_pulse is True, the first time step of a voiced
|
292 |
+
segment is always sin(np.pi) or cos(0)
|
293 |
+
"""
|
294 |
+
|
295 |
+
def __init__(
|
296 |
+
self,
|
297 |
+
samp_rate,
|
298 |
+
harmonic_num=0,
|
299 |
+
sine_amp=0.1,
|
300 |
+
noise_std=0.003,
|
301 |
+
voiced_threshold=0,
|
302 |
+
flag_for_pulse=False,
|
303 |
+
):
|
304 |
+
super(SineGen, self).__init__()
|
305 |
+
self.sine_amp = sine_amp
|
306 |
+
self.noise_std = noise_std
|
307 |
+
self.harmonic_num = harmonic_num
|
308 |
+
self.dim = self.harmonic_num + 1
|
309 |
+
self.sampling_rate = samp_rate
|
310 |
+
self.voiced_threshold = voiced_threshold
|
311 |
+
|
312 |
+
def _f02uv(self, f0):
|
313 |
+
# generate uv signal
|
314 |
+
uv = torch.ones_like(f0)
|
315 |
+
uv = uv * (f0 > self.voiced_threshold)
|
316 |
+
return uv
|
317 |
+
|
318 |
+
def forward(self, f0, upp):
|
319 |
+
"""sine_tensor, uv = forward(f0)
|
320 |
+
input F0: tensor(batchsize=1, length, dim=1)
|
321 |
+
f0 for unvoiced steps should be 0
|
322 |
+
output sine_tensor: tensor(batchsize=1, length, dim)
|
323 |
+
output uv: tensor(batchsize=1, length, 1)
|
324 |
+
"""
|
325 |
+
with torch.no_grad():
|
326 |
+
f0 = f0[:, None].transpose(1, 2)
|
327 |
+
f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim, device=f0.device)
|
328 |
+
# fundamental component
|
329 |
+
f0_buf[:, :, 0] = f0[:, :, 0]
|
330 |
+
for idx in np.arange(self.harmonic_num):
|
331 |
+
f0_buf[:, :, idx + 1] = f0_buf[:, :, 0] * (
|
332 |
+
idx + 2
|
333 |
+
) # idx + 2: the (idx+1)-th overtone, (idx+2)-th harmonic
|
334 |
+
rad_values = (f0_buf / self.sampling_rate) % 1 ###%1意味着n_har的乘积无法后处理优化
|
335 |
+
rand_ini = torch.rand(
|
336 |
+
f0_buf.shape[0], f0_buf.shape[2], device=f0_buf.device
|
337 |
+
)
|
338 |
+
rand_ini[:, 0] = 0
|
339 |
+
rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
|
340 |
+
tmp_over_one = torch.cumsum(rad_values, 1) # % 1 #####%1意味着后面的cumsum无法再优化
|
341 |
+
tmp_over_one *= upp
|
342 |
+
tmp_over_one = F.interpolate(
|
343 |
+
tmp_over_one.transpose(2, 1),
|
344 |
+
scale_factor=upp,
|
345 |
+
mode="linear",
|
346 |
+
align_corners=True,
|
347 |
+
).transpose(2, 1)
|
348 |
+
rad_values = F.interpolate(
|
349 |
+
rad_values.transpose(2, 1), scale_factor=upp, mode="nearest"
|
350 |
+
).transpose(
|
351 |
+
2, 1
|
352 |
+
) #######
|
353 |
+
tmp_over_one %= 1
|
354 |
+
tmp_over_one_idx = (tmp_over_one[:, 1:, :] - tmp_over_one[:, :-1, :]) < 0
|
355 |
+
cumsum_shift = torch.zeros_like(rad_values)
|
356 |
+
cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
|
357 |
+
sine_waves = torch.sin(
|
358 |
+
torch.cumsum(rad_values + cumsum_shift, dim=1) * 2 * np.pi
|
359 |
+
)
|
360 |
+
sine_waves = sine_waves * self.sine_amp
|
361 |
+
uv = self._f02uv(f0)
|
362 |
+
uv = F.interpolate(
|
363 |
+
uv.transpose(2, 1), scale_factor=upp, mode="nearest"
|
364 |
+
).transpose(2, 1)
|
365 |
+
noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
|
366 |
+
noise = noise_amp * torch.randn_like(sine_waves)
|
367 |
+
sine_waves = sine_waves * uv + noise
|
368 |
+
return sine_waves, uv, noise
|
369 |
+
|
370 |
+
|
371 |
+
class SourceModuleHnNSF(torch.nn.Module):
|
372 |
+
"""SourceModule for hn-nsf
|
373 |
+
SourceModule(sampling_rate, harmonic_num=0, sine_amp=0.1,
|
374 |
+
add_noise_std=0.003, voiced_threshod=0)
|
375 |
+
sampling_rate: sampling_rate in Hz
|
376 |
+
harmonic_num: number of harmonic above F0 (default: 0)
|
377 |
+
sine_amp: amplitude of sine source signal (default: 0.1)
|
378 |
+
add_noise_std: std of additive Gaussian noise (default: 0.003)
|
379 |
+
note that amplitude of noise in unvoiced is decided
|
380 |
+
by sine_amp
|
381 |
+
voiced_threshold: threhold to set U/V given F0 (default: 0)
|
382 |
+
Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
|
383 |
+
F0_sampled (batchsize, length, 1)
|
384 |
+
Sine_source (batchsize, length, 1)
|
385 |
+
noise_source (batchsize, length 1)
|
386 |
+
uv (batchsize, length, 1)
|
387 |
+
"""
|
388 |
+
|
389 |
+
def __init__(
|
390 |
+
self,
|
391 |
+
sampling_rate,
|
392 |
+
harmonic_num=0,
|
393 |
+
sine_amp=0.1,
|
394 |
+
add_noise_std=0.003,
|
395 |
+
voiced_threshod=0,
|
396 |
+
is_half=True,
|
397 |
+
):
|
398 |
+
super(SourceModuleHnNSF, self).__init__()
|
399 |
+
|
400 |
+
self.sine_amp = sine_amp
|
401 |
+
self.noise_std = add_noise_std
|
402 |
+
self.is_half = is_half
|
403 |
+
# to produce sine waveforms
|
404 |
+
self.l_sin_gen = SineGen(
|
405 |
+
sampling_rate, harmonic_num, sine_amp, add_noise_std, voiced_threshod
|
406 |
+
)
|
407 |
+
|
408 |
+
# to merge source harmonics into a single excitation
|
409 |
+
self.l_linear = torch.nn.Linear(harmonic_num + 1, 1)
|
410 |
+
self.l_tanh = torch.nn.Tanh()
|
411 |
+
|
412 |
+
def forward(self, x, upp=None):
|
413 |
+
sine_wavs, uv, _ = self.l_sin_gen(x, upp)
|
414 |
+
if self.is_half:
|
415 |
+
sine_wavs = sine_wavs.half()
|
416 |
+
sine_merge = self.l_tanh(self.l_linear(sine_wavs))
|
417 |
+
return sine_merge, None, None # noise, uv
|
418 |
+
|
419 |
+
|
420 |
+
class GeneratorNSF(torch.nn.Module):
|
421 |
+
def __init__(
|
422 |
+
self,
|
423 |
+
initial_channel,
|
424 |
+
resblock,
|
425 |
+
resblock_kernel_sizes,
|
426 |
+
resblock_dilation_sizes,
|
427 |
+
upsample_rates,
|
428 |
+
upsample_initial_channel,
|
429 |
+
upsample_kernel_sizes,
|
430 |
+
gin_channels,
|
431 |
+
sr,
|
432 |
+
is_half=False,
|
433 |
+
):
|
434 |
+
super(GeneratorNSF, self).__init__()
|
435 |
+
self.num_kernels = len(resblock_kernel_sizes)
|
436 |
+
self.num_upsamples = len(upsample_rates)
|
437 |
+
|
438 |
+
self.f0_upsamp = torch.nn.Upsample(scale_factor=np.prod(upsample_rates))
|
439 |
+
self.m_source = SourceModuleHnNSF(
|
440 |
+
sampling_rate=sr, harmonic_num=0, is_half=is_half
|
441 |
+
)
|
442 |
+
self.noise_convs = nn.ModuleList()
|
443 |
+
self.conv_pre = Conv1d(
|
444 |
+
initial_channel, upsample_initial_channel, 7, 1, padding=3
|
445 |
+
)
|
446 |
+
resblock = modules.ResBlock1 if resblock == "1" else modules.ResBlock2
|
447 |
+
|
448 |
+
self.ups = nn.ModuleList()
|
449 |
+
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
|
450 |
+
c_cur = upsample_initial_channel // (2 ** (i + 1))
|
451 |
+
self.ups.append(
|
452 |
+
weight_norm(
|
453 |
+
ConvTranspose1d(
|
454 |
+
upsample_initial_channel // (2**i),
|
455 |
+
upsample_initial_channel // (2 ** (i + 1)),
|
456 |
+
k,
|
457 |
+
u,
|
458 |
+
padding=(k - u) // 2,
|
459 |
+
)
|
460 |
+
)
|
461 |
+
)
|
462 |
+
if i + 1 < len(upsample_rates):
|
463 |
+
stride_f0 = np.prod(upsample_rates[i + 1 :])
|
464 |
+
self.noise_convs.append(
|
465 |
+
Conv1d(
|
466 |
+
1,
|
467 |
+
c_cur,
|
468 |
+
kernel_size=stride_f0 * 2,
|
469 |
+
stride=stride_f0,
|
470 |
+
padding=stride_f0 // 2,
|
471 |
+
)
|
472 |
+
)
|
473 |
+
else:
|
474 |
+
self.noise_convs.append(Conv1d(1, c_cur, kernel_size=1))
|
475 |
+
|
476 |
+
self.resblocks = nn.ModuleList()
|
477 |
+
for i in range(len(self.ups)):
|
478 |
+
ch = upsample_initial_channel // (2 ** (i + 1))
|
479 |
+
for j, (k, d) in enumerate(
|
480 |
+
zip(resblock_kernel_sizes, resblock_dilation_sizes)
|
481 |
+
):
|
482 |
+
self.resblocks.append(resblock(ch, k, d))
|
483 |
+
|
484 |
+
self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
|
485 |
+
self.ups.apply(init_weights)
|
486 |
+
|
487 |
+
if gin_channels != 0:
|
488 |
+
self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
|
489 |
+
|
490 |
+
self.upp = np.prod(upsample_rates)
|
491 |
+
|
492 |
+
def forward(self, x, f0, g=None):
|
493 |
+
har_source, noi_source, uv = self.m_source(f0, self.upp)
|
494 |
+
har_source = har_source.transpose(1, 2)
|
495 |
+
x = self.conv_pre(x)
|
496 |
+
if g is not None:
|
497 |
+
x = x + self.cond(g)
|
498 |
+
|
499 |
+
for i in range(self.num_upsamples):
|
500 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
501 |
+
x = self.ups[i](x)
|
502 |
+
x_source = self.noise_convs[i](har_source)
|
503 |
+
x = x + x_source
|
504 |
+
xs = None
|
505 |
+
for j in range(self.num_kernels):
|
506 |
+
if xs is None:
|
507 |
+
xs = self.resblocks[i * self.num_kernels + j](x)
|
508 |
+
else:
|
509 |
+
xs += self.resblocks[i * self.num_kernels + j](x)
|
510 |
+
x = xs / self.num_kernels
|
511 |
+
x = F.leaky_relu(x)
|
512 |
+
x = self.conv_post(x)
|
513 |
+
x = torch.tanh(x)
|
514 |
+
return x
|
515 |
+
|
516 |
+
def remove_weight_norm(self):
|
517 |
+
for l in self.ups:
|
518 |
+
remove_weight_norm(l)
|
519 |
+
for l in self.resblocks:
|
520 |
+
l.remove_weight_norm()
|
521 |
+
|
522 |
+
|
523 |
+
sr2sr = {
|
524 |
+
"32k": 32000,
|
525 |
+
"40k": 40000,
|
526 |
+
"48k": 48000,
|
527 |
+
}
|
528 |
+
|
529 |
+
|
530 |
+
class SynthesizerTrnMs256NSFsidM(nn.Module):
|
531 |
+
def __init__(
|
532 |
+
self,
|
533 |
+
spec_channels,
|
534 |
+
segment_size,
|
535 |
+
inter_channels,
|
536 |
+
hidden_channels,
|
537 |
+
filter_channels,
|
538 |
+
n_heads,
|
539 |
+
n_layers,
|
540 |
+
kernel_size,
|
541 |
+
p_dropout,
|
542 |
+
resblock,
|
543 |
+
resblock_kernel_sizes,
|
544 |
+
resblock_dilation_sizes,
|
545 |
+
upsample_rates,
|
546 |
+
upsample_initial_channel,
|
547 |
+
upsample_kernel_sizes,
|
548 |
+
spk_embed_dim,
|
549 |
+
gin_channels,
|
550 |
+
sr,
|
551 |
+
**kwargs
|
552 |
+
):
|
553 |
+
super().__init__()
|
554 |
+
if type(sr) == type("strr"):
|
555 |
+
sr = sr2sr[sr]
|
556 |
+
self.spec_channels = spec_channels
|
557 |
+
self.inter_channels = inter_channels
|
558 |
+
self.hidden_channels = hidden_channels
|
559 |
+
self.filter_channels = filter_channels
|
560 |
+
self.n_heads = n_heads
|
561 |
+
self.n_layers = n_layers
|
562 |
+
self.kernel_size = kernel_size
|
563 |
+
self.p_dropout = p_dropout
|
564 |
+
self.resblock = resblock
|
565 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
566 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
567 |
+
self.upsample_rates = upsample_rates
|
568 |
+
self.upsample_initial_channel = upsample_initial_channel
|
569 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
570 |
+
self.segment_size = segment_size
|
571 |
+
self.gin_channels = gin_channels
|
572 |
+
# self.hop_length = hop_length#
|
573 |
+
self.spk_embed_dim = spk_embed_dim
|
574 |
+
self.enc_p = TextEncoder256(
|
575 |
+
inter_channels,
|
576 |
+
hidden_channels,
|
577 |
+
filter_channels,
|
578 |
+
n_heads,
|
579 |
+
n_layers,
|
580 |
+
kernel_size,
|
581 |
+
p_dropout,
|
582 |
+
)
|
583 |
+
self.dec = GeneratorNSF(
|
584 |
+
inter_channels,
|
585 |
+
resblock,
|
586 |
+
resblock_kernel_sizes,
|
587 |
+
resblock_dilation_sizes,
|
588 |
+
upsample_rates,
|
589 |
+
upsample_initial_channel,
|
590 |
+
upsample_kernel_sizes,
|
591 |
+
gin_channels=gin_channels,
|
592 |
+
sr=sr,
|
593 |
+
is_half=kwargs["is_half"],
|
594 |
+
)
|
595 |
+
self.enc_q = PosteriorEncoder(
|
596 |
+
spec_channels,
|
597 |
+
inter_channels,
|
598 |
+
hidden_channels,
|
599 |
+
5,
|
600 |
+
1,
|
601 |
+
16,
|
602 |
+
gin_channels=gin_channels,
|
603 |
+
)
|
604 |
+
self.flow = ResidualCouplingBlock(
|
605 |
+
inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels
|
606 |
+
)
|
607 |
+
self.emb_g = nn.Embedding(self.spk_embed_dim, gin_channels)
|
608 |
+
print("gin_channels:", gin_channels, "self.spk_embed_dim:", self.spk_embed_dim)
|
609 |
+
|
610 |
+
def remove_weight_norm(self):
|
611 |
+
self.dec.remove_weight_norm()
|
612 |
+
self.flow.remove_weight_norm()
|
613 |
+
self.enc_q.remove_weight_norm()
|
614 |
+
|
615 |
+
def forward(self, phone, phone_lengths, pitch, nsff0, sid, rnd, max_len=None):
|
616 |
+
g = self.emb_g(sid).unsqueeze(-1)
|
617 |
+
m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths)
|
618 |
+
z_p = (m_p + torch.exp(logs_p) * rnd) * x_mask
|
619 |
+
z = self.flow(z_p, x_mask, g=g, reverse=True)
|
620 |
+
o = self.dec((z * x_mask)[:, :, :max_len], nsff0, g=g)
|
621 |
+
return o
|
622 |
+
|
623 |
+
|
624 |
+
class SynthesizerTrnMs256NSFsid_sim(nn.Module):
|
625 |
+
"""
|
626 |
+
Synthesizer for Training
|
627 |
+
"""
|
628 |
+
|
629 |
+
def __init__(
|
630 |
+
self,
|
631 |
+
spec_channels,
|
632 |
+
segment_size,
|
633 |
+
inter_channels,
|
634 |
+
hidden_channels,
|
635 |
+
filter_channels,
|
636 |
+
n_heads,
|
637 |
+
n_layers,
|
638 |
+
kernel_size,
|
639 |
+
p_dropout,
|
640 |
+
resblock,
|
641 |
+
resblock_kernel_sizes,
|
642 |
+
resblock_dilation_sizes,
|
643 |
+
upsample_rates,
|
644 |
+
upsample_initial_channel,
|
645 |
+
upsample_kernel_sizes,
|
646 |
+
spk_embed_dim,
|
647 |
+
# hop_length,
|
648 |
+
gin_channels=0,
|
649 |
+
use_sdp=True,
|
650 |
+
**kwargs
|
651 |
+
):
|
652 |
+
super().__init__()
|
653 |
+
self.spec_channels = spec_channels
|
654 |
+
self.inter_channels = inter_channels
|
655 |
+
self.hidden_channels = hidden_channels
|
656 |
+
self.filter_channels = filter_channels
|
657 |
+
self.n_heads = n_heads
|
658 |
+
self.n_layers = n_layers
|
659 |
+
self.kernel_size = kernel_size
|
660 |
+
self.p_dropout = p_dropout
|
661 |
+
self.resblock = resblock
|
662 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
663 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
664 |
+
self.upsample_rates = upsample_rates
|
665 |
+
self.upsample_initial_channel = upsample_initial_channel
|
666 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
667 |
+
self.segment_size = segment_size
|
668 |
+
self.gin_channels = gin_channels
|
669 |
+
# self.hop_length = hop_length#
|
670 |
+
self.spk_embed_dim = spk_embed_dim
|
671 |
+
self.enc_p = TextEncoder256Sim(
|
672 |
+
inter_channels,
|
673 |
+
hidden_channels,
|
674 |
+
filter_channels,
|
675 |
+
n_heads,
|
676 |
+
n_layers,
|
677 |
+
kernel_size,
|
678 |
+
p_dropout,
|
679 |
+
)
|
680 |
+
self.dec = GeneratorNSF(
|
681 |
+
inter_channels,
|
682 |
+
resblock,
|
683 |
+
resblock_kernel_sizes,
|
684 |
+
resblock_dilation_sizes,
|
685 |
+
upsample_rates,
|
686 |
+
upsample_initial_channel,
|
687 |
+
upsample_kernel_sizes,
|
688 |
+
gin_channels=gin_channels,
|
689 |
+
is_half=kwargs["is_half"],
|
690 |
+
)
|
691 |
+
|
692 |
+
self.flow = ResidualCouplingBlock(
|
693 |
+
inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels
|
694 |
+
)
|
695 |
+
self.emb_g = nn.Embedding(self.spk_embed_dim, gin_channels)
|
696 |
+
print("gin_channels:", gin_channels, "self.spk_embed_dim:", self.spk_embed_dim)
|
697 |
+
|
698 |
+
def remove_weight_norm(self):
|
699 |
+
self.dec.remove_weight_norm()
|
700 |
+
self.flow.remove_weight_norm()
|
701 |
+
self.enc_q.remove_weight_norm()
|
702 |
+
|
703 |
+
def forward(
|
704 |
+
self, phone, phone_lengths, pitch, pitchf, ds, max_len=None
|
705 |
+
): # y是spec不需要了现在
|
706 |
+
g = self.emb_g(ds.unsqueeze(0)).unsqueeze(-1) # [b, 256, 1]##1是t,广播的
|
707 |
+
x, x_mask = self.enc_p(phone, pitch, phone_lengths)
|
708 |
+
x = self.flow(x, x_mask, g=g, reverse=True)
|
709 |
+
o = self.dec((x * x_mask)[:, :, :max_len], pitchf, g=g)
|
710 |
+
return o
|
711 |
+
|
712 |
+
|
713 |
+
class MultiPeriodDiscriminator(torch.nn.Module):
|
714 |
+
def __init__(self, use_spectral_norm=False):
|
715 |
+
super(MultiPeriodDiscriminator, self).__init__()
|
716 |
+
periods = [2, 3, 5, 7, 11, 17]
|
717 |
+
# periods = [3, 5, 7, 11, 17, 23, 37]
|
718 |
+
|
719 |
+
discs = [DiscriminatorS(use_spectral_norm=use_spectral_norm)]
|
720 |
+
discs = discs + [
|
721 |
+
DiscriminatorP(i, use_spectral_norm=use_spectral_norm) for i in periods
|
722 |
+
]
|
723 |
+
self.discriminators = nn.ModuleList(discs)
|
724 |
+
|
725 |
+
def forward(self, y, y_hat):
|
726 |
+
y_d_rs = [] #
|
727 |
+
y_d_gs = []
|
728 |
+
fmap_rs = []
|
729 |
+
fmap_gs = []
|
730 |
+
for i, d in enumerate(self.discriminators):
|
731 |
+
y_d_r, fmap_r = d(y)
|
732 |
+
y_d_g, fmap_g = d(y_hat)
|
733 |
+
# for j in range(len(fmap_r)):
|
734 |
+
# print(i,j,y.shape,y_hat.shape,fmap_r[j].shape,fmap_g[j].shape)
|
735 |
+
y_d_rs.append(y_d_r)
|
736 |
+
y_d_gs.append(y_d_g)
|
737 |
+
fmap_rs.append(fmap_r)
|
738 |
+
fmap_gs.append(fmap_g)
|
739 |
+
|
740 |
+
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
|
741 |
+
|
742 |
+
|
743 |
+
class DiscriminatorS(torch.nn.Module):
|
744 |
+
def __init__(self, use_spectral_norm=False):
|
745 |
+
super(DiscriminatorS, self).__init__()
|
746 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
747 |
+
self.convs = nn.ModuleList(
|
748 |
+
[
|
749 |
+
norm_f(Conv1d(1, 16, 15, 1, padding=7)),
|
750 |
+
norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
|
751 |
+
norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
|
752 |
+
norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
|
753 |
+
norm_f(Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
|
754 |
+
norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
|
755 |
+
]
|
756 |
+
)
|
757 |
+
self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
|
758 |
+
|
759 |
+
def forward(self, x):
|
760 |
+
fmap = []
|
761 |
+
|
762 |
+
for l in self.convs:
|
763 |
+
x = l(x)
|
764 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
765 |
+
fmap.append(x)
|
766 |
+
x = self.conv_post(x)
|
767 |
+
fmap.append(x)
|
768 |
+
x = torch.flatten(x, 1, -1)
|
769 |
+
|
770 |
+
return x, fmap
|
771 |
+
|
772 |
+
|
773 |
+
class DiscriminatorP(torch.nn.Module):
|
774 |
+
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
|
775 |
+
super(DiscriminatorP, self).__init__()
|
776 |
+
self.period = period
|
777 |
+
self.use_spectral_norm = use_spectral_norm
|
778 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
779 |
+
self.convs = nn.ModuleList(
|
780 |
+
[
|
781 |
+
norm_f(
|
782 |
+
Conv2d(
|
783 |
+
1,
|
784 |
+
32,
|
785 |
+
(kernel_size, 1),
|
786 |
+
(stride, 1),
|
787 |
+
padding=(get_padding(kernel_size, 1), 0),
|
788 |
+
)
|
789 |
+
),
|
790 |
+
norm_f(
|
791 |
+
Conv2d(
|
792 |
+
32,
|
793 |
+
128,
|
794 |
+
(kernel_size, 1),
|
795 |
+
(stride, 1),
|
796 |
+
padding=(get_padding(kernel_size, 1), 0),
|
797 |
+
)
|
798 |
+
),
|
799 |
+
norm_f(
|
800 |
+
Conv2d(
|
801 |
+
128,
|
802 |
+
512,
|
803 |
+
(kernel_size, 1),
|
804 |
+
(stride, 1),
|
805 |
+
padding=(get_padding(kernel_size, 1), 0),
|
806 |
+
)
|
807 |
+
),
|
808 |
+
norm_f(
|
809 |
+
Conv2d(
|
810 |
+
512,
|
811 |
+
1024,
|
812 |
+
(kernel_size, 1),
|
813 |
+
(stride, 1),
|
814 |
+
padding=(get_padding(kernel_size, 1), 0),
|
815 |
+
)
|
816 |
+
),
|
817 |
+
norm_f(
|
818 |
+
Conv2d(
|
819 |
+
1024,
|
820 |
+
1024,
|
821 |
+
(kernel_size, 1),
|
822 |
+
1,
|
823 |
+
padding=(get_padding(kernel_size, 1), 0),
|
824 |
+
)
|
825 |
+
),
|
826 |
+
]
|
827 |
+
)
|
828 |
+
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
829 |
+
|
830 |
+
def forward(self, x):
|
831 |
+
fmap = []
|
832 |
+
|
833 |
+
# 1d to 2d
|
834 |
+
b, c, t = x.shape
|
835 |
+
if t % self.period != 0: # pad first
|
836 |
+
n_pad = self.period - (t % self.period)
|
837 |
+
x = F.pad(x, (0, n_pad), "reflect")
|
838 |
+
t = t + n_pad
|
839 |
+
x = x.view(b, c, t // self.period, self.period)
|
840 |
+
|
841 |
+
for l in self.convs:
|
842 |
+
x = l(x)
|
843 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
844 |
+
fmap.append(x)
|
845 |
+
x = self.conv_post(x)
|
846 |
+
fmap.append(x)
|
847 |
+
x = torch.flatten(x, 1, -1)
|
848 |
+
|
849 |
+
return x, fmap
|
infer_pack/modules.py
ADDED
@@ -0,0 +1,522 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import copy
|
2 |
+
import math
|
3 |
+
import numpy as np
|
4 |
+
import scipy
|
5 |
+
import torch
|
6 |
+
from torch import nn
|
7 |
+
from torch.nn import functional as F
|
8 |
+
|
9 |
+
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
10 |
+
from torch.nn.utils import weight_norm, remove_weight_norm
|
11 |
+
|
12 |
+
from infer_pack import commons
|
13 |
+
from infer_pack.commons import init_weights, get_padding
|
14 |
+
from infer_pack.transforms import piecewise_rational_quadratic_transform
|
15 |
+
|
16 |
+
|
17 |
+
LRELU_SLOPE = 0.1
|
18 |
+
|
19 |
+
|
20 |
+
class LayerNorm(nn.Module):
|
21 |
+
def __init__(self, channels, eps=1e-5):
|
22 |
+
super().__init__()
|
23 |
+
self.channels = channels
|
24 |
+
self.eps = eps
|
25 |
+
|
26 |
+
self.gamma = nn.Parameter(torch.ones(channels))
|
27 |
+
self.beta = nn.Parameter(torch.zeros(channels))
|
28 |
+
|
29 |
+
def forward(self, x):
|
30 |
+
x = x.transpose(1, -1)
|
31 |
+
x = F.layer_norm(x, (self.channels,), self.gamma, self.beta, self.eps)
|
32 |
+
return x.transpose(1, -1)
|
33 |
+
|
34 |
+
|
35 |
+
class ConvReluNorm(nn.Module):
|
36 |
+
def __init__(
|
37 |
+
self,
|
38 |
+
in_channels,
|
39 |
+
hidden_channels,
|
40 |
+
out_channels,
|
41 |
+
kernel_size,
|
42 |
+
n_layers,
|
43 |
+
p_dropout,
|
44 |
+
):
|
45 |
+
super().__init__()
|
46 |
+
self.in_channels = in_channels
|
47 |
+
self.hidden_channels = hidden_channels
|
48 |
+
self.out_channels = out_channels
|
49 |
+
self.kernel_size = kernel_size
|
50 |
+
self.n_layers = n_layers
|
51 |
+
self.p_dropout = p_dropout
|
52 |
+
assert n_layers > 1, "Number of layers should be larger than 0."
|
53 |
+
|
54 |
+
self.conv_layers = nn.ModuleList()
|
55 |
+
self.norm_layers = nn.ModuleList()
|
56 |
+
self.conv_layers.append(
|
57 |
+
nn.Conv1d(
|
58 |
+
in_channels, hidden_channels, kernel_size, padding=kernel_size // 2
|
59 |
+
)
|
60 |
+
)
|
61 |
+
self.norm_layers.append(LayerNorm(hidden_channels))
|
62 |
+
self.relu_drop = nn.Sequential(nn.ReLU(), nn.Dropout(p_dropout))
|
63 |
+
for _ in range(n_layers - 1):
|
64 |
+
self.conv_layers.append(
|
65 |
+
nn.Conv1d(
|
66 |
+
hidden_channels,
|
67 |
+
hidden_channels,
|
68 |
+
kernel_size,
|
69 |
+
padding=kernel_size // 2,
|
70 |
+
)
|
71 |
+
)
|
72 |
+
self.norm_layers.append(LayerNorm(hidden_channels))
|
73 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
74 |
+
self.proj.weight.data.zero_()
|
75 |
+
self.proj.bias.data.zero_()
|
76 |
+
|
77 |
+
def forward(self, x, x_mask):
|
78 |
+
x_org = x
|
79 |
+
for i in range(self.n_layers):
|
80 |
+
x = self.conv_layers[i](x * x_mask)
|
81 |
+
x = self.norm_layers[i](x)
|
82 |
+
x = self.relu_drop(x)
|
83 |
+
x = x_org + self.proj(x)
|
84 |
+
return x * x_mask
|
85 |
+
|
86 |
+
|
87 |
+
class DDSConv(nn.Module):
|
88 |
+
"""
|
89 |
+
Dialted and Depth-Separable Convolution
|
90 |
+
"""
|
91 |
+
|
92 |
+
def __init__(self, channels, kernel_size, n_layers, p_dropout=0.0):
|
93 |
+
super().__init__()
|
94 |
+
self.channels = channels
|
95 |
+
self.kernel_size = kernel_size
|
96 |
+
self.n_layers = n_layers
|
97 |
+
self.p_dropout = p_dropout
|
98 |
+
|
99 |
+
self.drop = nn.Dropout(p_dropout)
|
100 |
+
self.convs_sep = nn.ModuleList()
|
101 |
+
self.convs_1x1 = nn.ModuleList()
|
102 |
+
self.norms_1 = nn.ModuleList()
|
103 |
+
self.norms_2 = nn.ModuleList()
|
104 |
+
for i in range(n_layers):
|
105 |
+
dilation = kernel_size**i
|
106 |
+
padding = (kernel_size * dilation - dilation) // 2
|
107 |
+
self.convs_sep.append(
|
108 |
+
nn.Conv1d(
|
109 |
+
channels,
|
110 |
+
channels,
|
111 |
+
kernel_size,
|
112 |
+
groups=channels,
|
113 |
+
dilation=dilation,
|
114 |
+
padding=padding,
|
115 |
+
)
|
116 |
+
)
|
117 |
+
self.convs_1x1.append(nn.Conv1d(channels, channels, 1))
|
118 |
+
self.norms_1.append(LayerNorm(channels))
|
119 |
+
self.norms_2.append(LayerNorm(channels))
|
120 |
+
|
121 |
+
def forward(self, x, x_mask, g=None):
|
122 |
+
if g is not None:
|
123 |
+
x = x + g
|
124 |
+
for i in range(self.n_layers):
|
125 |
+
y = self.convs_sep[i](x * x_mask)
|
126 |
+
y = self.norms_1[i](y)
|
127 |
+
y = F.gelu(y)
|
128 |
+
y = self.convs_1x1[i](y)
|
129 |
+
y = self.norms_2[i](y)
|
130 |
+
y = F.gelu(y)
|
131 |
+
y = self.drop(y)
|
132 |
+
x = x + y
|
133 |
+
return x * x_mask
|
134 |
+
|
135 |
+
|
136 |
+
class WN(torch.nn.Module):
|
137 |
+
def __init__(
|
138 |
+
self,
|
139 |
+
hidden_channels,
|
140 |
+
kernel_size,
|
141 |
+
dilation_rate,
|
142 |
+
n_layers,
|
143 |
+
gin_channels=0,
|
144 |
+
p_dropout=0,
|
145 |
+
):
|
146 |
+
super(WN, self).__init__()
|
147 |
+
assert kernel_size % 2 == 1
|
148 |
+
self.hidden_channels = hidden_channels
|
149 |
+
self.kernel_size = (kernel_size,)
|
150 |
+
self.dilation_rate = dilation_rate
|
151 |
+
self.n_layers = n_layers
|
152 |
+
self.gin_channels = gin_channels
|
153 |
+
self.p_dropout = p_dropout
|
154 |
+
|
155 |
+
self.in_layers = torch.nn.ModuleList()
|
156 |
+
self.res_skip_layers = torch.nn.ModuleList()
|
157 |
+
self.drop = nn.Dropout(p_dropout)
|
158 |
+
|
159 |
+
if gin_channels != 0:
|
160 |
+
cond_layer = torch.nn.Conv1d(
|
161 |
+
gin_channels, 2 * hidden_channels * n_layers, 1
|
162 |
+
)
|
163 |
+
self.cond_layer = torch.nn.utils.weight_norm(cond_layer, name="weight")
|
164 |
+
|
165 |
+
for i in range(n_layers):
|
166 |
+
dilation = dilation_rate**i
|
167 |
+
padding = int((kernel_size * dilation - dilation) / 2)
|
168 |
+
in_layer = torch.nn.Conv1d(
|
169 |
+
hidden_channels,
|
170 |
+
2 * hidden_channels,
|
171 |
+
kernel_size,
|
172 |
+
dilation=dilation,
|
173 |
+
padding=padding,
|
174 |
+
)
|
175 |
+
in_layer = torch.nn.utils.weight_norm(in_layer, name="weight")
|
176 |
+
self.in_layers.append(in_layer)
|
177 |
+
|
178 |
+
# last one is not necessary
|
179 |
+
if i < n_layers - 1:
|
180 |
+
res_skip_channels = 2 * hidden_channels
|
181 |
+
else:
|
182 |
+
res_skip_channels = hidden_channels
|
183 |
+
|
184 |
+
res_skip_layer = torch.nn.Conv1d(hidden_channels, res_skip_channels, 1)
|
185 |
+
res_skip_layer = torch.nn.utils.weight_norm(res_skip_layer, name="weight")
|
186 |
+
self.res_skip_layers.append(res_skip_layer)
|
187 |
+
|
188 |
+
def forward(self, x, x_mask, g=None, **kwargs):
|
189 |
+
output = torch.zeros_like(x)
|
190 |
+
n_channels_tensor = torch.IntTensor([self.hidden_channels])
|
191 |
+
|
192 |
+
if g is not None:
|
193 |
+
g = self.cond_layer(g)
|
194 |
+
|
195 |
+
for i in range(self.n_layers):
|
196 |
+
x_in = self.in_layers[i](x)
|
197 |
+
if g is not None:
|
198 |
+
cond_offset = i * 2 * self.hidden_channels
|
199 |
+
g_l = g[:, cond_offset : cond_offset + 2 * self.hidden_channels, :]
|
200 |
+
else:
|
201 |
+
g_l = torch.zeros_like(x_in)
|
202 |
+
|
203 |
+
acts = commons.fused_add_tanh_sigmoid_multiply(x_in, g_l, n_channels_tensor)
|
204 |
+
acts = self.drop(acts)
|
205 |
+
|
206 |
+
res_skip_acts = self.res_skip_layers[i](acts)
|
207 |
+
if i < self.n_layers - 1:
|
208 |
+
res_acts = res_skip_acts[:, : self.hidden_channels, :]
|
209 |
+
x = (x + res_acts) * x_mask
|
210 |
+
output = output + res_skip_acts[:, self.hidden_channels :, :]
|
211 |
+
else:
|
212 |
+
output = output + res_skip_acts
|
213 |
+
return output * x_mask
|
214 |
+
|
215 |
+
def remove_weight_norm(self):
|
216 |
+
if self.gin_channels != 0:
|
217 |
+
torch.nn.utils.remove_weight_norm(self.cond_layer)
|
218 |
+
for l in self.in_layers:
|
219 |
+
torch.nn.utils.remove_weight_norm(l)
|
220 |
+
for l in self.res_skip_layers:
|
221 |
+
torch.nn.utils.remove_weight_norm(l)
|
222 |
+
|
223 |
+
|
224 |
+
class ResBlock1(torch.nn.Module):
|
225 |
+
def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5)):
|
226 |
+
super(ResBlock1, self).__init__()
|
227 |
+
self.convs1 = nn.ModuleList(
|
228 |
+
[
|
229 |
+
weight_norm(
|
230 |
+
Conv1d(
|
231 |
+
channels,
|
232 |
+
channels,
|
233 |
+
kernel_size,
|
234 |
+
1,
|
235 |
+
dilation=dilation[0],
|
236 |
+
padding=get_padding(kernel_size, dilation[0]),
|
237 |
+
)
|
238 |
+
),
|
239 |
+
weight_norm(
|
240 |
+
Conv1d(
|
241 |
+
channels,
|
242 |
+
channels,
|
243 |
+
kernel_size,
|
244 |
+
1,
|
245 |
+
dilation=dilation[1],
|
246 |
+
padding=get_padding(kernel_size, dilation[1]),
|
247 |
+
)
|
248 |
+
),
|
249 |
+
weight_norm(
|
250 |
+
Conv1d(
|
251 |
+
channels,
|
252 |
+
channels,
|
253 |
+
kernel_size,
|
254 |
+
1,
|
255 |
+
dilation=dilation[2],
|
256 |
+
padding=get_padding(kernel_size, dilation[2]),
|
257 |
+
)
|
258 |
+
),
|
259 |
+
]
|
260 |
+
)
|
261 |
+
self.convs1.apply(init_weights)
|
262 |
+
|
263 |
+
self.convs2 = nn.ModuleList(
|
264 |
+
[
|
265 |
+
weight_norm(
|
266 |
+
Conv1d(
|
267 |
+
channels,
|
268 |
+
channels,
|
269 |
+
kernel_size,
|
270 |
+
1,
|
271 |
+
dilation=1,
|
272 |
+
padding=get_padding(kernel_size, 1),
|
273 |
+
)
|
274 |
+
),
|
275 |
+
weight_norm(
|
276 |
+
Conv1d(
|
277 |
+
channels,
|
278 |
+
channels,
|
279 |
+
kernel_size,
|
280 |
+
1,
|
281 |
+
dilation=1,
|
282 |
+
padding=get_padding(kernel_size, 1),
|
283 |
+
)
|
284 |
+
),
|
285 |
+
weight_norm(
|
286 |
+
Conv1d(
|
287 |
+
channels,
|
288 |
+
channels,
|
289 |
+
kernel_size,
|
290 |
+
1,
|
291 |
+
dilation=1,
|
292 |
+
padding=get_padding(kernel_size, 1),
|
293 |
+
)
|
294 |
+
),
|
295 |
+
]
|
296 |
+
)
|
297 |
+
self.convs2.apply(init_weights)
|
298 |
+
|
299 |
+
def forward(self, x, x_mask=None):
|
300 |
+
for c1, c2 in zip(self.convs1, self.convs2):
|
301 |
+
xt = F.leaky_relu(x, LRELU_SLOPE)
|
302 |
+
if x_mask is not None:
|
303 |
+
xt = xt * x_mask
|
304 |
+
xt = c1(xt)
|
305 |
+
xt = F.leaky_relu(xt, LRELU_SLOPE)
|
306 |
+
if x_mask is not None:
|
307 |
+
xt = xt * x_mask
|
308 |
+
xt = c2(xt)
|
309 |
+
x = xt + x
|
310 |
+
if x_mask is not None:
|
311 |
+
x = x * x_mask
|
312 |
+
return x
|
313 |
+
|
314 |
+
def remove_weight_norm(self):
|
315 |
+
for l in self.convs1:
|
316 |
+
remove_weight_norm(l)
|
317 |
+
for l in self.convs2:
|
318 |
+
remove_weight_norm(l)
|
319 |
+
|
320 |
+
|
321 |
+
class ResBlock2(torch.nn.Module):
|
322 |
+
def __init__(self, channels, kernel_size=3, dilation=(1, 3)):
|
323 |
+
super(ResBlock2, self).__init__()
|
324 |
+
self.convs = nn.ModuleList(
|
325 |
+
[
|
326 |
+
weight_norm(
|
327 |
+
Conv1d(
|
328 |
+
channels,
|
329 |
+
channels,
|
330 |
+
kernel_size,
|
331 |
+
1,
|
332 |
+
dilation=dilation[0],
|
333 |
+
padding=get_padding(kernel_size, dilation[0]),
|
334 |
+
)
|
335 |
+
),
|
336 |
+
weight_norm(
|
337 |
+
Conv1d(
|
338 |
+
channels,
|
339 |
+
channels,
|
340 |
+
kernel_size,
|
341 |
+
1,
|
342 |
+
dilation=dilation[1],
|
343 |
+
padding=get_padding(kernel_size, dilation[1]),
|
344 |
+
)
|
345 |
+
),
|
346 |
+
]
|
347 |
+
)
|
348 |
+
self.convs.apply(init_weights)
|
349 |
+
|
350 |
+
def forward(self, x, x_mask=None):
|
351 |
+
for c in self.convs:
|
352 |
+
xt = F.leaky_relu(x, LRELU_SLOPE)
|
353 |
+
if x_mask is not None:
|
354 |
+
xt = xt * x_mask
|
355 |
+
xt = c(xt)
|
356 |
+
x = xt + x
|
357 |
+
if x_mask is not None:
|
358 |
+
x = x * x_mask
|
359 |
+
return x
|
360 |
+
|
361 |
+
def remove_weight_norm(self):
|
362 |
+
for l in self.convs:
|
363 |
+
remove_weight_norm(l)
|
364 |
+
|
365 |
+
|
366 |
+
class Log(nn.Module):
|
367 |
+
def forward(self, x, x_mask, reverse=False, **kwargs):
|
368 |
+
if not reverse:
|
369 |
+
y = torch.log(torch.clamp_min(x, 1e-5)) * x_mask
|
370 |
+
logdet = torch.sum(-y, [1, 2])
|
371 |
+
return y, logdet
|
372 |
+
else:
|
373 |
+
x = torch.exp(x) * x_mask
|
374 |
+
return x
|
375 |
+
|
376 |
+
|
377 |
+
class Flip(nn.Module):
|
378 |
+
def forward(self, x, *args, reverse=False, **kwargs):
|
379 |
+
x = torch.flip(x, [1])
|
380 |
+
if not reverse:
|
381 |
+
logdet = torch.zeros(x.size(0)).to(dtype=x.dtype, device=x.device)
|
382 |
+
return x, logdet
|
383 |
+
else:
|
384 |
+
return x
|
385 |
+
|
386 |
+
|
387 |
+
class ElementwiseAffine(nn.Module):
|
388 |
+
def __init__(self, channels):
|
389 |
+
super().__init__()
|
390 |
+
self.channels = channels
|
391 |
+
self.m = nn.Parameter(torch.zeros(channels, 1))
|
392 |
+
self.logs = nn.Parameter(torch.zeros(channels, 1))
|
393 |
+
|
394 |
+
def forward(self, x, x_mask, reverse=False, **kwargs):
|
395 |
+
if not reverse:
|
396 |
+
y = self.m + torch.exp(self.logs) * x
|
397 |
+
y = y * x_mask
|
398 |
+
logdet = torch.sum(self.logs * x_mask, [1, 2])
|
399 |
+
return y, logdet
|
400 |
+
else:
|
401 |
+
x = (x - self.m) * torch.exp(-self.logs) * x_mask
|
402 |
+
return x
|
403 |
+
|
404 |
+
|
405 |
+
class ResidualCouplingLayer(nn.Module):
|
406 |
+
def __init__(
|
407 |
+
self,
|
408 |
+
channels,
|
409 |
+
hidden_channels,
|
410 |
+
kernel_size,
|
411 |
+
dilation_rate,
|
412 |
+
n_layers,
|
413 |
+
p_dropout=0,
|
414 |
+
gin_channels=0,
|
415 |
+
mean_only=False,
|
416 |
+
):
|
417 |
+
assert channels % 2 == 0, "channels should be divisible by 2"
|
418 |
+
super().__init__()
|
419 |
+
self.channels = channels
|
420 |
+
self.hidden_channels = hidden_channels
|
421 |
+
self.kernel_size = kernel_size
|
422 |
+
self.dilation_rate = dilation_rate
|
423 |
+
self.n_layers = n_layers
|
424 |
+
self.half_channels = channels // 2
|
425 |
+
self.mean_only = mean_only
|
426 |
+
|
427 |
+
self.pre = nn.Conv1d(self.half_channels, hidden_channels, 1)
|
428 |
+
self.enc = WN(
|
429 |
+
hidden_channels,
|
430 |
+
kernel_size,
|
431 |
+
dilation_rate,
|
432 |
+
n_layers,
|
433 |
+
p_dropout=p_dropout,
|
434 |
+
gin_channels=gin_channels,
|
435 |
+
)
|
436 |
+
self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1)
|
437 |
+
self.post.weight.data.zero_()
|
438 |
+
self.post.bias.data.zero_()
|
439 |
+
|
440 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
441 |
+
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
442 |
+
h = self.pre(x0) * x_mask
|
443 |
+
h = self.enc(h, x_mask, g=g)
|
444 |
+
stats = self.post(h) * x_mask
|
445 |
+
if not self.mean_only:
|
446 |
+
m, logs = torch.split(stats, [self.half_channels] * 2, 1)
|
447 |
+
else:
|
448 |
+
m = stats
|
449 |
+
logs = torch.zeros_like(m)
|
450 |
+
|
451 |
+
if not reverse:
|
452 |
+
x1 = m + x1 * torch.exp(logs) * x_mask
|
453 |
+
x = torch.cat([x0, x1], 1)
|
454 |
+
logdet = torch.sum(logs, [1, 2])
|
455 |
+
return x, logdet
|
456 |
+
else:
|
457 |
+
x1 = (x1 - m) * torch.exp(-logs) * x_mask
|
458 |
+
x = torch.cat([x0, x1], 1)
|
459 |
+
return x
|
460 |
+
|
461 |
+
def remove_weight_norm(self):
|
462 |
+
self.enc.remove_weight_norm()
|
463 |
+
|
464 |
+
|
465 |
+
class ConvFlow(nn.Module):
|
466 |
+
def __init__(
|
467 |
+
self,
|
468 |
+
in_channels,
|
469 |
+
filter_channels,
|
470 |
+
kernel_size,
|
471 |
+
n_layers,
|
472 |
+
num_bins=10,
|
473 |
+
tail_bound=5.0,
|
474 |
+
):
|
475 |
+
super().__init__()
|
476 |
+
self.in_channels = in_channels
|
477 |
+
self.filter_channels = filter_channels
|
478 |
+
self.kernel_size = kernel_size
|
479 |
+
self.n_layers = n_layers
|
480 |
+
self.num_bins = num_bins
|
481 |
+
self.tail_bound = tail_bound
|
482 |
+
self.half_channels = in_channels // 2
|
483 |
+
|
484 |
+
self.pre = nn.Conv1d(self.half_channels, filter_channels, 1)
|
485 |
+
self.convs = DDSConv(filter_channels, kernel_size, n_layers, p_dropout=0.0)
|
486 |
+
self.proj = nn.Conv1d(
|
487 |
+
filter_channels, self.half_channels * (num_bins * 3 - 1), 1
|
488 |
+
)
|
489 |
+
self.proj.weight.data.zero_()
|
490 |
+
self.proj.bias.data.zero_()
|
491 |
+
|
492 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
493 |
+
x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
|
494 |
+
h = self.pre(x0)
|
495 |
+
h = self.convs(h, x_mask, g=g)
|
496 |
+
h = self.proj(h) * x_mask
|
497 |
+
|
498 |
+
b, c, t = x0.shape
|
499 |
+
h = h.reshape(b, c, -1, t).permute(0, 1, 3, 2) # [b, cx?, t] -> [b, c, t, ?]
|
500 |
+
|
501 |
+
unnormalized_widths = h[..., : self.num_bins] / math.sqrt(self.filter_channels)
|
502 |
+
unnormalized_heights = h[..., self.num_bins : 2 * self.num_bins] / math.sqrt(
|
503 |
+
self.filter_channels
|
504 |
+
)
|
505 |
+
unnormalized_derivatives = h[..., 2 * self.num_bins :]
|
506 |
+
|
507 |
+
x1, logabsdet = piecewise_rational_quadratic_transform(
|
508 |
+
x1,
|
509 |
+
unnormalized_widths,
|
510 |
+
unnormalized_heights,
|
511 |
+
unnormalized_derivatives,
|
512 |
+
inverse=reverse,
|
513 |
+
tails="linear",
|
514 |
+
tail_bound=self.tail_bound,
|
515 |
+
)
|
516 |
+
|
517 |
+
x = torch.cat([x0, x1], 1) * x_mask
|
518 |
+
logdet = torch.sum(logabsdet * x_mask, [1, 2])
|
519 |
+
if not reverse:
|
520 |
+
return x, logd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BIN_HEIGHT = 1e-3
|
9 |
+
DEFAULT_MIN_DERIVATIVE = 1e-3
|
10 |
+
|
11 |
+
|
12 |
+
def piecewise_rational_quadratic_transform(
|
13 |
+
inputs,
|
14 |
+
unnormalized_widths,
|
15 |
+
unnormalized_heights,
|
16 |
+
unnormalized_derivatives,
|
17 |
+
inverse=False,
|
18 |
+
tails=None,
|
19 |
+
tail_bound=1.0,
|
20 |
+
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
21 |
+
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
22 |
+
min_derivative=DEFAULT_MIN_DERIVATIVE,
|
23 |
+
):
|
24 |
+
if tails is None:
|
25 |
+
spline_fn = rational_quadratic_spline
|
26 |
+
spline_kwargs = {}
|
27 |
+
else:
|
28 |
+
spline_fn = unconstrained_rational_quadratic_spline
|
29 |
+
spline_kwargs = {"tails": tails, "tail_bound": tail_bound}
|
30 |
+
|
31 |
+
outputs, logabsdet = spline_fn(
|
32 |
+
inputs=inputs,
|
33 |
+
unnormalized_widths=unnormalized_widths,
|
34 |
+
unnormalized_heights=unnormalized_heights,
|
35 |
+
unnormalized_derivatives=unnormalized_derivatives,
|
36 |
+
inverse=inverse,
|
37 |
+
min_bin_width=min_bin_width,
|
38 |
+
min_bin_height=min_bin_height,
|
39 |
+
min_derivative=min_derivative,
|
40 |
+
**spline_kwargs
|
41 |
+
)
|
42 |
+
return outputs, logabsdet
|
43 |
+
|
44 |
+
|
45 |
+
def searchsorted(bin_locations, inputs, eps=1e-6):
|
46 |
+
bin_locations[..., -1] += eps
|
47 |
+
return torch.sum(inputs[..., None] >= bin_locations, dim=-1) - 1
|
48 |
+
|
49 |
+
|
50 |
+
def unconstrained_rational_quadratic_spline(
|
51 |
+
inputs,
|
52 |
+
unnormalized_widths,
|
53 |
+
unnormalized_heights,
|
54 |
+
unnormalized_derivatives,
|
55 |
+
inverse=False,
|
56 |
+
tails="linear",
|
57 |
+
tail_bound=1.0,
|
58 |
+
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
59 |
+
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
60 |
+
min_derivative=DEFAULT_MIN_DERIVATIVE,
|
61 |
+
):
|
62 |
+
inside_interval_mask = (inputs >= -tail_bound) & (inputs <= tail_bound)
|
63 |
+
outside_interval_mask = ~inside_interval_mask
|
64 |
+
|
65 |
+
outputs = torch.zeros_like(inputs)
|
66 |
+
logabsdet = torch.zeros_like(inputs)
|
67 |
+
|
68 |
+
if tails == "linear":
|
69 |
+
unnormalized_derivatives = F.pad(unnormalized_derivatives, pad=(1, 1))
|
70 |
+
constant = np.log(np.exp(1 - min_derivative) - 1)
|
71 |
+
unnormalized_derivatives[..., 0] = constant
|
72 |
+
unnormalized_derivatives[..., -1] = constant
|
73 |
+
|
74 |
+
outputs[outside_interval_mask] = inputs[outside_interval_mask]
|
75 |
+
logabsdet[outside_interval_mask] = 0
|
76 |
+
else:
|
77 |
+
raise RuntimeError("{} tails are not implemented.".format(tails))
|
78 |
+
|
79 |
+
(
|
80 |
+
outputs[inside_interval_mask],
|
81 |
+
logabsdet[inside_interval_mask],
|
82 |
+
) = rational_quadratic_spline(
|
83 |
+
inputs=inputs[inside_interval_mask],
|
84 |
+
unnormalized_widths=unnormalized_widths[inside_interval_mask, :],
|
85 |
+
unnormalized_heights=unnormalized_heights[inside_interval_mask, :],
|
86 |
+
unnormalized_derivatives=unnormalized_derivatives[inside_interval_mask, :],
|
87 |
+
inverse=inverse,
|
88 |
+
left=-tail_bound,
|
89 |
+
right=tail_bound,
|
90 |
+
bottom=-tail_bound,
|
91 |
+
top=tail_bound,
|
92 |
+
min_bin_width=min_bin_width,
|
93 |
+
min_bin_height=min_bin_height,
|
94 |
+
min_derivative=min_derivative,
|
95 |
+
)
|
96 |
+
|
97 |
+
return outputs, logabsdet
|
98 |
+
|
99 |
+
|
100 |
+
def rational_quadratic_spline(
|
101 |
+
inputs,
|
102 |
+
unnormalized_widths,
|
103 |
+
unnormalized_heights,
|
104 |
+
unnormalized_derivatives,
|
105 |
+
inverse=False,
|
106 |
+
left=0.0,
|
107 |
+
right=1.0,
|
108 |
+
bottom=0.0,
|
109 |
+
top=1.0,
|
110 |
+
min_bin_width=DEFAULT_MIN_BIN_WIDTH,
|
111 |
+
min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
|
112 |
+
min_derivative=DEFAULT_MIN_DERIVATIVE,
|
113 |
+
):
|
114 |
+
if torch.min(inputs) < left or torch.max(inputs) > right:
|
115 |
+
raise ValueError("Input to a transform is not within its domain")
|
116 |
+
|
117 |
+
num_bins = unnormalized_widths.shape[-1]
|
118 |
+
|
119 |
+
if min_bin_width * num_bins > 1.0:
|
120 |
+
raise ValueError("Minimal bin width too large for the number of bins")
|
121 |
+
if min_bin_height * num_bins > 1.0:
|
122 |
+
raise ValueError("Minimal bin height too large for the number of bins")
|
123 |
+
|
124 |
+
widths = F.softmax(unnormalized_widths, dim=-1)
|
125 |
+
widths = min_bin_width + (1 - min_bin_width * num_bins) * widths
|
126 |
+
cumwidths = torch.cumsum(widths, dim=-1)
|
127 |
+
cumwidths = F.pad(cumwidths, pad=(1, 0), mode="constant", value=0.0)
|
128 |
+
cumwidths = (right - left) * cumwidths + left
|
129 |
+
cumwidths[..., 0] = left
|
130 |
+
cumwidths[..., -1] = right
|
131 |
+
widths = cumwidths[..., 1:] - cumwidths[..., :-1]
|
132 |
+
|
133 |
+
derivatives = min_derivative + F.softplus(unnormalized_derivatives)
|
134 |
+
|
135 |
+
heights = F.softmax(unnormalized_heights, dim=-1)
|
136 |
+
heights = min_bin_height + (1 - min_bin_height * num_bins) * heights
|
137 |
+
cumheights = torch.cumsum(heights, dim=-1)
|
138 |
+
cumheights = F.pad(cumheights, pad=(1, 0), mode="constant", value=0.0)
|
139 |
+
cumheights = (top - bottom) * cumheights + bottom
|
140 |
+
cumheights[..., 0] = bottom
|
141 |
+
cumheights[..., -1] = top
|
142 |
+
heights = cumheights[..., 1:] - cumheights[..., :-1]
|
143 |
+
|
144 |
+
if inverse:
|
145 |
+
bin_idx = searchsorted(cumheights, inputs)[..., None]
|
146 |
+
else:
|
147 |
+
bin_idx = searchsorted(cumwidths, inputs)[..., None]
|
148 |
+
|
149 |
+
input_cumwidths = cumwidths.gather(-1, bin_idx)[..., 0]
|
150 |
+
input_bin_widths = widths.gather(-1, bin_idx)[..., 0]
|
151 |
+
|
152 |
+
input_cumheights = cumheights.gather(-1, bin_idx)[..., 0]
|
153 |
+
delta = heights / widths
|
154 |
+
input_delta = delta.gather(-1, bin_idx)[..., 0]
|
155 |
+
|
156 |
+
input_derivatives = derivatives.gather(-1, bin_idx)[..., 0]
|
157 |
+
input_derivatives_plus_one = derivatives[..., 1:].gather(-1, bin_idx)[..., 0]
|
158 |
+
|
159 |
+
input_heights = heights.gather(-1, bin_idx)[..., 0]
|
160 |
+
|
161 |
+
if inverse:
|
162 |
+
a = (inputs - input_cumheights) * (
|
163 |
+
input_derivatives + input_derivatives_plus_one - 2 * input_delta
|
164 |
+
) + input_heights * (input_delta - input_derivatives)
|
165 |
+
b = input_heights * input_derivatives - (inputs - input_cumheights) * (
|
166 |
+
input_derivatives + input_derivatives_plus_one - 2 * input_delta
|
167 |
+
)
|
168 |
+
c = -input_delta * (inputs - input_cumheights)
|
169 |
+
|
170 |
+
discriminant = b.pow(2) - 4 * a * c
|
171 |
+
assert (discriminant >= 0).all()
|
172 |
+
|
173 |
+
root = (2 * c) / (-b - torch.sqrt(discriminant))
|
174 |
+
outputs = root * input_bin_widths + input_cumwidths
|
175 |
+
|
176 |
+
theta_one_minus_theta = root * (1 - root)
|
177 |
+
denominator = input_delta + (
|
178 |
+
(input_derivatives + input_derivatives_plus_one - 2 * input_delta)
|
179 |
+
* theta_one_minus_theta
|
180 |
+
)
|
181 |
+
derivative_numerator = input_delta.pow(2) * (
|
182 |
+
input_derivatives_plus_one * root.pow(2)
|
183 |
+
+ 2 * input_delta * theta_one_minus_theta
|
184 |
+
+ input_derivatives * (1 - root).pow(2)
|
185 |
+
)
|
186 |
+
logabsdet = torch.log(derivative_numerator) - 2 * torch.log(denominator)
|
187 |
+
|
188 |
+
return outputs, -logabsdet
|
189 |
+
else:
|
190 |
+
theta = (inputs - input_cumwidths) / input_bin_widths
|
191 |
+
theta_one_minus_theta = theta * (1 - theta)
|
192 |
+
|
193 |
+
numerator = input_heights * (
|
194 |
+
input_delta * theta.pow(2) + input_derivatives * theta_one_minus_theta
|
195 |
+
)
|
196 |
+
denominator = input_delta + (
|
197 |
+
(input_derivatives + input_derivatives_plus_one - 2 * input_delta)
|
198 |
+
* theta_one_minus_theta
|
199 |
+
)
|
200 |
+
outputs = input_cumheights + numerator / denominator
|
201 |
+
|
202 |
+
derivative_numerator = input_delta.pow(2) * (
|
203 |
+
input_derivatives_plus_one * theta.pow(2)
|
204 |
+
+ 2 * input_delta * theta_one_minus_theta
|
205 |
+
+ input_derivatives * (1 - theta).pow(2)
|
206 |
+
)
|
207 |
+
logabsdet = torch.log(derivative_numerator) - 2 * torch.log(denominator)
|
208 |
+
|
209 |
+
return outputs, logabsdet
|
requirements-gcolab.txt
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numba==0.56.4
|
2 |
+
numpy==1.23.5
|
3 |
+
scipy==1.9.3
|
4 |
+
librosa==0.9.2
|
5 |
+
llvmlite==0.39.0
|
6 |
+
fairseq==0.12.2
|
7 |
+
faiss-cpu==1.7.0; sys_platform == "darwin"
|
8 |
+
faiss-cpu==1.7.2; sys_platform != "darwin"
|
9 |
+
gradio
|
10 |
+
Cython
|
11 |
+
future>=0.18.3
|
12 |
+
pydub>=0.25.1
|
13 |
+
soundfile>=0.12.1
|
14 |
+
ffmpeg-python>=0.2.0
|
15 |
+
tensorboardX
|
16 |
+
functorch>=2.0.0
|
17 |
+
Jinja2>=3.1.2
|
18 |
+
json5>=0.9.11
|
19 |
+
Markdown
|
20 |
+
matplotlib>=3.7.1
|
21 |
+
matplotlib-inline>=0.1.6
|
22 |
+
praat-parselmouth>=0.4.3
|
23 |
+
Pillow>=9.1.1
|
24 |
+
pyworld>=0.3.2
|
25 |
+
resampy>=0.4.2
|
26 |
+
scikit-learn>=1.2.2
|
27 |
+
starlette>=0.26.1
|
28 |
+
tensorboard
|
29 |
+
tensorboard-data-server
|
30 |
+
tensorboard-plugin-wit
|
31 |
+
torchgen>=0.0.1
|
32 |
+
tqdm>=4.65.0
|
33 |
+
tornado>=6.2
|
34 |
+
Werkzeug>=2.2.3
|
35 |
+
uc-micro-py>=1.0.1
|
36 |
+
sympy>=1.11.1
|
37 |
+
tabulate>=0.9.0
|
38 |
+
PyYAML>=6.0
|
39 |
+
pyasn1>=0.4.8
|
40 |
+
pyasn1-modules>=0.2.8
|
41 |
+
fsspec>=2023.3.0
|
42 |
+
absl-py>=1.4.0
|
43 |
+
audioread
|
44 |
+
uvicorn>=0.21.1
|
45 |
+
colorama>=0.4.6
|
46 |
+
edge-tts
|
47 |
+
demucs
|
48 |
+
yt_dlp
|
49 |
+
ffmpeg
|
requirements.txt
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numba==0.56.4
|
2 |
+
numpy==1.23.5
|
3 |
+
scipy==1.9.3
|
4 |
+
librosa==0.9.2
|
5 |
+
llvmlite==0.39.0
|
6 |
+
fairseq==0.12.2
|
7 |
+
faiss-cpu==1.7.0; sys_platform == "darwin"
|
8 |
+
faiss-cpu==1.7.2; sys_platform != "darwin"
|
9 |
+
gradio
|
10 |
+
Cython
|
11 |
+
future>=0.18.3
|
12 |
+
pydub>=0.25.1
|
13 |
+
soundfile>=0.12.1
|
14 |
+
ffmpeg-python>=0.2.0
|
15 |
+
tensorboardX
|
16 |
+
functorch>=2.0.0
|
17 |
+
Jinja2>=3.1.2
|
18 |
+
json5>=0.9.11
|
19 |
+
Markdown
|
20 |
+
matplotlib>=3.7.1
|
21 |
+
matplotlib-inline>=0.1.6
|
22 |
+
praat-parselmouth>=0.4.3
|
23 |
+
Pillow>=9.1.1
|
24 |
+
pyworld>=0.3.2
|
25 |
+
resampy>=0.4.2
|
26 |
+
scikit-learn>=1.2.2
|
27 |
+
starlette>=0.26.1
|
28 |
+
tensorboard
|
29 |
+
tensorboard-data-server
|
30 |
+
tensorboard-plugin-wit
|
31 |
+
torchgen>=0.0.1
|
32 |
+
tqdm>=4.65.0
|
33 |
+
tornado>=6.2
|
34 |
+
Werkzeug>=2.2.3
|
35 |
+
uc-micro-py>=1.0.1
|
36 |
+
sympy>=1.11.1
|
37 |
+
tabulate>=0.9.0
|
38 |
+
PyYAML>=6.0
|
39 |
+
pyasn1>=0.4.8
|
40 |
+
pyasn1-modules>=0.2.8
|
41 |
+
fsspec>=2023.3.0
|
42 |
+
absl-py>=1.4.0
|
43 |
+
audioread
|
44 |
+
uvicorn>=0.21.1
|
45 |
+
colorama>=0.4.6
|
46 |
+
edge-tts
|
vc_infer_pipeline.py
ADDED
@@ -0,0 +1,320 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np, parselmouth, torch, pdb
|
2 |
+
from time import time as ttime
|
3 |
+
import torch.nn.functional as F
|
4 |
+
import scipy.signal as signal
|
5 |
+
import pyworld, os, traceback, faiss
|
6 |
+
from scipy import signal
|
7 |
+
|
8 |
+
bh, ah = signal.butter(N=5, Wn=48, btype="high", fs=16000)
|
9 |
+
|
10 |
+
|
11 |
+
class VC(object):
|
12 |
+
def __init__(self, tgt_sr, config):
|
13 |
+
self.x_pad, self.x_query, self.x_center, self.x_max, self.is_half = (
|
14 |
+
config.x_pad,
|
15 |
+
config.x_query,
|
16 |
+
config.x_center,
|
17 |
+
config.x_max,
|
18 |
+
config.is_half,
|
19 |
+
)
|
20 |
+
self.sr = 16000 # hubert输入采样率
|
21 |
+
self.window = 160 # 每帧点数
|
22 |
+
self.t_pad = self.sr * self.x_pad # 每条前后pad时间
|
23 |
+
self.t_pad_tgt = tgt_sr * self.x_pad
|
24 |
+
self.t_pad2 = self.t_pad * 2
|
25 |
+
self.t_query = self.sr * self.x_query # 查询切点前后查询时间
|
26 |
+
self.t_center = self.sr * self.x_center # 查询切点位置
|
27 |
+
self.t_max = self.sr * self.x_max # 免查询时长阈值
|
28 |
+
self.device = config.device
|
29 |
+
|
30 |
+
def get_f0(self, x, p_len, f0_up_key, f0_method, inp_f0=None):
|
31 |
+
time_step = self.window / self.sr * 1000
|
32 |
+
f0_min = 50
|
33 |
+
f0_max = 1100
|
34 |
+
f0_mel_min = 1127 * np.log(1 + f0_min / 700)
|
35 |
+
f0_mel_max = 1127 * np.log(1 + f0_max / 700)
|
36 |
+
if f0_method == "pm":
|
37 |
+
f0 = (
|
38 |
+
parselmouth.Sound(x, self.sr)
|
39 |
+
.to_pitch_ac(
|
40 |
+
time_step=time_step / 1000,
|
41 |
+
voicing_threshold=0.6,
|
42 |
+
pitch_floor=f0_min,
|
43 |
+
pitch_ceiling=f0_max,
|
44 |
+
)
|
45 |
+
.selected_array["frequency"]
|
46 |
+
)
|
47 |
+
pad_size = (p_len - len(f0) + 1) // 2
|
48 |
+
if pad_size > 0 or p_len - len(f0) - pad_size > 0:
|
49 |
+
f0 = np.pad(
|
50 |
+
f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant"
|
51 |
+
)
|
52 |
+
elif f0_method == "harvest":
|
53 |
+
f0, t = pyworld.harvest(
|
54 |
+
x.astype(np.double),
|
55 |
+
fs=self.sr,
|
56 |
+
f0_ceil=f0_max,
|
57 |
+
f0_floor=f0_min,
|
58 |
+
frame_period=10,
|
59 |
+
)
|
60 |
+
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.sr)
|
61 |
+
f0 = signal.medfilt(f0, 3)
|
62 |
+
f0 *= pow(2, f0_up_key / 12)
|
63 |
+
# with open("test.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()]))
|
64 |
+
tf0 = self.sr // self.window # 每秒f0点数
|
65 |
+
if inp_f0 is not None:
|
66 |
+
delta_t = np.round(
|
67 |
+
(inp_f0[:, 0].max() - inp_f0[:, 0].min()) * tf0 + 1
|
68 |
+
).astype("int16")
|
69 |
+
replace_f0 = np.interp(
|
70 |
+
list(range(delta_t)), inp_f0[:, 0] * 100, inp_f0[:, 1]
|
71 |
+
)
|
72 |
+
shape = f0[self.x_pad * tf0 : self.x_pad * tf0 + len(replace_f0)].shape[0]
|
73 |
+
f0[self.x_pad * tf0 : self.x_pad * tf0 + len(replace_f0)] = replace_f0[
|
74 |
+
:shape
|
75 |
+
]
|
76 |
+
# with open("test_opt.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()]))
|
77 |
+
f0bak = f0.copy()
|
78 |
+
f0_mel = 1127 * np.log(1 + f0 / 700)
|
79 |
+
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * 254 / (
|
80 |
+
f0_mel_max - f0_mel_min
|
81 |
+
) + 1
|
82 |
+
f0_mel[f0_mel <= 1] = 1
|
83 |
+
f0_mel[f0_mel > 255] = 255
|
84 |
+
f0_coarse = np.rint(f0_mel).astype(np.int)
|
85 |
+
return f0_coarse, f0bak # 1-0
|
86 |
+
|
87 |
+
def vc(
|
88 |
+
self,
|
89 |
+
model,
|
90 |
+
net_g,
|
91 |
+
sid,
|
92 |
+
audio0,
|
93 |
+
pitch,
|
94 |
+
pitchf,
|
95 |
+
times,
|
96 |
+
index,
|
97 |
+
big_npy,
|
98 |
+
index_rate,
|
99 |
+
): # ,file_index,file_big_npy
|
100 |
+
feats = torch.from_numpy(audio0)
|
101 |
+
if self.is_half:
|
102 |
+
feats = feats.half()
|
103 |
+
else:
|
104 |
+
feats = feats.float()
|
105 |
+
if feats.dim() == 2: # double channels
|
106 |
+
feats = feats.mean(-1)
|
107 |
+
assert feats.dim() == 1, feats.dim()
|
108 |
+
feats = feats.view(1, -1)
|
109 |
+
padding_mask = torch.BoolTensor(feats.shape).to(self.device).fill_(False)
|
110 |
+
|
111 |
+
inputs = {
|
112 |
+
"source": feats.to(self.device),
|
113 |
+
"padding_mask": padding_mask,
|
114 |
+
"output_layer": 9, # layer 9
|
115 |
+
}
|
116 |
+
t0 = ttime()
|
117 |
+
with torch.no_grad():
|
118 |
+
logits = model.extract_features(**inputs)
|
119 |
+
feats = model.final_proj(logits[0])
|
120 |
+
|
121 |
+
if (
|
122 |
+
isinstance(index, type(None)) == False
|
123 |
+
and isinstance(big_npy, type(None)) == False
|
124 |
+
and index_rate != 0
|
125 |
+
):
|
126 |
+
npy = feats[0].cpu().numpy()
|
127 |
+
if self.is_half:
|
128 |
+
npy = npy.astype("float32")
|
129 |
+
|
130 |
+
# _, I = index.search(npy, 1)
|
131 |
+
# npy = big_npy[I.squeeze()]
|
132 |
+
|
133 |
+
score, ix = index.search(npy, k=8)
|
134 |
+
weight = np.square(1 / score)
|
135 |
+
weight /= weight.sum(axis=1, keepdims=True)
|
136 |
+
npy = np.sum(big_npy[ix] * np.expand_dims(weight, axis=2), axis=1)
|
137 |
+
|
138 |
+
if self.is_half:
|
139 |
+
npy = npy.astype("float16")
|
140 |
+
feats = (
|
141 |
+
torch.from_numpy(npy).unsqueeze(0).to(self.device) * index_rate
|
142 |
+
+ (1 - index_rate) * feats
|
143 |
+
)
|
144 |
+
|
145 |
+
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
|
146 |
+
t1 = ttime()
|
147 |
+
p_len = audio0.shape[0] // self.window
|
148 |
+
if feats.shape[1] < p_len:
|
149 |
+
p_len = feats.shape[1]
|
150 |
+
if pitch != None and pitchf != None:
|
151 |
+
pitch = pitch[:, :p_len]
|
152 |
+
pitchf = pitchf[:, :p_len]
|
153 |
+
p_len = torch.tensor([p_len], device=self.device).long()
|
154 |
+
with torch.no_grad():
|
155 |
+
if pitch != None and pitchf != None:
|
156 |
+
audio1 = (
|
157 |
+
(net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0] * 32768)
|
158 |
+
.data.cpu()
|
159 |
+
.float()
|
160 |
+
.numpy()
|
161 |
+
.astype(np.int16)
|
162 |
+
)
|
163 |
+
else:
|
164 |
+
audio1 = (
|
165 |
+
(net_g.infer(feats, p_len, sid)[0][0, 0] * 32768)
|
166 |
+
.data.cpu()
|
167 |
+
.float()
|
168 |
+
.numpy()
|
169 |
+
.astype(np.int16)
|
170 |
+
)
|
171 |
+
del feats, p_len, padding_mask
|
172 |
+
if torch.cuda.is_available():
|
173 |
+
torch.cuda.empty_cache()
|
174 |
+
t2 = ttime()
|
175 |
+
times[0] += t1 - t0
|
176 |
+
times[2] += t2 - t1
|
177 |
+
return audio1
|
178 |
+
|
179 |
+
def pipeline(
|
180 |
+
self,
|
181 |
+
model,
|
182 |
+
net_g,
|
183 |
+
sid,
|
184 |
+
audio,
|
185 |
+
times,
|
186 |
+
f0_up_key,
|
187 |
+
f0_method,
|
188 |
+
file_index,
|
189 |
+
# file_big_npy,
|
190 |
+
index_rate,
|
191 |
+
if_f0,
|
192 |
+
f0_file=None,
|
193 |
+
):
|
194 |
+
if (
|
195 |
+
file_index != ""
|
196 |
+
# and file_big_npy != ""
|
197 |
+
# and os.path.exists(file_big_npy) == True
|
198 |
+
and os.path.exists(file_index) == True
|
199 |
+
and index_rate != 0
|
200 |
+
):
|
201 |
+
try:
|
202 |
+
index = faiss.read_index(file_index)
|
203 |
+
# big_npy = np.load(file_big_npy)
|
204 |
+
big_npy = index.reconstruct_n(0, index.ntotal)
|
205 |
+
except:
|
206 |
+
traceback.print_exc()
|
207 |
+
index = big_npy = None
|
208 |
+
else:
|
209 |
+
index = big_npy = None
|
210 |
+
audio = signal.filtfilt(bh, ah, audio)
|
211 |
+
audio_pad = np.pad(audio, (self.window // 2, self.window // 2), mode="reflect")
|
212 |
+
opt_ts = []
|
213 |
+
if audio_pad.shape[0] > self.t_max:
|
214 |
+
audio_sum = np.zeros_like(audio)
|
215 |
+
for i in range(self.window):
|
216 |
+
audio_sum += audio_pad[i : i - self.window]
|
217 |
+
for t in range(self.t_center, audio.shape[0], self.t_center):
|
218 |
+
opt_ts.append(
|
219 |
+
t
|
220 |
+
- self.t_query
|
221 |
+
+ np.where(
|
222 |
+
np.abs(audio_sum[t - self.t_query : t + self.t_query])
|
223 |
+
== np.abs(audio_sum[t - self.t_query : t + self.t_query]).min()
|
224 |
+
)[0][0]
|
225 |
+
)
|
226 |
+
s = 0
|
227 |
+
audio_opt = []
|
228 |
+
t = None
|
229 |
+
t1 = ttime()
|
230 |
+
audio_pad = np.pad(audio, (self.t_pad, self.t_pad), mode="reflect")
|
231 |
+
p_len = audio_pad.shape[0] // self.window
|
232 |
+
inp_f0 = None
|
233 |
+
if hasattr(f0_file, "name") == True:
|
234 |
+
try:
|
235 |
+
with open(f0_file.name, "r") as f:
|
236 |
+
lines = f.read().strip("\n").split("\n")
|
237 |
+
inp_f0 = []
|
238 |
+
for line in lines:
|
239 |
+
inp_f0.append([float(i) for i in line.split(",")])
|
240 |
+
inp_f0 = np.array(inp_f0, dtype="float32")
|
241 |
+
except:
|
242 |
+
traceback.print_exc()
|
243 |
+
sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()
|
244 |
+
pitch, pitchf = None, None
|
245 |
+
if if_f0 == 1:
|
246 |
+
pitch, pitchf = self.get_f0(audio_pad, p_len, f0_up_key, f0_method, inp_f0)
|
247 |
+
pitch = pitch[:p_len]
|
248 |
+
pitchf = pitchf[:p_len]
|
249 |
+
pitch = torch.tensor(pitch, device=self.device).unsqueeze(0).long()
|
250 |
+
pitchf = torch.tensor(pitchf, device=self.device).unsqueeze(0).float()
|
251 |
+
t2 = ttime()
|
252 |
+
times[1] += t2 - t1
|
253 |
+
for t in opt_ts:
|
254 |
+
t = t // self.window * self.window
|
255 |
+
if if_f0 == 1:
|
256 |
+
audio_opt.append(
|
257 |
+
self.vc(
|
258 |
+
model,
|
259 |
+
net_g,
|
260 |
+
sid,
|
261 |
+
audio_pad[s : t + self.t_pad2 + self.window],
|
262 |
+
pitch[:, s // self.window : (t + self.t_pad2) // self.window],
|
263 |
+
pitchf[:, s // self.window : (t + self.t_pad2) // self.window],
|
264 |
+
times,
|
265 |
+
index,
|
266 |
+
big_npy,
|
267 |
+
index_rate,
|
268 |
+
)[self.t_pad_tgt : -self.t_pad_tgt]
|
269 |
+
)
|
270 |
+
else:
|
271 |
+
audio_opt.append(
|
272 |
+
self.vc(
|
273 |
+
model,
|
274 |
+
net_g,
|
275 |
+
sid,
|
276 |
+
audio_pad[s : t + self.t_pad2 + self.window],
|
277 |
+
None,
|
278 |
+
None,
|
279 |
+
times,
|
280 |
+
index,
|
281 |
+
big_npy,
|
282 |
+
index_rate,
|
283 |
+
)[self.t_pad_tgt : -self.t_pad_tgt]
|
284 |
+
)
|
285 |
+
s = t
|
286 |
+
if if_f0 == 1:
|
287 |
+
audio_opt.append(
|
288 |
+
self.vc(
|
289 |
+
model,
|
290 |
+
net_g,
|
291 |
+
sid,
|
292 |
+
audio_pad[t:],
|
293 |
+
pitch[:, t // self.window :] if t is not None else pitch,
|
294 |
+
pitchf[:, t // self.window :] if t is not None else pitchf,
|
295 |
+
times,
|
296 |
+
index,
|
297 |
+
big_npy,
|
298 |
+
index_rate,
|
299 |
+
)[self.t_pad_tgt : -self.t_pad_tgt]
|
300 |
+
)
|
301 |
+
else:
|
302 |
+
audio_opt.append(
|
303 |
+
self.vc(
|
304 |
+
model,
|
305 |
+
net_g,
|
306 |
+
sid,
|
307 |
+
audio_pad[t:],
|
308 |
+
None,
|
309 |
+
None,
|
310 |
+
times,
|
311 |
+
index,
|
312 |
+
big_npy,
|
313 |
+
index_rate,
|
314 |
+
)[self.t_pad_tgt : -self.t_pad_tgt]
|
315 |
+
)
|
316 |
+
audio_opt = np.concatenate(audio_opt)
|
317 |
+
del pitch, pitchf, sid
|
318 |
+
if torch.cuda.is_available():
|
319 |
+
torch.cuda.empty_cache()
|
320 |
+
return audio_opt
|
weights/nilou-jp/added_IVF218_Flat_nprobe_5.index
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:269e4975471b98f9f85d3bd5a6832d2e8496f5a54953a9e6018dbe62e7584fe9
|
3 |
+
size 9006403
|
weights/nilou-jp/cover.png
ADDED
weights/nilou-jp/nilou-jp.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e64fe080b45e1a2b539090f88eb1813fc7310239bdc5e3abe3250d28e383d4af
|
3 |
+
size 55027130
|