Merge pull request #1473 from longjon/pytest
authorEvan Shelhamer <shelhamer@imaginarynumber.net>
Thu, 22 Jan 2015 08:29:35 +0000 (00:29 -0800)
committerEvan Shelhamer <shelhamer@imaginarynumber.net>
Thu, 22 Jan 2015 08:29:35 +0000 (00:29 -0800)
Python testing

41 files changed:
Makefile
Makefile.config.example
README.md
docs/index.md
docs/installation.md
docs/tutorial/forward_backward.md
docs/tutorial/interfaces.md
examples/cifar10/readme.md
examples/classification.ipynb
examples/filter_visualization.ipynb
examples/net_surgery.ipynb
include/caffe/common.hpp
include/caffe/data_transformer.hpp
include/caffe/filler.hpp
include/caffe/internal_thread.hpp
include/caffe/net.hpp
include/caffe/util/io.hpp
include/caffe/util/thread.hpp [deleted file]
models/bvlc_alexnet/readme.md
models/bvlc_reference_caffenet/readme.md
models/bvlc_reference_rcnn_ilsvrc13/readme.md
python/caffe/__init__.py
python/caffe/_caffe.cpp
python/caffe/_caffe.hpp
python/caffe/classifier.py
python/caffe/detector.py
python/caffe/io.py
python/caffe/pycaffe.py
python/requirements.txt
scripts/build_docs.sh
src/caffe/data_transformer.cpp
src/caffe/internal_thread.cpp
src/caffe/layer_factory.cpp
src/caffe/layers/data_layer.cpp
src/caffe/layers/image_data_layer.cpp
src/caffe/net.cpp
src/caffe/proto/caffe.proto
src/caffe/solver.cpp
src/caffe/util/io.cpp
tools/caffe.cpp
tools/convert_imageset.cpp

index d3bddea..53c16ea 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -234,15 +234,14 @@ endif
 # libstdc++ instead of libc++ for CUDA compatibility on 10.9
 ifeq ($(OSX), 1)
        CXX := /usr/bin/clang++
+       CXXFLAGS += -stdlib=libstdc++
+       LINKFLAGS += -stdlib=libstdc++
        # clang throws this warning for cuda headers
        WARNINGS += -Wno-unneeded-internal-declaration
-       ifneq ($(findstring 10.9, $(shell sw_vers -productVersion)),)
-               CXXFLAGS += -stdlib=libstdc++
-               LINKFLAGS += -stdlib=libstdc++
-       endif
+       # gtest needs to use its own tuple to not conflict with clang
+       CXXFLAGS += -DGTEST_USE_OWN_TR1_TUPLE=1
        # boost::thread is called boost_thread-mt to mark multithreading on OS X
        LIBRARIES += boost_thread-mt
-        NVCCFLAGS += -DOSX
 endif
 
 # Custom compiler
@@ -304,9 +303,16 @@ else
                endif
        else ifeq ($(OSX), 1)
                # OS X packages atlas as the vecLib framework
-               BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
                LIBRARIES += cblas
-               LDFLAGS += -framework vecLib
+               # 10.10 has accelerate while 10.9 has veclib
+               XCODE_CLT_VER := $(shell pkgutil --pkg-info=com.apple.pkg.CLTools_Executables | grep -o 'version: 6')
+               ifneq (,$(findstring version: 6,$(XCODE_CLT_VER)))
+                       BLAS_INCLUDE ?= /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
+                       LDFLAGS += -framework Accelerate
+               else
+                       BLAS_INCLUDE ?= /System/Library/Frameworks/vecLib.framework/Versions/Current/Headers/
+                       LDFLAGS += -framework vecLib
+               endif
        endif
 endif
 INCLUDE_DIRS += $(BLAS_INCLUDE)
@@ -334,6 +340,7 @@ endif
 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(PKG_CONFIG) \
                $(foreach library,$(LIBRARIES),-l$(library))
 PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
+DYNAMIC_LDFLAGS := -l$(PROJECT) -Wl,-rpath,\$$ORIGIN/../lib
 
 # 'superclean' target recursively* deletes all files ending with an extension
 # in $(SUPERCLEAN_EXTS) below.  This may be useful if you've built older
