Skip to content

Commit 4d3b184

Browse files
committed
Travis scripts for python3 and pytest for cmake. Also fixes CUDA CMake build issue #2722.
1 parent ff4e286 commit 4d3b184

12 files changed

+90
-33
lines changed

.travis.yml

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,31 @@ env:
66
- WITH_CUDA=false WITH_CMAKE=true
77
- WITH_CUDA=true WITH_CMAKE=false
88
- WITH_CUDA=true WITH_CMAKE=true
9+
- WITH_CUDA=false WITH_CMAKE=true PYTHON_VERSION=3
910

1011
language: cpp
1112

1213
# Cache Ubuntu apt packages.
13-
cache: apt
14+
cache:
15+
apt: true
16+
directories:
17+
- /home/travis/miniconda
18+
- /home/travis/miniconda2
19+
- /home/travis/miniconda3
1420

1521
compiler: gcc
1622

1723
before_install:
1824
- export NUM_THREADS=4
1925
- export SCRIPTS=./scripts/travis
26+
- export CONDA_DIR="/home/travis/miniconda$PYTHON_VERSION"
2027

2128
install:
2229
- sudo -E $SCRIPTS/travis_install.sh
2330

2431
before_script:
25-
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/cuda/lib64
26-
- export PATH=/home/travis/miniconda/bin:$PATH
32+
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:/usr/local/cuda/lib64:$CONDA_DIR/lib
33+
- export PATH=$CONDA_DIR/bin:$PATH
2734
- if ! $WITH_CMAKE; then $SCRIPTS/travis_setup_makefile_config.sh; fi
2835

2936
script: $SCRIPTS/travis_build_and_test.sh

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ add_subdirectory(docs)
6262
# ---[ Linter target
6363
add_custom_target(lint COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/lint.cmake)
6464

65+
# ---[ pytest target
66+
add_custom_target(pytest COMMAND python${python_version} -m unittest discover -s caffe/test WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/python )
67+
add_dependencies(pytest pycaffe)
68+
6569
# ---[ Configuration summary
6670
caffe_print_configuration_summary()
6771

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ ifeq ($(LINUX), 1)
228228
CXX ?= /usr/bin/g++
229229
GCCVERSION := $(shell $(CXX) -dumpversion | cut -f1,2 -d.)
230230
# older versions of gcc are too dumb to build boost with -Wuninitalized
231-
ifeq ($(shell echo $(GCCVERSION) \< 4.6 | bc), 1)
231+
ifeq ($(shell echo | awk '{exit $(GCCVERSION) < 4.6;}'), 1)
232232
WARNINGS += -Wno-uninitialized
233233
endif
234234
# boost::thread is reasonably called boost_thread (compare OS X)
@@ -243,7 +243,7 @@ ifeq ($(OSX), 1)
243243
CXX := /usr/bin/clang++
244244
ifneq ($(CPU_ONLY), 1)
245245
CUDA_VERSION := $(shell $(CUDA_DIR)/bin/nvcc -V | grep -o 'release \d' | grep -o '\d')
246-
ifeq ($(shell echo $(CUDA_VERSION) \< 7.0 | bc), 1)
246+
ifeq ($(shell echo | awk '{exit $(CUDA_VERSION) < 7.0;}'), 1)
247247
CXXFLAGS += -stdlib=libstdc++
248248
LINKFLAGS += -stdlib=libstdc++
249249
endif

cmake/Dependencies.cmake

+5-4
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,15 @@ if(BUILD_python)
106106

107107
while(NOT "${version}" STREQUAL "" AND NOT Boost_PYTHON_FOUND)
108108
STRING( REGEX REPLACE "([0-9.]+).[0-9]+" "\\1" version ${version} )
109-
STRING( REGEX MATCHALL "([0-9.]+).[0-9]+" has_more_version ${version} )
110-
if("${has_more_version}" STREQUAL "")
111-
break()
112-
endif()
113109

114110
STRING( REPLACE "." "" boost_py_version ${version} )
115111
find_package(Boost 1.46 COMPONENTS "python-py${boost_py_version}")
116112
set(Boost_PYTHON_FOUND ${Boost_PYTHON-PY${boost_py_version}_FOUND})
113+
114+
STRING( REGEX MATCHALL "([0-9.]+).[0-9]+" has_more_version ${version} )
115+
if("${has_more_version}" STREQUAL "")
116+
break()
117+
endif()
117118
endwhile()
118119
if(NOT Boost_PYTHON_FOUND)
119120
find_package(Boost 1.46 COMPONENTS python)

python/caffe/test/test_net.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import tempfile
33
import os
44
import numpy as np
5+
import six
56

67
import caffe
78

@@ -10,7 +11,7 @@ def simple_net_file(num_output):
1011
"""Make a simple net prototxt, based on test_net.cpp, returning the name
1112
of the (temporary) file."""
1213

