MCPcopy Create free account
hub / github.com/BVLC/caffe / CopyTrainedLayersFrom

Method CopyTrainedLayersFrom

src/caffe/net.cpp:733–768  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

731
732template <typename Dtype>
733void Net<Dtype>::CopyTrainedLayersFrom(const NetParameter& param) {
734 int num_source_layers = param.layer_size();
735 for (int i = 0; i < num_source_layers; ++i) {
736 const LayerParameter& source_layer = param.layer(i);
737 const string& source_layer_name = source_layer.name();
738 int target_layer_id = 0;
739 while (target_layer_id != layer_names_.size() &&
740 layer_names_[target_layer_id] != source_layer_name) {
741 ++target_layer_id;
742 }
743 if (target_layer_id == layer_names_.size()) {
744 LOG(INFO) << "Ignoring source layer " << source_layer_name;
745 continue;
746 }
747 DLOG(INFO) << "Copying source layer " << source_layer_name;
748 vector<shared_ptr<Blob<Dtype> > >& target_blobs =
749 layers_[target_layer_id]->blobs();
750 CHECK_EQ(target_blobs.size(), source_layer.blobs_size())
751 << "Incompatible number of blobs for layer " << source_layer_name;
752 for (int j = 0; j < target_blobs.size(); ++j) {
753 if (!target_blobs[j]->ShapeEquals(source_layer.blobs(j))) {
754 Blob<Dtype> source_blob;
755 const bool kReshape = true;
756 source_blob.FromProto(source_layer.blobs(j), kReshape);
757 LOG(FATAL) << "Cannot copy param " << j << " weights from layer '"
758 << source_layer_name << "'; shape mismatch. Source param shape is "
759 << source_blob.shape_string() << "; target param shape is "
760 << target_blobs[j]->shape_string() << ". "
761 << "To learn this layer's parameters from scratch rather than "
762 << "copying from a saved net, rename the layer.";
763 }
764 const bool kReshape = false;
765 target_blobs[j]->FromProto(source_layer.blobs(j), kReshape);
766 }
767 }
768}
769
770template <typename Dtype>
771void Net<Dtype>::CopyTrainedLayersFrom(const string trained_filename) {

Callers 10

CopyLayersFunction · 0.80
testFunction · 0.80
Net_InitFunction · 0.80
Net_Init_LoadFunction · 0.80
TYPED_TESTFunction · 0.80
net_copy_fromFunction · 0.80
ClassifierMethod · 0.80

Calls 4

ShapeEqualsMethod · 0.80
FromProtoMethod · 0.80
shape_stringMethod · 0.80
sizeMethod · 0.45

Tested by 1

TYPED_TESTFunction · 0.64