Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
bearpaw committed Apr 13, 2016
2 parents 9b27363 + 857eb24 commit d383ce6
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,9 @@ ifeq ($(BLAS), mkl)
# MKL
LIBRARIES += mkl_rt
COMMON_FLAGS += -DUSE_MKL
MKL_DIR ?= /opt/intel/mkl
BLAS_INCLUDE ?= $(MKL_DIR)/include
BLAS_LIB ?= $(MKL_DIR)/lib $(MKL_DIR)/lib/intel64
MKLROOT ?= /opt/intel/mkl
BLAS_INCLUDE ?= $(MKLROOT)/include
BLAS_LIB ?= $(MKLROOT)/lib $(MKLROOT)/lib/intel64
else ifeq ($(BLAS), open)
# OpenBLAS
LIBRARIES += openblas
Expand Down
28 changes: 21 additions & 7 deletions python/caffe/pycaffe.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def _Net_blobs(self):
An OrderedDict (bottom to top, i.e., input to output) of network
blobs indexed by name
"""
return OrderedDict(zip(self._blob_names, self._blobs))
if not hasattr(self, '_blobs_dict'):
self._blobs_dict = OrderedDict(zip(self._blob_names, self._blobs))
return self._blobs_dict


@property
Expand All @@ -36,7 +38,10 @@ def _Net_blob_loss_weights(self):
An OrderedDict (bottom to top, i.e., input to output) of network
blob loss weights indexed by name
"""
return OrderedDict(zip(self._blob_names, self._blob_loss_weights))
if not hasattr(self, '_blobs_loss_weights_dict'):
self._blob_loss_weights_dict = OrderedDict(zip(self._blob_names,
self._blob_loss_weights))
return self._blob_loss_weights_dict


