-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcaltech101_prepare.sh
78 lines (67 loc) · 2.15 KB
/
caltech101_prepare.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
#!/usr/bin/env bash
# This file download the caltech 101 dataset
# (http://www.vision.caltech.edu/Image_Datasets/Caltech101/), and split it into
# example_images directory.
set -u
shopt -s expand_aliases
PWD=$(pwd)
# number of images per class for training
TRAIN_NUM=60
VALID_NUM=20
TEST_NUM=20
# target classes (10 classes)
CLASSES="airplanes Motorbikes Faces watch Leopards bonsai car_side ketch chandelier hawksbill"
if [ ! -e "$PWD/101_ObjectCategories.tar.gz" ]; then
if which wget > /dev/null 2>&1; then
wget -O "$PWD/101_ObjectCategories.tar.gz" http://www.vision.caltech.edu/Image_Datasets/Caltech101/101_ObjectCategories.tar.gz
else
if which curl > /dev/null 2>&1; then
curl http://www.vision.caltech.edu/Image_Datasets/Caltech101/101_ObjectCategories.tar.gz > "$PWD/101_ObjectCategories.tar.gz"
else
echo 'wget and curl commands not found.' 1>&2
exit 1
fi
fi
fi
if which shuf > /dev/null; then
alias shuffle='shuf'
else
if which gshuf > /dev/null; then
alias shuffle='gshuf'
else
echo 'shuf or gshuf command not found.' 1>&2
exit 1
fi
fi
# check example_images
if [ -e "$PWD/example_images" ]; then
echo './example_images directory already exits. Remove it and retry.' 1>&2
exit 1
fi
# split into train, validation and test set
tar -xf "$PWD/101_ObjectCategories.tar.gz"
TRAIN_DIR="$PWD/example_images/train"
VALID_DIR="$PWD/example_images/valid"
TEST_DIR="$PWD/example_images/test"
mkdir -p "$TRAIN_DIR" "$VALID_DIR" "$TEST_DIR"
for i in ${PWD}/101_ObjectCategories/*; do
c=$(basename "$i")
if echo "$CLASSES" | grep -q "$c"
then
echo "processing $c"
mkdir -p "$TRAIN_DIR/$c" "$VALID_DIR/$c" "$TEST_DIR/$c"
for j in $(find "$i" -name '*.jpg' | shuffle | head -n "$TRAIN_NUM"); do
mv "$j" "$TRAIN_DIR/$c/"
done
for j in $(find "$i" -name '*.jpg' | shuffle | head -n "$VALID_NUM"); do
mv "$j" "$VALID_DIR/$c/"
done
for j in $(find "$i" -name '*.jpg' | shuffle | head -n "$TEST_NUM"); do
mv "$j" "$TEST_DIR/$c/"
done
fi
done
# touch .gitignore
touch "$TRAIN_DIR/.gitkeep" "$VALID_DIR/.gitkeep" "$TEST_DIR/.gitkeep"
# clean
rm -rf "$PWD/101_ObjectCategories/"