Skip to content
Merged
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
5 changes: 4 additions & 1 deletion Makefile.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
# CHANGE YOUR CUDA PATH IF IT IS NOT THIS
CUDA_DIR := /usr/local/cuda
# CHANGE YOUR CUDA ARCH IF IT IS NOT THIS
CUDA_ARCH := -arch=sm_30
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
# CHANGE YOUR MKL PATH IF IT IS NOT THIS
MKL_DIR := /opt/intel/mkl
# CHANGE YOUR MATLAB PATH IF IT IS NOT THIS
Expand Down
8 changes: 8 additions & 0 deletions include/caffe/blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class Blob {
inline int count() const {return count_; }
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;
}
// Copy from source. If copy_diff is false, we copy the data; if copy_diff
Expand Down
3 changes: 3 additions & 0 deletions include/caffe/util/math_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ void caffe_div(const int N, const Dtype* a, const Dtype* b, Dtype* y);
template <typename Dtype>
void caffe_powx(const int n, const Dtype* a, const Dtype b, Dtype* y);

template <typename Dtype>
Dtype caffe_nextafter(const Dtype b);

template <typename Dtype>
void caffe_vRngUniform(const int n, Dtype* r, const Dtype a, const Dtype b);

Expand Down
1 change: 1 addition & 0 deletions include/caffe/vision_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ class DataLayer : public Layer<Dtype> {
public:
explicit DataLayer(const LayerParameter& param)
: Layer<Dtype>(param) {}
virtual ~DataLayer();
virtual void SetUp(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top);

Expand Down
112 changes: 112 additions & 0 deletions install-dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Determine OS and various other system properties.

Determine the name of the platform used and other system properties such as
the location of Chrome. This is used, for example, to determine the correct
Toolchain to invoke.
"""

import optparse
import os
import re
import subprocess
import sys

##import oshelpers


SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))

if sys.version_info < (2, 6, 0):
sys.stderr.write("python 2.6 or later is required run this script\n")
sys.exit(1)


class Error(Exception):
pass

def GetPlatform():
if sys.platform.startswith('cygwin') or sys.platform.startswith('win'):
return 'win'
elif sys.platform.startswith('darwin'):
return 'mac'
elif sys.platform.startswith('linux'):
return 'linux'
else:
raise Error("Unknown platform: %s" % sys.platform)


## The above is part of
## http://src.chromium.org/chrome/trunk/src/native_client_sdk/src/tools/getos.py

"""Determine OS distributor and install caffe's dependencies

Determine OS distributor to choose the proper package management system.
This script currently only supports Ubuntu.
TODO: RHEL/Centos/Fedora, Mac etc.
"""
def get_linux_distributor(platform):
platform = platform.lower()
if platform == 'win':
return 'microsoft'

if platform == 'mac':
return 'apple'

distributor = None
if platform == 'linux':
try:
pobj = subprocess.Popen(['lsb_release', '-i'], stdout= subprocess.PIPE)
distributor = pobj.communicate()[0]
distributor = distributor.split(':')[-1].strip().lower()
if distributor.startswith('ubuntu'):
distributor = 'ubuntu'
except Exception:
pass
return distributor

def get_distribution_version(platform):
version = None
if platform == 'linux':
try:
pobj = subprocess.Popen(['lsb_release', '-r'], stdout= subprocess.PIPE)
version = pobj.communicate()[0]
version = version.split(':')[-1].strip().lower()
except Exception:
pass
return version


def install_caffe_dependencies():
platform = GetPlatform()
distributor = get_linux_distributor(platform)
dist_version = get_distribution_version(platform)
dev_libs = ['protobuf', 'leveldb', 'snappy', 'opencv', 'atlas-base']
ubuntu_boost_version = {'13.10':'1.53', '12.10':'1.50',
'12.04':'1.48', '12.04.3':'1.48'}
if distributor == 'ubuntu':
boost_version = ubuntu_boost_version[dist_version]
dev_libs.append('boost' + boost_version)
cmd = 'sudo apt-get -y --force-yes install ' + \
' '.join(['lib%s-dev' % lib for lib in dev_libs])
os.system(cmd)
try:
print cmd
subprocess.Popen(cmd, stdout= subprocess.PIPE)
except Exception:
return Error('Failed to install dependencies(platform: %s, \
distributor: %s, release: %s)'
% (platform, distributor, dist_version))
return 0


if __name__ == '__main__':
try:
sys.exit(install_caffe_dependencies())
except Error as e:
sys.stderr.write(str(e) + '\n')
sys.exit(1)
10 changes: 10 additions & 0 deletions src/caffe/layers/data_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ namespace caffe {

template <typename Dtype>
void* DataLayerPrefetch(void* layer_pointer) {
CHECK(layer_pointer);
DataLayer<Dtype>* layer = reinterpret_cast<DataLayer<Dtype>*>(layer_pointer);
CHECK(layer);
Datum datum;
CHECK(layer->prefetch_data_);
Dtype* top_data = layer->prefetch_data_->mutable_cpu_data();
Dtype* top_label = layer->prefetch_label_->mutable_cpu_data();
const Dtype scale = layer->layer_param_.scale();
Expand All @@ -38,6 +41,8 @@ void* DataLayerPrefetch(void* layer_pointer) {
const Dtype* mean = layer->data_mean_.cpu_data();
for (int itemid = 0; itemid < batchsize; ++itemid) {
// get a blob
CHECK(layer->iter_);
CHECK(layer->iter_->Valid());
datum.ParseFromString(layer->iter_->value().ToString());
const string& data = datum.data();
if (cropsize) {
Expand Down Expand Up @@ -109,6 +114,11 @@ void* DataLayerPrefetch(void* layer_pointer) {
return (void*)NULL;
}

template <typename Dtype>
DataLayer<Dtype>::~DataLayer<Dtype>() {
// Finally, join the thread
CHECK(!pthread_join(thread_, NULL)) << "Pthread joining failed.";
}

template <typename Dtype>
void DataLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
Expand Down
2 changes: 2 additions & 0 deletions src/caffe/layers/flatten_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Dtype FlattenLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const Dtype* top_diff = top[0]->cpu_diff();
Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff();
caffe_copy(count_, top_diff, bottom_diff);
return Dtype(0);
}


Expand All @@ -52,6 +53,7 @@ Dtype FlattenLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
const Dtype* top_diff = top[0]->gpu_diff();
Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff();
caffe_gpu_copy(count_, top_diff, bottom_diff);
return Dtype(0);
}

INSTANTIATE_CLASS(FlattenLayer);
Expand Down
4 changes: 2 additions & 2 deletions src/caffe/test/test_data_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ TYPED_TEST(DataLayerTest, TestRead) {
EXPECT_EQ(this->blob_top_label_->channels(), 1);
EXPECT_EQ(this->blob_top_label_->height(), 1);
EXPECT_EQ(this->blob_top_label_->width(), 1);
// Go throught the data twice
for (int iter = 0; iter < 2; ++iter) {
// Go through the data 100 times
for (int iter = 0; iter < 100; ++iter) {
layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_);
for (int i = 0; i < 5; ++i) {
EXPECT_EQ(i, this->blob_top_label_->cpu_data()[i]);
Expand Down
3 changes: 3 additions & 0 deletions src/caffe/test/test_flatten_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class FlattenLayerTest : public ::testing::Test {
FlattenLayerTest()
: blob_bottom_(new Blob<Dtype>(2, 3, 6, 5)),
blob_top_(new Blob<Dtype>()) {
Caffe::set_random_seed(1701);
// fill the values
FillerParameter filler_param;
GaussianFiller<Dtype> filler(filler_param);
Expand Down Expand Up @@ -72,6 +73,8 @@ TYPED_TEST(FlattenLayerTest, TestGPU) {
for (int c = 0; c < 3 * 6 * 5; ++c) {
EXPECT_EQ(this->blob_top_->data_at(0, c, 0, 0),
this->blob_bottom_->data_at(0, c / (6 * 5), (c / 5) % 6, c % 5));
EXPECT_EQ(this->blob_top_->data_at(1, c, 0, 0),
this->blob_bottom_->data_at(1, c / (6 * 5), (c / 5) % 6, c % 5));
}
}

Expand Down
21 changes: 13 additions & 8 deletions src/caffe/test/test_gradient_check_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ void GradientChecker<Dtype>::CheckGradientSingle(Layer<Dtype>& layer,
blobs_to_check.push_back(bottom[check_bottom]);
}
// go through the bottom and parameter blobs
// LOG(ERROR) << "Checking " << blobs_to_check.size() << " blobs.";
// LOG(ERROR) << "Checking " << blobs_to_check.size() << " blobs.";
for (int blobid = 0; blobid < blobs_to_check.size(); ++blobid) {
Blob<Dtype>* current_blob = blobs_to_check[blobid];
// LOG(ERROR) << "Blob " << blobid << ": checking " << current_blob->count()
// << " parameters.";
// LOG(ERROR) << "Blob " << blobid << ": checking " << current_blob->count()
// << " parameters.";
// go through the values
for (int feat_id = 0; feat_id < current_blob->count(); ++feat_id) {
// First, obtain the original data
Expand All @@ -96,25 +96,28 @@ void GradientChecker<Dtype>::CheckGradientSingle(Layer<Dtype>& layer,
// Get any additional loss from the layer
computed_objective += layer.Backward(top, true, &bottom);
Dtype computed_gradient = current_blob->cpu_diff()[feat_id];

// compute score by adding stepsize
current_blob->mutable_cpu_data()[feat_id] += stepsize_;
Caffe::set_random_seed(seed_);
layer.Forward(bottom, &top);
Dtype positive_objective = GetObjAndGradient(top, top_id, top_data_id);
positive_objective += layer.Backward(top, true, &bottom);

// compute score by subtracting stepsize
current_blob->mutable_cpu_data()[feat_id] -= stepsize_ * 2;
Caffe::set_random_seed(seed_);
layer.Forward(bottom, &top);
Dtype negative_objective = GetObjAndGradient(top, top_id, top_data_id);
negative_objective += layer.Backward(top, true, &bottom);

// Recover stepsize
current_blob->mutable_cpu_data()[feat_id] += stepsize_;
Dtype estimated_gradient = (positive_objective - negative_objective) /
stepsize_ / 2.;
Dtype feature = current_blob->cpu_data()[feat_id];
// LOG(ERROR) << "debug: " << current_blob->cpu_data()[feat_id] << " "
// << current_blob->cpu_diff()[feat_id];
// LOG(ERROR) << "debug: " << current_blob->cpu_data()[feat_id] << " "
// << current_blob->cpu_diff()[feat_id];
if (kink_ - kink_range_ > feature || feature > kink_ + kink_range_) {
// We check relative accuracy, but for too small values, we threshold
// the scale factor by 1.
Expand All @@ -126,10 +129,12 @@ void GradientChecker<Dtype>::CheckGradientSingle(Layer<Dtype>& layer,
EXPECT_LT(computed_gradient, estimated_gradient + threshold_ * scale)
<< "debug: (top_id, top_data_id, blob_id, feat_id)="
<< top_id << "," << top_data_id << "," << blobid << "," << feat_id;
// LOG(ERROR) << "computed gradient: " << computed_gradient
// << " estimated_gradient: " << estimated_gradient
// << " positive_objective: " << positive_objective
// << " negative_objective: " << negative_objective;
}
// LOG(ERROR) << "Feature: " << current_blob->cpu_data()[feat_id];
// LOG(ERROR) << "computed gradient: " << computed_gradient
// << " estimated_gradient: " << estimated_gradient;
// LOG(ERROR) << "Feature: " << current_blob->cpu_data()[feat_id]
}
}
}
Expand Down
Loading