Return-Path: X-Original-To: apmail-singa-commits-archive@minotaur.apache.org Delivered-To: apmail-singa-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 866AE187F8 for ; Sun, 11 Oct 2015 10:19:22 +0000 (UTC) Received: (qmail 32469 invoked by uid 500); 11 Oct 2015 10:19:22 -0000 Delivered-To: apmail-singa-commits-archive@singa.apache.org Received: (qmail 32450 invoked by uid 500); 11 Oct 2015 10:19:22 -0000 Mailing-List: contact commits-help@singa.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@singa.incubator.apache.org Delivered-To: mailing list commits@singa.incubator.apache.org Received: (qmail 32441 invoked by uid 99); 11 Oct 2015 10:19:22 -0000 Received: from Unknown (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 11 Oct 2015 10:19:22 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 0065D1A27BA for ; Sun, 11 Oct 2015 10:19:22 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.791 X-Spam-Level: * X-Spam-Status: No, score=1.791 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, T_RP_MATCHES_RCVD=-0.01, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-west.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id vKTHYsriYwwC for ; Sun, 11 Oct 2015 10:19:08 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-west.apache.org (ASF Mail Server at mx1-us-west.apache.org) with SMTP id 0B6FC2045A for ; Sun, 11 Oct 2015 10:19:08 +0000 (UTC) Received: (qmail 32347 invoked by uid 99); 11 Oct 2015 10:19:07 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 11 Oct 2015 10:19:07 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A300FDFE61; Sun, 11 Oct 2015 10:19:07 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: wangsh@apache.org To: commits@singa.incubator.apache.org Date: Sun, 11 Oct 2015 10:19:07 -0000 Message-Id: <56f911345be94ba2bc25e6321996d93b@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/4] incubator-singa git commit: SINGA-85 Add functions for extracting features and test new data Repository: incubator-singa Updated Branches: refs/heads/master f90e4dcdc -> 288a2ab94 http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e928ebba/src/test/test_csv_input_layer.cc ---------------------------------------------------------------------- diff --git a/src/test/test_csv_input_layer.cc b/src/test/test_csv_input_layer.cc new file mode 100644 index 0000000..ce5847d --- /dev/null +++ b/src/test/test_csv_input_layer.cc @@ -0,0 +1,92 @@ +/************************************************************ +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*************************************************************/ +#include +#include +#include + +#include "gtest/gtest.h" +#include "singa/neuralnet/input_layer/csv.h" +#include "singa/proto/job.pb.h" + +class CSVInputLayerTest : public ::testing::Test { + protected: + virtual void SetUp() { + std::string path ="src/test/test.csv"; + std::ofstream ofs(path, std::ofstream::out); + ASSERT_TRUE(ofs.is_open()); + ofs << "12,3.2,1,14.1\n"; + ofs << "2,0.2,0,1.1\n"; + ofs << "1,2.2,1,4.1\n"; + ofs.close(); + auto conf = csv_conf.mutable_store_conf(); + conf->set_path(path); + conf->set_batchsize(2); + conf->add_shape(3); + conf->set_backend("textfile"); + } + singa::LayerProto csv_conf; +}; + +TEST_F(CSVInputLayerTest, Setup) { + singa::CSVInputLayer layer; + layer.Setup(csv_conf, std::vector{}); + EXPECT_EQ(2, static_cast(layer.aux_data().size())); + EXPECT_EQ(6, layer.data(nullptr).count()); +} + +TEST_F(CSVInputLayerTest, ComputeFeature) { + singa::CSVInputLayer csv; + csv.Setup(csv_conf, std::vector{}); + csv.ComputeFeature(singa::kTrain, std::vector{}); + + EXPECT_EQ(12, csv.aux_data()[0]); + EXPECT_EQ(2, csv.aux_data()[1]); + auto data = csv.data(nullptr); + EXPECT_EQ(3.2f, data.cpu_data()[0]); + EXPECT_EQ(14.1f, data.cpu_data()[2]); + EXPECT_EQ(0.2f, data.cpu_data()[3]); + EXPECT_EQ(1.1f, data.cpu_data()[5]); +} +TEST_F(CSVInputLayerTest, ComputeFeatureDeploy) { + singa::CSVInputLayer csv; + csv_conf.mutable_store_conf()->set_shape(0, 4); + csv.Setup(csv_conf, std::vector{}); + csv.ComputeFeature(singa::kDeploy, std::vector{}); + + auto data = csv.data(nullptr); + EXPECT_EQ(12.f, data.cpu_data()[0]); + EXPECT_EQ(1.f, data.cpu_data()[2]); + EXPECT_EQ(14.1f, data.cpu_data()[3]); + EXPECT_EQ(0.2f, data.cpu_data()[5]); +} + +TEST_F(CSVInputLayerTest, SeekToFirst) { + singa::CSVInputLayer csv; + csv.Setup(csv_conf, std::vector{}); + csv.ComputeFeature(singa::kTrain, std::vector{}); + csv.ComputeFeature(singa::kTrain, std::vector{}); + + auto data = csv.data(nullptr); + EXPECT_EQ(2.2f, data.cpu_data()[0]); + EXPECT_EQ(4.1f, data.cpu_data()[2]); + EXPECT_EQ(3.2f, data.cpu_data()[3]); + EXPECT_EQ(14.1f, data.cpu_data()[5]); +} http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e928ebba/src/test/test_csv_record_layer.cc ---------------------------------------------------------------------- diff --git a/src/test/test_csv_record_layer.cc b/src/test/test_csv_record_layer.cc deleted file mode 100644 index 3f55e9f..0000000 --- a/src/test/test_csv_record_layer.cc +++ /dev/null @@ -1,92 +0,0 @@ -/************************************************************ -* -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you under the Apache License, Version 2.0 (the -* "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*************************************************************/ -#include -#include -#include - -#include "gtest/gtest.h" -#include "singa/neuralnet/input_layer/csv_record.h" -#include "singa/proto/job.pb.h" - -class CSVRecordLayerTest : public ::testing::Test { - protected: - virtual void SetUp() { - std::string path ="src/test/test.csv"; - std::ofstream ofs(path, std::ofstream::out); - ASSERT_TRUE(ofs.is_open()); - ofs << "12,3.2,1,14.1\n"; - ofs << "2,0.2,0,1.1\n"; - ofs << "1,2.2,1,4.1\n"; - ofs.close(); - auto conf = csv_conf.mutable_store_conf(); - conf->set_path(path); - conf->set_batchsize(2); - conf->add_shape(3); - conf->set_backend("textfile"); - } - singa::LayerProto csv_conf; -}; - -TEST_F(CSVRecordLayerTest, Setup) { - singa::CSVRecordLayer layer; - layer.Setup(csv_conf, std::vector{}); - EXPECT_EQ(2, static_cast(layer.aux_data().size())); - EXPECT_EQ(6, layer.data(nullptr).count()); -} - -TEST_F(CSVRecordLayerTest, ComputeFeature) { - singa::CSVRecordLayer csv; - csv.Setup(csv_conf, std::vector{}); - csv.ComputeFeature(singa::kTrain, std::vector{}); - - EXPECT_EQ(12, csv.aux_data()[0]); - EXPECT_EQ(2, csv.aux_data()[1]); - auto data = csv.data(nullptr); - EXPECT_EQ(3.2f, data.cpu_data()[0]); - EXPECT_EQ(14.1f, data.cpu_data()[2]); - EXPECT_EQ(0.2f, data.cpu_data()[3]); - EXPECT_EQ(1.1f, data.cpu_data()[5]); -} -TEST_F(CSVRecordLayerTest, ComputeFeatureDeploy) { - singa::CSVRecordLayer csv; - csv_conf.mutable_store_conf()->set_shape(0, 4); - csv.Setup(csv_conf, std::vector{}); - csv.ComputeFeature(singa::kDeploy, std::vector{}); - - auto data = csv.data(nullptr); - EXPECT_EQ(12.f, data.cpu_data()[0]); - EXPECT_EQ(1.f, data.cpu_data()[2]); - EXPECT_EQ(14.1f, data.cpu_data()[3]); - EXPECT_EQ(0.2f, data.cpu_data()[5]); -} - -TEST_F(CSVRecordLayerTest, SeekToFirst) { - singa::CSVRecordLayer csv; - csv.Setup(csv_conf, std::vector{}); - csv.ComputeFeature(singa::kTrain, std::vector{}); - csv.ComputeFeature(singa::kTrain, std::vector{}); - - auto data = csv.data(nullptr); - EXPECT_EQ(2.2f, data.cpu_data()[0]); - EXPECT_EQ(4.1f, data.cpu_data()[2]); - EXPECT_EQ(3.2f, data.cpu_data()[3]); - EXPECT_EQ(14.1f, data.cpu_data()[5]); -} http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e928ebba/src/test/test_kvfile.cc ---------------------------------------------------------------------- diff --git a/src/test/test_kvfile.cc b/src/test/test_kvfile.cc index 9c30734..5707ca9 100644 --- a/src/test/test_kvfile.cc +++ b/src/test/test_kvfile.cc @@ -7,9 +7,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at -* +* * http://www.apache.org/licenses/LICENSE-2.0 -* +* * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -37,48 +37,47 @@ std::string tuple[] = {"firsttuple", namespace singa { namespace io { TEST(KVFileTest, CreateKVFile) { - std::string path = "src/test/shard_test"; - mkdir(path.c_str(), 0755); - KVFile shard(path, KVFile::kCreate, 50); - shard.Insert(key[0], tuple[0]); - shard.Insert(key[1], tuple[1]); - shard.Insert(key[2], tuple[2]); - shard.Flush(); + std::string path = "src/test/kvfile.bin"; + KVFile kvfile(path, KVFile::kCreate, 50); + kvfile.Insert(key[0], tuple[0]); + kvfile.Insert(key[1], tuple[1]); + kvfile.Insert(key[2], tuple[2]); + kvfile.Flush(); } TEST(KVFileTest, AppendKVFile) { - std::string path = "src/test/shard_test"; - KVFile shard(path, KVFile::kAppend, 50); - shard.Insert(key[3], tuple[3]); - shard.Insert(key[4], tuple[4]); - shard.Flush(); + std::string path = "src/test/kvfile.bin"; + KVFile kvfile(path, KVFile::kAppend, 50); + kvfile.Insert(key[3], tuple[3]); + kvfile.Insert(key[4], tuple[4]); + kvfile.Flush(); } TEST(KVFileTest, CountKVFile) { - std::string path = "src/test/shard_test"; - KVFile shard(path, KVFile::kRead, 50); - int count = shard.Count(); + std::string path = "src/test/kvfile.bin"; + KVFile kvfile(path, KVFile::kRead, 50); + int count = kvfile.Count(); ASSERT_EQ(5, count); } TEST(KVFileTest, ReadKVFile) { - std::string path = "src/test/shard_test"; - KVFile shard(path, KVFile::kRead, 50); + std::string path = "src/test/kvfile.bin"; + KVFile kvfile(path, KVFile::kRead, 50); std::string k, t; - ASSERT_TRUE(shard.Next(&k, &t)); + ASSERT_TRUE(kvfile.Next(&k, &t)); ASSERT_STREQ(key[0].c_str(), k.c_str()); ASSERT_STREQ(tuple[0].c_str(), t.c_str()); - ASSERT_TRUE(shard.Next(&k, &t)); + ASSERT_TRUE(kvfile.Next(&k, &t)); ASSERT_STREQ(key[1].c_str(), k.c_str()); ASSERT_STREQ(tuple[1].c_str(), t.c_str()); - ASSERT_TRUE(shard.Next(&k, &t)); - ASSERT_TRUE(shard.Next(&k, &t)); - ASSERT_TRUE(shard.Next(&k, &t)); + ASSERT_TRUE(kvfile.Next(&k, &t)); + ASSERT_TRUE(kvfile.Next(&k, &t)); + ASSERT_TRUE(kvfile.Next(&k, &t)); ASSERT_STREQ(key[4].c_str(), k.c_str()); ASSERT_STREQ(tuple[4].c_str(), t.c_str()); - ASSERT_FALSE(shard.Next(&k, &t)); - shard.SeekToFirst(); - ASSERT_TRUE(shard.Next(&k, &t)); + ASSERT_FALSE(kvfile.Next(&k, &t)); + kvfile.SeekToFirst(); + ASSERT_TRUE(kvfile.Next(&k, &t)); ASSERT_STREQ(key[0].c_str(), k.c_str()); ASSERT_STREQ(tuple[0].c_str(), t.c_str()); } http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e928ebba/src/test/test_proto_record_layer.cc ---------------------------------------------------------------------- diff --git a/src/test/test_proto_record_layer.cc b/src/test/test_proto_record_layer.cc deleted file mode 100644 index b1fb1ee..0000000 --- a/src/test/test_proto_record_layer.cc +++ /dev/null @@ -1,122 +0,0 @@ -/************************************************************ -* -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you under the Apache License, Version 2.0 (the -* "License"); you may not use this file except in compliance -* with the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*************************************************************/ -#include -#include - -#include "gtest/gtest.h" -#include "singa/neuralnet/input_layer/proto_record.h" -#include "singa/proto/job.pb.h" -#include "singa/proto/common.pb.h" - -class ProtoRecordLayerTest : public ::testing::Test { - protected: - virtual void SetUp() { - std::string path ="src/test/test.bin"; - auto* store = singa::io::CreateStore("kvfile"); - store->Open(path, singa::io::kCreate); - { - singa::SingleLabelImageRecord image; - image.add_data(3.2); - image.add_data(1); - image.add_data(14.1); - image.set_label(12); - std::string val; - image.SerializeToString(&val); - store->Write("0", val); - } - - { - singa::SingleLabelImageRecord image; - image.add_data(0.2); - image.add_data(0); - image.add_data(1.1); - image.set_label(2); - std::string val; - image.SerializeToString(&val); - store->Write("1", val); - } - - { - singa::SingleLabelImageRecord image; - image.add_data(2.2); - image.add_data(1); - image.add_data(4.1); - image.set_label(1); - std::string val; - image.SerializeToString(&val); - store->Write("2", val); - } - store->Flush(); - store->Close(); - - auto conf = image_conf.mutable_store_conf(); - conf->set_path(path); - conf->set_batchsize(2); - conf->add_shape(3); - conf->set_backend("kvfile"); - } - singa::LayerProto image_conf; -}; - -TEST_F(ProtoRecordLayerTest, Setup) { - singa::ProtoRecordLayer layer; - layer.Setup(image_conf, std::vector{}); - EXPECT_EQ(2, static_cast(layer.aux_data().size())); - EXPECT_EQ(6, layer.data(nullptr).count()); -} - -TEST_F(ProtoRecordLayerTest, ComputeFeature) { - singa::ProtoRecordLayer image; - image.Setup(image_conf, std::vector{}); - image.ComputeFeature(singa::kTrain, std::vector{}); - - EXPECT_EQ(12, image.aux_data()[0]); - EXPECT_EQ(2, image.aux_data()[1]); - auto data = image.data(nullptr); - EXPECT_EQ(3.2f, data.cpu_data()[0]); - EXPECT_EQ(14.1f, data.cpu_data()[2]); - EXPECT_EQ(0.2f, data.cpu_data()[3]); - EXPECT_EQ(1.1f, data.cpu_data()[5]); -} -TEST_F(ProtoRecordLayerTest, ComputeFeatureDeploy) { - singa::ProtoRecordLayer image; - image.Setup(image_conf, std::vector{}); - image.ComputeFeature(singa::kDeploy, std::vector{}); - - auto data = image.data(nullptr); - EXPECT_EQ(3.2f, data.cpu_data()[0]); - EXPECT_EQ(14.1f, data.cpu_data()[2]); - EXPECT_EQ(0.2f, data.cpu_data()[3]); - EXPECT_EQ(1.1f, data.cpu_data()[5]); -} - -TEST_F(ProtoRecordLayerTest, SeekToFirst) { - singa::ProtoRecordLayer image; - image.Setup(image_conf, std::vector{}); - image.ComputeFeature(singa::kTrain, std::vector{}); - image.ComputeFeature(singa::kTrain, std::vector{}); - - auto data = image.data(nullptr); - EXPECT_EQ(2.2f, data.cpu_data()[0]); - EXPECT_EQ(4.1f, data.cpu_data()[2]); - EXPECT_EQ(3.2f, data.cpu_data()[3]); - EXPECT_EQ(14.1f, data.cpu_data()[5]); -} http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e928ebba/src/test/test_record_input_layer.cc ---------------------------------------------------------------------- diff --git a/src/test/test_record_input_layer.cc b/src/test/test_record_input_layer.cc new file mode 100644 index 0000000..78e6047 --- /dev/null +++ b/src/test/test_record_input_layer.cc @@ -0,0 +1,122 @@ +/************************************************************ +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*************************************************************/ +#include +#include + +#include "gtest/gtest.h" +#include "singa/neuralnet/input_layer/record.h" +#include "singa/proto/job.pb.h" +#include "singa/proto/common.pb.h" + +class RecordInputLayerTest : public ::testing::Test { + protected: + virtual void SetUp() { + std::string path ="src/test/test.bin"; + auto* store = singa::io::CreateStore("kvfile"); + store->Open(path, singa::io::kCreate); + { + singa::RecordProto image; + image.add_data(3.2); + image.add_data(1); + image.add_data(14.1); + image.set_label(12); + std::string val; + image.SerializeToString(&val); + store->Write("0", val); + } + + { + singa::SingleLabelImageRecord image; + image.add_data(0.2); + image.add_data(0); + image.add_data(1.1); + image.set_label(2); + std::string val; + image.SerializeToString(&val); + store->Write("1", val); + } + + { + singa::SingleLabelImageRecord image; + image.add_data(2.2); + image.add_data(1); + image.add_data(4.1); + image.set_label(1); + std::string val; + image.SerializeToString(&val); + store->Write("2", val); + } + store->Flush(); + store->Close(); + + auto conf = image_conf.mutable_store_conf(); + conf->set_path(path); + conf->set_batchsize(2); + conf->add_shape(3); + conf->set_backend("kvfile"); + } + singa::LayerProto image_conf; +}; + +TEST_F(RecordInputLayerTest, Setup) { + singa::RecordInputLayer layer; + layer.Setup(image_conf, std::vector{}); + EXPECT_EQ(2, static_cast(layer.aux_data().size())); + EXPECT_EQ(6, layer.data(nullptr).count()); +} + +TEST_F(RecordInputLayerTest, ComputeFeature) { + singa::RecordInputLayer image; + image.Setup(image_conf, std::vector{}); + image.ComputeFeature(singa::kTrain, std::vector{}); + + EXPECT_EQ(12, image.aux_data()[0]); + EXPECT_EQ(2, image.aux_data()[1]); + auto data = image.data(nullptr); + EXPECT_EQ(3.2f, data.cpu_data()[0]); + EXPECT_EQ(14.1f, data.cpu_data()[2]); + EXPECT_EQ(0.2f, data.cpu_data()[3]); + EXPECT_EQ(1.1f, data.cpu_data()[5]); +} +TEST_F(RecordInputLayerTest, ComputeFeatureDeploy) { + singa::RecordInputLayer image; + image.Setup(image_conf, std::vector{}); + image.ComputeFeature(singa::kDeploy, std::vector{}); + + auto data = image.data(nullptr); + EXPECT_EQ(3.2f, data.cpu_data()[0]); + EXPECT_EQ(14.1f, data.cpu_data()[2]); + EXPECT_EQ(0.2f, data.cpu_data()[3]); + EXPECT_EQ(1.1f, data.cpu_data()[5]); +} + +TEST_F(RecordInputLayerTest, SeekToFirst) { + singa::RecordInputLayer image; + image.Setup(image_conf, std::vector{}); + image.ComputeFeature(singa::kTrain, std::vector{}); + image.ComputeFeature(singa::kTrain, std::vector{}); + + auto data = image.data(nullptr); + EXPECT_EQ(2.2f, data.cpu_data()[0]); + EXPECT_EQ(4.1f, data.cpu_data()[2]); + EXPECT_EQ(3.2f, data.cpu_data()[3]); + EXPECT_EQ(14.1f, data.cpu_data()[5]); +} http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/e928ebba/src/worker.cc ---------------------------------------------------------------------- diff --git a/src/worker.cc b/src/worker.cc index 431654a..e6c2279 100644 --- a/src/worker.cc +++ b/src/worker.cc @@ -78,32 +78,24 @@ void Worker::InitNetParams(const JobProto& job_conf, NeuralNet* net) { } } } - // load from checkpoints. get param blob based on param name. - // the param from previous checkpoint files will be overwritten by - // the param with the same name in later checkpoint files. - for (const auto path : job_conf.checkpoint_path()) { - LOG(ERROR) << "Load from checkpoint file " << path; - BlobProtos bps; - ReadProtoFromBinaryFile(path.c_str(), &bps); - for (int i = 0; i < bps.name_size(); i++) { - if (name2param.find(bps.name(i)) != name2param.end()) { - name2param.at(bps.name(i))->FromProto(bps.blob(i)); - // if load from pre-training params, reset version to start step - if (job_conf.reset_param_version()) - name2param.at(bps.name(i))->set_version(job_conf.step()); - else // if resume training, use the same version as last checkpoint - name2param.at(bps.name(i))->set_version(bps.version(i)); - } - } - } + vector paths; + for (const auto& p : job_conf_.checkpoint_path()) + paths.push_back(p); + net->Load(paths, name2param); // init other params who do not have checkpoint version - for (auto entry : name2param) - if (entry.second->version() < 0) { + for (auto entry : name2param) { + if (entry.second->version() > 0) { + // if load from pre-training params, reset version to start step + if (job_conf.reset_param_version()) { + entry.second->set_version(job_conf.step()); + } + } else { entry.second->InitValues(job_conf.step()); if (!job_conf.reset_param_version()) LOG(ERROR) << "better reset version of params from checkpoints " << "to the same as other newly initialized params!"; } + } // warmup training before put params to servers for (; step_ < job_conf.warmup_steps(); step_++) @@ -131,6 +123,12 @@ void ConnectStub(int grp, int id, Dealer* dealer, EntityType entity) { dealer->Send(&ping); } +void Worker::Test(int steps, Phase phase, NeuralNet* net) { + for (int step = 0; step < steps; step++) + TestOneBatch(step, phase, net); + Display(phase, " ", net); +} + void Worker::Run() { LOG(ERROR) << "Worker (group = " << grp_id_ <<", id = " << id_ << ") start"; auto cluster = Cluster::Get(); @@ -156,15 +154,13 @@ void Worker::Run() { while (!StopNow(step_)) { if (ValidateNow(step_) && val_net_ != nullptr) { CollectAll(step_, val_net_); - for (int step = 0; step < job_conf_.validate_steps(); step++) - TestOneBatch(step, kVal, val_net_); - Display(kVal, "Validation @ step " + std::to_string(step_), val_net_); + LOG(ERROR) << "Validation @ step " + std::to_string(step_); + Test(job_conf_.validate_steps(), kVal, val_net_); } if (TestNow(step_) && test_net_ != nullptr) { CollectAll(step_, test_net_); - for (int step = 0; step < job_conf_.test_steps(); step++) - TestOneBatch(step, kTest, test_net_); - Display(kTest, "Test @ step " + std::to_string(step_), test_net_); + LOG(ERROR) << "Test @ step " + std::to_string(step_); + Test(job_conf_.test_steps(), kTest, test_net_); } if (CheckpointNow(step_) && grp_id_ == 0) { CollectAll(step_, train_net_); @@ -212,7 +208,7 @@ void Worker::Checkpoint(int step, const std::string& folder, NeuralNet* net) { int Worker::Put(int step, Param* param) { if (dealer_ == nullptr) { - LOG(ERROR) << "Null dealer in worker (" << grp_id_ << ", " << id_ << ")"; + LOG(WARNING) << "Null dealer in worker (" << grp_id_ << ", " << id_ << ")"; return 1; } Msg* msg = new Msg(Addr(grp_id_, id_, kWorkerParam), Addr(-1, -1, kStub)); @@ -226,7 +222,7 @@ int Worker::Get(int step, Param* param) { if (param->version() >= step) return 1; if (dealer_ == nullptr) { - LOG(ERROR) << "Null dealer in worker (" << grp_id_ << ", " << id_ << ")"; + LOG(WARNING) << "Null dealer in worker (" << grp_id_ << ", " << id_ << ")"; return 1; } Msg* msg = new Msg(Addr(grp_id_, id_, kWorkerParam), Addr(-1, -1, kStub)); @@ -239,7 +235,7 @@ int Worker::Get(int step, Param* param) { int Worker::Update(int step, Param* param) { param->set_local_version(param->version()); if (dealer_ == nullptr) { - LOG(ERROR) << "Null dealer in worker (" << grp_id_ << ", " << id_ << ")"; + LOG(WARNING) << "Null dealer in worker (" << grp_id_ << ", " << id_ << ")"; return 1; } Msg* msg = new Msg(Addr(grp_id_, id_, kWorkerParam), Addr(-1, -1, kStub)); @@ -272,7 +268,7 @@ void Worker::Display(int flag, const std::string& prefix, NeuralNet* net) { if (layer->partition_id() == id_) { const string& disp = layer->ToString(false, flag); if (disp.length()) - LOG(ERROR) << prefix << ": " << disp; + LOG(ERROR) << prefix << " " << disp; if (job_conf_.debug()) { const string& info = layer->ToString(true, flag); if (info.length()) {