-
Notifications
You must be signed in to change notification settings - Fork 133
/
splitDataByLength.sh
executable file
·109 lines (84 loc) · 2.73 KB
/
splitDataByLength.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
#!/bin/bash
# Copyright xmuspeech (Author:Snowdar 2019-04-25)
vad=true
outputdir= # If NULL, default $datadir/split*
force=false # If true, split again whatever
. subtools/parse_options.sh
. subtools/path.sh
set -e
if [[ $# != 2 ]];then
echo "[exit] Num of parameters is not equal to 2"
echo "usage:$0 [--outputdir ""] [--force false|true] <data-dir> <num-nj>"
exit 1
fi
data=$1
nj=$2
[ ! -d $data ] && echo "[exit] No such dir $data." && exit 1
for x in feats.scp;do
[ ! -f $data/$x ] && echo "[exit] expect $data/$x to exist." && exit 1
done
[ "$outputdir" == "" ] && outputdir=$data/split${nj}order
[ "$force" != "true" ] && [ -f "$outputdir/.done" ] && echo "[Note] Do not split $data again..." && exit 0
echo "Split $data with $nj nj according to length-order ..."
if [ "$vad" == "true" ];then
utt2num_frames=utt2num_frames.nosil
subtools/get_utt2num_frames_from_vad.sh --nosil true --nj $nj $data
else
utt2num_frames=utt2num_frames
subtools/get_utt2num_frames_from_feats.sh $data
fi
mkdir -p $outputdir
sort -r -n -k 2 $data/$utt2num_frames > $outputdir/$utt2num_frames.order
tot_num=$(wc -l $outputdir/$utt2num_frames.order | awk '{print $1}')
[[ "$tot_num" -lt "$nj" ]] && echo "nj $nj is too large for $tot_num utterances." && exit 1
num_frames=$(awk '{a=a+$2}END{print a}' $outputdir/$utt2num_frames.order )
average_frames=$num_frames
[ "$nj" != 1 ] && average_frames=$[$num_frames/$nj + 1]
echo -e "num_frames:$num_frames\naverage_frames:$average_frames"
for i in $(seq $nj);do
mkdir -p $outputdir/$i
> $outputdir/$i/$utt2num_frames.order
done
# split
awk -v nj=$nj -v mean=$average_frames -v dir=$outputdir -v name=$utt2num_frames '{a[NR]=$1;b[NR]=$2;}END{
num=1;
max=b[num]+1;
avoid_dead_cycle=1;
while(num<=NR){
out=0;
for(i=1;i<=nj;i++){
while(c[i]<max && c[i]+b[num]<mean){
c[i]=c[i]+b[num];
print a[num],b[num] >> dir"/"i"/"name".order"
out=out+1;
num=num+1;
if(num>NR){break;}
}
if(num>NR){break;}
if(c[i]>=max){max=c[i];}
}
for(i=nj;i>=1;i--){
while(c[i]<=max && c[i]<mean && NR-num+avoid_dead_cycle>=i){
c[i]=c[i]+b[num];
print a[num],b[num] >> dir"/"i"/"name".order"
out=out+1;
num=num+1;
if(num>NR){break;}
}
if(num>NR){break;}
if(c[i]>=max){max=c[i];}
}
if(out==0){
avoid_dead_cycle=avoid_dead_cycle+1;
}else if(avoid_dead_cycle>1){
avoid_dead_cycle=avoid_dead_cycle-1;
}
}
}' $outputdir/$utt2num_frames.order
# filter
for i in $(seq $nj);do
subtools/filterDataDir.sh --check false $data $outputdir/$i/$utt2num_frames.order $outputdir/$i/ >/dev/null
done
> $outputdir/.done # a mark file
rm -rf $outputdir/*/.backup
echo "Split done."