@@ -468,11 +475,11 @@ $(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)
        @ mkdir -p $@
 
 $(DYNAMIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
-       @ echo LD $<
+       @ echo LD -o $@
        $(Q)$(CXX) -shared -o $@ $(OBJS) $(LINKFLAGS) $(LDFLAGS)
 
 $(STATIC_NAME): $(OBJS) | $(LIB_BUILD_DIR)
-       @ echo AR $<
+       @ echo AR -o $@
        $(Q)ar rcs $@ $(OBJS)
 
 $(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
@@ -506,7 +513,7 @@ $(TEST_ALL_DYNLINK_BIN): $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) $(DYNAMIC_NA
                | $(TEST_BIN_DIR)
        @ echo CXX/LD -o $@ $<
        $(Q)$(CXX) $(TEST_MAIN_SRC) $(TEST_OBJS) $(GTEST_OBJ) \
-               -o $@ $(LINKFLAGS) $(LDFLAGS) -l$(PROJECT) -Wl,-rpath,$(LIB_BUILD_DIR)
+               -o $@ $(LINKFLAGS) $(LDFLAGS) $(DYNAMIC_LDFLAGS)
 
 $(TEST_CU_BINS): $(TEST_BIN_DIR)/%.testbin: $(TEST_CU_BUILD_DIR)/%.o \
        $(GTEST_OBJ) $(STATIC_NAME) | $(TEST_BIN_DIR)
index 0c99603..9d13b45 100644 (file)
@@ -17,15 +17,14 @@ CUDA_DIR := /usr/local/cuda
 # "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
 # CUDA_DIR := /usr
 
-# CUDA architecture setting: going with all of them (up to CUDA 5.5 compatible).
-# For the latest architecture, you need to install CUDA >= 6.0 and uncomment
-# the *_50 lines below.
+# CUDA architecture setting: going with all of them.
+# For CUDA < 6.0, comment the *_50 lines for compatibility.
 CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
                -gencode arch=compute_20,code=sm_21 \
                -gencode arch=compute_30,code=sm_30 \
                -gencode arch=compute_35,code=sm_35 \
-               #-gencode arch=compute_50,code=sm_50 \
-               #-gencode arch=compute_50,code=compute_50
+               -gencode arch=compute_50,code=sm_50 \
+               -gencode arch=compute_50,code=compute_50
 
 # BLAS choice:
 # atlas for ATLAS (default)
index 0e3c833..9a9b3ee 100644 (file)
--- a/README.md
+++ b/README.md
@@ -2,3 +2,7 @@
 
 Caffe is a deep learning framework developed with cleanliness, readability, and speed in mind.<br />
 Consult the [project website](http://caffe.berkeleyvision.org) for all documentation.
+
+
+Please ask usage questions and how to model different tasks on the [caffe-users mailing list](https://groups.google.com/forum/#!forum/caffe-users).
+
index e90b06b..ccc8f75 100644 (file)
@@ -17,7 +17,7 @@ Networks are specified in simple config files, with no hard-coded parameters in
 Switching between CPU and GPU is as simple as setting a flag -- so models can be trained on a GPU machine, and then used on commodity clusters.
 
 **Readable & modifiable implementation** fosters active development.
-In Caffe's first six months, it has been forked by over 300 developers on Github, and many have pushed significant changes.
+In Caffe's first year, it has been forked by over 600 developers on Github, and many have pushed significant changes.
 
 **Speed** makes Caffe perfect for industry use.
 Caffe can process over **40M images per day** with a single NVIDIA K40 or Titan GPU\*.
@@ -35,11 +35,11 @@ Consult performance [details](/performance_hardware.html).
 ## Documentation
 
 - [DIY Deep Learning for Vision with Caffe](https://docs.google.com/presentation/d/1UeKXVgRvvxg9OUdh_UiC5G71UMscNPlvArsWER41PsU/edit#slide=id.p)<br>
-Caffe tutorial slides.
-- [ACM MM paper](http://ucb-icsi-vision-group.github.io/caffe-paper/caffe.pdf)<br>
-A 4-page report for the ACM Multimedia Open Source competition.
-- [Caffe Tutorial](/tutorial)<br>
-DIY deep learning with this hands-on tutorial to Caffe.
+Tutorial presentation.
+- [Tutorial Documentation](/tutorial)<br>
+Practical guide and framework reference.
+- [arXiv / ACM MM '14 paper](http://arxiv.org/abs/1408.5093)<br>
+A 4-page report for the ACM Multimedia Open Source competition (arXiv:1408.5093v1).
 - [Installation instructions](/installation.html)<br>
 Tested on Ubuntu, Red Hat, OS X.
 * [Model Zoo](/model_zoo.html)<br>
@@ -67,11 +67,11 @@ Developer documentation automagically generated from code comments.
 
 Please cite Caffe in your publications if it helps your research:
 
-    @misc{Jia13caffe,
-       Author = {Yangqing Jia},
-       Title = { {Caffe}: An Open Source Convolutional Architecture for Fast Feature Embedding},
-       Year  = {2013},
-       Howpublished = {\url{http://caffe.berkeleyvision.org/}}
+    @article{jia2014caffe,
+      Author = {Jia, Yangqing and Shelhamer, Evan and Donahue, Jeff and Karayev, Sergey and Long, Jonathan and Girshick, Ross and Guadarrama, Sergio and Darrell, Trevor},
+      Journal = {arXiv preprint arXiv:1408.5093},
+      Title = {Caffe: Convolutional Architecture for Fast Feature Embedding},
+      Year = {2014}
     }
 
 If you do publish a paper where Caffe helped your research, we encourage you to update the [publications wiki](https://github.com/BVLC/caffe/wiki/Publications).
@@ -79,17 +79,19 @@ Citations are also tracked automatically by [Google Scholar](http://scholar.goog
 
 ## Acknowledgements
 
-Yangqing would like to thank the NVIDIA Academic program for providing GPUs, [Oriol Vinyals](http://www1.icsi.berkeley.edu/~vinyals/) for discussions along the journey, and BVLC PI [Trevor Darrell](http://www.eecs.berkeley.edu/~trevor/) for guidance.
+The BVLC Caffe developers would like to thank NVIDIA for GPU donation, A9 and Amazon Web Services for a research grant in support of Caffe development and reproducible research in deep learning, and BVLC PI [Trevor Darrell](http://www.eecs.berkeley.edu/~trevor/) for guidance.
 
-A core set of BVLC members have contributed much new functionality and many fixes since the original release (alphabetical by first name):
-[Eric Tzeng](https://github.com/erictzeng), [Evan Shelhamer](http://imaginarynumber.net/), [Jeff Donahue](http://jeffdonahue.com/), [Jon Long](https://github.com/longjon), [Ross Girshick](http://www.cs.berkeley.edu/~rbg/), [Sergey Karayev](http://sergeykarayev.com/), [Sergio Guadarrama](http://www.eecs.berkeley.edu/~sguada/).
+The BVLC members who have contributed to Caffe are (alphabetical by first name):
+[Eric Tzeng](https://github.com/erictzeng), [Evan Shelhamer](http://imaginarynumber.net/), [Jeff Donahue](http://jeffdonahue.com/), [Jon Long](https://github.com/longjon), [Ross Girshick](http://www.cs.berkeley.edu/~rbg/), [Sergey Karayev](http://sergeykarayev.com/), [Sergio Guadarrama](http://www.eecs.berkeley.edu/~sguada/), and [Yangqing Jia](http://daggerfs.com/).
 
-Additionally, the open-source community plays a large and growing role in Caffe's development.
+The open-source community plays an important and growing role in Caffe's development.
 Check out the Github [project pulse](https://github.com/BVLC/caffe/pulse) for recent activity, and the [contributors](https://github.com/BVLC/caffe/graphs/contributors) for a sorted list.
 
 We sincerely appreciate your interest and contributions!
 If you'd like to contribute, please read the [developing & contributing](development.html) guide.
 
+Yangqing would like to give a personal thanks to the NVIDIA Academic program for providing GPUs, [Oriol Vinyals](http://www1.icsi.berkeley.edu/~vinyals/) for discussions along the journey, and BVLC PI [Trevor Darrell](http://www.eecs.berkeley.edu/~trevor/) for advice.
+
 ## Contacting us
 
 All questions about usage, installation, code, and applications should be searched for and asked on the [caffe-users mailing list](https://groups.google.com/forum/#!forum/caffe-users).
index c667cd8..c2b16a2 100644 (file)
@@ -5,7 +5,7 @@ title: Installation
 # Installation
 
 Prior to installing, it is best to read through this guide and take note of the details for your platform.
-We have installed Caffe on Ubuntu 14.04, Ubuntu 12.04, OS X 10.9, and OS X 10.8.
+We have installed Caffe on Ubuntu 14.04, Ubuntu 12.04, OS X 10.10, 10.9, and 10.8.
 
 - [Prerequisites](#prerequisites)
 - [Compilation](#compilation)
@@ -17,7 +17,7 @@ Caffe depends on several software packages.
 
 * [CUDA](https://developer.nvidia.com/cuda-zone) library version 6.5 (recommended), 6.0, 5.5, or 5.0 and the latest driver version for CUDA 6 or 319.* for CUDA 5 (and NOT 331.*)
 * [BLAS](http://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms) (provided via ATLAS, MKL, or OpenBLAS).
-* [OpenCV](http://opencv.org/).
+* [OpenCV](http://opencv.org/) (>= 2.4)
 * [Boost](http://www.boost.org/) (>= 1.55, although only 1.55 and 1.56 are tested)
 * `glog`, `gflags`, `protobuf`, `leveldb`, `snappy`, `hdf5`, `lmdb`
 * For the Python wrapper
@@ -141,7 +141,7 @@ Do `brew edit opencv` and change the lines that look like the two lines below to
 **NOTE**: We find that everything compiles successfully if `$LD_LIBRARY_PATH` is not set at all, and `$DYLD_FALLBACK_LIBRARY_PATH` is set to to provide CUDA, Python, and other relevant libraries (e.g. `/usr/local/cuda/lib:$HOME/anaconda/lib:/usr/local/lib:/usr/lib`).
 In other `ENV` settings, things may not work as expected.
 
-**NOTE**: There is currently a conflict between boost 1.56 and CUDA in some configurations. Check the [conflict description](https://github.com/BVLC/caffe/issues/1193#issuecomment-57491906) and try downgrading to 1.55.
+**NOTE**: There is currently a conflict between boost 1.56 and CUDA in some configurations. Check the [conflict description](https://github.com/BVLC/caffe/issues/1193#issuecomment-57491906) and try downgrading to 1.55 or upgrading to 1.57.
 
 #### 10.8-specific Instructions
 
@@ -156,9 +156,9 @@ Building boost from source is needed to link against your local Python (exceptio
 **Note** that the HDF5 dependency is provided by Anaconda Python in this case.
 If you're not using Anaconda, include `hdf5` in the list above.
 
-#### 10.9-specific Instructions
+#### 10.10- and 10.9-specific Instructions
 
-In OS X 10.9, clang++ is the default C++ compiler and uses `libc++` as the standard library.
+In OS X 10.9+, clang++ is the default C++ compiler and uses `libc++` as the standard library.
 However, NVIDIA CUDA (even version 6.0) currently links only with `libstdc++`.
 This makes it necessary to change the compilation settings for each of the dependencies.
 
@@ -226,10 +226,10 @@ Then, whenever you want to update homebrew, switch back to the master branches,
     git checkout master
     cd /usr/local/Library/Taps/homebrew/homebrew-science
     git checkout master
-    
+
     # Update homebrew; hopefully this works without errors!
     brew update
-    
+
     # Switch back to the caffe branches with the forumlae that you modified earlier
     cd /usr/local
     git rebase master caffe
@@ -237,8 +237,8 @@ Then, whenever you want to update homebrew, switch back to the master branches,
     cd /usr/local/Library/Taps/homebrew/homebrew-science
     git rebase master caffe
     # Fix any merge conflicts and commit to caffe branch
-    
-    # Done!            
+
+    # Done!
 
 At this point, you should be running the latest Homebrew packages and your Caffe-related modifications will remain in place.
 
index f58b9ca..a645f00 100644 (file)
@@ -15,7 +15,7 @@ This pass goes from bottom to top.
 
 <img src="fig/forward.jpg" alt="Forward pass" width="320">
 
-The data $x$ is passed through an inner product layer for $g(x)$ then through a softmax for $h(g(x))$ and softmax loss to give $f_W(x)$.
+The data $$x$$ is passed through an inner product layer for $$g(x)$$ then through a softmax for $$h(g(x))$$ and softmax loss to give $$f_W(x)$$.
 
 The **backward** pass computes the gradient given the loss for learning.
 In backward Caffe reverse-composes the gradient of each layer to compute the gradient of the whole model by automatic differentiation.
@@ -24,7 +24,7 @@ This pass goes from top to bottom.
 
 <img src="fig/backward.jpg" alt="Backward pass" width="320">
 
-The backward pass begins with the loss and computes the gradient with respect to the output $\frac{\partial f_W}{\partial h}$. The gradient with respect to the rest of the model is computed layer-by-layer through the chain rule. Layers with parameters, like the `INNER_PRODUCT` layer, compute the gradient with respect to their parameters $\frac{\partial f_W}{\partial W_{\text{ip}}}$ during the backward step.
+The backward pass begins with the loss and computes the gradient with respect to the output $$\frac{\partial f_W}{\partial h}$$. The gradient with respect to the rest of the model is computed layer-by-layer through the chain rule. Layers with parameters, like the `INNER_PRODUCT` layer, compute the gradient with respect to their parameters $$\frac{\partial f_W}{\partial W_{\text{ip}}}$$ during the backward step.
 
 These computations follow immediately from defining the model: Caffe plans and carries out the forward and backward passes for you.
 
index 6b0ec34..17430b3 100644 (file)
@@ -9,7 +9,13 @@ Caffe has command line, Python, and MATLAB interfaces for day-to-day usage, inte
 
 The command line interface -- cmdcaffe -- is the `caffe` tool for model training, scoring, and diagnostics. Run `caffe` without any arguments for help. This tool and others are found in caffe/build/tools. (The following example calls require completing the LeNet / MNIST example first.)
 
-**Training**: `caffe train` learns models from scratch, resumes learning from saved snapshots, and fine-tunes models to new data and tasks. All training requires a solver configuration through the `-solver solver.prototxt` argument. Resuming requires the `-snapshot model_iter_1000.solverstate` argument to load the solver snapshot. Fine-tuning requires the `-weights model.caffemodel` argument for the model initialization.
+**Training**: `caffe train` learns models from scratch, resumes learning from saved snapshots, and fine-tunes models to new data and tasks:
+
+* All training requires a solver configuration through the `-solver solver.prototxt` argument. 
+* Resuming requires the `-snapshot model_iter_1000.solverstate` argument to load the solver snapshot. 
+* Fine-tuning requires the `-weights model.caffemodel` argument for the model initialization.
+
+For example, you can run:
 
     # train LeNet
     caffe train -solver examples/mnist/lenet_solver.prototxt
@@ -26,17 +32,19 @@ For a full example of fine-tuning, see examples/finetuning_on_flickr_style, but
 **Testing**: `caffe test` scores models by running them in the test phase and reports the net output as its score. The net architecture must be properly defined to output an accuracy measure or loss as its output. The per-batch score is reported and then the grand average is reported last.
 
     #
-    # score the learned LeNet model on the validation set as defined in the model architeture lenet_train_test.prototxt
-    caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000 -gpu 0 -iterations 100
+    # score the learned LeNet model on the validation set as defined in the 
+    # model architeture lenet_train_test.prototxt
+    caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 100
 
 **Benchmarking**: `caffe time` benchmarks model execution layer-by-layer through timing and synchronization. This is useful to check system performance and measure relative execution times for models.
 
     # (These example calls require you complete the LeNet / MNIST example first.)
     # time LeNet training on CPU for 10 iterations
     caffe time -model examples/mnist/lenet_train_test.prototxt -iterations 10
-    # time a model architecture with the given weights on the first GPU for 10 iterations
     # time LeNet training on GPU for the default 50 iterations
     caffe time -model examples/mnist/lenet_train_test.prototxt -gpu 0
+    # time a model architecture with the given weights on the first GPU for 10 iterations
+    caffe time -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 10
 
 **Diagnostics**: `caffe device_query` reports GPU details for reference and checking device ordinals for running on a given device in multi-GPU machines.
 
index a329b49..4a95cee 100644 (file)
@@ -24,8 +24,8 @@ You will first need to download and convert the data format from the [CIFAR-10 w
 
     cd $CAFFE_ROOT/data/cifar10
     ./get_cifar10.sh
-    cd $CAFFE_ROOT/examples/cifar10
-    ./create_cifar10.sh
+    cd $CAFFE_ROOT
+    ./examples/cifar10/create_cifar10.sh
 
 If it complains that `wget` or `gunzip` are not installed, you need to install them respectively. After running the script there should be the dataset, `./cifar10-leveldb`, and the data set image mean `./mean.binaryproto`.
 
@@ -39,8 +39,8 @@ Training and Testing the "Quick" Model
 
 Training the model is simple after you have written the network definition protobuf and solver protobuf files (refer to [MNIST Tutorial](../examples/mnist.html)). Simply run `train_quick.sh`, or the following command directly:
 
-    cd $CAFFE_ROOT/examples/cifar10
-    ./train_quick.sh
+    cd $CAFFE_ROOT
+    ./examples/cifar10/train_quick.sh
 
 `train_quick.sh` is a simple script, so have a look inside. The main tool for training is `caffe` with the `train` action, and the solver protobuf text file as its argument.
 
index fb44da8..6b6c548 100644 (file)
@@ -3,7 +3,8 @@
   "description": "Use the pre-trained ImageNet model to classify images with the Python interface.",
   "example_name": "ImageNet classification",
   "include_in_docs": true,
-  "priority": 1
+  "priority": 1,
+  "signature": "sha256:2caae2c1fe3e282b8f836d380a45622351c91db18a1591e4f2fa67faba9ab72c"
  },
  "nbformat": 3,
  "nbformat_minor": 0,
@@ -19,7 +20,7 @@
       "\n",
       "Caffe provides a general Python interface for models with `caffe.Net` in `python/caffe/pycaffe.py`, but to make off-the-shelf classification easy we provide a `caffe.Classifier` class and `classify.py` script. Both Python and MATLAB wrappers are provided. However, the Python wrapper has more features so we will describe it here. For MATLAB, refer to `matlab/caffe/matcaffe_demo.m`.\n",
       "\n",
-      "Before we begin, you must compile Caffe and install the python wrapper by setting your `PYTHONPATH`. If you haven't yet done so, please refer to the [installation instructions](installation.html). This example uses our pre-trained CaffeNet model, an ILSVRC12 image classifier. You can download it by running `./scripts/download_model_binary.py models/bvlc_reference_caffenet`. Note that this pre-trained model is licensed for academic research / non-commercial use only.\n",
+      "Before we begin, you must compile Caffe and install the python wrapper by setting your `PYTHONPATH`. If you haven't yet done so, please refer to the [installation instructions](installation.html). This example uses our pre-trained CaffeNet model, an ILSVRC12 image classifier. You can download it by running `./scripts/download_model_binary.py models/bvlc_reference_caffenet`.\n",
       "\n",
       "Ready? Let's start."
      ]
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "Loading a network is easy. `caffe.Classifier` takes care of everything. Note the arguments for configuring input preprocessing: mean subtraction switched on by giving a mean array, input channel swapping takes care of mapping RGB into the reference ImageNet model's BGR order, and raw scaling multiplies the feature scale from the input [0,1] to the ImageNet model's [0,255]."
+      "Loading a network is easy. `caffe.Classifier` takes care of everything. Note the arguments for configuring input preprocessing: mean subtraction switched on by giving a mean array, input channel swapping takes care of mapping RGB into the reference ImageNet model's BGR order, and raw scaling multiplies the feature scale from the input [0,1] to the ImageNet model's [0,255].\n",
+      "\n",
+      "We will set the phase to test since we are doing testing, and will first use CPU for the computation."
      ]
     },
     {
      "cell_type": "code",
      "collapsed": false,
      "input": [
+      "caffe.set_phase_test()\n",
+      "caffe.set_mode_cpu()\n",
       "net = caffe.Classifier(MODEL_FILE, PRETRAINED,\n",
       "                       mean=np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy'),\n",
       "                       channel_swap=(2,1,0),\n",
      "cell_type": "markdown",
      "metadata": {},
      "source": [
-      "We will set the phase to test since we are doing testing, and will first use CPU for the computation."
-     ]
-    },
-    {
-     "cell_type": "code",
-     "collapsed": false,
-     "input": [
-      "net.set_phase_test()\n",
-      "net.set_mode_cpu()"
-     ],
-     "language": "python",
-     "metadata": {},
-     "outputs": [],
-     "prompt_number": 3
-    },
-    {
-     "cell_type": "markdown",
-     "metadata": {},
-     "source": [
       "Let's take a look at our example image with Caffe's image loading helper."
      ]
     },
       {
        "metadata": {},
        "output_type": "pyout",
-       "prompt_number": 4,
+       "prompt_number": 3,
        "text": [
         "<matplotlib.image.AxesImage at 0x7fda204c0e10>"
        ]
        ]
       }
      ],
-     "prompt_number": 4
+     "prompt_number": 3
     },
     {
      "cell_type": "markdown",
        ]
       }
      ],
-     "prompt_number": 5
+     "prompt_number": 4
     },
     {
      "cell_type": "markdown",
        ]
       }
      ],
-     "prompt_number": 6
+     "prompt_number": 5
     },
     {
      "cell_type": "markdown",
        ]
       }
      ],
-     "prompt_number": 7
+     "prompt_number": 6
     },
     {
      "cell_type": "markdown",
        ]
       }
      ],
-     "prompt_number": 8
+     "prompt_number": 7
     },
     {
      "cell_type": "markdown",
      "cell_type": "code",
      "collapsed": false,
      "input": [
-      "net.set_mode_gpu()"
+      "caffe.set_mode_gpu()"
      ],
      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 9
+     "prompt_number": 8
     },
     {
      "cell_type": "markdown",
       {
        "metadata": {},
        "output_type": "pyout",
-       "prompt_number": 10,
+       "prompt_number": 9,
        "text": [
         "[<matplotlib.lines.Line2D at 0x7fda1ac309d0>]"
        ]
        ]
       }
      ],
-     "prompt_number": 10
+     "prompt_number": 9
     },
     {
      "cell_type": "markdown",
        ]
       }
      ],
-     "prompt_number": 11
+     "prompt_number": 10
     },
     {
      "cell_type": "code",
        ]
       }
      ],
-     "prompt_number": 12
+     "prompt_number": 11
     },
     {
      "cell_type": "markdown",
    "metadata": {}
   }
  ]
-}
+}
\ No newline at end of file
index 09b416f..8e06880 100644 (file)
@@ -3,7 +3,8 @@
   "description": "Extracting features and visualizing trained filters with an example image, viewed layer-by-layer.",
   "example_name": "Filter visualization",
   "include_in_docs": true,
-  "priority": 2
+  "priority": 2,
+  "signature": "sha256:526501b358e0f60c489eaf5799e8603a75019cc65401533baa307b5603fdefa1"
  },
  "nbformat": 3,
  "nbformat_minor": 0,
      "cell_type": "code",
      "collapsed": false,
      "input": [
+      "caffe.set_phase_test()\n",
+      "caffe.set_mode_cpu()\n",
       "net = caffe.Classifier(caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt',\n",
       "                       caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel')\n",
-      "net.set_phase_test()\n",
-      "net.set_mode_cpu()\n",
       "# input preprocessing: 'data' is the name of the input blob == net.inputs[0]\n",
       "net.set_mean('data', np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy'))  # ImageNet mean\n",
       "net.set_raw_scale('data', 255)  # the reference model operates on images in [0,255] range instead of [0,1]\n",
    "metadata": {}
   }
  ]
