From: Yangqing Jia Date: Sat, 28 Sep 2013 01:05:02 +0000 (-0700) Subject: test loading from text file X-Git-Url: https://git.ti.com/gitweb?p=jacinto-ai%2Fcaffe-jacinto.git;a=commitdiff_plain;h=f6f54966b85dcc8b40c61044647776a78117b376 test loading from text file --- diff --git a/src/caffe/test/data/lenet.prototxt b/src/caffe/test/data/lenet.prototxt new file mode 100644 index 00000000..a7d578ef --- /dev/null +++ b/src/caffe/test/data/lenet.prototxt @@ -0,0 +1,113 @@ +name: "LeNet" +bottom: "data" +bottom: "label" +layers { + layer { + name: "conv1" + type: "conv" + num_output: 20 + kernelsize: 5 + stride: 1 + weight_filler { + type: "xavier" + } + bias_filler { + type: "constant" + } + } + bottom: "data" + top: "conv1" +} +layers { + layer { + name: "pool1" + type: "pool" + kernelsize: 2 + stride: 2 + pool: MAX + } + bottom: "conv1" + top: "pool1" +} +layers { + layer { + name: "conv2" + type: "conv" + num_output: 50 + kernelsize: 5 + stride: 1 + weight_filler { + type: "xavier" + } + bias_filler { + type: "constant" + } + } + bottom: "pool1" + top: "conv2" +} +layers { + layer { + name: "pool2" + type: "pool" + kernelsize: 2 + stride: 2 + pool: MAX + } + bottom: "conv2" + top: "pool2" +} +layers { + layer { + name: "ip1" + type: "innerproduct" + num_output: 500 + weight_filler { + type: "xavier" + } + bias_filler { + type: "constant" + } + } + bottom: "pool2" + top: "ip1" +} +layers { + layer { + name: "relu1" + type: "relu" + } + bottom: "ip1" + top: "relu1" +} +layers { + layer { + name: "ip2" + type: "innerproduct" + num_output: 10 + weight_filler { + type: "xavier" + } + bias_filler { + type: "constant" + } + } + bottom: "relu1" + top: "ip2" +} +layers { + layer { + name: "prob" + type: "softmax" + } + bottom: "ip2" + top: "prob" +} +layers { + layer { + name: "loss" + type: "multinomial_logistic_loss" + } + bottom: "prob" + bottom: "label" +} diff --git a/src/caffe/test/test_net_proto.cpp b/src/caffe/test/test_net_proto.cpp index 013bd67a..b2403460 100644 --- a/src/caffe/test/test_net_proto.cpp +++ b/src/caffe/test/test_net_proto.cpp @@ -1,15 +1,19 @@ // Copyright 2013 Yangqing Jia -#include #include +#include #include +#include #include +#include + #include "caffe/blob.hpp" #include "caffe/common.hpp" #include "caffe/net.hpp" #include "caffe/filler.hpp" #include "caffe/proto/caffe.pb.h" +#include "caffe/util/io.hpp" #include "caffe/test/lenet.hpp" #include "caffe/test/test_caffe_main.hpp" @@ -22,6 +26,11 @@ class NetProtoTest : public ::testing::Test {}; typedef ::testing::Types Dtypes; TYPED_TEST_CASE(NetProtoTest, Dtypes); +TYPED_TEST(NetProtoTest, TestLoadFromText) { + NetParameter net_param; + ReadProtoFromTextFile("caffe/test/data/lenet.prototxt", &net_param); +} + TYPED_TEST(NetProtoTest, TestSetup) { NetParameter net_param; string lenet_string(kLENET); @@ -65,7 +74,6 @@ TYPED_TEST(NetProtoTest, TestSetup) { caffe_net.Forward(bottom_vec, &top_vec); LOG(ERROR) << "Performing Backward"; LOG(ERROR) << caffe_net.Backward(); - } } // namespace caffe diff --git a/src/caffe/util/io.cpp b/src/caffe/util/io.cpp index d827176a..c1f5a962 100644 --- a/src/caffe/util/io.cpp +++ b/src/caffe/util/io.cpp @@ -1,6 +1,9 @@ // Copyright 2013 Yangqing Jia #include +#include +#include +#include #include #include @@ -15,6 +18,7 @@ using cv::Mat; using cv::Vec3b; using std::max; using std::string; +using google::protobuf::io::FileInputStream; namespace caffe { @@ -57,5 +61,13 @@ void WriteProtoToImage(const string& filename, const BlobProto& proto) { CHECK(cv::imwrite(filename, cv_img)); } +void ReadProtoFromTextFile(const char* filename, + ::google::protobuf::Message* proto) { + int fd = open(filename, O_RDONLY); + FileInputStream* input = new FileInputStream(fd); + CHECK(google::protobuf::TextFormat::Parse(input, proto)); + delete input; + close(fd); +} } // namespace caffe diff --git a/src/caffe/util/io.hpp b/src/caffe/util/io.hpp index f55de8ae..90617be1 100644 --- a/src/caffe/util/io.hpp +++ b/src/caffe/util/io.hpp @@ -3,7 +3,10 @@ #ifndef CAFFE_UTIL_IO_H_ #define CAFFE_UTIL_IO_H_ +#include + #include + #include "caffe/proto/caffe.pb.h" using std::string; @@ -13,6 +16,12 @@ namespace caffe { void ReadImageToProto(const string& filename, BlobProto* proto); void WriteProtoToImage(const string& filename, const BlobProto& proto); +void ReadProtoFromTextFile(const char* filename, + ::google::protobuf::Message* proto); +inline void ReadProtoFromTextFile(const string& filename, + ::google::protobuf::Message* proto) { + ReadProtoFromTextFile(filename.c_str(), proto); +} } // namespace caffe