forked from vlfeat/matconvnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess-imagenet.sh
executable file
·121 lines (111 loc) · 3.04 KB
/
preprocess-imagenet.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
#!/bin/bash
# file: preprocess-imagenet.sh
# auhtor: Andrea Vedaldi
# Use as:
# preprocess-imagenet.sh SRC_PATH DEST_PATH
#
# The script creates a copy of the ImageNet ILSVRC CLS-LOC challenge
# data while rescaling the images. Images are rescaled to a minimum
# side of 256 pixels. The data is supposed to be in the format defined
# by examples/cnn_imagenet_setup_data.m
#
# Note that the default scripts in MatConvNet expect the following structure:
#
# ILSVRC2012/ILSVRC2012_devkit_t12/
# ILSVRC2012/images/train/n01440764/
# ILSVRC2012/images/train/n01484850/
# ...
# ILSVRC2012/images/train/n15075141/
# ILSVRC2012/images/val/ILSVRC2012_val_00000001.JPEG
# ILSVRC2012/images/val/ILSVRC2012_val_00000002.JPEG
# ...
# ILSVRC2012/images/val/ILSVRC2012_val_00050000.JPEG
# ILSVRC2012/images/test/ILSVRC2012_test_00000001.JPEG
# ILSVRC2012/images/test/ILSVRC2012_test_00000002.JPEG
# ...
# ILSVRC2012/images/test/ILSVRC2012_test_00100000.JPEG
#
# Symbolic links within the ILSVRC2012/images hierarchy are supported
# by this script.
#
# Example:
# Create a copy of the ILSVRC2012 data in the data/ILSVRC2012
# subfolder. Create a link to a ramdisk directory
# data/ram/ILSVRC2012 to contain the transformed images (provided
# that your server has GBs of RAM!). Then:
#
# cd <MatConvNet>
# ./utils/preprocess-imagenet data/ILSVRC2012 data/ram/ILSVRC2012
data=$1
ram=$2
# graphics magick (much faster)
num_cpus=1
method=gm
# image size
size=256 # most common
#size=310 # for inception
# image magick
# num_cpus=8
# method=im
mkdir -p "$ram"/images ;
rsync -rv --chmod=ugo=rwX "$data"/*devkit* "$ram/"
function convert_some_im()
{
out="$1"
shift
size="$1"
shift
for infile in "$@"
do
outfile="$out/$(basename $infile)"
if test -f "$outfile"
then
continue ;
fi
convert "${infile}" \
-verbose \
-quality 90 \
-colorspace RGB \
-resize "${size}x${size}^" \
JPEG:"${outfile}.temp"
mv "${outfile}.temp" "$outfile"
done
}
export -f convert_some_im
function convert_some_gm()
{
gm=gm
out="$1"
shift
size="$1"
shift
for infile in "$@"
do
outfile="$out/$(basename $infile)"
if test -f "$outfile"
then
continue ;
fi
echo convert "'${infile}'" \
-verbose \
-quality 90 \
-colorspace RGB \
-resize "${size}x${size}^" \
"JPEG:'${outfile}'"
done | ${gm} batch -echo on -feedback on -
}
export -f convert_some_gm
dirs=$(find $data/images/* -maxdepth 2 -type d)
for d in $dirs
do
sub=${d#${data}/images/}
out="$ram/images/$sub"
echo "Converting $d -> $out"
mkdir -p "$out"
find "$d" -maxdepth 1 -type f -name '*.JPEG' | \
xargs -n 1000 --max-procs=$num_cpus \
bash -c "convert_some_$method \"$out\" ${size} \"\$@\" " _
done
# copy any symlink
find "$data/images/" -type l -printf '%P\n' | \
rsync -lv --files-from=- "$data/images/" "$ram/images/"