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

Subsample #1

Open
wants to merge 18 commits into
base: future
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ include(cmake/Summary.cmake)
include(cmake/ConfigGen.cmake)

# ---[ Options
caffe_option(CPU_ONLY "Build Caffe wihtout CUDA support" OFF) # TODO: rename to USE_CUDA
caffe_option(CPU_ONLY "Build Caffe wihtout CUDA support" ON) # TODO: rename to USE_CUDA
caffe_option(USE_CUDNN "Build Caffe with cuDNN libary support" ON IF NOT CPU_ONLY)
caffe_option(BUILD_SHARED_LIBS "Build shared libraries" ON)
caffe_option(BUILD_python "Build Python wrapper" ON)
Expand Down
35 changes: 4 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
# Caffe
This is a pre-release Caffe branch for fully convolutional networks. This includes unmerged PRs and no guarantees.

Caffe is a deep learning framework made with expression, speed, and modularity in mind.
It is developed by the Berkeley Vision and Learning Center ([BVLC](http://bvlc.eecs.berkeley.edu)) and community contributors.
Everything here is subject to change, including the history of this branch.

Check out the [project site](http://caffe.berkeleyvision.org) for all the details like
Consider PR #2016 for reducing memory usage.

- [DIY Deep Learning for Vision with Caffe](https://docs.google.com/presentation/d/1UeKXVgRvvxg9OUdh_UiC5G71UMscNPlvArsWER41PsU/edit#slide=id.p)
- [Tutorial Documentation](http://caffe.berkeleyvision.org/tutorial/)
- [BVLC reference models](http://caffe.berkeleyvision.org/model_zoo.html) and the [community model zoo](https://github.com/BVLC/caffe/wiki/Model-Zoo)
- [Installation instructions](http://caffe.berkeleyvision.org/installation.html)

and step-by-step examples.

[![Join the chat at https://gitter.im/BVLC/caffe](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/BVLC/caffe?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Please join the [caffe-users group](https://groups.google.com/forum/#!forum/caffe-users) or [gitter chat](https://gitter.im/BVLC/caffe) to ask questions and talk about methods and models.
Framework development discussions and thorough bug reports are collected on [Issues](https://github.com/BVLC/caffe/issues).

Happy brewing!

## License and Citation

Caffe is released under the [BSD 2-Clause license](https://github.com/BVLC/caffe/blob/master/LICENSE).
The BVLC reference models are released for unrestricted use.

Please cite Caffe in your publications if it helps your research:

@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}
}
See `future.sh` for details.
3,610 changes: 3,249 additions & 361 deletions examples/classification.ipynb

Large diffs are not rendered by default.

7,364 changes: 6,933 additions & 431 deletions examples/net_surgery.ipynb

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions examples/python_nets/caffenet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from caffe import layers as L, params as P, to_proto
from caffe.proto import caffe_pb2

# helper function for common structures

def conv_relu(bottom, ks, nout, stride=1, pad=0, group=1):
conv = L.Convolution(bottom, kernel_size=ks, stride=stride,
num_output=nout, pad=pad, group=group)
return conv, L.ReLU(conv, in_place=True)

def fc_relu(bottom, nout):
fc = L.InnerProduct(bottom, num_output=nout)
return fc, L.ReLU(fc, in_place=True)

def max_pool(bottom, ks, stride=1):
return L.Pooling(bottom, pool=P.Pooling.MAX, kernel_size=ks, stride=stride)

def alexnet(lmdb, batch_size=256, include_acc=False):
data, label = L.Data(source=lmdb, backend=P.Data.LMDB, batch_size=batch_size, ntop=2,
transform_param=dict(crop_size=227, mean_value=[104, 117, 123], mirror=True))

# the net itself
conv1, relu1 = conv_relu(data, 11, 96, stride=4)
pool1 = max_pool(relu1, 3, stride=2)
norm1 = L.LRN(pool1, local_size=5, alpha=1e-4, beta=0.75)
conv2, relu2 = conv_relu(norm1, 5, 256, pad=2, group=2)
pool2 = max_pool(relu2, 3, stride=2)
norm2 = L.LRN(pool2, local_size=5, alpha=1e-4, beta=0.75)
conv3, relu3 = conv_relu(norm2, 3, 384, pad=1)
conv4, relu4 = conv_relu(relu3, 3, 384, pad=1, group=2)
conv5, relu5 = conv_relu(relu4, 3, 256, pad=1, group=2)
pool5 = max_pool(relu5, 3, stride=2)
fc6, relu6 = fc_relu(pool5, 4096)
drop6 = L.Dropout(relu6, in_place=True)
fc7, relu7 = fc_relu(drop6, 4096)
drop7 = L.Dropout(relu7, in_place=True)
fc8 = L.InnerProduct(drop7, num_output=1000)
loss = L.SoftmaxWithLoss(fc8, label)

if include_acc:
acc = L.Accuracy(fc8, label)
return to_proto((loss, acc), {v: k for k, v in locals().iteritems()})
else:
return to_proto(loss, {v: k for k, v in locals().iteritems()})

def make_net():
with open('train.prototxt', 'w') as f:
print >>f, alexnet('/path/to/caffe-train-lmdb')

with open('test.prototxt', 'w') as f:
print >>f, alexnet('/path/to/caffe-val-lmdb', batch_size=50, include_acc=True)

if __name__ == '__main__':
make_net()
24 changes: 24 additions & 0 deletions future.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
git checkout master
git branch -D future
git checkout -b future
## merge PRs
# coord maps, net pointer, crop layer
hub merge https://github.com/BVLC/caffe/pull/1976
# gradient accumulation
hub merge https://github.com/BVLC/caffe/pull/1977
# python net spec
hub merge https://github.com/BVLC/caffe/pull/2086
## commit
cat <<"END" > README.md
This is a pre-release Caffe branch for fully convolutional networks. This includes unmerged PRs and no guarantees.

Everything here is subject to change, including the history of this branch.

Consider PR #2016 for reducing memory usage.

See `future.sh` for details.
END
git add README.md
git add future.sh
git commit -m 'add README + creation script'
18 changes: 18 additions & 0 deletions include/caffe/common_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class ConcatLayer : public Layer<Dtype> {
virtual inline const char* type() const { return "Concat"; }
virtual inline int MinBottomBlobs() const { return 2; }
virtual inline int ExactNumTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
/**
Expand Down Expand Up @@ -162,6 +165,9 @@ class EltwiseLayer : public Layer<Dtype> {
virtual inline const char* type() const { return "Eltwise"; }
virtual inline int MinBottomBlobs() const { return 2; }
virtual inline int ExactNumTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down Expand Up @@ -280,6 +286,9 @@ class MVNLayer : public Layer<Dtype> {
virtual inline const char* type() const { return "MVN"; }
virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int ExactNumTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down Expand Up @@ -342,6 +351,9 @@ class SoftmaxLayer : public Layer<Dtype> {
virtual inline const char* type() const { return "Softmax"; }
virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int ExactNumTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down Expand Up @@ -408,6 +420,9 @@ class SplitLayer : public Layer<Dtype> {
virtual inline const char* type() const { return "Split"; }
virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int MinTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down Expand Up @@ -441,6 +456,9 @@ class SliceLayer : public Layer<Dtype> {
virtual inline const char* type() const { return "Slice"; }
virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int MinTopBlobs() const { return 2; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}

protected:
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
Expand Down
17 changes: 17 additions & 0 deletions include/caffe/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@

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

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/layer_factory.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/coords.hpp"
#include "caffe/util/device_alternate.hpp"

namespace caffe {

template <typename Dtype> class Net;

/**
* @brief An interface for the units of computation which can be composed into a
* Net.
Expand Down Expand Up @@ -285,6 +289,16 @@ class Layer {
param_propagate_down_[param_id] = value;
}

virtual DiagonalAffineMap<Dtype> coord_map() {
NOT_IMPLEMENTED;
// suppress warnings
return DiagonalAffineMap<Dtype>(vector<pair<Dtype, Dtype> >());
}

/**
* @brief Used by Net to give layers a pointer to their owning net.
*/
void set_net(Net<Dtype>* net) { net_ = net; }

protected:
/** The protobuf that stores the layer parameters */
Expand All @@ -300,6 +314,9 @@ class Layer {
* the objective function. */
vector<Dtype> loss_;

/** The net to which this layer belongs. */
Net<Dtype>* net_;

/** @brief Using the CPU device, compute the layer output. */
virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) = 0;
Expand Down
3 changes: 3 additions & 0 deletions include/caffe/neuron_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class NeuronLayer : public Layer<Dtype> {

virtual inline int ExactNumBottomBlobs() const { return 1; }
virtual inline int ExactNumTopBlobs() const { return 1; }
virtual inline DiagonalAffineMap<Dtype> coord_map() {
return DiagonalAffineMap<Dtype>::identity(2);
}
};

/**
Expand Down
46 changes: 46 additions & 0 deletions include/caffe/tempmem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef CAFFE_TEMPMEM_HPP_
#define CAFFE_TEMPMEM_HPP_

#include <cstdlib>

#include "caffe/common.hpp"
#include "caffe/syncedmem.hpp"
#include "caffe/util/math_functions.hpp"

namespace caffe {

/**
* @brief Holds a block of temporary memory that can be shared between
* different parts of caffe. The CPU and GPU memory is *not*
* synchronized.
*
* TODO(dox): more thorough description.
*/
template<typename Dtype>
class TemporaryMemory {
public:
TemporaryMemory():cpu_ptr_(NULL), gpu_ptr_(NULL), size_(0) {}
explicit TemporaryMemory(size_t size);
~TemporaryMemory();

void acquire_gpu();
void release_gpu();
void acquire_cpu();
void release_cpu();
const Dtype* cpu_data() const;
const Dtype* gpu_data() const;
Dtype* mutable_cpu_data();
Dtype* mutable_gpu_data();
size_t size() { return size_; }
void resize(size_t size);
private:
Dtype* cpu_ptr_;
Dtype* gpu_ptr_;
size_t size_;

DISABLE_COPY_AND_ASSIGN(TemporaryMemory);
}; // class TemporaryMemory

} // namespace caffe

#endif // CAFFE_TEMPMEM_HPP_
7 changes: 5 additions & 2 deletions include/caffe/test/test_gradient_check_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ void GradientChecker<Dtype>::CheckGradientSingle(Layer<Dtype>* layer,
CHECK_EQ(top_count, bottom[blob_id]->count());
}
}
// First, figure out what blobs we need to check against.
// First, figure out what blobs we need to check against, and zero init
// parameter blobs.
vector<Blob<Dtype>*> blobs_to_check;
vector<bool> propagate_down(bottom.size(), check_bottom < 0);
for (int i = 0; i < layer->blobs().size(); ++i) {
blobs_to_check.push_back(layer->blobs()[i].get());
Blob<Dtype>* blob = layer->blobs()[i].get();
caffe_set(blob->count(), static_cast<Dtype>(0), blob->mutable_cpu_diff());
blobs_to_check.push_back(blob);
}
if (check_bottom < 0) {
for (int i = 0; i < bottom.size(); ++i) {
Expand Down
61 changes: 61 additions & 0 deletions include/caffe/util/coords.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef CAFFE_UTIL_COORDS_H_
#define CAFFE_UTIL_COORDS_H_

#include <algorithm>
#include <utility>
#include <vector>

namespace caffe {

template <typename Dtype>
class DiagonalAffineMap {
public:
explicit DiagonalAffineMap(const vector<pair<Dtype, Dtype> > coefs)
: coefs_(coefs) { }
static DiagonalAffineMap identity(const int nd) {
return DiagonalAffineMap(vector<pair<Dtype, Dtype> >(nd, make_pair(1, 0)));
}

inline DiagonalAffineMap compose(const DiagonalAffineMap& other) const {
CHECK_EQ(coefs_.size(), other.coefs_.size())
<< "Attempt to compose DiagonalAffineMaps of different dimensions";
DiagonalAffineMap<Dtype> out;
transform(coefs_.begin(), coefs_.end(), other.coefs_.begin(),
std::back_inserter(out.coefs_), &compose_coefs);
return out;
}
inline DiagonalAffineMap inv() const {
DiagonalAffineMap<Dtype> out;
transform(coefs_.begin(), coefs_.end(), std::back_inserter(out.coefs_),
&inv_coefs);
return out;
}
inline vector<pair<Dtype, Dtype> > coefs() { return coefs_; }

private:
DiagonalAffineMap() { }
static inline pair<Dtype, Dtype> compose_coefs(pair<Dtype, Dtype> left,
pair<Dtype, Dtype> right) {
return make_pair(left.first * right.first,
left.first * right.second + left.second);
}
static inline pair<Dtype, Dtype> inv_coefs(pair<Dtype, Dtype> coefs) {
return make_pair(1 / coefs.first, - coefs.second / coefs.first);
}
vector<pair<Dtype, Dtype> > coefs_;
};

template <typename Dtype>
DiagonalAffineMap<Dtype> FilterMap(const int kernel_h, const int kernel_w,
const int stride_h, const int stride_w, const int pad_h, const int pad_w) {
vector<pair<Dtype, Dtype> > coefs;
coefs.push_back(make_pair(stride_h,
static_cast<Dtype>(kernel_h - 1) / 2 - pad_h));
coefs.push_back(make_pair(stride_w,
static_cast<Dtype>(kernel_w - 1) / 2 - pad_w));
return DiagonalAffineMap<Dtype>(coefs);
}

} // namespace caffe

#endif // CAFFE_UTIL_COORDS_H_
Loading