@property
Expand All @@ -46,19 +51,28 @@ def _Net_params(self):
parameters indexed by name; each is a list of multiple blobs (e.g.,
weights and biases)
"""
return OrderedDict([(name, lr.blobs)
for name, lr in zip(self._layer_names, self.layers)
if len(lr.blobs) > 0])
if not hasattr(self, '_params_dict'):
self._params_dict = OrderedDict([(name, lr.blobs)
for name, lr in zip(
self._layer_names, self.layers)
if len(lr.blobs) > 0])
return self._params_dict


@property
def _Net_inputs(self):
return [list(self.blobs.keys())[i] for i in self._inputs]
if not hasattr(self, '_input_list'):
keys = list(self.blobs.keys())
self._input_list = [keys[i] for i in self._inputs]
return self._input_list


@property
def _Net_outputs(self):
return [list(self.blobs.keys())[i] for i in self._outputs]
if not hasattr(self, '_output_list'):
keys = list(self.blobs.keys())
self._output_list = [keys[i] for i in self._outputs]
return self._output_list


def _Net_forward(self, blobs=None, start=None, end=None, **kwargs):
Expand Down
3 changes: 2 additions & 1 deletion src/caffe/layers/exp_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ void ExpLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const Dtype input_scale = this->layer_param_.exp_param().scale();
const Dtype input_shift = this->layer_param_.exp_param().shift();
inner_scale_ = log_base * input_scale;
outer_scale_ = (input_shift == Dtype(0)) ? Dtype(1) : pow(base, input_shift);
outer_scale_ = (input_shift == Dtype(0)) ? Dtype(1) :
( (base != Dtype(-1)) ? pow(base, input_shift) : exp(input_shift) );
}

template <typename Dtype>
Expand Down
9 changes: 4 additions & 5 deletions src/caffe/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,11 @@ int Net<Dtype>::AppendBottom(const NetParameter& param, const int layer_id,
bottom_vecs_[layer_id].push_back(blobs_[blob_id].get());
bottom_id_vecs_[layer_id].push_back(blob_id);
available_blobs->erase(blob_name);
bool propagate_down = true;
bool need_backward = blob_need_backward_[blob_id];
// Check if the backpropagation on bottom_id should be skipped
if (layer_param.propagate_down_size() > 0)
propagate_down = layer_param.propagate_down(bottom_id);
const bool need_backward = blob_need_backward_[blob_id] &&
propagate_down;
if (layer_param.propagate_down_size() > 0) {
need_backward = layer_param.propagate_down(bottom_id);
}
bottom_need_backward_[layer_id].push_back(need_backward);
return blob_id;
}
Expand Down
7 changes: 6 additions & 1 deletion src/caffe/proto/caffe.proto
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,12 @@ message LayerParameter {
// The blobs containing the numeric parameters of the layer.
repeated BlobProto blobs = 7;

// Specifies on which bottoms the backpropagation should be skipped.
// Specifies whether to backpropagate to each bottom. If unspecified,
// Caffe will automatically infer whether each input needs backpropagation
// to compute parameter gradients. If set to true for some inputs,
// backpropagation to those inputs is forced; if set false for some inputs,
// backpropagation to those inputs is skipped.
//
// The size must be either 0 or equal to the number of bottoms.
repeated bool propagate_down = 11;

Expand Down
102 changes: 102 additions & 0 deletions src/caffe/test/test_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,61 @@ class NetTest : public MultiDeviceTest<TypeParam> {
InitNetFromProtoString(proto);
}

virtual void InitForcePropNet(bool test_force_true) {
string proto =
"name: 'ForcePropTestNetwork' "
"layer { "
" name: 'data' "
" type: 'DummyData' "
" dummy_data_param { "
" shape { "
" dim: 5 "
" dim: 2 "
" dim: 3 "
" dim: 4 "
" } "
" data_filler { "
" type: 'gaussian' "
" std: 0.01 "
" } "
" shape { "
" dim: 5 "
" } "
" data_filler { "
" type: 'constant' "
" value: 0 "
" } "
" } "
" top: 'data' "
" top: 'label' "
"} "
"layer { "
" name: 'innerproduct' "
" type: 'InnerProduct' "
" inner_product_param { "
" num_output: 1 "
" weight_filler { "
" type: 'gaussian' "
" std: 0.01 "
" } "
" } "
" bottom: 'data' "
" top: 'innerproduct' ";
if (test_force_true) {
proto += " propagate_down: true ";
}
proto +=
"} "
"layer { "
" name: 'loss' "
" bottom: 'innerproduct' "
" bottom: 'label' "
" top: 'cross_entropy_loss' "
" type: 'SigmoidCrossEntropyLoss' "
"} ";
InitNetFromProtoString(proto);
}

int seed_;
shared_ptr<Net<Dtype> > net_;
};
Expand Down Expand Up @@ -2371,4 +2426,51 @@ TYPED_TEST(NetTest, TestSkipPropagateDown) {
}
}

TYPED_TEST(NetTest, TestForcePropagateDown) {
this->InitForcePropNet(false);
vector<bool> layer_need_backward = this->net_->layer_need_backward();
for (int layer_id = 0; layer_id < this->net_->layers().size(); ++layer_id) {
const string& layer_name = this->net_->layer_names()[layer_id];
const vector<bool> need_backward =
this->net_->bottom_need_backward()[layer_id];
if (layer_name == "data") {
ASSERT_EQ(need_backward.size(), 0);
EXPECT_FALSE(layer_need_backward[layer_id]);
} else if (layer_name == "innerproduct") {
ASSERT_EQ(need_backward.size(), 1);
EXPECT_FALSE(need_backward[0]); // data
EXPECT_TRUE(layer_need_backward[layer_id]);
} else if (layer_name == "loss") {
ASSERT_EQ(need_backward.size(), 2);
EXPECT_TRUE(need_backward[0]); // innerproduct
EXPECT_FALSE(need_backward[1]); // label
EXPECT_TRUE(layer_need_backward[layer_id]);
} else {
LOG(FATAL) << "Unknown layer: " << layer_name;
}
}
this->InitForcePropNet(true);
layer_need_backward = this->net_->layer_need_backward();
for (int layer_id = 0; layer_id < this->net_->layers().size(); ++layer_id) {
const string& layer_name = this->net_->layer_names()[layer_id];
const vector<bool> need_backward =
this->net_->bottom_need_backward()[layer_id];
if (layer_name == "data") {
ASSERT_EQ(need_backward.size(), 0);
EXPECT_FALSE(layer_need_backward[layer_id]);
} else if (layer_name == "innerproduct") {
ASSERT_EQ(need_backward.size(), 1);
EXPECT_TRUE(need_backward[0]); // data
EXPECT_TRUE(layer_need_backward[layer_id]);
} else if (layer_name == "loss") {
ASSERT_EQ(need_backward.size(), 2);
EXPECT_TRUE(need_backward[0]); // innerproduct
EXPECT_FALSE(need_backward[1]); // label
EXPECT_TRUE(layer_need_backward[layer_id]);
} else {
LOG(FATAL) << "Unknown layer: " << layer_name;
}
}
}

} // namespace caffe
20 changes: 20 additions & 0 deletions src/caffe/test/test_neuron_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,26 @@ TYPED_TEST(NeuronLayerTest, TestExpGradient) {
this->TestExpGradient(kBase, kScale, kShift);
}

TYPED_TEST(NeuronLayerTest, TestExpLayerWithShift) {
typedef typename TypeParam::Dtype Dtype;
// Test default base of "-1" -- should actually set base := e,
// with a non-zero shift
const Dtype kBase = -1;
const Dtype kScale = 1;
const Dtype kShift = 1;
this->TestExpForward(kBase, kScale, kShift);
}

TYPED_TEST(NeuronLayerTest, TestExpGradientWithShift) {
typedef typename TypeParam::Dtype Dtype;
// Test default base of "-1" -- should actually set base := e,
// with a non-zero shift
const Dtype kBase = -1;
const Dtype kScale = 1;
const Dtype kShift = 1;
this->TestExpGradient(kBase, kScale, kShift);
}

TYPED_TEST(NeuronLayerTest, TestExpLayerBase2) {
typedef typename TypeParam::Dtype Dtype;
const Dtype kBase = 2;
Expand Down
2 changes: 1 addition & 1 deletion tools/caffe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ DEFINE_string(gpu, "",
DEFINE_string(solver, "",
"The solver definition protocol buffer text file.");
DEFINE_string(model, "",
"The model definition protocol buffer text file..");
"The model definition protocol buffer text file.");
DEFINE_string(snapshot, "",
"Optional; the snapshot solver state to resume training.");
DEFINE_string(weights, "",
Expand Down

0 comments on commit d383ce6

Please sign in to comment.