Skip to content

Commit

Permalink
Updated Caffe
Browse files Browse the repository at this point in the history
  • Loading branch information
gineshidalgo99 committed Nov 3, 2017
1 parent 97c5c2c commit 4147ea0
Show file tree
Hide file tree
Showing 15 changed files with 452 additions and 231 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/Versions.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Unix:
- Caffe:
- Version 1.0.0, extracted from GitHub on 09/30/2017 from the current master branch.
- Version 1.0.0, extracted from GitHub on 11/03/2017 from the current master branch.
- Link: https://github.com/BVLC/caffe

Windows:
Expand Down
2 changes: 1 addition & 1 deletion 3rdparty/caffe/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,6 @@ $(DISTRIBUTE_DIR): all py | $(DISTRIBUTE_SUBDIRS)
install -m 644 $(DYNAMIC_NAME) $(DISTRIBUTE_DIR)/lib
cd $(DISTRIBUTE_DIR)/lib; rm -f $(DYNAMIC_NAME_SHORT); ln -s $(DYNAMIC_VERSIONED_NAME_SHORT) $(DYNAMIC_NAME_SHORT)
# add python - it's not the standard way, indeed...
cp -r python $(DISTRIBUTE_DIR)/python
cp -r python $(DISTRIBUTE_DIR)/

-include $(DEPS)
2 changes: 1 addition & 1 deletion 3rdparty/caffe/docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,5 @@ To get a list of all options `googletest` provides, simply pass the `--help` fla

