-
Notifications
You must be signed in to change notification settings - Fork 3
/
export-checkpoints.sh
executable file
·146 lines (118 loc) · 4.83 KB
/
export-checkpoints.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env bash
set -eo pipefail
# Exports checkpoints to onnx voice files.
# Requires: ffmpeg jq
# Requires: https://github.com/daquexian/onnx-simplifier
if [ -z "$2" ]; then
echo 'Usage: export-checkpoints.sh <piper-checkpoints> <piper-voices>'
exit 1
fi
this_dir="$( cd "$( dirname "$0" )" && pwd )"
repo_dir="$(realpath "${this_dir}/../../")"
venv="${repo_dir}/src/python/.venv"
if [ -d "${venv}" ]; then
source "${venv}/bin/activate"
fi
# -----------------------------------------------------------------------------
piper_checkpoints="$1"
piper_voices="$2"
piper_binary="${repo_dir}/install/piper"
# -----------------------------------------------------------------------------
function has_dash {
echo "$1" | grep '[-]'
}
# language/language_COUNTRY/dataset/quality/<checkpoint>.ckpt
find "${piper_checkpoints}" -name '*.ckpt' | sort | \
while read -r checkpoint; do
voice_dir="$(dirname "${checkpoint}")";
echo "Processing ${voice_dir}"
model_card="${voice_dir}/MODEL_CARD"
if [ ! -s "${model_card}" ]; then
# MODEL_CARD must exist
echo "[ERROR] Missing ${model_card}" >&2;
continue;
fi
quality="$(basename "${voice_dir}")"
case $quality in
'x_low' | 'low' | 'medium' | 'high')
# Valid
;;
*)
echo "[ERROR] Invalid quality: ${quality}" >&2;
continue;
;;
esac
dataset_dir="$(dirname "${voice_dir}")";
dataset="$(basename "${dataset_dir}")"
language_dir="$(dirname "${dataset_dir}")";
language="$(basename "${language_dir}")"
language_family_dir="$(dirname "${language_dir}")";
language_family="$(basename "${language_family_dir}")"
# Dashes are used to separate fields, so they cannot be present in the
# parts.
if [ "$(has_dash "${quality}${dataset}${language}")" ]; then
echo "[ERROR] Dash found in voice: ${voice_dir}" >&2;
continue;
fi
voice_name="${language}-${dataset}-${quality}"
relative_dir="${language_family}/${language}/${dataset}/${quality}"
output_dir="${piper_voices}/${relative_dir}"
mkdir -p "${output_dir}"
onnx="${output_dir}/${voice_name}.onnx"
if [ ! -s "${output_dir}/MODEL_CARD" ]; then
cp "${voice_dir}/MODEL_CARD" "${output_dir}/"
fi
if [ ! -s "${onnx}.json" ]; then
cp "${voice_dir}/config.json" "${onnx}.json"
fi
if [ ! -s "${onnx}" ]; then
# Export to onnx and optimize
PYTHONPATH="${repo_dir}/src/python" \
python3 -m piper_train.export_onnx \
"${checkpoint}" \
"${onnx}.unoptimized";
# https://github.com/daquexian/onnx-simplifier
onnxsim "${onnx}.unoptimized" "${onnx}";
rm "${onnx}.unoptimized"
fi
done
# Generate samples
find "${piper_voices}" -name '*.onnx' | sort | \
while read -r onnx; do
voice_dir="$(dirname "${onnx}")";
echo "Generating samples for ${voice_dir}"
quality="$(basename "${voice_dir}")"
dataset_dir="$(dirname "${voice_dir}")";
dataset="$(basename "${dataset_dir}")"
language_dir="$(dirname "${dataset_dir}")";
language="$(basename "${language_dir}")"
language_family_dir="$(dirname "${language_dir}")";
language_family="$(basename "${language_family_dir}")"
test_sentences="${repo_dir}/etc/test_sentences/${language_family}.txt"
if [ ! -s "${test_sentences}" ]; then
echo "[ERROR] Missing ${test_sentences}" >&2;
continue;
fi
samples_dir="${voice_dir}/samples"
mkdir -p "${samples_dir}"
num_speakers="$(jq --raw-output '.num_speakers' "${onnx}.json")"
sample_rate="$(jq --raw-output '.audio.sample_rate' "${onnx}.json")"
last_speaker_id="$((num_speakers-1))"
# Generate a sample from the first test sentence for each speaker
for speaker_id in `seq 0 ${last_speaker_id}`; do
sample_mp3="${samples_dir}/speaker_${speaker_id}.mp3"
if [ -s "${sample_mp3}" ]; then
sample_mp3_size="$(stat --printf='%s' "${sample_mp3}")"
else
sample_mp3_size='0'
fi
if [ "${sample_mp3_size}" -lt 1000 ]; then
# Compress to MP3 with ffmpeg
head -n1 "${test_sentences}" | \
"${piper_binary}" --model "${onnx}" --speaker "${speaker_id}" --output_raw | \
ffmpeg -hide_banner -loglevel warning -y \
-sample_rate "${sample_rate}" -f s16le -ac 1 -i - \
-codec:a libmp3lame -qscale:a 2 "${sample_mp3}";
fi;
done
done