Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cuDNN R2 #1731

Closed
wants to merge 83 commits into from
Closed
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
2f869e7
clarify draw_net.py usage: net prototxt, not caffemodel
shelhamer Jan 25, 2015
61c63f6
[docs] ask install + hardware questions on caffe-users
shelhamer Jan 25, 2015
4cc8195
[docs] send API link to class list
shelhamer Jan 29, 2015
1f7c3de
[docs] add check mode hint to CPU-only mode error
shelhamer Jan 29, 2015
8b96472
Brief explanation of SLICE layer's attributes
boechat107 Jan 30, 2015
75d0e16
lint 1f7c3de
shelhamer Jan 30, 2015
e3c895b
Merge pull request #1817 from boechat107/patch-1
shelhamer Jan 30, 2015
1e0d49a
Correct 'epochs' to 'iterations'
Feb 16, 2015
3e9b050
Merge pull request #1879 from bamos/patch-1
sguada Feb 16, 2015
f998127
Merge pull request #1849 from BVLC/next
shelhamer Feb 20, 2015
af01b9c
Updated the path for get_ilsvrc_aux.sh to match what is found in the …
Feb 20, 2015
5ee85b7
Merge pull request #1914 from eerwitt/master
shelhamer Feb 20, 2015
eabbccd
[build] fix dynamic linking of tools
shelhamer Feb 20, 2015
682d9da
Merge pull request #1921 from shelhamer/fix-tool-linking
shelhamer Feb 20, 2015
5a26333
check caffe tool runs in runtest
shelhamer Feb 21, 2015
a1e951d
ignore pycharm files
Feb 22, 2015
fca05c3
set proper CMAKE_INSTALL_RPATH for _caffe.so and tools
Feb 22, 2015
645aa03
fixed bug in install-tree: _caffe.so installed by install(TARGET ...)…
Feb 22, 2015
5e06d16
minor cmake sumamry log fix
Feb 22, 2015
569ae01
cpp_lint.py fails silently with Python3 (which is the default on some…
jsupancic Feb 22, 2015
cb1f4d6
Merge pull request #1939 from Nerei/bugfix/install_rpath_for_pycaffe
shelhamer Feb 22, 2015
845f9ea
APPLE was misspelled. in Line 27
spmallick Feb 24, 2015
486360d
Merge pull request #1948 from spmallick/patch-1
longjon Feb 24, 2015
c091197
Merge pull request #1941 from jsupancic/cpp_lint_python2
longjon Feb 24, 2015
360dbfd
Merge pull request #1926 from shelhamer/test-caffe-tool
longjon Feb 24, 2015
54037d3
Making python3 work with cmake and the new python wrapper
philkr Feb 17, 2015
b915f9d
Merge pull request #1923 from philkr/python3_master
longjon Feb 24, 2015
2cf5089
Decoding the datum before feeding it into the reshaping data layer
philkr Feb 25, 2015
4a3887a
fixed matcaffe printout to specify num of args (now including train/t…
forresti Feb 25, 2015
d2beb8a
Replaced illegal tab in Makefile with spaces.
gustavla Feb 25, 2015
1377e1b
Makefile fix for OS X 10.10
sergeyk Feb 25, 2015
3a1195a
Merge pull request #1961 from sergeyk/master
shelhamer Feb 25, 2015
b9aa166
Merge pull request #1960 from gustavla/makefile_fix
longjon Feb 25, 2015
7651970
Fixing two bugs related to python3 and PROJECT_SOURCE_DIR
philkr Feb 25, 2015
25cdd35
Small fix (visualization) on SLICE layer's documentation
boechat107 Feb 25, 2015
a677076
Merge pull request #1955 from philkr/reshaping_encoded
shelhamer Feb 26, 2015
4fba3da
Merge pull request #1999 from boechat107/patch-2
longjon Feb 27, 2015
1434e87
Blobs are ND arrays (for N not necessarily equals 4).
jeffdonahue Nov 26, 2014
5407f82
Add BlobShape message; use for Net input shapes
jeffdonahue Jan 1, 2015
119a1c6
add offset, {data,diff}_at nd blob accessors
jeffdonahue Feb 4, 2015
c4e9ec4
TestBlob: test that legacy BlobProtos are correctly handled by ShapeE…
jeffdonahue Nov 30, 2014
559ff3a
InnerProductLayer weights are 2D; biases are 1D
jeffdonahue Nov 26, 2014
cf9fdda
Fix sparse GaussianFiller for new IPLayer weight axes
jeffdonahue Feb 16, 2015
29581e6
InnerProductLayer can multiply along any axis
jeffdonahue Nov 29, 2014
94179cc
ConvLayer biases are 1D
jeffdonahue Nov 30, 2014
a0fa2a9
LossLayer output is 0D (scalar)
jeffdonahue Nov 26, 2014
d8c6aeb
AccuracyLayer output is 0D (scalar)
jeffdonahue Nov 30, 2014
6b8a765
AccuracyLayer generalized to N instance axes
jeffdonahue Jan 31, 2015
8e96445
Test{Net,Solver} fixes for AccuracyLayer generalization
jeffdonahue Feb 13, 2015
bf73cb1
EltwiseLayer need not assume old 4D dim names
jeffdonahue Nov 26, 2014
1b97c06
FlattenLayer: generalized Blob axes
jeffdonahue Nov 26, 2014
fb9caee
common_layers.hpp: remove unused "Blob col_bob_"
jeffdonahue Nov 26, 2014
704e524
TestConcatLayer: fix style errors
jeffdonahue Nov 26, 2014
d52e9a8
TestConcatLayer: add forward/gradient tests for concatenation along num
jeffdonahue Nov 26, 2014
8afdcd0
ConcatLayer: generalized Blob axes
jeffdonahue Nov 26, 2014
b868916
SliceLayer: generalized Blob axes
jeffdonahue Nov 26, 2014
abec302
SoftmaxLayer: generalized Blob axes
jeffdonahue Feb 15, 2015
60c288b
CuDNNSoftmaxLayer: generalized Blob axes
jeffdonahue Feb 10, 2015
94d93da
SoftmaxLossLayer generalized like SoftmaxLayer
jeffdonahue Jan 31, 2015
e2bc9f9
SplitLayer: change Reshape(n,h,c,w) to ReshapeLike(...)
jeffdonahue Nov 26, 2014
e6468e9
HDF5DataLayer shapes output according to HDF5 shape
jeffdonahue Nov 26, 2014
e56377d
DataLayer outputs 1D labels
jeffdonahue Nov 26, 2014
7c8725b
MemoryDataLayer outputs 1D labels
jeffdonahue Nov 26, 2014
c87a136
ImageDataLayer outputs 1D labels
jeffdonahue Nov 26, 2014
9505001
WindowDataLayer outputs 1D labels
jeffdonahue Nov 26, 2014
fcbb933
EuclideanLossLayer: generalized Blob axes
jeffdonahue Nov 30, 2014
7462c84
DummyDataLayer outputs blobs of arbitrary shape
jeffdonahue Jan 1, 2015
69fc1f6
Add CHECK_EQ(4, ...)s to "vision layers" to enforce that the
jeffdonahue Jan 16, 2015
269dafa
PyBlobs support generalized axes
jeffdonahue Jan 2, 2015
71df6f9
Add option not to reshape to Blob::FromProto; use when loading Blobs
jeffdonahue Jan 31, 2015
aa242aa
[pycaffe] expose Blob.reshape as *args function
longjon Mar 2, 2015
8c79d65
[pytest] use non-4d blobs in test_python_layer
longjon Mar 2, 2015
85bb397
Merge pull request #1970 from jeffdonahue/tensor-blob
shelhamer Mar 4, 2015
136139b
Merge pull request #1966 from philkr/python_fixes
longjon Mar 4, 2015
642619b
Add error checking for image mean
lukeyeager Feb 23, 2015
c0bc17c
Merge pull request #2031 from NVIDIA/image_mean
shelhamer Mar 4, 2015
dec148e
fix comment I forgot about from @shelhamer's review of #1970
jeffdonahue Mar 4, 2015
a3b0fbd
include/caffe/common.hpp: add <climits> for INT_MAX (now in blob.hpp)
jeffdonahue Mar 4, 2015
12e7317
Merge pull request #2035 from jeffdonahue/include-climits
jeffdonahue Mar 4, 2015
ccfa3dc
[pycaffe] check mean channels for transformation
shelhamer Mar 5, 2015
91a6597
switch to cuDNN R2
NV-slayton Feb 10, 2015
2ddbb04
replace cuDNN alphas and betas with coefficient values
shelhamer Feb 17, 2015
4beebcc
cuDNN pooling can pad now
shelhamer Feb 17, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 151 additions & 24 deletions include/caffe/blob.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#ifndef CAFFE_BLOB_HPP_
#define CAFFE_BLOB_HPP_

#include <algorithm>
#include <string>
#include <vector>

#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/syncedmem.hpp"
#include "caffe/util/math_functions.hpp"

const int kMaxBlobAxes = INT_MAX;

namespace caffe {

/**
Expand All @@ -19,10 +25,16 @@ template <typename Dtype>
class Blob {
public:
Blob()
: data_(), diff_(), num_(0), channels_(0), height_(0), width_(0),
count_(0), capacity_(0) {}
: data_(), diff_(), count_(0), capacity_(0) {}

/// @brief Deprecated; use <code>Blob(const vector<int>& shape)</code>.
explicit Blob(const int num, const int channels, const int height,
const int width);
const int width);
explicit Blob(const vector<int>& shape);

/// @brief Deprecated; use <code>Reshape(const vector<int>& shape)</code>.
void Reshape(const int num, const int channels, const int height,
const int width);
/**
* @brief Change the dimensions of the blob, allocating new memory if
* necessary.
Expand All @@ -37,25 +49,133 @@ class Blob {
* an error; either Net::Forward or Net::Reshape need to be called to
* propagate the new input shape to higher layers.
*/
void Reshape(const int num, const int channels, const int height,
const int width);
void Reshape(const vector<int>& shape);
void Reshape(const BlobShape& shape);
void ReshapeLike(const Blob& other);
inline int num() const { return num_; }
inline int channels() const { return channels_; }
inline int height() const { return height_; }
inline int width() const { return width_; }
inline string shape_string() const {
ostringstream stream;
for (int i = 0; i < shape_.size(); ++i) {
stream << shape_[i] << " ";
}
stream << "(" << count_ << ")";
return stream.str();
}
inline const vector<int>& shape() const { return shape_; }
/**
* @brief Returns the dimension of the index-th axis (or the negative index-th
* axis from the end, if index is negative).
*
* @param index the axis index, which may be negative as it will be
* "canonicalized" using CanonicalAxisIndex.
* Dies on out of range index.
*/
inline int shape(int index) const {
return shape_[CanonicalAxisIndex(index)];
}
inline int num_axes() const { return shape_.size(); }
inline int count() const { return count_; }

/**
* @brief Compute the volume of a slice; i.e., the product of dimensions
* among a range of axes.
*
* @param start_axis The first axis to include in the slice.
*
* @param end_axis The first axis to exclude from the slice.
*/
inline int count(int start_axis, int end_axis) const {
CHECK_LE(start_axis, end_axis);
CHECK_GE(start_axis, 0);
CHECK_GE(end_axis, 0);
CHECK_LE(start_axis, num_axes());
CHECK_LE(end_axis, num_axes());
int count = 1;
for (int i = start_axis; i < end_axis; ++i) {
count *= shape(i);
}
return count;
}
/**
* @brief Compute the volume of a slice spanning from a particular first
* axis to the final axis.
*
* @param start_axis The first axis to include in the slice.
*/
inline int count(int start_axis) const {
return count(start_axis, num_axes());
}

/**
* @brief Returns the 'canonical' version of a (usually) user-specified axis,
* allowing for negative indexing (e.g., -1 for the last axis).
*
* @param index the axis index.
* If 0 <= index < num_axes(), return index.
* If -num_axes <= index <= -1, return (num_axes() - (-index)),
* e.g., the last axis index (num_axes() - 1) if index == -1,
* the second to last if index == -2, etc.
* Dies on out of range index.
*/
inline int CanonicalAxisIndex(int axis_index) const {
CHECK_GE(axis_index, -num_axes())
<< "axis " << axis_index << " out of range for " << num_axes()
<< "-D Blob with shape " << shape_string();
CHECK_LT(axis_index, num_axes())
<< "axis " << axis_index << " out of range for " << num_axes()
<< "-D Blob with shape " << shape_string();
if (axis_index < 0) {
return axis_index + num_axes();
}
return axis_index;
}

/// @brief Deprecated legacy shape accessor num: use shape(0) instead.
inline int num() const { return LegacyShape(0); }
/// @brief Deprecated legacy shape accessor channels: use shape(1) instead.
inline int channels() const { return LegacyShape(1); }
/// @brief Deprecated legacy shape accessor height: use shape(2) instead.
inline int height() const { return LegacyShape(2); }
/// @brief Deprecated legacy shape accessor width: use shape(3) instead.
inline int width() const { return LegacyShape(3); }
inline int LegacyShape(int index) const {
CHECK_LE(num_axes(), 4)
<< "Cannot use legacy accessors on Blobs with > 4 axes.";
CHECK_LT(index, 4);
CHECK_GE(index, -4);
if (index >= num_axes() || index < -num_axes()) {
// Axis is out of range, but still in [0, 3] (or [-4, -1] for reverse
// indexing) -- this special case simulates the one-padding used to fill
// extraneous axes of legacy blobs.
return 1;
}
return shape(index);
}

inline int offset(const int n, const int c = 0, const int h = 0,
const int w = 0) const {
CHECK_GE(n, 0);
CHECK_LE(n, num_);
CHECK_GE(channels_, 0);
CHECK_LE(c, channels_);
CHECK_GE(height_, 0);
CHECK_LE(h, height_);
CHECK_GE(width_, 0);
CHECK_LE(w, width_);
return ((n * channels_ + c) * height_ + h) * width_ + w;
CHECK_LE(n, num());
CHECK_GE(channels(), 0);
CHECK_LE(c, channels());
CHECK_GE(height(), 0);
CHECK_LE(h, height());
CHECK_GE(width(), 0);
CHECK_LE(w, width());
return ((n * channels() + c) * height() + h) * width() + w;
}

inline int offset(const vector<int>& indices) const {
CHECK_LE(indices.size(), num_axes());
int offset = 0;
for (int i = 0; i < num_axes(); ++i) {
offset *= shape(i);
if (indices.size() > i) {
CHECK_GE(indices[i], 0);
CHECK_LT(indices[i], shape(i));
offset += indices[i];
}
}
return offset;
}
/**
* @brief Copy from a source Blob.
Expand All @@ -71,12 +191,20 @@ class Blob {

inline Dtype data_at(const int n, const int c, const int h,
const int w) const {
return *(cpu_data() + offset(n, c, h, w));
return cpu_data()[offset(n, c, h, w)];
}

inline Dtype diff_at(const int n, const int c, const int h,
const int w) const {
return *(cpu_diff() + offset(n, c, h, w));
return cpu_diff()[offset(n, c, h, w)];
}

inline Dtype data_at(const vector<int>& index) const {
return cpu_data()[offset(index)];
}

inline Dtype diff_at(const vector<int>& index) const {
return cpu_diff()[offset(index)];
}

inline const shared_ptr<SyncedMemory>& data() const {
Expand All @@ -99,7 +227,7 @@ class Blob {
Dtype* mutable_cpu_diff();
Dtype* mutable_gpu_diff();
void Update();
void FromProto(const BlobProto& proto);
void FromProto(const BlobProto& proto, bool reshape = true);
void ToProto(BlobProto* proto, bool write_diff = false) const;

/// @brief Compute the sum of absolute values (L1 norm) of the data.
Expand Down Expand Up @@ -135,13 +263,12 @@ class Blob {
*/
void ShareDiff(const Blob& other);

bool ShapeEquals(const BlobProto& other);

protected:
shared_ptr<SyncedMemory> data_;
shared_ptr<SyncedMemory> diff_;
int num_;
int channels_;
int height_;
int width_;
vector<int> shape_;
int count_;
int capacity_;

Expand Down
35 changes: 13 additions & 22 deletions include/caffe/common_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ class ConcatLayer : public Layer<Dtype> {
* - K @f$ (N \times C \times H \times W) @f$
* the inputs @f$ x_K @f$
* @param top output Blob vector (length 1)
* -# @f$ (KN \times C \times H \times W) @f$ if concat_dim == 0, or
* @f$ (N \times KC \times H \times W) @f$ if concat_dim == 1:
* -# @f$ (KN \times C \times H \times W) @f$ if axis == 0, or
* @f$ (N \times KC \times H \times W) @f$ if axis == 1:
* the concatenated output @f$
* y = [\begin{array}{cccc} x_1 & x_2 & ... & x_K \end{array}]
* @f$
Expand All @@ -115,8 +115,8 @@ class ConcatLayer : public Layer<Dtype> {
*
* @param top output Blob vector (length 1), providing the error gradient with
* respect to the outputs
* -# @f$ (KN \times C \times H \times W) @f$ if concat_dim == 0, or
* @f$ (N \times KC \times H \times W) @f$ if concat_dim == 1:
* -# @f$ (KN \times C \times H \times W) @f$ if axis == 0, or
* @f$ (N \times KC \times H \times W) @f$ if axis == 1:
* containing error gradients @f$ \frac{\partial E}{\partial y} @f$
* with respect to concatenated outputs @f$ y @f$
* @param propagate_down see Layer::Backward.
Expand All @@ -137,13 +137,10 @@ class ConcatLayer : public Layer<Dtype> {
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);

Blob<Dtype> col_bob_;
int count_;
int num_;
int channels_;
int height_;
int width_;
int concat_dim_;
int num_concats_;
int concat_input_size_;
int concat_axis_;
};

/**
Expand Down Expand Up @@ -216,8 +213,6 @@ class FlattenLayer : public Layer<Dtype> {
*/
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);

/**
* @brief Computes the error gradient w.r.t. the concatenate inputs.
Expand All @@ -230,10 +225,6 @@ class FlattenLayer : public Layer<Dtype> {
*/
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);

int count_;
};

/**
Expand Down Expand Up @@ -362,6 +353,9 @@ class SoftmaxLayer : public Layer<Dtype> {
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);

int outer_num_;
int inner_num_;
int softmax_axis_;
/// sum_multiplier is used to carry out sum using BLAS
Blob<Dtype> sum_multiplier_;
/// scale is an intermediate Blob to hold temporary results.
Expand Down Expand Up @@ -458,13 +452,10 @@ class SliceLayer : public Layer<Dtype> {
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);

Blob<Dtype> col_bob_;
int count_;
int num_;
int channels_;
int height_;
int width_;
int slice_dim_;
int num_slices_;
int slice_size_;
int slice_axis_;
vector<int> slice_point_;
};

Expand Down
5 changes: 2 additions & 3 deletions include/caffe/filler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ class GaussianFiller : public Filler<Dtype> {
// 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_outputs = blob->height();
CHECK_GE(blob->num_axes(), 1);
const int num_outputs = blob->shape(0);
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());
Expand Down
2 changes: 2 additions & 0 deletions include/caffe/loss_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,8 @@ class SoftmaxWithLossLayer : public LossLayer<Dtype> {
/// Whether to normalize the loss by the total number of values present
/// (otherwise just by the batch size).
bool normalize_;

int softmax_axis_, outer_num_, inner_num_;
};

} // namespace caffe
Expand Down
27 changes: 22 additions & 5 deletions python/caffe/_caffe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <boost/make_shared.hpp>
#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <numpy/arrayobject.h>

Expand Down Expand Up @@ -163,9 +164,10 @@ struct NdarrayCallPolicies : public bp::default_call_policies {
// the shape information from the blob.
void* data = PyArray_DATA(reinterpret_cast<PyArrayObject*>(result));
Py_DECREF(result);
npy_intp dims[] = {blob->num(), blob->channels(),
blob->height(), blob->width()};
PyObject* arr_obj = PyArray_SimpleNewFromData(4, dims, NPY_FLOAT32, data);
const int num_axes = blob->num_axes();
vector<npy_intp> dims(blob->shape().begin(), blob->shape().end());
PyObject *arr_obj = PyArray_SimpleNewFromData(num_axes, dims.data(),
NPY_FLOAT32, data);
// SetBaseObject steals a ref, so we need to INCREF.
Py_INCREF(pyblob.ptr());
PyArray_SetBaseObject(reinterpret_cast<PyArrayObject*>(arr_obj),
Expand All @@ -174,6 +176,20 @@ struct NdarrayCallPolicies : public bp::default_call_policies {
}
};

bp::object Blob_Reshape(bp::tuple args, bp::dict kwargs) {
if (bp::len(kwargs) > 0) {
throw std::runtime_error("Blob.reshape takes no kwargs");
}
Blob<Dtype>* self = bp::extract<Blob<Dtype>*>(args[0]);
vector<int> shape(bp::len(args) - 1);
for (int i = 1; i < bp::len(args); ++i) {
shape[i - 1] = bp::extract<int>(args[i]);
}
self->Reshape(shape);
// We need to explicitly return None to use bp::raw_function.
return bp::object();
}

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(SolveOverloads, Solve, 0, 1);

BOOST_PYTHON_MODULE(_caffe) {
Expand Down Expand Up @@ -218,8 +234,9 @@ BOOST_PYTHON_MODULE(_caffe) {
.add_property("channels", &Blob<Dtype>::channels)
.add_property("height", &Blob<Dtype>::height)
.add_property("width", &Blob<Dtype>::width)
.add_property("count", &Blob<Dtype>::count)
.def("reshape", &Blob<Dtype>::Reshape)
.add_property("count", static_cast<int (Blob<Dtype>::*)() const>(
&Blob<Dtype>::count))
.def("reshape", bp::raw_function(&Blob_Reshape))
.add_property("data", bp::make_function(&Blob<Dtype>::mutable_cpu_data,
NdarrayCallPolicies()))
.add_property("diff", bp::make_function(&Blob<Dtype>::mutable_cpu_diff,
Expand Down
Loading