-}
+}
\ No newline at end of file
index 6431001..a4fb4a6 100644 (file)
@@ -4,7 +4,7 @@
   "example_name": "Editing model parameters",
   "include_in_docs": true,
   "priority": 5,
-  "signature": "sha256:179fb20339497f5e64f6fbeb57987f27a962b7ae6d940c8fede2631aba9bffaf"
+  "signature": "sha256:bf84bcbd78fe007310f86d71c5969ba4205b6e06f408029860eec94821844bee"
  },
  "nbformat": 3,
  "nbformat_minor": 0,
       "import matplotlib.pyplot as plt\n",
       "%matplotlib inline\n",
       "\n",
+      "caffe.set_phase_test()\n",
+      "\n",
       "# load input and configure preprocessing\n",
       "im = caffe.io.load_image('images/cat.jpg')\n",
-      "net_full_conv.set_phase_test()\n",
       "net_full_conv.set_mean('data', np.load('../python/caffe/imagenet/ilsvrc_2012_mean.npy'))\n",
       "net_full_conv.set_channel_swap('data', (2,1,0))\n",
       "net_full_conv.set_raw_scale('data', 255.0)\n",
    "metadata": {}
   }
  ]
-}
+}
\ No newline at end of file
index 81b2e9a..5fc4ed3 100644 (file)
@@ -64,6 +64,9 @@ private:\
 // is executed we will see a fatal log.
 #define NOT_IMPLEMENTED LOG(FATAL) << "Not Implemented Yet"
 
