void Blob<Dtype>::FromProto(const BlobProto& proto, bool need_reshape = true){ //copy shape if (need_reshape){ vector<int> shape; shape.resize(proto.shape().dim_size()); for (int i = 0; i < shape.size(); i++) shape[i] = proto.shape().dim(i); reshape(shape); } if (proto.data_size()>0){ CHECK_EQ(proto.data_size(), count()); Dtype *data = mutable_cpu_data(); for (int i = 0; i < count_; i++) data[i] = proto.data(i); } if (proto.diff_size()>0){ CHECK_EQ(proto.diff_size(), count()); Dtype *diff = mutable_cpu_diff(); for (int i = 0; i < count_; i++) diff[i] = proto.diff(i); } }
void Blob<Dtype>::FromProto(const BlobProto& proto, bool reshape) { if (reshape) { vector<int> shape; if (proto.has_num() || proto.has_channels() || proto.has_height() || proto.has_width()) { // Using deprecated 4D Blob dimensions -- // shape is (num, channels, height, width). shape.resize(4); shape[0] = proto.num(); shape[1] = proto.channels(); shape[2] = proto.height(); shape[3] = proto.width(); } else { shape.resize(proto.shape().dim_size()); for (int i = 0; i < proto.shape().dim_size(); ++i) { shape[i] = proto.shape().dim(i); } } Reshape(shape); } else { CHECK(ShapeEquals(proto)) << "shape mismatch (reshape not set)"; } // copy data Dtype* data_vec = mutable_cpu_data(); if (proto.double_data_size() > 0) { CHECK_EQ(count_, proto.double_data_size()); for (int i = 0; i < count_; ++i) { data_vec[i] = proto.double_data(i); } } else { CHECK_EQ(count_, proto.data_size()); for (int i = 0; i < count_; ++i) { data_vec[i] = proto.data(i); } } if (proto.double_diff_size() > 0) { CHECK_EQ(count_, proto.double_diff_size()); Dtype* diff_vec = mutable_cpu_diff(); for (int i = 0; i < count_; ++i) { diff_vec[i] = proto.double_diff(i); } } else if (proto.diff_size() > 0) { CHECK_EQ(count_, proto.diff_size()); Dtype* diff_vec = mutable_cpu_diff(); for (int i = 0; i < count_; ++i) { diff_vec[i] = proto.diff(i); } } }
void Blob<Dtype>::FromProtoWithExtraCopy(const BlobProto& proto){ // reshape other dimention but remain the same number of channels; Reshape(proto.num(), this->channels(), proto.height(), proto.width(), proto.depth()); // copy data Dtype* data_vec = mutable_cpu_data(); int sourceDataSize =proto.data_size(); for (int i = 0; i < count_; ++i) { data_vec[i] = proto.data(i%sourceDataSize); } if (proto.diff_size() > 0) { Dtype* diff_vec = mutable_cpu_diff(); for (int i = 0; i < count_; ++i) { diff_vec[i] = proto.diff(i%sourceDataSize); } } }