13-
f = tempfile.NamedTemporaryFile(delete=False)
14+
f = tempfile.NamedTemporaryFile(mode='w+', delete=False)
1415
f.write("""name: 'testnet' force_backward: true
1516
layer { type: 'DummyData' name: 'data' top: 'data' top: 'label'
1617
dummy_data_param { num: 5 channels: 2 height: 3 width: 4
@@ -47,7 +48,7 @@ def setUp(self):
4748
def test_memory(self):
4849
"""Check that holding onto blob data beyond the life of a Net is OK"""
4950

50-
params = sum(map(list, self.net.params.itervalues()), [])
51+
params = sum(map(list, six.itervalues(self.net.params)), [])
5152
blobs = self.net.blobs.values()
5253
del self.net
5354

@@ -67,7 +68,7 @@ def test_inputs_outputs(self):
6768
self.assertEqual(self.net.outputs, ['loss'])
6869

6970
def test_save_and_read(self):
70-
f = tempfile.NamedTemporaryFile(delete=False)
71+
f = tempfile.NamedTemporaryFile(mode='w+', delete=False)
7172
f.close()
7273
self.net.save(f.name)
7374
net_file = simple_net_file(self.num_output)

python/caffe/test/test_net_spec.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def anon_lenet(batch_size):
4343

4444
class TestNetSpec(unittest.TestCase):
4545
def load_net(self, net_proto):
46-
f = tempfile.NamedTemporaryFile(delete=False)
46+
f = tempfile.NamedTemporaryFile(mode='w+', delete=False)
4747
f.write(str(net_proto))
4848
f.close()
4949
return caffe.Net(f.name, caffe.TEST)

python/caffe/test/test_python_layer.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
import tempfile
33
import os
4+
import six
45

56
import caffe
67

@@ -22,7 +23,7 @@ def backward(self, top, propagate_down, bottom):
2223

2324

2425
def python_net_file():
25-
with tempfile.NamedTemporaryFile(delete=False) as f:
26+
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as f:
2627
f.write("""name: 'pythonnet' force_backward: true
2728
input: 'data' input_shape { dim: 10 dim: 9 dim: 8 }
2829
layer { type: 'Python' name: 'one' bottom: 'data' top: 'one'
@@ -58,6 +59,6 @@ def test_reshape(self):
5859
s = 4
5960
self.net.blobs['data'].reshape(s, s, s, s)
6061
self.net.forward()
61-
for blob in self.net.blobs.itervalues():
62+
for blob in six.itervalues(self.net.blobs):
6263
for d in blob.data.shape:
6364
self.assertEqual(s, d)

python/caffe/test/test_solver.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import tempfile
33
import os
44
import numpy as np
5+
import six
56

67
import caffe
78
from test_net import simple_net_file
@@ -11,7 +12,7 @@ class TestSolver(unittest.TestCase):
1112
def setUp(self):
1213
self.num_output = 13
1314
net_f = simple_net_file(self.num_output)
14-
f = tempfile.NamedTemporaryFile(delete=False)
15+
f = tempfile.NamedTemporaryFile(mode='w+', delete=False)
1516
f.write("""net: '""" + net_f + """'
1617
test_iter: 10 test_interval: 10 base_lr: 0.01 momentum: 0.9
1718
weight_decay: 0.0005 lr_policy: 'inv' gamma: 0.0001 power: 0.75
@@ -45,8 +46,8 @@ def test_net_memory(self):
4546

4647
total = 0
4748
for net in nets:
48-
for ps in net.params.itervalues():
49+
for ps in six.itervalues(net.params):
4950
for p in ps:
5051
total += p.data.sum() + p.diff.sum()
51-
for bl in net.blobs.itervalues():
52+
for bl in six.itervalues(net.blobs):
5253
total += bl.data.sum() + bl.diff.sum()

scripts/travis/travis_build_and_test.sh

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@ MAKE="make --jobs=$NUM_THREADS --keep-going"
77
if $WITH_CMAKE; then
88
mkdir build
99
cd build
10-
cmake -DBUILD_python=ON -DCMAKE_BUILD_TYPE=Release -DCPU_ONLY=ON ..
10+
CPU_ONLY=" -DCPU_ONLY=ON"
11+
if ! $WITH_CUDA; then
12+
CPU_ONLY=" -DCPU_ONLY=OFF"
13+
fi
14+
PYTHON_ARGS=""
15+
if [ "$PYTHON_VERSION" = "3" ]; then
16+
PYTHON_ARGS="$PYTHON_ARGS -Dpython_version=3 -DBOOST_LIBRARYDIR=$CONDA_DIR/lib/"
17+
fi
18+
cmake -DBUILD_python=ON -DCMAKE_BUILD_TYPE=Release $CPU_ONLY $PYTHON_ARGS -DCMAKE_INCLUDE_PATH="$CONDA_DIR/include/" -DCMAKE_LIBRARY_PATH="$CONDA_DIR/lib/" ..
1119
$MAKE
20+
$MAKE pytest
1221
if ! $WITH_CUDA; then
1322
$MAKE runtest
1423
$MAKE lint

scripts/travis/travis_install.sh

+39-13
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
set -e
55

66
MAKE="make --jobs=$NUM_THREADS"
7-
87
# Install apt packages where the Ubuntu 12.04 default and ppa works for Caffe
98

109
# This ppa is for gflags and glog
1110
add-apt-repository -y ppa:tuleu/precise-backports
1211
apt-get -y update
1312
apt-get install \
1413
wget git curl \
15-
python-dev python-numpy \
14+
python-dev python-numpy python3-dev\
1615
libleveldb-dev libsnappy-dev libopencv-dev \
17-
libboost-dev libboost-system-dev libboost-python-dev libboost-thread-dev \
1816
libprotobuf-dev protobuf-compiler \
1917
libatlas-dev libatlas-base-dev \
2018
libhdf5-serial-dev libgflags-dev libgoogle-glog-dev \
@@ -24,9 +22,10 @@ apt-get install \
2422
# if needed. By default, Aptitude in Ubuntu 12.04 installs CMake 2.8.7, but
2523
# Caffe requires a minimum CMake version of 2.8.8.
2624
if $WITH_CMAKE; then
27-
add-apt-repository -y ppa:ubuntu-sdk-team/ppa
28-
apt-get -y update
29-
apt-get -y install cmake
25+
# cmake 3 will make sure that the python interpreter and libraries match
26+
wget http://www.cmake.org/files/v3.2/cmake-3.2.3-Linux-x86_64.sh -O cmake3.sh
27+
chmod +x cmake3.sh
28+
./cmake3.sh --prefix=/usr/ --skip-license --exclude-subdir
3029
fi
3130

3231
# Install CUDA, if needed
@@ -60,10 +59,37 @@ rm -f $LMDB_FILE
6059

6160
# Install the Python runtime dependencies via miniconda (this is much faster
6261
# than using pip for everything).
63-
wget http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh
64-
chmod +x miniconda.sh
65-
./miniconda.sh -b
66-
export PATH=/home/travis/miniconda/bin:$PATH
67-
conda update --yes conda
68-
conda install --yes numpy scipy matplotlib scikit-image pip
69-
pip install protobuf
62+
export PATH=$CONDA_DIR/bin:$PATH
63+
if [ ! -d $CONDA_DIR ]; then
64+
if [ "$PYTHON_VERSION" -eq "3" ]; then
65+
wget http://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
66+
else
67+
wget http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh
68+
fi
69+
chmod +x miniconda.sh
70+
./miniconda.sh -b -p $CONDA_DIR
71+
72+
conda update --yes conda
73+
conda install --yes numpy scipy matplotlib scikit-image pip
74+
# Let conda install boost (so that boost_python matches)
75+
conda install --yes -c https://conda.binstar.org/menpo boost=1.56.0
76+
fi
77+
78+
# install protobuf 3 (just use the miniconda3 directory to avoid having to setup the path again)
79+
if [ "$PYTHON_VERSION" -eq "3" ] && [ ! -e "$CONDA_DIR/bin/protoc" ]; then
80+
pushd .
81+
wget https://github.com/google/protobuf/archive/v3.0.0-alpha-3.1.tar.gz -O protobuf-3.tar.gz
82+
tar -C /tmp -xzvf protobuf-3.tar.gz
83+
cd /tmp/protobuf-3*/
84+
./autogen.sh
85+
./configure --prefix=$CONDA_DIR
86+
$MAKE
87+
$MAKE install
88+
popd
89+
fi
90+
91+
if [ "$PYTHON_VERSION" -eq "3" ]; then
92+
pip install --pre protobuf
93+
else
94+
pip install protobuf
95+
fi

scripts/travis/travis_setup_makefile_config.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ if $WITH_CUDA; then
1212
fi
1313

1414
cat << 'EOF' >> Makefile.config
15-
ANACONDA_HOME := $(HOME)/miniconda
15+
# Travis' nvcc doesn't like newer boost versions
16+
NVCCFLAGS := -Xcudafe --diag_suppress=cc_clobber_ignored -Xcudafe --diag_suppress=useless_using_declaration -Xcudafe --diag_suppress=set_but_not_used
17+
ANACONDA_HOME := $(CONDA_DIR)
1618
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
1719
$(ANACONDA_HOME)/include/python2.7 \
1820
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

src/caffe/layer_factory.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// Make sure we include Python.h before any system header
2+
// to avoid _POSIX_C_SOURCE redefinition
3+
#ifdef WITH_PYTHON_LAYER
4+
#include <boost/python.hpp>
5+
#endif
16
#include <string>
27

38
#include "caffe/layer.hpp"

0 commit comments

Comments
 (0)