-
Notifications
You must be signed in to change notification settings - Fork 18
/
apply.sh
executable file
·145 lines (114 loc) · 4.46 KB
/
apply.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
#!/usr/bin/env bash
start=`date +%s`
THISDIR="$( cd "$( dirname "$0" )" && pwd )"
hn=$(hostname)
if [[ $hn == "puck4.cluster" ]]; then
CUDA_VISIBLE_DEVICES=1
fi;
# Check sox has been installed
sox_installed=$(sox --version)
sox_installed=${sox_installed:0:3}
if [[ "${sox_installed}" != "sox" ]]; then
echo "Sox can't be found. Please intall sox before running this script"
exit
fi;
# Check pyannote-audio has been installed
pyannote_installed=$(pyannote-audio --version)
pyannote_installed=${pyannote_installed:0:14}
if [[ "${pyannote_installed}" != "pyannote-audio" ]]; then
echo "pyannote-audio can't be found."
echo "Check that you activated your conda environment and than you installed pyannote-audio."
exit
fi;
if [ $# -ge 4 ]; then
echo "Wrong call. Must provide 2 arguments at most."
echo "Example :"
echo "./apply.sh /path/to/my/folder (--device=gpu) (--batch=128)"
exit
fi;
# Parsing arguments such as provided by the user
DEVICE="cpu"
BATCH="32"
for i in {2..3}; do
ARG=${!i}
if [ "${ARG%=*}" == "--device" ]; then
DEVICE=${ARG#*device=}
elif [ "${ARG%=*}" == "--batch" ]; then
BATCH=${ARG#*batch=}
fi
done
TARGET_PATH=$(echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")")
if [[ "${TARGET_PATH}" == *.wav ]]; then
# We want to apply the model on a single wav
EXT=""
else
# We want to apply the model on a folder containing wav files
EXT="/*.wav"
fi
if [[ "$(ls -A ${TARGET_PATH}$EXT)" ]]; then
bn=$(basename ${TARGET_PATH})
if [[ "${TARGET_PATH}" == *.wav ]]; then
bn=${bn/.wav/}
DB_PATH="$(dirname ${TARGET_PATH})/{uri}.wav"
else
DB_PATH="${TARGET_PATH}/{uri}.wav"
fi;
echo "Creating config for pyannote."
# Create pyannote_tmp_config containing all the necessary files
rm -rf $THISDIR/pyannote_tmp_config/$bn
mkdir -p $THISDIR/pyannote_tmp_config/$bn
# Create database.yml
echo "Databases:
${bn}_protocol: ${DB_PATH}
Protocols:
${bn}_protocol:
SpeakerDiarization:
All:
test:
annotated: $THISDIR/pyannote_tmp_config/$bn/$bn.uem" > $THISDIR/pyannote_tmp_config/$bn/database.yml
# Create .uem file
for audio in $(ls -A ${TARGET_PATH}$EXT); do
duration=$(soxi -D $audio)
sr=$(soxi -r $audio)
if [[ $sr != 16000 ]]; then
>&2 echo "WRONG SAMPLING RATE ENCOUNTERED"
>&2 echo "$audio has a sampling rate of $sr. Please convert your audio files to 16kHz before using the vtc."
exit
fi;
echo "$(basename ${audio/.wav/}) 1 0.0 $duration"
done > $THISDIR/pyannote_tmp_config/$bn/$bn.uem
echo "Done creating config for pyannote."
export PYANNOTE_DATABASE_CONFIG=$THISDIR/pyannote_tmp_config/$bn/database.yml
OUTPUT=output_voice_type_classifier/$bn/
mkdir -p output_voice_type_classifier/$bn/
# Commenting these 2 lines as grep can't be problematic on MAC distrib.
#BEST_EPOCH=$(cat model/train/X.SpeakerDiarization.BBT2_LeaveOneDomainOut_paido.train/validate_average_detection_fscore/X.SpeakerDiarization.BBT2_LeaveOneDomainOut_paido.development/params.yml | grep -oP "(?<=epoch: )\d+")
#BEST_EPOCH=$(printf %04d $BEST_EPOCH)
BEST_EPOCH=0100
VAL_DIR=$THISDIR/model/train/X.SpeakerDiarization.BBT2_LeaveOneDomainOut_paido.train/validate_average_detection_fscore/X.SpeakerDiarization.BBT2_LeaveOneDomainOut_paido.development
# Check current class is in classes (provided by the user or by default the KCHI CHI MAL FEM SPEECH)
pyannote-audio mlt apply --$DEVICE --batch=$BATCH --subset=test --parallel=8 $VAL_DIR ${bn}_protocol.SpeakerDiarization.All
if [ $? -ne 0 ]; then
echo "Something went wrong when applying the model"
echo "Aborting."
exit
fi
classes=(KCHI CHI MAL FEM SPEECH)
for class in ${classes[*]}; do
mv ${VAL_DIR}/apply/${BEST_EPOCH}/${bn}_protocol.SpeakerDiarization.All.test.$class.rttm $OUTPUT/$class.rttm
done
# Clean up
rm -rf ${VAL_DIR}/apply/apply/${BEST_EPOCH}/$bn
rm -f $OUTPUT/all.rttm
cat $OUTPUT/*.rttm > $OUTPUT/all.rttm
# Super powerful sorting bash command :D !
# Sort alphabetically to the second column, and numerically to the fourth one.
sort -b -k2,2 -k4,4n $OUTPUT/all.rttm > $OUTPUT/all.tmp.rttm
rm $OUTPUT/all.rttm
mv $OUTPUT/all.tmp.rttm $OUTPUT/all.rttm
else
echo "The folder you provided doesn't contain any wav files."
fi;
end=`date +%s`
runtime=$((end-start))
echo "Took $runtime sec on $bn."