Skip to content

Commit

Permalink
Merge pull request #44 from lukeyeager/nvidia/cherry-picks
Browse files Browse the repository at this point in the history
Cherry pick some commits for v0.14
  • Loading branch information
lukeyeager committed Oct 22, 2015
2 parents f432085 + 749a636 commit 8468d18
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 126 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ set(python_version "2" CACHE STRING "Specify which Python version to use")
caffe_option(BUILD_matlab "Build Matlab wrapper" OFF IF UNIX OR APPLE)
caffe_option(BUILD_docs "Build documentation" ON IF UNIX OR APPLE)
caffe_option(BUILD_python_layer "Build the Caffe Python layer" ON)
caffe_option(USE_LMDB "Build with lmdb" ON)
caffe_option(USE_LEVELDB "Build with levelDB" ON)
caffe_option(USE_OPENCV "Build with OpenCV support" ON)
caffe_option(USE_LEVELDB "Build with levelDB" ON)
caffe_option(USE_LMDB "Build with lmdb" ON)
caffe_option(ALLOW_LMDB_NOLOCK "Allow MDB_NOLOCK when reading LMDB files (only if necessary)" OFF)

# ---[ Dependencies
include(cmake/Dependencies.cmake)
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ ifeq ($(USE_LEVELDB), 1)
endif
ifeq ($(USE_LMDB), 1)
COMMON_FLAGS += -DUSE_LMDB
ifeq ($(ALLOW_LMDB_NOLOCK), 1)
COMMON_FLAGS += -DALLOW_LMDB_NOLOCK
endif
endif

# CPU-only configuration
Expand Down
7 changes: 6 additions & 1 deletion Makefile.config.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
# CPU_ONLY := 1

# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0
# USE_OPENCV := 0

# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
# You should not set this flag if you will be reading LMDBs with any
# possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1

# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
Expand Down
3 changes: 3 additions & 0 deletions cmake/ConfigGen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ function(caffe_generate_export_configs)

if(USE_LMDB)
list(APPEND Caffe_DEFINITIONS -DUSE_LMDB)
if (ALLOW_LMDB_NOLOCK)
list(APPEND Caffe_DEFINITIONS -DALLOW_LMDB_NOLOCK)
endif()
endif()

if(USE_LEVELDB)
Expand Down
7 changes: 5 additions & 2 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ if(USE_LMDB)
include_directories(SYSTEM ${LMDB_INCLUDE_DIR})
list(APPEND Caffe_LINKER_LIBS ${LMDB_LIBRARIES})
add_definitions(-DUSE_LMDB)
if(ALLOW_LMDB_NOLOCK)
add_definitions(-DALLOW_LMDB_NOLOCK)
endif()
endif()

# ---[ LevelDB
Expand All @@ -55,9 +58,9 @@ endif()
include(cmake/Cuda.cmake)
if(NOT HAVE_CUDA)
if(CPU_ONLY)
message("-- CUDA is disabled. Building without it...")
message(STATUS "-- CUDA is disabled. Building without it...")
else()
message("-- CUDA is not detected by cmake. Building without it...")
message(WARNING "-- CUDA is not detected by cmake. Building without it...")
endif()

# TODO: remove this not cross platform define in future. Use caffe_config.h instead.
Expand Down
5 changes: 3 additions & 2 deletions cmake/Summary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ function(caffe_print_configuration_summary)
caffe_status(" BUILD_matlab : ${BUILD_matlab}")
caffe_status(" BUILD_docs : ${BUILD_docs}")
caffe_status(" CPU_ONLY : ${CPU_ONLY}")
caffe_status(" USE_LMDB : ${USE_LMDB}")
caffe_status(" USE_LEVELDB : ${USE_LEVELDB}")
caffe_status(" USE_OPENCV : ${USE_OPENCV}")
caffe_status(" USE_LEVELDB : ${USE_LEVELDB}")
caffe_status(" USE_LMDB : ${USE_LMDB}")
caffe_status(" ALLOW_LMDB_NOLOCK : ${ALLOW_LMDB_NOLOCK}")
caffe_status("")
caffe_status("Dependencies:")
caffe_status(" BLAS : " APPLE THEN "Yes (vecLib)" ELSE "Yes (${BLAS})")
Expand Down
3 changes: 2 additions & 1 deletion cmake/Templates/caffe_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@

