Skip to content

Commit 51f79a8

Browse files
committed
drop Net inputs + Forward with bottoms
Drop special cases for `input` fields, the `Net` input members, and the `Net` interface for Forward with bottoms along with Forward() / ForwardPrefilled() distinction.
1 parent bddd04b commit 51f79a8

File tree

9 files changed

+61
-259
lines changed

9 files changed

+61
-259
lines changed

include/caffe/net.hpp

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ class Net {
3232
void Init(const NetParameter& param);
3333

3434
/**
35-
* @brief Run Forward with the input Blob%s already fed separately.
35+
* @brief Run Forward and return the result.
3636
*
37-
* You can get the input blobs using input_blobs().
3837
*/
39-
const vector<Blob<Dtype>*>& ForwardPrefilled(Dtype* loss = NULL);
38+
const vector<Blob<Dtype>*>& Forward(Dtype* loss = NULL);
4039

4140
/**
4241
* The From and To variants of Forward and Backward operate on the
@@ -49,14 +48,6 @@ class Net {
4948
Dtype ForwardFromTo(int start, int end);
5049
Dtype ForwardFrom(int start);
5150
Dtype ForwardTo(int end);
52-
/// @brief Run forward using a set of bottom blobs, and return the result.
53-
const vector<Blob<Dtype>*>& Forward(const vector<Blob<Dtype>* > & bottom,
54-
Dtype* loss = NULL);
55-
/**
56-
* @brief Run forward using a serialized BlobProtoVector and return the
57-
* result as a serialized BlobProtoVector
58-
*/
59-
string Forward(const string& input_blob_protos, Dtype* loss = NULL);
6051

6152
/**
6253
* @brief Zeroes out the diffs of all net parameters.
@@ -82,9 +73,9 @@ class Net {
8273
*/
8374
void Reshape();
8475

85-
Dtype ForwardBackward(const vector<Blob<Dtype>* > & bottom) {
76+
Dtype ForwardBackward() {
8677
Dtype loss;
87-
Forward(bottom, &loss);
78+
Forward(&loss);
8879
Backward();
8980
return loss;
9081
}
@@ -194,18 +185,11 @@ class Net {
194185
inline const vector<string>& param_display_names() const {
195186
return param_display_names_;
196187
}
197-
/// @brief Input and output blob numbers
198-
inline int num_inputs() const { return net_input_blobs_.size(); }
188+
/// @brief output blob number
199189
inline int num_outputs() const { return net_output_blobs_.size(); }
200-
inline const vector<Blob<Dtype>*>& input_blobs() const {
201-
return net_input_blobs_;
202-
}
203190
inline const vector<Blob<Dtype>*>& output_blobs() const {
204191
return net_output_blobs_;
205192
}
206-
inline const vector<int>& input_blob_indices() const {
207-
return net_input_blob_indices_;
208-
}
209193
inline const vector<int>& output_blob_indices() const {
210194
return net_output_blob_indices_;
211195
}
@@ -229,7 +213,7 @@ class Net {
229213

230214
protected:
231215
// Helpers for Init.
232-
/// @brief Append a new input or top blob to the net.
216+
/// @brief Append a new top blob to the net.
233217
void AppendTop(const NetParameter& param, const int layer_id,
234218
const int top_id, set<string>* available_blobs,
235219
map<string, int>* blob_name_to_idx);
@@ -241,8 +225,6 @@ class Net {
241225
void AppendParam(const NetParameter& param, const int layer_id,
242226
const int param_id);
243227

244-
/// @brief Helper for displaying debug info in Forward about input Blobs.
245-
void InputDebugInfo(const int layer_id);
246228
/// @brief Helper for displaying debug info in Forward.
247229
void ForwardDebugInfo(const int layer_id);
248230
/// @brief Helper for displaying debug info in Backward.
@@ -281,10 +263,8 @@ class Net {
281263
vector<string> param_display_names_;
282264
vector<pair<int, int> > param_layer_indices_;
283265
map<string, int> param_names_index_;
284-
/// blob indices for the input and the output of the net
285-
vector<int> net_input_blob_indices_;
266+
/// blob indices for the output of the net
286267
vector<int> net_output_blob_indices_;
287-
vector<Blob<Dtype>*> net_input_blobs_;
288268
vector<Blob<Dtype>*> net_output_blobs_;
289269
/// The parameters in the network.
290270
vector<shared_ptr<Blob<Dtype> > > params_;

src/caffe/net.cpp

Lines changed: 10 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,7 @@ void Net<Dtype>::Init(const NetParameter& in_param) {
5656
name_ = param.name();
5757
map<string, int> blob_name_to_idx;
5858
set<string> available_blobs;
59-
CHECK(param.input_dim_size() == 0 || param.input_shape_size() == 0)
60-
<< "Must specify either input_shape OR deprecated input_dim, not both.";
61-
if (param.input_dim_size() > 0) {
62-
// Deprecated 4D dimensions.
63-
CHECK_EQ(param.input_size() * 4, param.input_dim_size())
64-
<< "Incorrect input blob dimension specifications.";
65-
} else {
66-
CHECK_EQ(param.input_size(), param.input_shape_size())
67-
<< "Exactly one input_shape must be specified per input.";
68-
}
6959
memory_used_ = 0;
70-
// set the input blobs
71-
for (int input_id = 0; input_id < param.input_size(); ++input_id) {
72-
const int layer_id = -1; // inputs have fake layer ID -1
73-
AppendTop(param, layer_id, input_id, &available_blobs, &blob_name_to_idx);
74-
}
7560
// For each layer, set up its input and output
7661
bottom_vecs_.resize(param.layer_size());
7762
top_vecs_.resize(param.layer_size());
@@ -379,19 +364,17 @@ bool Net<Dtype>::StateMeetsRule(const NetState& state,
379364
return true;
380365
}
381366

382-
// Helper for Net::Init: add a new input or top blob to the net. (Inputs have
383-
// layer_id == -1, tops have layer_id >= 0.)
367+
// Helper for Net::Init: add a new top blob to the net.
384368
template <typename Dtype>
385369
void Net<Dtype>::AppendTop(const NetParameter& param, const int layer_id,
386370
const int top_id, set<string>* available_blobs,
387371
map<string, int>* blob_name_to_idx) {
388-
shared_ptr<LayerParameter> layer_param((layer_id >= 0) ?
389-
(new LayerParameter(param.layer(layer_id))) : NULL);
390-
const string& blob_name = layer_param ?
391-
(layer_param->top_size() > top_id ?
392-
layer_param->top(top_id) : "(automatic)") : param.input(top_id);
372+
shared_ptr<LayerParameter> layer_param(
373+
new LayerParameter(param.layer(layer_id)));
374+
const string& blob_name = (layer_param->top_size() > top_id) ?
375+
layer_param->top(top_id) : "(automatic)";
393376
// Check if we are doing in-place computation
394-
if (blob_name_to_idx && layer_param && layer_param->bottom_size() > top_id &&
377+
if (blob_name_to_idx && layer_param->bottom_size() > top_id &&
395378
blob_name == layer_param->bottom(top_id)) {
396379
// In-place computation
397380
LOG_IF(INFO, Caffe::root_solver())
@@ -407,34 +390,16 @@ void Net<Dtype>::AppendTop(const NetParameter& param, const int layer_id,
407390
} else {
408391
// Normal output.
409392
if (Caffe::root_solver()) {
410-
if (layer_param) {
411-
LOG(INFO) << layer_param->name() << " -> " << blob_name;
412-
} else {
413-
LOG(INFO) << "Input " << top_id << " -> " << blob_name;
414-
}
393+
LOG(INFO) << layer_param->name() << " -> " << blob_name;
415394
}
416395
shared_ptr<Blob<Dtype> > blob_pointer(new Blob<Dtype>());
417396
const int blob_id = blobs_.size();
418397
blobs_.push_back(blob_pointer);
419398
blob_names_.push_back(blob_name);
420399
blob_need_backward_.push_back(false);
421400
if (blob_name_to_idx) { (*blob_name_to_idx)[blob_name] = blob_id; }
422-
if (layer_id == -1) {
423-
// Set the (explicitly specified) dimensions of the input blob.
424-
if (param.input_dim_size() > 0) {
425-
blob_pointer->Reshape(param.input_dim(top_id * 4),
426-
param.input_dim(top_id * 4 + 1),
427-
param.input_dim(top_id * 4 + 2),
428-
param.input_dim(top_id * 4 + 3));
429-
} else {
430-
blob_pointer->Reshape(param.input_shape(top_id));
431-
}
432-
net_input_blob_indices_.push_back(blob_id);
433-
net_input_blobs_.push_back(blob_pointer.get());
434-
} else {
435-
top_id_vecs_[layer_id].push_back(blob_id);
436-
top_vecs_[layer_id].push_back(blob_pointer.get());
437-
}
401+
top_id_vecs_[layer_id].push_back(blob_id);
402+
top_vecs_[layer_id].push_back(blob_pointer.get());
438403
}
439404
if (available_blobs) { available_blobs->insert(blob_name); }
440405
}
@@ -566,11 +531,6 @@ Dtype Net<Dtype>::ForwardFromTo(int start, int end) {
566531
CHECK_GE(start, 0);
567532
CHECK_LT(end, layers_.size());
568533
Dtype loss = 0;
569-
if (debug_info_) {
570-
for (int i = 0; i < net_input_blobs_.size(); ++i) {
571-
InputDebugInfo(i);
572-
}
573-
}
574534
for (int i = start; i <= end; ++i) {
575535
// LOG(ERROR) << "Forwarding " << layer_names_[i];
576536
Dtype layer_loss = layers_[i]->Forward(bottom_vecs_[i], top_vecs_[i]);
@@ -591,7 +551,7 @@ Dtype Net<Dtype>::ForwardTo(int end) {
591551
}
592552

593553
template <typename Dtype>
594-
const vector<Blob<Dtype>*>& Net<Dtype>::ForwardPrefilled(Dtype* loss) {
554+
const vector<Blob<Dtype>*>& Net<Dtype>::Forward(Dtype* loss) {
595555
if (loss != NULL) {
596556
*loss = ForwardFromTo(0, layers_.size() - 1);
597557
} else {
@@ -600,37 +560,6 @@ const vector<Blob<Dtype>*>& Net<Dtype>::ForwardPrefilled(Dtype* loss) {
600560
return net_output_blobs_;
601561
}
602562

603-
template <typename Dtype>
604-
const vector<Blob<Dtype>*>& Net<Dtype>::Forward(
605-
const vector<Blob<Dtype>*> & bottom, Dtype* loss) {
606-
// Copy bottom to internal bottom
607-
for (int i = 0; i < bottom.size(); ++i) {
608-
net_input_blobs_[i]->CopyFrom(*bottom[i]);
609-
}
610-
return ForwardPrefilled(loss);
611-
}
612-
613-
template <typename Dtype>
614-
string Net<Dtype>::Forward(const string& input_blob_protos, Dtype* loss) {
615-
BlobProtoVector blob_proto_vec;
616-
if (net_input_blobs_.size()) {
617-
blob_proto_vec.ParseFromString(input_blob_protos);
618-
CHECK_EQ(blob_proto_vec.blobs_size(), net_input_blobs_.size())
619-
<< "Incorrect input size.";
620-
for (int i = 0; i < blob_proto_vec.blobs_size(); ++i) {
621-
net_input_blobs_[i]->FromProto(blob_proto_vec.blobs(i));
622-
}
623-
}
624-
ForwardPrefilled(loss);
625-
blob_proto_vec.Clear();
626-
for (int i = 0; i < net_output_blobs_.size(); ++i) {
627-
net_output_blobs_[i]->ToProto(blob_proto_vec.add_blobs());
628-
}
629-
string output;
630-
blob_proto_vec.SerializeToString(&output);
631-
return output;
632-
}
633-
634563
template <typename Dtype>
635564
void Net<Dtype>::BackwardFromTo(int start, int end) {
636565
CHECK_GE(end, 0);
@@ -644,16 +573,6 @@ void Net<Dtype>::BackwardFromTo(int start, int end) {
644573
}
645574
}
646575

647-
template <typename Dtype>
648-
void Net<Dtype>::InputDebugInfo(const int input_id) {
649-
const Blob<Dtype>& blob = *net_input_blobs_[input_id];
650-
const string& blob_name = blob_names_[net_input_blob_indices_[input_id]];
651-
const Dtype data_abs_val_mean = blob.asum_data() / blob.count();
652-
LOG_IF(INFO, Caffe::root_solver())
653-
<< " [Forward] "
654-
<< "Input " << blob_name << " data: " << data_abs_val_mean;
655-
}
656-
657576
template <typename Dtype>
658577
void Net<Dtype>::ForwardDebugInfo(const int layer_id) {
659578
for (int top_id = 0; top_id < top_vecs_[layer_id].size(); ++top_id) {
@@ -912,9 +831,6 @@ void Net<Dtype>::ToProto(NetParameter* param, bool write_diff) const {
912831
param->Clear();
913832
param->set_name(name_);
914833
// Add bottom and top
915-
for (int i = 0; i < net_input_blob_indices_.size(); ++i) {
916-
param->add_input(blob_names_[net_input_blob_indices_[i]]);
917-
}
918834
DLOG(INFO) << "Serializing " << layers_.size() << " layers";
919835
for (int i = 0; i < layers_.size(); ++i) {
920836
LayerParameter* layer_param = param->add_layer();

src/caffe/solver.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ void Solver<Dtype>::InitTestNets() {
192192

193193
template <typename Dtype>
194194
void Solver<Dtype>::Step(int iters) {
195-
vector<Blob<Dtype>*> bottom_vec;
196195
const int start_iter = iter_;
197196
const int stop_iter = iter_ + iters;
198197
int average_loss = this->param_.average_loss();
@@ -220,7 +219,7 @@ void Solver<Dtype>::Step(int iters) {
220219
// accumulate the loss and gradient
221220
Dtype loss = 0;
222221
for (int i = 0; i < param_.iter_size(); ++i) {
223-
loss += net_->ForwardBackward(bottom_vec);
222+
loss += net_->ForwardBackward();
224223
}
225224
loss /= param_.iter_size();
226225
// average the loss across iterations for smoothed reporting
@@ -311,7 +310,7 @@ void Solver<Dtype>::Solve(const char* resume_file) {
311310
if (param_.display() && iter_ % param_.display() == 0) {
312311
int average_loss = this->param_.average_loss();
313312
Dtype loss;
314-
net_->ForwardPrefilled(&loss);
313+
net_->Forward(&loss);
315314

316315
UpdateSmoothedLoss(loss, start_iter, average_loss);
317316

@@ -341,7 +340,6 @@ void Solver<Dtype>::Test(const int test_net_id) {
341340
ShareTrainedLayersWith(net_.get());
342341
vector<Dtype> test_score;
343342
vector<int> test_score_output_id;
344-
vector<Blob<Dtype>*> bottom_vec;
345343
const shared_ptr<Net<Dtype> >& test_net = test_nets_[test_net_id];
346344
Dtype loss = 0;
347345
for (int i = 0; i < param_.test_iter(test_net_id); ++i) {
@@ -362,7 +360,7 @@ void Solver<Dtype>::Test(const int test_net_id) {
362360

363361
Dtype iter_loss;
364362
const vector<Blob<Dtype>*>& result =
365-
test_net->Forward(bottom_vec, &iter_loss);
363+
test_net->Forward(&iter_loss);
366364
if (param_.test_compute_loss()) {
367365
loss += iter_loss;
368366
}

src/caffe/test/test_gradient_based_solver.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,8 @@ class GradientBasedSolverTest : public MultiDeviceTest<TypeParam> {
185185
this->InitSolverFromProtoString(proto.str());
186186
if (from_snapshot != NULL) {
187187
this->solver_->Restore(from_snapshot);
188-
vector<Blob<Dtype>*> empty_bottom_vec;
189188
for (int i = 0; i < this->solver_->iter(); ++i) {
190-
this->solver_->net()->Forward(empty_bottom_vec);
189+
this->solver_->net()->Forward();
191190
}
192191
}
193192
if (devices == 1) {
@@ -231,8 +230,7 @@ class GradientBasedSolverTest : public MultiDeviceTest<TypeParam> {
231230
// Run a forward pass, and manually compute the update values from the
232231
// result.
233232
Net<Dtype>& net = *this->solver_->net();
234-
vector<Blob<Dtype>*> empty_bottom_vec;
235-
net.Forward(empty_bottom_vec);
233+
net.Forward();
236234
ASSERT_TRUE(net.has_blob("data"));
237235
const Blob<Dtype>& data = *net.blob_by_name("data");
238236
ASSERT_TRUE(net.has_blob("targets"));

0 commit comments

Comments
 (0)