-
Notifications
You must be signed in to change notification settings - Fork 34
/
preprocess_dataset.sh
216 lines (182 loc) · 7.88 KB
/
preprocess_dataset.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
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
# Preprocess head images for training our head reenactment system.
#
# Usage:
# 1. Choose if you want to preprocess videos OR images. Preprocessing both at once isn't supported!
# 2. Lay out your media as e.g.
#
# `$DATASET_ROOT`/
# images/
# monalisa/
# painting.jpg
# id00017/
# 5MkXgwdrmJw/
# 001.jpg
# 002.jpg
# ...
#
# Or, if you want to preprocess videos,
#
# `$DATASET_ROOT`/
# videos/
# monalisa/
# X.avi
# id00017/
# 5MkXgwdrmJw/
# Y.avi
#
# 3. Edit this file: set `DATASET_ROOT` and `IDENTITIES` (read below how).
# 4. Also, set `DO_...` variables to `true` or `false`.
# Usually, you'll set `DO_CROP=true` and `DO_COMPUTE_SEGMENTATION=true`,
# and will set `DO_DECODE_VIDEOS` depending on whether you have video data.
# 5. Run `$ cd utils; bash preprocess_dataset.sh [FIRST_IDX [LAST_IDX]]` (see explanation below).
set -e
# Please use an ABSOLUTE path here!
DATASET_ROOT="/Vol1/dbstore/datasets/violet/VoxCeleb2_test_finetuning"
# echo "Unnamed: 0,path" > $dataset_dir/split.csv
# Initialize `IDENTITIES` -- the list of folders (paths relative to $DATASET_ROOT/images
# or $DATASET_ROOT/videos), each containing raw images or one video of some person.
cd "$DATASET_ROOT/images" # or e.g. `"$DATASET_ROOT/videos"`
IDENTITIES=(*) # or `(*/*)`, or whatever else
cd -
# Alternatively, you can specify them manually, e.g.:
# IDENTITIES=(
# "monalisa"
# "id00017/5MkXgwdrmJw"
# )
# Specify the range (segment) of identities to process. Useful for parallelizing.
FIRST_IDX=${1:-0}
LAST_IDX=${2:-999999999}
echo "Got ${#IDENTITIES[@]} folders, will process from ${FIRST_IDX}-th to ${LAST_IDX}-th"
# Switch off (set to `false` or comment out) unnecessary operations
DO_DECODE_VIDEOS=\
false
DO_CROP=\
true
DO_COMPUTE_SEGMENTATION=\
true
DO_COMPUTE_LANDMARKS=\
false
DO_COMPUTE_POSE_3DMM=\
false
DO_CROP_FFHQ=\
false
DO_COMPUTE_SEGMENTATION_FFHQ=\
false
################################# Extract frames from encoded videos ################################################
if [ "$DO_DECODE_VIDEOS" = true ]; then
i=0
for IDENTITY in "${IDENTITIES[@]}"; do
if (($i >= $FIRST_IDX && $i < $LAST_IDX)); then
IMAGES_OUTPUT="$DATASET_ROOT/images/$IDENTITY"
mkdir -p "$IMAGES_OUTPUT"
ffmpeg -hide_banner -i "$DATASET_ROOT/videos/${IDENTITY}"* -q:v 2 "$IMAGES_OUTPUT/%05d.jpg"
fi
let "i += 1"
done
fi
################################# Crop using only face detector ("latent pose style" crop) ############################
IMAGES_CROPPED_DIR_NAME="images-cropped"
KEYPOINTS_CROPPED_DIR_NAME="keypoints-cropped"
SEGMENTATION_DIR_NAME="segmentation-cropped"
POSE_3DMM_DIR_NAME="3dmm-descriptors"
if [ "$DO_CROP" = true ]; then
i=0
for IDENTITY in "${IDENTITIES[@]}"; do #$DATASET_ROOT/images/*; do
if (($i >= $FIRST_IDX && $i < $LAST_IDX)); then
# vid=$IDENTITY/$(ls -1 $IDENTITY | shuf -n1)
vid="$DATASET_ROOT/images/$IDENTITY"
filename="$(basename -- "$vid")"
filename="${filename%.*}"
dir="$(dirname "$vid")"
dir="$(basename "$dir")"
echo $i $dir $vid $filename
# echo "$i,SAIC-selfie-videos/$dir/$filename" >> $DATASET_ROOT/split.csv
mkdir -p "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/"
if [ "$DO_COMPUTE_LANDMARKS" = true ]; then
python3 crop_as_in_dataset.py --crop-style=latentpose --save-extra-data "$DATASET_ROOT/images/$IDENTITY/" "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/"
mkdir -p "$DATASET_ROOT/$KEYPOINTS_CROPPED_DIR_NAME/$IDENTITY/"
# Move '.npy' files to a separate folder
find "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/" -type f -name "*.npy" -exec mv {} "$DATASET_ROOT/$KEYPOINTS_CROPPED_DIR_NAME/$IDENTITY/" \;
else
python3 crop_as_in_dataset.py --crop-style=latentpose "$DATASET_ROOT/images/$IDENTITY/" "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/"
fi
fi
let "i += 1"
done;
fi
# Compute segmentation
if [ "$DO_COMPUTE_SEGMENTATION" = true ]; then
TMPFILE=$(mktemp)
i=0
for IDENTITY in "${IDENTITIES[@]}"; do
if (($i >= $FIRST_IDX && $i < $LAST_IDX)); then
for FILE in $(ls -1 "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/"); do
echo "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/$FILE" >> "$TMPFILE"
done
fi
let "i += 1"
done
SEGMENTATION_OUTPUT="$DATASET_ROOT/$SEGMENTATION_DIR_NAME"
cd Graphonomy
python3 exp/inference/inference_folder.py --images_path "$TMPFILE" --output_dir "$SEGMENTATION_OUTPUT" --common_prefix "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME" --model_path data/model/universal_trained.pth --tta 0.75,1.0,1.5,2.0
cd -
fi
# Compute 3DMM pose+expression vectors
if [ "$DO_COMPUTE_POSE_3DMM" = true ]; then
TMPFILE=$(mktemp)
i=0
for IDENTITY in "${IDENTITIES[@]}"; do
if (($i >= $FIRST_IDX && $i < $LAST_IDX)); then
for FILE in $(ls -1 "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/"); do
echo "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/$FILE" >> "$TMPFILE"
done
fi
let "i += 1"
done
POSE_3DMM_OUTPUT="$DATASET_ROOT/$POSE_3DMM_DIR_NAME"
cd /Vol0/user/e.burkov/Projects/Expression-Net
python2 compute_3DMM_coefficients_noBboxes.py --images_path "$TMPFILE" --output_dir "$POSE_3DMM_OUTPUT" --common_prefix "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME"
cd -
fi
###################################### Crop using landmarks ("FFHQ style" crop) #######################################
IMAGES_CROPPED_DIR_NAME="images-cropped-ffhq"
KEYPOINTS_CROPPED_DIR_NAME="keypoints-cropped-ffhq"
SEGMENTATION_DIR_NAME="segmentation-cropped-ffhq"
if [ "$DO_CROP_FFHQ" = true ]; then
i=0
for IDENTITY in "${IDENTITIES[@]}"; do #$DATASET_ROOT/images/*; do
if (($i >= $FIRST_IDX && $i < $LAST_IDX)); then
# vid=$IDENTITY/$(ls -1 $IDENTITY | shuf -n1)
vid="$DATASET_ROOT/images/$IDENTITY"
filename="$(basename -- "$vid")"
filename="${filename%.*}"
dir="$(dirname "$vid")"
dir="$(basename "$dir")"
echo $i $dir $vid $filename
# echo "$i,SAIC-selfie-videos/$dir/$filename" >> $DATASET_ROOT/split.csv
mkdir -p "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/"
python3 crop_as_in_dataset.py --crop-style=ffhq --save-extra-data "$DATASET_ROOT/images/$IDENTITY/" "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/"
mkdir -p "$DATASET_ROOT/$KEYPOINTS_CROPPED_DIR_NAME/$IDENTITY/"
# Move '.npy' files to a separate folder
find "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/" -type f -name "*.npy" -exec mv {} "$DATASET_ROOT/$KEYPOINTS_CROPPED_DIR_NAME/$IDENTITY/" \;
fi
let "i += 1"
done;
fi
# Compute segmentation
if [ "$DO_COMPUTE_SEGMENTATION_FFHQ" = true ]; then
TMPFILE=$(mktemp)
i=0
for IDENTITY in "${IDENTITIES[@]}"; do
if (($i >= $FIRST_IDX && $i < $LAST_IDX)); then
for FILE in $(ls -1 "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/"); do
echo "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME/$IDENTITY/$FILE" >> "$TMPFILE"
done
fi
let "i += 1"
done
SEGMENTATION_OUTPUT="$DATASET_ROOT/$SEGMENTATION_DIR_NAME"
cd Graphonomy
python3 exp/inference/inference_folder.py --images_path "$TMPFILE" --output_dir "$SEGMENTATION_OUTPUT" --common_prefix "$DATASET_ROOT/$IMAGES_CROPPED_DIR_NAME" --model_path data/model/universal_trained.pth --tta 0.75,1.0,1.5,2.0
cd -
fi