- **Run `make lint` to check C++ code.**
- Wrap lines at 80 chars.
- Follow [Google C++ style](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml) and [Google python style](http://google-styleguide.googlecode.com/svn/trunk/pyguide.html) + [PEP 8](http://legacy.python.org/dev/peps/pep-0008/).
- Follow [Google C++ style](https://google.github.io/styleguide/cppguide.html) and [Google python style](https://google.github.io/styleguide/pyguide.html) + [PEP 8](http://legacy.python.org/dev/peps/pep-0008/).
- Remember that “a foolish consistency is the hobgoblin of little minds,” so use your best judgement to write the clearest code for your particular case.
7 changes: 1 addition & 6 deletions 3rdparty/caffe/docs/install_apt.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Continue with [compilation](installation.html#compilation).

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

**CUDA**: Install by `apt-get` or the NVIDIA `.run` package.
The NVIDIA package tends to follow more recent library and driver versions, but the installation is more manual.
Expand All @@ -54,12 +55,6 @@ This can be skipped for CPU-only installation.

CUDA 8 is required on Ubuntu 16.04.

**Remaining dependencies, 14.04**

Everything is packaged in 14.04.

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

**Remaining dependencies, 12.04**

These dependencies need manual installation in 12.04.
Expand Down
2 changes: 1 addition & 1 deletion 3rdparty/caffe/docs/tutorial/layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Layers:
* [ELU](layers/elu.html) - exponential linear rectification.
* [Sigmoid](layers/sigmoid.html)
* [TanH](layers/tanh.html)
* [Absolute Value](layers/abs.html)
* [Absolute Value](layers/absval.html)
* [Power](layers/power.html) - f(x) = (shift + scale * x) ^ power.
* [Exp](layers/exp.html) - f(x) = base ^ (shift + scale * x).
* [Log](layers/log.html) - f(x) = log(x).
Expand Down
2 changes: 1 addition & 1 deletion 3rdparty/caffe/examples/web_demo/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ priority: 10
## Requirements

The demo server requires Python with some dependencies.
To make sure you have the dependencies, please run `pip install -r examples/web_demo/requirements.txt`, and also make sure that you've compiled the Python Caffe interface and that it is on your `PYTHONPATH` (see [installation instructions](/installation.html)).
To make sure you have the dependencies, please run `pip install -r examples/web_demo/requirements.txt`, and also make sure that you've compiled the Python Caffe interface and that it is on your `PYTHONPATH` (see [installation instructions](http://caffe.berkeleyvision.org/installation.html)).

Make sure that you have obtained the Reference CaffeNet Model and the ImageNet Auxiliary Data:

Expand Down
6 changes: 3 additions & 3 deletions 3rdparty/caffe/include/caffe/filler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ class BilinearFiller : public Filler<Dtype> {
CHECK_EQ(blob->width(), blob->height()) << "Filter must be square";
Dtype* data = blob->mutable_cpu_data();
int f = ceil(blob->width() / 2.);
float c = (2 * f - 1 - f % 2) / (2. * f);
Dtype c = (blob->width() - 1) / (2. * f);
for (int i = 0; i < blob->count(); ++i) {
float x = i % blob->width();
float y = (i / blob->width()) % blob->height();
Dtype x = i % blob->width();
Dtype y = (i / blob->width()) % blob->height();
data[i] = (1 - fabs(x / f - c)) * (1 - fabs(y / f - c));
}
CHECK_EQ(this->filler_param_.sparse(), -1)
Expand Down
4 changes: 4 additions & 0 deletions 3rdparty/caffe/include/caffe/layers/accuracy_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class AccuracyLayer : 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 Not implemented -- AccuracyLayer cannot be used as a loss.
Expand All @@ -77,6 +79,8 @@ class AccuracyLayer : public Layer<Dtype> {
if (propagate_down[i]) { NOT_IMPLEMENTED; }
}
}
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);

int label_axis_, outer_num_, inner_num_;

Expand Down
21 changes: 11 additions & 10 deletions 3rdparty/caffe/include/caffe/layers/infogain_loss_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
namespace caffe {

/**
* @brief A generalization of MultinomialLogisticLossLayer that takes an
* @brief A generalization of SoftmaxWithLossLayer that takes an
* "information gain" (infogain) matrix specifying the "value" of all label
* pairs.
*
* Equivalent to the MultinomialLogisticLossLayer if the infogain matrix is the
* Equivalent to the SoftmaxWithLossLayer if the infogain matrix is the
* identity.
*
* @param bottom input Blob vector (length 2-3)
* -# @f$ (N \times C \times H \times W) @f$
* the predictions @f$ \hat{p} @f$, a Blob with values in
* @f$ [0, 1] @f$ indicating the predicted probability of each of the
* @f$ K = CHW @f$ classes. Each prediction vector @f$ \hat{p}_n @f$
* should sum to 1 as in a probability distribution: @f$
* \forall n \sum\limits_{k=1}^K \hat{p}_{nk} = 1 @f$.
* the predictions @f$ x @f$, a Blob with values in
* @f$ [-\infty, +\infty] @f$ indicating the predicted score for each of
* the @f$ K = CHW @f$ classes. This layer maps these scores to a
* probability distribution over classes using the softmax function
* @f$ \hat{p}_{nk} = \exp(x_{nk}) /
* \left[\sum_{k'} \exp(x_{nk'})\right] @f$ (see SoftmaxLayer).
* -# @f$ (N \times 1 \times 1 \times 1) @f$
* the labels @f$ l @f$, an integer-valued Blob with values
* @f$ l_n \in [0, 1, 2, ..., K - 1] @f$
Expand All @@ -35,7 +36,7 @@ namespace caffe {
* (\b optional) the infogain matrix @f$ H @f$. This must be provided as
* the third bottom blob input if not provided as the infogain_mat in the
* InfogainLossParameter. If @f$ H = I @f$, this layer is equivalent to the
* MultinomialLogisticLossLayer.
* SoftmaxWithLossLayer.
* @param top output Blob vector (length 1)
* -# @f$ (1 \times 1 \times 1 \times 1) @f$
* the computed infogain multinomial logistic loss: @f$ E =
Expand Down Expand Up @@ -98,8 +99,8 @@ class InfogainLossLayer : public LossLayer<Dtype> {
* infogain matrix, if provided as bottom[2])
* @param bottom input Blob vector (length 2-3)
* -# @f$ (N \times C \times H \times W) @f$
* the predictions @f$ \hat{p} @f$; Backward computes diff
* @f$ \frac{\partial E}{\partial \hat{p}} @f$
* the predictions @f$ x @f$; Backward computes diff
* @f$ \frac{\partial E}{\partial x} @f$
* -# @f$ (N \times 1 \times 1 \times 1) @f$
* the labels -- ignored as we can't compute their error gradients
* -# @f$ (1 \times 1 \times K \times K) @f$
Expand Down
2 changes: 1 addition & 1 deletion 3rdparty/caffe/python/caffe/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def resize_image(im, new_dims, interp_order=1):
# 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_std = resize(im_std, new_dims, order=interp_order, mode='constant')
resized_im = resized_std * (im_max - im_min) + im_min
else:
# the image is a constant -- avoid divide by 0
Expand Down
50 changes: 25 additions & 25 deletions 3rdparty/caffe/python/caffe/test/test_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,41 +72,41 @@ def test_forward_backward(self):
self.net.backward()

def test_forward_start_end(self):
conv_blob=self.net.blobs['conv'];
ip_blob=self.net.blobs['ip_blob'];
sample_data=np.random.uniform(size=conv_blob.data.shape);
sample_data=sample_data.astype(np.float32);
conv_blob.data[:]=sample_data;
forward_blob=self.net.forward(start='ip',end='ip');
self.assertIn('ip_blob',forward_blob);

manual_forward=[];
conv_blob=self.net.blobs['conv']
ip_blob=self.net.blobs['ip_blob']
sample_data=np.random.uniform(size=conv_blob.data.shape)
sample_data=sample_data.astype(np.float32)
conv_blob.data[:]=sample_data
forward_blob=self.net.forward(start='ip',end='ip')
self.assertIn('ip_blob',forward_blob)

manual_forward=[]
for i in range(0,conv_blob.data.shape[0]):
dot=np.dot(self.net.params['ip'][0].data,
conv_blob.data[i].reshape(-1));
manual_forward.append(dot+self.net.params['ip'][1].data);
manual_forward=np.array(manual_forward);
conv_blob.data[i].reshape(-1))
manual_forward.append(dot+self.net.params['ip'][1].data)
manual_forward=np.array(manual_forward)

np.testing.assert_allclose(ip_blob.data,manual_forward,rtol=1e-3);
np.testing.assert_allclose(ip_blob.data,manual_forward,rtol=1e-3,atol=1e-5)

def test_backward_start_end(self):
conv_blob=self.net.blobs['conv'];
ip_blob=self.net.blobs['ip_blob'];
conv_blob=self.net.blobs['conv']
ip_blob=self.net.blobs['ip_blob']
sample_data=np.random.uniform(size=ip_blob.data.shape)
sample_data=sample_data.astype(np.float32);
ip_blob.diff[:]=sample_data;
backward_blob=self.net.backward(start='ip',end='ip');
self.assertIn('conv',backward_blob);
sample_data=sample_data.astype(np.float32)
ip_blob.diff[:]=sample_data
backward_blob=self.net.backward(start='ip',end='ip')
self.assertIn('conv',backward_blob)

manual_backward=[];
manual_backward=[]
for i in range(0,conv_blob.data.shape[0]):
dot=np.dot(self.net.params['ip'][0].data.transpose(),
sample_data[i].reshape(-1));
manual_backward.append(dot);
manual_backward=np.array(manual_backward);
manual_backward=manual_backward.reshape(conv_blob.data.shape);
sample_data[i].reshape(-1))
manual_backward.append(dot)
manual_backward=np.array(manual_backward)
manual_backward=manual_backward.reshape(conv_blob.data.shape)

np.testing.assert_allclose(conv_blob.diff,manual_backward,rtol=1e-3);
np.testing.assert_allclose(conv_blob.diff,manual_backward,rtol=1e-3,atol=1e-5)

def test_clear_param_diffs(self):
# Run a forward/backward step to have non-zero diffs
Expand Down
33 changes: 16 additions & 17 deletions 3rdparty/caffe/src/caffe/layers/accuracy_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ void AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const Dtype* bottom_label = bottom[1]->cpu_data();
const int dim = bottom[0]->count() / outer_num_;
const int num_labels = bottom[0]->shape(label_axis_);
vector<Dtype> maxval(top_k_+1);
vector<int> max_id(top_k_+1);
if (top.size() > 1) {
caffe_set(nums_buffer_.count(), Dtype(0), nums_buffer_.mutable_cpu_data());
caffe_set(top[1]->count(), Dtype(0), top[1]->mutable_cpu_data());
Expand All @@ -66,25 +64,22 @@ void AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
if (has_ignore_label_ && label_value == ignore_label_) {
continue;
}
if (top.size() > 1) ++nums_buffer_.mutable_cpu_data()[label_value];
DCHECK_GE(label_value, 0);
DCHECK_LT(label_value, num_labels);
if (top.size() > 1) ++nums_buffer_.mutable_cpu_data()[label_value];
const Dtype prob_of_true_class = bottom_data[i * dim
+ label_value * inner_num_
+ j];
int num_better_predictions = -1; // true_class also counts as "better"
// Top-k accuracy
std::vector<std::pair<Dtype, int> > bottom_data_vector;
for (int k = 0; k < num_labels; ++k) {
bottom_data_vector.push_back(std::make_pair(
bottom_data[i * dim + k * inner_num_ + j], k));
for (int k = 0; k < num_labels && num_better_predictions < top_k_; ++k) {
num_better_predictions +=
(bottom_data[i * dim + k * inner_num_ + j] >= prob_of_true_class);
}
std::partial_sort(
bottom_data_vector.begin(), bottom_data_vector.begin() + top_k_,
bottom_data_vector.end(), std::greater<std::pair<Dtype, int> >());
// check if true label is in top k predictions
for (int k = 0; k < top_k_; k++) {
if (bottom_data_vector[k].second == label_value) {
++accuracy;
if (top.size() > 1) ++top[1]->mutable_cpu_data()[label_value];
break;
}
// check if there are less than top_k_ predictions
if (num_better_predictions < top_k_) {
++accuracy;
if (top.size() > 1) ++top[1]->mutable_cpu_data()[label_value];
}
++count;
}
Expand All @@ -102,6 +97,10 @@ void AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
// Accuracy layer should not be used as a loss function.
}

#ifdef CPU_ONLY
STUB_GPU(AccuracyLayer);
#endif

INSTANTIATE_CLASS(AccuracyLayer);
REGISTER_LAYER_CLASS(Accuracy);

Expand Down
Loading

0 comments on commit 4147ea0

Please sign in to comment.