Added the loss graph plotting.
[dyncnn.git] / run.sh
1 #!/bin/bash
2
3 # dyncnn is a deep-learning algorithm for the prediction of
4 # interacting object dynamics
5 #
6 # Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
7 # Written by Francois Fleuret <francois.fleuret@idiap.ch>
8 #
9 # This file is part of dyncnn.
10 #
11 # dyncnn is free software: you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License version 3 as
13 # published by the Free Software Foundation.
14 #
15 # dyncnn is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with dyncnn.  If not, see <http://www.gnu.org/licenses/>.
22
23 # This script creates the synthetic data-set for shape collision
24
25 set -e
26 set -o pipefail
27
28 [[ "${TORCH_NB_THREADS}" ]] || echo "You can set \$TORCH_NB_THREADS to the proper value (default 1)."
29 [[ "${TORCH_USE_GPU}" ]] || echo "You can set \$TORCH_USE_GPU to 'yes' or 'no' (default 'no')."
30 [[ "${DYNCNN_DATA_DIR}" ]] || DYNCNN_DATA_DIR="./data/10p-mg"
31 [[ "${DYNCNN_RESULT_DIR}" ]] || DYNCNN_RESULT_DIR="./results"
32
33 ######################################################################
34 # Create the data-set if needed
35
36 if [[ -d "${DYNCNN_DATA_DIR}" ]]; then
37     echo "Found ${DYNCNN_DATA_DIR}, checking the number of images in there."
38     if [[ $(find "${DYNCNN_DATA_DIR}" -name "dyn_*.png" | wc -l) == 150000 ]]; then
39         echo "Looks good !"
40     else
41         echo "I do not find the proper number of images. Please remove the dir and re-run this scripts, or fix manually."
42         exit 1
43     fi
44 else
45     # Creating the data-base
46     make -j -k
47     mkdir -p "${DYNCNN_DATA_DIR}"
48     ./flatland 50000 \
49                --every_nth 4 --nb_frames 5 \
50                --multi_grasp --nb_shapes 10 \
51                --dir "${DYNCNN_DATA_DIR}"
52 fi
53
54 ######################################################################
55 # Train the model (~30h on a GTX1080)
56
57 if [[ ! -f "${DYNCNN_RESULT_DIR}"/epoch_01000_model ]]; then
58     ./dyncnn.lua --heavy --dataDir="${DYNCNN_DATA_DIR}" \
59                  --resultFreq=100 \
60                  --resultDir "${DYNCNN_RESULT_DIR}" \
61                  --nbEpochs 1000
62 fi
63
64 ######################################################################
65 # Create the images of internal activations
66
67 for n in 2 12; do
68     ./dyncnn.lua --heavy --dataDir=./data/10p-mg/ \
69                  --learningStateFile="${DYNCNN_RESULT_DIR}"/epoch_01000_model \
70                  --resultDir="${DYNCNN_RESULT_DIR}" \
71                  --noLog \
72                  --exampleInternals=${n}
73 done
74
75 ######################################################################
76 # Plot the loss curves if gnuplot is here
77
78 if [[ $(which gnuplot) ]]; then
79     echo "Plotting losses.pdf."
80
81     TERMINAL="pdfcairo color transparent enhanced font \"Times,14\""
82     EXTENSION="pdf"
83
84     gnuplot <<EOF
85 set terminal ${TERMINAL}
86 set output "${DYNCNN_RESULT_DIR}/losses.${EXTENSION}"
87 set logscale x
88 set logscale y
89 set size ratio 0.75
90 set xlabel "Number of epochs"
91 set ylabel "Loss"
92 plot '< grep "LOSS " "${DYNCNN_RESULT_DIR}"/log' using 2:4 with l lw 3 lc rgb '#c0c0ff' title 'Validation loss',\
93      '< grep "LOSS " "${DYNCNN_RESULT_DIR}"/log' using 2:3 with l lw 1 lc rgb '#000000' title 'Train loss'
94
95 EOF
96
97 fi
98
99 ######################################################################