+// See PR #1236
+namespace cv {class Mat;}
+
 namespace caffe {
 
 // We will use the boost shared_ptr instead of the new C++11 one mainly
index 84ebba2..0cfb5dc 100644 (file)
@@ -1,10 +1,6 @@
 #ifndef CAFFE_DATA_TRANSFORMER_HPP
 #define CAFFE_DATA_TRANSFORMER_HPP
 
-#ifndef OSX
-#include <opencv2/core/core.hpp>
-#endif
-
 #include <vector>
 
 #include "caffe/blob.hpp"
@@ -64,9 +60,7 @@ class DataTransformer {
    *    This is destination blob. It can be part of top blob's data if
    *    set_cpu_data() is used. See image_data_layer.cpp for an example.
    */
-#ifndef OSX
   void Transform(const cv::Mat& cv_img, Blob<Dtype>* transformed_blob);
-#endif
 
   /**
    * @brief Applies the same transformation defined in the data layer's
index 136ce95..eebf565 100644 (file)
@@ -76,13 +76,13 @@ class GaussianFiller : public Filler<Dtype> {
     CHECK_GE(sparse, -1);
     if (sparse >= 0) {
       // Sparse initialization is implemented for "weight" blobs; i.e. matrices.
-      // These have num == channels == 1; height is number of inputs; width is
+      // These have num == channels == 1; width is number of inputs; height is
       // number of outputs.  The 'sparse' variable specifies the mean number
       // of non-zero input weights for a given output.
       CHECK_EQ(blob->num(), 1);
       CHECK_EQ(blob->channels(), 1);
-      int num_inputs = blob->height();
-      Dtype non_zero_probability = Dtype(sparse) / Dtype(num_inputs);
+      int num_outputs = blob->height();
+      Dtype non_zero_probability = Dtype(sparse) / Dtype(num_outputs);
       rand_vec_.reset(new SyncedMemory(blob->count() * sizeof(int)));
       int* mask = reinterpret_cast<int*>(rand_vec_->mutable_cpu_data());
       caffe_rng_bernoulli(blob->count(), non_zero_probability, mask);
index 6a106e6..815ca54 100644 (file)
@@ -3,21 +3,13 @@
 
 #include "caffe/common.hpp"
 
-namespace caffe {
-
 /**
- * A minimal wrapper for boost::thread to force host compilation for boost
- * Defined in caffe/util/thread.hpp
+ Forward declare boost::thread instead of including boost/thread.hpp
+ to avoid a boost/NVCC issues (#1009, #1010) on OSX.
  */
-class Thread {
- public:
-  template<typename Callable, class A1>
-  Thread(Callable func, A1 a1);
-  void join();
-  bool joinable();
- private:
-  void* thread_;
-};
+namespace boost { class thread; }
+
+namespace caffe {
 
 /**
  * Virtual class encapsulate boost::thread for use in base class
@@ -26,7 +18,7 @@ class Thread {
  */
 class InternalThread {
  public:
-  InternalThread() : thread_(NULL) {}
+  InternalThread() : thread_() {}
   virtual ~InternalThread();
 
   /** Returns true if the thread was successfully started. **/
@@ -35,16 +27,16 @@ class InternalThread {
   /** Will not return until the internal thread has exited. */
   bool WaitForInternalThreadToExit();
 
-  bool is_started() const { return thread_ != NULL && thread_->joinable(); }
+  bool is_started() const;
 
  protected:
   /* Implement this method in your subclass
       with the code you want your thread to run. */
   virtual void InternalThreadEntry() {}
 
-  caffe::Thread* thread_;
+  shared_ptr<boost::thread> thread_;
 };
 
 }  // namespace caffe
 
-#endif
+#endif  // CAFFE_INTERNAL_THREAD_HPP_
index 1d06dc4..bbb220e 100644 (file)
@@ -89,7 +89,7 @@ class Net {
    * @brief For an already initialized net, implicitly copies (i.e., using no
    *        additional memory) the pre-trained layers from another Net.
    */
-  void ShareTrainedLayersWith(Net* other);
+  void ShareTrainedLayersWith(const Net* other);
   // For an already initialized net, CopyTrainedLayersFrom() copies the already
   // trained layers from another net parameter instance.
   /**
@@ -99,51 +99,73 @@ class Net {
   void CopyTrainedLayersFrom(const NetParameter& param);
   void CopyTrainedLayersFrom(const string trained_filename);
   /// @brief Writes the net to a proto.
-  void ToProto(NetParameter* param, bool write_diff = false);
+  void ToProto(NetParameter* param, bool write_diff = false) const;
 
   /// @brief returns the network name.
-  inline const string& name() { return name_; }
+  inline const string& name() const { return name_; }
   /// @brief returns the layer names
-  inline const vector<string>& layer_names() { return layer_names_; }
+  inline const vector<string>& layer_names() const { return layer_names_; }
   /// @brief returns the blob names
-  inline const vector<string>& blob_names() { return blob_names_; }
+  inline const vector<string>& blob_names() const { return blob_names_; }
   /// @brief returns the blobs
-  inline const vector<shared_ptr<Blob<Dtype> > >& blobs() { return blobs_; }
+  inline const vector<shared_ptr<Blob<Dtype> > >& blobs() const {
+    return blobs_;
+  }
   /// @brief returns the layers
-  inline const vector<shared_ptr<Layer<Dtype> > >& layers() { return layers_; }
+  inline const vector<shared_ptr<Layer<Dtype> > >& layers() const {
+    return layers_;
+  }
   /**
    * @brief returns the bottom vecs for each layer -- usually you won't
    *        need this unless you do per-layer checks such as gradients.
    */
-  inline vector<vector<Blob<Dtype>*> >& bottom_vecs() { return bottom_vecs_; }
+  inline const vector<vector<Blob<Dtype>*> >& bottom_vecs() const {
+    return bottom_vecs_;
+  }
   /**
    * @brief returns the top vecs for each layer -- usually you won't
    *        need this unless you do per-layer checks such as gradients.
    */
-  inline vector<vector<Blob<Dtype>*> >& top_vecs() { return top_vecs_; }
-  inline vector<vector<bool> >& bottom_need_backward() {
+  inline const vector<vector<Blob<Dtype>*> >& top_vecs() const {
+    return top_vecs_;
+  }
+  inline const vector<vector<bool> >& bottom_need_backward() const {
     return bottom_need_backward_;
   }
-  inline vector<Dtype>& blob_loss_weights() {
+  inline const vector<Dtype>& blob_loss_weights() const {
     return blob_loss_weights_;
   }
   /// @brief returns the parameters
-  inline vector<shared_ptr<Blob<Dtype> > >& params() { return params_; }
+  inline const vector<shared_ptr<Blob<Dtype> > >& params() const {
+    return params_;
+  }
   /// @brief returns the parameter learning rate multipliers
-  inline vector<float>& params_lr() { return params_lr_; }
-  inline vector<float>& params_weight_decay() { return params_weight_decay_; }
-  const map<string, int>& param_names_index() { return param_names_index_; }
+  inline const vector<float>& params_lr() const { return params_lr_; }
+  inline const vector<float>& params_weight_decay() const {
+    return params_weight_decay_;
+  }
+  const map<string, int>& param_names_index() const {
+    return param_names_index_;
+  }
   /// @brief Input and output blob numbers
-  inline int num_inputs() { return net_input_blobs_.size(); }
-  inline int num_outputs() { return net_output_blobs_.size(); }
-  inline vector<Blob<Dtype>*>& input_blobs() { return net_input_blobs_; }
-  inline vector<Blob<Dtype>*>& output_blobs() { return net_output_blobs_; }
-  inline vector<int>& input_blob_indices() { return net_input_blob_indices_; }
-  inline vector<int>& output_blob_indices() { return net_output_blob_indices_; }
-  bool has_blob(const string& blob_name);
-  const shared_ptr<Blob<Dtype> > blob_by_name(const string& blob_name);
-  bool has_layer(const string& layer_name);
-  const shared_ptr<Layer<Dtype> > layer_by_name(const string& layer_name);
+  inline int num_inputs() const { return net_input_blobs_.size(); }
+  inline int num_outputs() const { return net_output_blobs_.size(); }
+  inline const vector<Blob<Dtype>*>& input_blobs() const {
+    return net_input_blobs_;
+  }
+  inline const vector<Blob<Dtype>*>& output_blobs() const {
+    return net_output_blobs_;
+  }
+  inline const vector<int>& input_blob_indices() const {
+    return net_input_blob_indices_;
+  }
+  inline const vector<int>& output_blob_indices() const {
+    return net_output_blob_indices_;
+  }
+  bool has_blob(const string& blob_name) const;
+  const shared_ptr<Blob<Dtype> > blob_by_name(const string& blob_name) const;
+  bool has_layer(const string& layer_name) const;
+  const shared_ptr<Layer<Dtype> > layer_by_name(const string& layer_name) const;
 
   void set_debug_info(const bool value) { debug_info_ = value; }
 
index 64df015..a01f917 100644 (file)
@@ -1,10 +1,6 @@
 #ifndef CAFFE_UTIL_IO_H_
 #define CAFFE_UTIL_IO_H_
 
-#ifndef OSX
-#include <opencv2/core/core.hpp>
-#endif
-
 #include <unistd.h>
 #include <string>
 
@@ -13,6 +9,7 @@
 #include "hdf5_hl.h"
 
 #include "caffe/blob.hpp"
+#include "caffe/common.hpp"
 #include "caffe/proto/caffe.pb.h"
 
 #define HDF5_NUM_DIMS 4
@@ -127,57 +124,43 @@ inline bool DecodeDatum(Datum* datum) {
   return DecodeDatum(0, 0, true, datum);
 }
 
-#ifndef OSX
 cv::Mat ReadImageToCVMat(const string& filename,
     const int height, const int width, const bool is_color);
 
-inline cv::Mat ReadImageToCVMat(const string& filename,
-    const int height, const int width) {
-  return ReadImageToCVMat(filename, height, width, true);
-}
+cv::Mat ReadImageToCVMat(const string& filename,
+    const int height, const int width);
 
-inline cv::Mat ReadImageToCVMat(const string& filename,
-    const bool is_color) {
-  return ReadImageToCVMat(filename, 0, 0, is_color);
-}
+cv::Mat ReadImageToCVMat(const string& filename,
+    const bool is_color);
 
-inline cv::Mat ReadImageToCVMat(const string& filename) {
-  return ReadImageToCVMat(filename, 0, 0, true);
-}
+cv::Mat ReadImageToCVMat(const string& filename);
 
 cv::Mat DecodeDatumToCVMat(const Datum& datum,
     const int height, const int width, const bool is_color);
 
-inline cv::Mat DecodeDatumToCVMat(const Datum& datum,
-    const int height, const int width) {
-  return DecodeDatumToCVMat(datum, height, width, true);
-}
+cv::Mat DecodeDatumToCVMat(const Datum& datum,
+    const int height, const int width);
 
-inline cv::Mat DecodeDatumToCVMat(const Datum& datum,
-    const bool is_color) {
-  return DecodeDatumToCVMat(datum, 0, 0, is_color);
-}
+cv::Mat DecodeDatumToCVMat(const Datum& datum,
+    const bool is_color);
 
-inline cv::Mat DecodeDatumToCVMat(const Datum& datum) {
-  return DecodeDatumToCVMat(datum, 0, 0, true);
-}
+cv::Mat DecodeDatumToCVMat(const Datum& datum);
 
 void CVMatToDatum(const cv::Mat& cv_img, Datum* datum);
-#endif
 
 template <typename Dtype>
 void hdf5_load_nd_dataset_helper(
-  hid_t file_id, const char* dataset_name_, int min_dim, int max_dim,
-  Blob<Dtype>* blob);
+    hid_t file_id, const char* dataset_name_, int min_dim, int max_dim,
+    Blob<Dtype>* blob);
 
 template <typename Dtype>
 void hdf5_load_nd_dataset(
-  hid_t file_id, const char* dataset_name_, int min_dim, int max_dim,
-  Blob<Dtype>* blob);
+    hid_t file_id, const char* dataset_name_, int min_dim, int max_dim,
+    Blob<Dtype>* blob);
 
 template <typename Dtype>
 void hdf5_save_nd_dataset(
-  const hid_t file_id, const string dataset_name, const Blob<Dtype>& blob);
+    const hid_t file_id, const string& dataset_name, const Blob<Dtype>& blob);
 
 }  // namespace caffe
 
diff --git a/include/caffe/util/thread.hpp b/include/caffe/util/thread.hpp
deleted file mode 100644 (file)
index 7251402..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-#ifndef CAFFE_THREAD_CPP_HPP_
-#define CAFFE_THREAD_CPP_HPP_
-
-#include <boost/thread.hpp>
-#include "caffe/common.hpp"
-#include "caffe/internal_thread.hpp"
-
-namespace caffe {
-
-template<typename Callable, class A1>
-Thread::Thread(Callable func, A1 a1) {
-  this->thread_ = new boost::thread(func, a1);
-}
-
-void Thread::join() {
-  static_cast<boost::thread*>(this->thread_)->join();
-}
-
-bool Thread::joinable() {
-  return static_cast<boost::thread*>(this->thread_)->joinable();
-}
-
-}  // namespace caffe
-
-#endif
index c25fd4f..008d690 100644 (file)
@@ -2,7 +2,7 @@
 name: BVLC AlexNet Model
 caffemodel: bvlc_alexnet.caffemodel
 caffemodel_url: http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel
-license: non-commercial
+license: unrestricted
 sha1: 9116a64c0fbe4459d18f4bb6b56d647b63920377
 caffe_commit: 709dc15af4a06bebda027c1eb2b3f3e3375d5077
 ---
@@ -22,6 +22,4 @@ This model was trained by Evan Shelhamer @shelhamer
 
 ## License
 
-The data used to train this model comes from the ImageNet project, which distributes its database to researchers who agree to a following term of access:
-"Researcher shall use the Database only for non-commercial research and educational purposes."
-Accordingly, this model is distributed under a non-commercial license.
+This model is released for unrestricted use.
index b867e73..671e47a 100644 (file)
@@ -2,7 +2,7 @@
 name: BVLC CaffeNet Model
 caffemodel: bvlc_reference_caffenet.caffemodel
 caffemodel_url: http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel
-license: non-commercial
+license: unrestricted
 sha1: 4c8d77deb20ea792f84eb5e6d0a11ca0a8660a46
 caffe_commit: 709dc15af4a06bebda027c1eb2b3f3e3375d5077
 ---
@@ -22,6 +22,4 @@ This model was trained by Jeff Donahue @jeffdonahue
 
 ## License
 
-The data used to train this model comes from the ImageNet project, which distributes its database to researchers who agree to a following term of access:
-"Researcher shall use the Database only for non-commercial research and educational purposes."
-Accordingly, this model is distributed under a non-commercial license.
+This model is released for unrestricted use.
index 5d4bc5a..9a11a24 100644 (file)
@@ -2,7 +2,7 @@
 name: BVLC Reference RCNN ILSVRC13 Model
 caffemodel: bvlc_reference_rcnn_ilsvrc13.caffemodel
 caffemodel_url: http://dl.caffe.berkeleyvision.org/bvlc_reference_rcnn_ilsvrc13.caffemodel
-license: non-commercial
+license: unrestricted
 sha1: bdd8abb885819cba5e2fe1eb36235f2319477e64
 caffe_commit: a7e397abbda52c0b90323c23ab95bdeabee90a98
 ---
@@ -17,6 +17,4 @@ This model was trained by Ross Girshick @rbgirshick
 
 ## License
 
-The data used to train this model comes from the ImageNet project, which distributes its database to researchers who agree to a following term of access:
-"Researcher shall use the Database only for non-commercial research and educational purposes."
-Accordingly, this model is distributed under a non-commercial license.
+This model is released for unrestricted use.
index 430bfce..8dd2464 100644 (file)
@@ -1,4 +1,6 @@
 from .pycaffe import Net, SGDSolver
+from .pycaffe import set_mode_cpu, set_mode_gpu, set_phase_train, set_phase_test
+from .pycaffe import set_device
 from .classifier import Classifier
 from .detector import Detector
 import io
index 156b518..e576eeb 100644 (file)
@@ -148,6 +148,13 @@ void PySGDSolver::SolveResume(const string& resume_file) {
 }
 
 BOOST_PYTHON_MODULE(_caffe) {
+  // Caffe utility methods
+  bp::def("set_mode_cpu",          &set_mode_cpu);
+  bp::def("set_mode_gpu",          &set_mode_gpu);
+  bp::def("set_phase_train",       &set_phase_train);
+  bp::def("set_phase_test",        &set_phase_test);
+  bp::def("set_device",            &Caffe::SetDevice);
+
   // below, we prepend an underscore to methods that will be replaced
   // in Python
   bp::class_<PyNet, shared_ptr<PyNet> >(
@@ -158,11 +165,6 @@ BOOST_PYTHON_MODULE(_caffe) {
       .def("_forward",              &PyNet::Forward)
       .def("_backward",             &PyNet::Backward)
       .def("reshape",               &PyNet::Reshape)
-      .def("set_mode_cpu",          &PyNet::set_mode_cpu)
-      .def("set_mode_gpu",          &PyNet::set_mode_gpu)
-      .def("set_phase_train",       &PyNet::set_phase_train)
-      .def("set_phase_test",        &PyNet::set_phase_test)
-      .def("set_device",            &PyNet::set_device)
       .add_property("_blobs",       &PyNet::blobs)
       .add_property("layers",       &PyNet::layers)
       .add_property("_blob_names",  &PyNet::blob_names)
index a5cef74..78470c0 100644 (file)
@@ -20,6 +20,12 @@ using boost::shared_ptr;
 
 namespace caffe {
 
+// Selecting mode and phase.
+void set_mode_cpu() { Caffe::set_mode(Caffe::CPU); }
+void set_mode_gpu() { Caffe::set_mode(Caffe::GPU); }
+void set_phase_train() { Caffe::set_phase(Caffe::TRAIN); }
+void set_phase_test() { Caffe::set_phase(Caffe::TEST); }
+
 // wrap shared_ptr<Blob> in a class that we construct in C++ and pass
 // to Python
 template <typename Dtype>
@@ -117,13 +123,6 @@ class PyNet {
     WriteProtoToBinaryFile(net_param, filename.c_str());
   }
 
-  // The caffe::Caffe utility functions.
-  void set_mode_cpu() { Caffe::set_mode(Caffe::CPU); }
-  void set_mode_gpu() { Caffe::set_mode(Caffe::GPU); }
-  void set_phase_train() { Caffe::set_phase(Caffe::TRAIN); }
-  void set_phase_test() { Caffe::set_phase(Caffe::TEST); }
-  void set_device(int device_id) { Caffe::SetDevice(device_id); }
-
   vector<PyBlob<float> > blobs() {
     return vector<PyBlob<float> >(net_->blobs().begin(), net_->blobs().end());
   }
index fe471ca..f9a13a3 100644 (file)
@@ -24,12 +24,12 @@ class Classifier(caffe.Net):
             preprocessing options.
         """
         caffe.Net.__init__(self, model_file, pretrained_file)
-        self.set_phase_test()
+        caffe.set_phase_test()
 
         if gpu:
-            self.set_mode_gpu()
+            caffe.set_mode_gpu()
         else:
-            self.set_mode_cpu()
+            caffe.set_mode_cpu()
 
         if mean is not None:
             self.set_mean(self.inputs[0], mean)
index f219b61..b78abd1 100644 (file)
@@ -36,12 +36,12 @@ class Detector(caffe.Net):
             R-CNN feature extraction.
         """
         caffe.Net.__init__(self, model_file, pretrained_file)
-        self.set_phase_test()
+        caffe.set_phase_test()
 
         if gpu:
-            self.set_mode_gpu()
+            caffe.set_mode_gpu()
         else:
-            self.set_mode_cpu()
+            caffe.set_mode_cpu()
 
         if mean is not None:
             self.set_mean(self.inputs[0], mean)
index aabcfdd..a835402 100644 (file)
@@ -43,11 +43,17 @@ def resize_image(im, new_dims, interp_order=1):
     im: resized ndarray with shape (new_dims[0], new_dims[1], K)
     """
     if im.shape[-1] == 1 or im.shape[-1] == 3:
-        # skimage is fast but only understands {1,3} channel images in [0, 1].
         im_min, im_max = im.min(), im.max()
-        im_std = (im - im_min) / (im_max - im_min)
-        resized_std = resize(im_std, new_dims, order=interp_order)
-        resized_im = resized_std * (im_max - im_min) + im_min
+        if im_max > im_min:
+            # skimage is fast but only understands {1,3} channel images in [0, 1].
+            im_std = (im - im_min) / (im_max - im_min)
+            resized_std = resize(im_std, new_dims, order=interp_order)
+            resized_im = resized_std * (im_max - im_min) + im_min
+        else:
+            # the image is a constant -- avoid divide by 0
+            ret = np.empty((new_dims[0], new_dims[1], im.shape[-1]), dtype=np.float32)
+            ret.fill(im_min)
+            return ret
     else:
         # ndimage interpolates anything but more slowly.
         scale = tuple(np.array(new_dims) / np.array(im.shape[:2]))
index 31dc1f9..8899309 100644 (file)
@@ -8,6 +8,8 @@ from itertools import izip_longest
 import numpy as np
 
 from ._caffe import Net, SGDSolver
+from ._caffe import set_mode_cpu, set_mode_gpu, set_phase_train, set_phase_test
+from ._caffe import set_device
 import caffe.io
 
 # We directly update methods from Net here (rather than using composition or
index 4c35dcb..908373b 100644 (file)
@@ -13,3 +13,4 @@ pandas>=0.12.0
 python-dateutil>=1.4,<2
 protobuf>=2.5.0
 python-gflags>=2.0
+pyyaml>=3.10
index f8ace0e..0e28bd7 100755 (executable)
@@ -3,7 +3,7 @@
 
 PORT=${1:-4000}
 
-echo "usage: build.sh [port]"
+echo "usage: build_docs.sh [port]"
 
 # Find the docs dir, no matter where the script is called
 ROOT_DIR="$( cd "$(dirname "$0")"/.. ; pwd -P )"
index 023396c..209dc03 100644 (file)
@@ -1,6 +1,4 @@
-#ifndef OSX
 #include <opencv2/core/core.hpp>
-#endif
 
 #include <string>
 #include <vector>
@@ -175,7 +173,6 @@ void DataTransformer<Dtype>::Transform(const vector<Datum> & datum_vector,
   }
 }
 
-#ifndef OSX
 template<typename Dtype>
 void DataTransformer<Dtype>::Transform(const cv::Mat& cv_img,
                                        Blob<Dtype>* transformed_blob) {
@@ -276,7 +273,6 @@ void DataTransformer<Dtype>::Transform(const cv::Mat& cv_img,
     }
   }
 }
-#endif
 
 template<typename Dtype>
 void DataTransformer<Dtype>::Transform(Blob<Dtype>* input_blob,
index d7b6ae2..c2d19d4 100644 (file)
@@ -1,23 +1,24 @@
+#include <boost/thread.hpp>
 #include "caffe/internal_thread.hpp"
 
-#include "caffe/util/thread.hpp"
-
 namespace caffe {
 
 InternalThread::~InternalThread() {
   WaitForInternalThreadToExit();
-  if (thread_ != NULL) {
-    delete thread_;
-  }
 }
 
+bool InternalThread::is_started() const {
+  return thread_.get() != NULL && thread_->joinable();
+}
+
+
 bool InternalThread::StartInternalThread() {
   if (!WaitForInternalThreadToExit()) {
     return false;
   }
   try {
-    thread_ = new caffe::Thread
-        (&InternalThread::InternalThreadEntry, this);
+    thread_.reset(
+        new boost::thread(&InternalThread::InternalThreadEntry, this));
   } catch (...) {
     return false;
   }
index 5a286cd..8069483 100644 (file)
@@ -154,5 +154,5 @@ Layer<Dtype>* GetTanHLayer(const LayerParameter& param) {
 REGISTER_LAYER_CREATOR(TANH, GetTanHLayer);
 
 // Layers that use their constructor as their default creator should be
-// registered in their corresponding cpp files. Do not registere them here.
+// registered in their corresponding cpp files. Do not register them here.
 }  // namespace caffe
index 5d6f058..a503089 100644 (file)
@@ -1,3 +1,5 @@
+#include <opencv2/core/core.hpp>
+
 #include <stdint.h>
 
 #include <string>
index 50997a2..ef6a342 100644 (file)
@@ -1,3 +1,5 @@
+#include <opencv2/core/core.hpp>
+
 #include <fstream>  // NOLINT(readability/streams)
 #include <iostream>  // NOLINT(readability/streams)
 #include <string>
index e4492cf..47fc844 100644 (file)
@@ -636,7 +636,7 @@ void Net<Dtype>::UpdateDebugInfo(const int param_id) {
 }
 
 template <typename Dtype>
-void Net<Dtype>::ShareTrainedLayersWith(Net* other) {
+void Net<Dtype>::ShareTrainedLayersWith(const Net* other) {
   int num_source_layers = other->layers().size();
   for (int i = 0; i < num_source_layers; ++i) {
     Layer<Dtype>* source_layer = other->layers()[i].get();
@@ -726,7 +726,7 @@ void Net<Dtype>::CopyTrainedLayersFrom(const string trained_filename) {
 }
 
 template <typename Dtype>
-void Net<Dtype>::ToProto(NetParameter* param, bool write_diff) {
+void Net<Dtype>::ToProto(NetParameter* param, bool write_diff) const {
   param->Clear();
   param->set_name(name_);
   // Add bottom and top
@@ -785,16 +785,16 @@ void Net<Dtype>::Update() {
 }
 
 template <typename Dtype>
-bool Net<Dtype>::has_blob(const string& blob_name) {
+bool Net<Dtype>::has_blob(const string& blob_name) const {
   return blob_names_index_.find(blob_name) != blob_names_index_.end();
 }
 
 template <typename Dtype>
 const shared_ptr<Blob<Dtype> > Net<Dtype>::blob_by_name(
-    const string& blob_name) {
+    const string& blob_name) const {
   shared_ptr<Blob<Dtype> > blob_ptr;
   if (has_blob(blob_name)) {
-    blob_ptr = blobs_[blob_names_index_[blob_name]];
+    blob_ptr = blobs_[blob_names_index_.find(blob_name)->second];
   } else {
     blob_ptr.reset((Blob<Dtype>*)(NULL));
     LOG(WARNING) << "Unknown blob name " << blob_name;
@@ -803,16 +803,16 @@ const shared_ptr<Blob<Dtype> > Net<Dtype>::blob_by_name(
 }
 
 template <typename Dtype>
-bool Net<Dtype>::has_layer(const string& layer_name) {
+bool Net<Dtype>::has_layer(const string& layer_name) const {
   return layer_names_index_.find(layer_name) != layer_names_index_.end();
 }
 
 template <typename Dtype>
 const shared_ptr<Layer<Dtype> > Net<Dtype>::layer_by_name(
-    const string& layer_name) {
+    const string& layer_name) const {
   shared_ptr<Layer<Dtype> > layer_ptr;
   if (has_layer(layer_name)) {
-    layer_ptr = layers_[layer_names_index_[layer_name]];
+    layer_ptr = layers_[layer_names_index_.find(layer_name)->second];
   } else {
     layer_ptr.reset((Layer<Dtype>*)(NULL));
     LOG(WARNING) << "Unknown layer name " << layer_name;
index 8086ad6..d6eec02 100644 (file)
@@ -38,7 +38,7 @@ message FillerParameter {
   optional float max = 4 [default = 1]; // the max value in uniform filler
   optional float mean = 5 [default = 0]; // the mean value in Gaussian filler
   optional float std = 6 [default = 1]; // the std value in Gaussian filler
-  // The expected number of non-zero input weights for a given output in
+  // The expected number of non-zero output weights for a given input in
   // Gaussian filler -- the default -1 means don't perform sparsification.
   optional int32 sparse = 7 [default = -1];
 }
index ab9c00e..3fa0e2d 100644 (file)
@@ -418,7 +418,7 @@ Dtype SGDSolver<Dtype>::GetLearningRate() {
 template <typename Dtype>
 void SGDSolver<Dtype>::PreSolve() {
   // Initialize the history
-  vector<shared_ptr<Blob<Dtype> > >& net_params = this->net_->params();
+  const vector<shared_ptr<Blob<Dtype> > >& net_params = this->net_->params();
   history_.clear();
   update_.clear();
   temp_.clear();
@@ -439,9 +439,10 @@ void SGDSolver<Dtype>::PreSolve() {
 
 template <typename Dtype>
 void SGDSolver<Dtype>::ComputeUpdateValue() {
-  vector<shared_ptr<Blob<Dtype> > >& net_params = this->net_->params();
-  vector<float>& net_params_lr = this->net_->params_lr();
-  vector<float>& net_params_weight_decay = this->net_->params_weight_decay();
+  const vector<shared_ptr<Blob<Dtype> > >& net_params = this->net_->params();
+  const vector<float>& net_params_lr = this->net_->params_lr();
+  const vector<float>& net_params_weight_decay =
+      this->net_->params_weight_decay();
   // get the learning rate
   Dtype rate = GetLearningRate();
   if (this->param_.display() && this->iter_ % this->param_.display() == 0) {
@@ -552,9 +553,10 @@ void SGDSolver<Dtype>::RestoreSolverState(const SolverState& state) {
 
 template <typename Dtype>
 void NesterovSolver<Dtype>::ComputeUpdateValue() {
-  vector<shared_ptr<Blob<Dtype> > >& net_params = this->net_->params();
-  vector<float>& net_params_lr = this->net_->params_lr();
-  vector<float>& net_params_weight_decay = this->net_->params_weight_decay();
+  const vector<shared_ptr<Blob<Dtype> > >& net_params = this->net_->params();
+  const vector<float>& net_params_lr = this->net_->params_lr();
+  const vector<float>& net_params_weight_decay =
+      this->net_->params_weight_decay();
   // get the learning rate
   Dtype rate = this->GetLearningRate();
   if (this->param_.display() && this->iter_ % this->param_.display() == 0) {
@@ -667,9 +669,10 @@ void NesterovSolver<Dtype>::ComputeUpdateValue() {
 
 template <typename Dtype>
 void AdaGradSolver<Dtype>::ComputeUpdateValue() {
-  vector<shared_ptr<Blob<Dtype> > >& net_params = this->net_->params();
-  vector<float>& net_params_lr = this->net_->params_lr();
-  vector<float>& net_params_weight_decay = this->net_->params_weight_decay();
+  const vector<shared_ptr<Blob<Dtype> > >& net_params = this->net_->params();
+  const vector<float>& net_params_lr = this->net_->params_lr();
+  const vector<float>& net_params_weight_decay =
+      this->net_->params_weight_decay();
   // get the learning rate
   Dtype rate = this->GetLearningRate();
   Dtype delta = this->param_.delta();
index b136bc8..f738e05 100644 (file)
@@ -17,6 +17,8 @@
 #include "caffe/proto/caffe.pb.h"
 #include "caffe/util/io.hpp"
 
+const int kProtoReadBytesLimit = INT_MAX;  // Max size of 2 GB minus 1 byte.
+
 namespace caffe {
 
 using google::protobuf::io::FileInputStream;
@@ -50,7 +52,7 @@ bool ReadProtoFromBinaryFile(const char* filename, Message* proto) {
   CHECK_NE(fd, -1) << "File not found: " << filename;
   ZeroCopyInputStream* raw_input = new FileInputStream(fd);
   CodedInputStream* coded_input = new CodedInputStream(raw_input);
-  coded_input->SetTotalBytesLimit(1073741824, 536870912);
+  coded_input->SetTotalBytesLimit(kProtoReadBytesLimit, 536870912);
 
   bool success = proto->ParseFromCodedStream(coded_input);
 
@@ -83,6 +85,20 @@ cv::Mat ReadImageToCVMat(const string& filename,
   return cv_img;
 }
 
+cv::Mat ReadImageToCVMat(const string& filename,
+    const int height, const int width) {
+  return ReadImageToCVMat(filename, height, width, true);
+}
+
+cv::Mat ReadImageToCVMat(const string& filename,
+    const bool is_color) {
+  return ReadImageToCVMat(filename, 0, 0, is_color);
+}
+
+cv::Mat ReadImageToCVMat(const string& filename) {
+  return ReadImageToCVMat(filename, 0, 0, true);
+}
+
 bool ReadImageToDatum(const string& filename, const int label,
     const int height, const int width, const bool is_color, Datum* datum) {
   cv::Mat cv_img = ReadImageToCVMat(filename, height, width, is_color);
@@ -135,6 +151,20 @@ cv::Mat DecodeDatumToCVMat(const Datum& datum,
   return cv_img;
 }
 
+cv::Mat DecodeDatumToCVMat(const Datum& datum,
+    const int height, const int width) {
+  return DecodeDatumToCVMat(datum, height, width, true);
+}
+
+cv::Mat DecodeDatumToCVMat(const Datum& datum,
+    const bool is_color) {
+  return DecodeDatumToCVMat(datum, 0, 0, is_color);
+}
+
+cv::Mat DecodeDatumToCVMat(const Datum& datum) {
+  return DecodeDatumToCVMat(datum, 0, 0, true);
+}
+
 // If Datum is encoded will decoded using DecodeDatumToCVMat and CVMatToDatum
 // if height and width are set it will resize it
 // If Datum is not encoded will do nothing
@@ -226,7 +256,7 @@ void hdf5_load_nd_dataset<double>(hid_t file_id, const char* dataset_name_,
 
 template <>
 void hdf5_save_nd_dataset<float>(
-    const hid_t file_id, const string dataset_name, const Blob<float>& blob) {
+    const hid_t file_id, const string& dataset_name, const Blob<float>& blob) {
   hsize_t dims[HDF5_NUM_DIMS];
   dims[0] = blob.num();
   dims[1] = blob.channels();
@@ -239,7 +269,7 @@ void hdf5_save_nd_dataset<float>(
 
 template <>
 void hdf5_save_nd_dataset<double>(
-    const hid_t file_id, const string dataset_name, const Blob<double>& blob) {
+    const hid_t file_id, const string& dataset_name, const Blob<double>& blob) {
   hsize_t dims[HDF5_NUM_DIMS];
   dims[0] = blob.num();
   dims[1] = blob.channels();
index 9f9d975..ad54bc3 100644 (file)
@@ -220,8 +220,8 @@ int time() {
   caffe_net.Backward();
 
   const vector<shared_ptr<Layer<float> > >& layers = caffe_net.layers();
-  vector<vector<Blob<float>*> >& bottom_vecs = caffe_net.bottom_vecs();
-  vector<vector<Blob<float>*> >& top_vecs = caffe_net.top_vecs();
+  const vector<vector<Blob<float>*> >& bottom_vecs = caffe_net.bottom_vecs();
+  const vector<vector<Blob<float>*> >& top_vecs = caffe_net.top_vecs();
   const vector<vector<bool> >& bottom_need_backward =
       caffe_net.bottom_need_backward();
   LOG(INFO) << "*** Benchmark begins ***";
index c554ed3..5d42e69 100644 (file)
@@ -52,7 +52,7 @@ int main(int argc, char** argv) {
         "    http://www.image-net.org/download-images\n");
   gflags::ParseCommandLineFlags(&argc, &argv, true);
 
-  if (argc != 4) {
+  if (argc < 4) {
     gflags::ShowUsageWithFlagsRestrict(argv[0], "tools/convert_imageset");
     return 1;
   }