/* IO libraries */
#cmakedefine USE_OPENCV
#cmakedefine USE_LMDB
#cmakedefine USE_LEVELDB
#cmakedefine USE_LMDB
#cmakedefine ALLOW_LMDB_NOLOCK
6 changes: 3 additions & 3 deletions python/caffe/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ def get_layer_label(layer, rankdir):
separator,
layer.type,
separator,
layer.convolution_param.kernel_size,
layer.convolution_param.kernel_size[0] if len(layer.convolution_param.kernel_size._values) else 1,
separator,
layer.convolution_param.stride,
layer.convolution_param.stride[0] if len(layer.convolution_param.stride._values) else 1,
separator,
layer.convolution_param.pad)
layer.convolution_param.pad[0] if len(layer.convolution_param.pad._values) else 0)
elif layer.type == 'Pooling':
pooling_types_dict = get_pooling_types_dict()
node_label = '"%s%s(%s %s)%skernel size: %d%sstride: %d%spad: %d"' %\
Expand Down
19 changes: 11 additions & 8 deletions python/caffe/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,26 @@ def blobproto_to_array(blob, return_diff=False):
Convert a blob proto to an array. In default, we will just return the data,
unless return_diff is True, in which case we will return the diff.
"""
# Read the data into an array
if return_diff:
return np.array(blob.diff).reshape(
blob.num, blob.channels, blob.height, blob.width)
data = np.array(blob.diff)
else:
return np.array(blob.data).reshape(
blob.num, blob.channels, blob.height, blob.width)
data = np.array(blob.data)

# Reshape the array
if blob.HasField('num') or blob.HasField('channels') or blob.HasField('height') or blob.HasField('width'):
# Use legacy 4D shape
return data.reshape(blob.num, blob.channels, blob.height, blob.width)
else:
return data.reshape(blob.shape.dim)

def array_to_blobproto(arr, diff=None):
"""Converts a 4-dimensional array to blob proto. If diff is given, also
"""Converts a N-dimensional array to blob proto. If diff is given, also
convert the diff. You need to make sure that arr and diff have the same
shape, and this function does not do sanity check.
"""
if arr.ndim != 4:
raise ValueError('Incorrect array shape.')
blob = caffe_pb2.BlobProto()
blob.num, blob.channels, blob.height, blob.width = arr.shape
blob.shape.dim.extend(arr.shape)
blob.data.extend(arr.astype(float).flat)
if diff is not None:
blob.diff.extend(diff.astype(float).flat)
Expand Down
41 changes: 41 additions & 0 deletions python/caffe/test/test_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import numpy as np
import unittest

import caffe

class TestBlobProtoToArray(unittest.TestCase):

def test_old_format(self):
data = np.zeros((10,10))
blob = caffe.proto.caffe_pb2.BlobProto()
blob.data.extend(list(data.flatten()))
shape = (1,1,10,10)
blob.num, blob.channels, blob.height, blob.width = shape

arr = caffe.io.blobproto_to_array(blob)
self.assertEqual(arr.shape, shape)

def test_new_format(self):
data = np.zeros((10,10))
blob = caffe.proto.caffe_pb2.BlobProto()
blob.data.extend(list(data.flatten()))
blob.shape.dim.extend(list(data.shape))

arr = caffe.io.blobproto_to_array(blob)
self.assertEqual(arr.shape, data.shape)

def test_no_shape(self):
data = np.zeros((10,10))
blob = caffe.proto.caffe_pb2.BlobProto()
blob.data.extend(list(data.flatten()))

with self.assertRaises(ValueError):
caffe.io.blobproto_to_array(blob)

def test_scalar(self):
data = np.ones((1)) * 123
blob = caffe.proto.caffe_pb2.BlobProto()
blob.data.extend(list(data.flatten()))

arr = caffe.io.blobproto_to_array(blob)
self.assertEqual(arr, 123)
Loading

0 comments on commit 8468d18

Please sign in to comment.