This repository was archived by the owner on Oct 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfb_01_split_train_test.sh
More file actions
executable file
·245 lines (223 loc) · 8.38 KB
/
Copy pathfb_01_split_train_test.sh
File metadata and controls
executable file
·245 lines (223 loc) · 8.38 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/bin/bash
#
# A script that creates the fileids and transcriptions files inside the etc/
# folder and also create symlinks of the audio dataset within the wav folder
#
# Grupo FalaBrasil (2018)
# Federal University of Pará (UFPA)
#
# Author: March 2018
# cassio batista - https://cassota.gitlab.io/
#
# Reference:
# https://cmusphinx.github.io/wiki/tutorialam/
TAG="FB_01"
NJ=6
SPLIT_RANDOM=false
TEST_DIR="lapsbm16k"
SKIP_DIRS="tedx|male-female|Anderson|lapsmail" # TIP: add "alcaim|constituicao" to speedup debugging
STAGE=0
SUBSETS_PREFIX=("train" "test") # TODO asset length==2
function print_fb_ascii() {
echo -e "\033[94m ____ \033[93m _____ _ \033[0m"
echo -e "\033[94m / ___| _ __ _ _ _ __ ___ \033[93m| ___|_ _| | __ _ \033[0m"
echo -e "\033[94m| | _ | '__| | | | '_ \ / _ \ \033[93m| |_ / _\` | |/ _\` | \033[0m"
echo -e "\033[94m| |_| \| | | |_| | |_) | (_) |\033[93m| _| (_| | | (_| | \033[0m"
echo -e "\033[94m \____||_| \__,_| .__/ \___/ \033[93m|_| \__,_|_|\__,_| \033[0m"
echo -e " \033[94m|_| \033[32m ____ _ _\033[0m\033[91m _ _ _____ ____ _ \033[0m"
echo -e " \033[32m| __ ) _ __ __ _ ___(_) |\033[0m\033[91m| | | | ___| _ \ / \ \033[0m"
echo -e " \033[32m| _ \| '_ / _\` / __| | |\033[0m\033[91m| | | | |_ | |_) |/ ∆ \ \033[0m"
echo -e " \033[32m| |_) | | | (_| \__ \ | |\033[0m\033[91m| |_| | _| | __// ___ \ \033[0m"
echo -e " \033[32m|____/|_| \__,_|___/_|_|\033[0m\033[91m \___/|_| |_| /_/ \_\ \033[0m"
echo -e " https://ufpafalabrasil.gitlab.io/"
echo
}
if test $# -ne 2 ; then
print_fb_ascii
echo "Usage: $0 <audio_dataset_dir> <sphinx_project_dir>"
echo " <audio_dataset_dir> is the folder that contains all your audio base (wav + transcript.)."
echo " <sphinx_project_dir> is the folder where you previously hosted your project."
echo " e.g.: ${HOME}/sphinx/MEUPROJETO"
exit 1
elif [ ! -d $1 ] || [ ! -d $2 ] ; then
echo "Error: both '$1' and '$2' must be dirs"
exit 1
fi
# https://askubuntu.com/questions/674333/how-to-pass-an-array-as-function-argument
# wait to background processes to finish (oct 17, 2019)
# args: <array_of_pids>
function wait_for_processes() {
PIDS=("$@")
echo -n " done: "
sleep 3
for pid in ${PIDS[@]} ; do
while [[ ! -z "$(ps --no-headers -p $pid)" ]] ; do
sleep 10
continue
done
echo -n "$pid "
done
echo
}
# merge split intermediate files into official outfile required by sphinx (oct 23, 2019)
# args: <subset_prefix> <sphinx_file_type> <out_file>
function merge() {
rm -f $3
for i in $(seq -f "%03g" 0 $((NJ-1))); do
filename="${1}_${i}.split.${2}.out"
cat $filename >> $3
done
}
# 0.) split train test (oct 10, 2019)
# args: <file_list> <sphinx_project_dir>
function split_dataset() {
while read line ; do
# define the ID speaker (same name of the folder)
# get the fullpath of audio and transcriptions files
spkr=$(basename $(dirname $line))
wav=$(readlink -f ${line}.wav)
txt=$(readlink -f ${line}.txt)
# define symlinks
mkdir -p ${2}/wav/${spkr}
ln -s $wav ${2}/wav/${spkr}
ln -s $txt ${2}/wav/${spkr}
done < $1
}
# 1.) create fileids (oct 12, 2019)
# args: <file_list> <sphinx_project_dir> <train_or_test>
# egs.:
# speaker_1/file_1
# speaker_2/file_2
function create_fileids() {
fout=$(echo $1 | sed 's/\.in/\.fileids.out/g')
rm -f $fout
while read line ; do
# define the ID speaker (same name of the folder)
# get the filename of wav audio file
spkr=$(basename $(dirname $line))
wav=$(basename $line)
echo "${spkr}/${wav}" >> $fout
done < $1
}
# 2.) create transcription files (oct 12, 2019)
# args: <file_list> <sphinx_project_dir> <train_or_test>
# egs.:
# <s> hello world </s> (file_1)
# <s> foo bar </s> (file_2)
function create_transcription() {
fout=$(echo $1 | sed 's/\.in/\.transcription.out/g')
rm -f $fout
while read line ; do
# get the filename of wav audio file
# get the fullpath of transcriptions files
wav=$(basename $line)
txt=$(readlink -f ${line}.txt) # FIXME readlink really necessary?
echo "<s> $(cat $txt) </s> ($wav)" >> $fout
done < $1
}
### main ###
if $SPLIT_RANDOM ; then
echo -en "\033[1m"
echo "[$TAG] shuffling dataset..."
echo -en "\033[0m"
find "$1" -name '*.wav' | sed 's/.wav//g' | sort -R > filelist.tmp
ntotal=$(wc -l filelist.tmp | awk '{print $1}')
ntest=$((ntotal/10)) # 10% test
ntrain=$((ntotal-ntest)) # 90% train
head -n $ntrain filelist.tmp > ${SUBSETS_PREFIX[0]}.list
tail -n $ntest filelist.tmp > ${SUBSETS_PREFIX[1]}.list
else
echo -en "\033[1m"
echo "[$TAG] splitting dataset, but using only '$TEST_DIR' for test"
echo "[$TAG] dirs to skip: '$SKIP_DIRS'"
echo -en "\033[0m"
find "${1}" -name '*.wav' |\
grep -vE "${TEST_DIR}|${SKIP_DIRS}" | sed 's/.wav//g' > ${SUBSETS_PREFIX[0]}.list
find "${1}/${TEST_DIR}" -name '*.wav' | sed 's/.wav//g' > ${SUBSETS_PREFIX[1]}.list
fi
# falabrasil requirement for speed up efficiency
# split list of files into multiple files of list of files for parallel
# processing with no bottleneck / queueing
if [ $STAGE -eq 0 ] ; then
echo -en "\033[1m"
echo "[$TAG] creating >$NJ< temp files for parallel processing jobs..."
echo -en "\033[0m"
split -de -a 3 -n l/${NJ} --additional-suffix '.split.in' ${SUBSETS_PREFIX[0]}.list "${SUBSETS_PREFIX[0]}_"
split -de -a 3 -n l/${NJ} --additional-suffix '.split.in' ${SUBSETS_PREFIX[1]}.list "${SUBSETS_PREFIX[1]}_"
STAGE=$((STAGE+1))
fi
# falabrasil requirement for precaution:
# creating symlinks for wav and txt files in order to avoid messing up with real
# data on its original directory
if [ $STAGE -eq 1 ] ; then
rm -rf ${2}/wav
for prefix in ${SUBSETS_PREFIX[@]} ; do
echo -en "\033[1m"
echo "[$TAG] creating symlinks for $prefix dataset..."
echo -en "\033[0m"
PIDS=()
echo -n " pids: "
for i in $(seq -f "%03g" 0 $((NJ-1))); do
filename="${prefix}_${i}.split.in"
(split_dataset $filename $2)&
PIDS+=($!)
echo -n "$! "
done
echo
wait_for_processes "${PIDS[@]}"
done
STAGE=$((STAGE+1))
fi
# cmu sphinx requirement:
# create .fileids files
if [ $STAGE -eq 2 ] ; then
for prefix in ${SUBSETS_PREFIX[@]} ; do
echo -en "\033[1m"
echo "[$TAG] creating .fileids file for $prefix dataset..."
echo -en "\033[0m"
PIDS=()
echo -n " pids: "
for i in $(seq -f "%03g" 0 $((NJ-1))); do
filename="${prefix}_${i}.split.in"
(create_fileids $filename $2 $prefix)&
PIDS+=($!)
echo -n "$! "
done
echo
wait_for_processes "${PIDS[@]}"
# merge files created in bg into .fileids output file
filename=${2}/etc/$(basename $2)_${prefix}.fileids
merge $prefix "fileids" $filename
done
STAGE=$((STAGE+1))
fi
# cmu sphinx requirement
# create .transcription files
if [ $STAGE -eq 3 ] ; then
for prefix in ${SUBSETS_PREFIX[@]} ; do
echo -en "\033[1m"
echo "[$TAG] creating .transcription file for $prefix dataset..."
echo -en "\033[0m"
PIDS=()
echo -n " pids: "
for i in $(seq -f "%03g" 0 $((NJ-1))); do
filename="${prefix}_${i}.split.in"
(create_transcription $filename $2 $prefix)&
PIDS+=($!)
echo -n "$! "
done
echo
wait_for_processes "${PIDS[@]}"
# merge files created in bg into .transcription output file
filename=${2}/etc/$(basename $2)_${prefix}.transcription
merge $prefix "transcription" $filename
done
STAGE=$((STAGE+1))
fi
rm -f *.list *.tmp *.in *.out
echo -en "\033[1m"
echo "[$TAG] done!"
echo -en "\033[0m"
(play -q "/usr/share/sounds/freedesktop/stereo/complete.oga")&
notify-send -i $(readlink -f doc/logo_fb_github_footer.png) -t 8000 \
"'$0' finished" "time to run <b>fb_02</b>. check out your project at '$1'"