From 6f4221814356baa5d50c870ce883d691394237f0 Mon Sep 17 00:00:00 2001 From: immars Date: Tue, 24 Mar 2015 13:45:05 +0800 Subject: [PATCH 1/6] datum file initial commit --- include/caffe/util/db.hpp | 83 +++++++++++++++++++++++++++++++++++++ src/caffe/proto/caffe.proto | 1 + src/caffe/util/db.cpp | 76 +++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) diff --git a/include/caffe/util/db.hpp b/include/caffe/util/db.hpp index afdb8d2c4f8..1e6263db10a 100644 --- a/include/caffe/util/db.hpp +++ b/include/caffe/util/db.hpp @@ -181,6 +181,89 @@ class LMDB : public DB { MDB_dbi mdb_dbi_; }; + +#define MAX_BUF 10485760 //max entry size +class DatumFileCursor : public Cursor { + public: + explicit DatumFileCursor(string& path) { + this->path = path; + in = NULL; + SeekToFirst(); + } + virtual ~DatumFileCursor() { + + } + virtual void SeekToFirst(); + + virtual void Next(); + + virtual string key() { + if(!valid()){ + LOG(WARNING) << "not valid at key()"; + return ""; + } + return _key; + } + virtual string value() { + if(!valid()){ + LOG(WARNING) << "not valid at value()"; + return ""; + } + return _value; + } + + virtual bool valid() { return valid_; } + + private: + string path; + std::ifstream* in; + bool valid_; + + string _key, _value; +}; + +class DatumFileTransaction : public Transaction { + public: + explicit DatumFileTransaction(std::ofstream* out){ + this->out = out; + } + + virtual void Put(const string& key, const string& value); + + virtual void Commit() { + out->flush(); + } + + private: + + std::ofstream* out; + DISABLE_COPY_AND_ASSIGN(DatumFileTransaction); +}; + + +class DatumFileDB : public DB { + public: + DatumFileDB() { out = NULL; } + virtual ~DatumFileDB() { Close(); } + virtual void Open(const string& source, Mode mode){ + path = source; + this->can_write = mode != db::READ; + } + virtual void Close() { + if(out){ + out->close(); + out = NULL; + } + } + virtual DatumFileCursor* NewCursor(){return new DatumFileCursor(this->path);} + virtual Transaction* NewTransaction(); + + private: + string path; + std::ofstream* out; + bool can_write; +}; + DB* GetDB(DataParameter::DB backend); DB* GetDB(const string& backend); diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index 5b21cf20028..13ac872648b 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -436,6 +436,7 @@ message DataParameter { enum DB { LEVELDB = 0; LMDB = 1; + DATUMFILE = 2; } // Specify the data source. optional string source = 1; diff --git a/src/caffe/util/db.cpp b/src/caffe/util/db.cpp index 7f7018107ec..6b5faafc4c8 100644 --- a/src/caffe/util/db.cpp +++ b/src/caffe/util/db.cpp @@ -59,12 +59,86 @@ void LMDBTransaction::Put(const string& key, const string& value) { MDB_CHECK(mdb_put(mdb_txn_, *mdb_dbi_, &mdb_key, &mdb_value, 0)); } + +void DatumFileCursor::SeekToFirst() { + if(in && in->is_open()){ + in->close(); + } + LOG(INFO) << "reset ifstream" << path; + in = new std::ifstream(path.c_str(), std::ifstream::in|std::ifstream::binary); + Next(); + } + +void DatumFileCursor::Next() { + valid_ = false; + if(!in->is_open()){ + LOG(WARNING) << "file not open!" << path; + } + uint32_t record_size, key_size, value_size; + in->read(reinterpret_cast(&record_size), sizeof record_size); + if(in->gcount() != (sizeof record_size) || record_size > MAX_BUF){ + if(!in->eof()){ + LOG(WARNING) << "record_size read error: gcount\t" << in->gcount() << "\trecord_size\t" << record_size; + } + return; + } + in->read(reinterpret_cast(&key_size), sizeof key_size); + if(in->gcount() != sizeof key_size || key_size > MAX_BUF){ + LOG(WARNING) << "key_size read error: gcount\t" << in->gcount() << "\tkey_size\t" << key_size; + return; + } + _key.resize(key_size); + in->read(&_key[0], key_size); + if(in->gcount() != key_size){ + LOG(WARNING) << "key read error: gcount\t" << in->gcount() << "\tkey_size\t" << key_size; + return; + } + in->read(reinterpret_cast(&value_size), sizeof value_size); + if(in->gcount() != sizeof value_size || value_size > MAX_BUF){ + LOG(WARNING) << "value_size read error: gcount\t" << in->gcount() << "\tvalue_size\t" << value_size; + return; + } + _value.resize(value_size); + in->read(&_value[0], value_size); + if(in->gcount() != value_size){ + LOG(WARNING) << "value read error: gcount\t" << in->gcount() << "\tvalue_size\t" << value_size; + return; + } + valid_=true; +} + +void DatumFileTransaction::Put(const string& key, const string& value){ + try{ + uint32_t key_size = key.size(), value_size = value.size(); + uint32_t record_size = key_size + value_size + sizeof key_size + sizeof value_size; + out->write(reinterpret_cast(&record_size), sizeof record_size); + out->write(reinterpret_cast(&key_size), sizeof key_size); + out->write(key.data(), key_size); + out->write(reinterpret_cast(&value_size), sizeof value_size); + out->write(value.data(), value_size); + }catch(std::ios_base::failure& e){ + LOG(WARNING) << "exception: "<< e.what() << "rdstate: " << out->rdstate() << '\n'; + } +} + +Transaction* DatumFileDB::NewTransaction(){ + if(!this->out){ + out = new std::ofstream(); + out->open(this->path.c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary); + out->exceptions(out->exceptions() | std::ios::failbit); + LOG(INFO) << "out created!" << path << std::endl; + } + return new DatumFileTransaction(this->out); +}; + DB* GetDB(DataParameter::DB backend) { switch (backend) { case DataParameter_DB_LEVELDB: return new LevelDB(); case DataParameter_DB_LMDB: return new LMDB(); + case DataParameter_DB_DATUMFILE: + return new DatumFileDB(); default: LOG(FATAL) << "Unknown database backend"; } @@ -75,6 +149,8 @@ DB* GetDB(const string& backend) { return new LevelDB(); } else if (backend == "lmdb") { return new LMDB(); + } else if (backend == "datumfile") { + return new DatumFileDB(); } else { LOG(FATAL) << "Unknown database backend"; } From afee83b021cd8c08b568b11d2110edefcde5f527 Mon Sep 17 00:00:00 2001 From: Lisen Mu Date: Tue, 24 Mar 2015 23:01:58 +0800 Subject: [PATCH 2/6] script change --- examples/imagenet/create_imagenet.sh | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/imagenet/create_imagenet.sh b/examples/imagenet/create_imagenet.sh index e912ac43cd7..836a213d979 100755 --- a/examples/imagenet/create_imagenet.sh +++ b/examples/imagenet/create_imagenet.sh @@ -9,6 +9,9 @@ TOOLS=build/tools TRAIN_DATA_ROOT=/path/to/imagenet/train/ VAL_DATA_ROOT=/path/to/imagenet/val/ +# leveldb|lmdb|datumfile +DB_TYPE=datumfile + # Set RESIZE=true to resize the images to 256x256. Leave as false if images have # already been resized using another tool. RESIZE=false @@ -34,24 +37,26 @@ if [ ! -d "$VAL_DATA_ROOT" ]; then exit 1 fi -echo "Creating train lmdb..." +echo "Creating train $DB_TYPE ..." GLOG_logtostderr=1 $TOOLS/convert_imageset \ --resize_height=$RESIZE_HEIGHT \ --resize_width=$RESIZE_WIDTH \ --shuffle \ + --backend=$DB_TYPE \ $TRAIN_DATA_ROOT \ $DATA/train.txt \ - $EXAMPLE/ilsvrc12_train_lmdb + $EXAMPLE/ilsvrc12_train_$DB_TYPE -echo "Creating val lmdb..." +echo "Creating val $DB_TYPE ..." GLOG_logtostderr=1 $TOOLS/convert_imageset \ --resize_height=$RESIZE_HEIGHT \ --resize_width=$RESIZE_WIDTH \ --shuffle \ + --backend=$DB_TYPE \ $VAL_DATA_ROOT \ $DATA/val.txt \ - $EXAMPLE/ilsvrc12_val_lmdb + $EXAMPLE/ilsvrc12_val_$DB_TYPE echo "Done." From bbbda4492cb3038e209560a056b51e73218b6200 Mon Sep 17 00:00:00 2001 From: Lisen Mu Date: Wed, 25 Mar 2015 10:11:12 +0800 Subject: [PATCH 3/6] lint --- include/caffe/util/db.hpp | 26 +++++++------- src/caffe/util/db.cpp | 73 ++++++++++++++++++++++----------------- 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/include/caffe/util/db.hpp b/include/caffe/util/db.hpp index 1e6263db10a..9544452fe0f 100644 --- a/include/caffe/util/db.hpp +++ b/include/caffe/util/db.hpp @@ -182,30 +182,29 @@ class LMDB : public DB { }; -#define MAX_BUF 10485760 //max entry size +#define MAX_BUF 10485760 // max entry size class DatumFileCursor : public Cursor { public: - explicit DatumFileCursor(string& path) { + explicit DatumFileCursor(const string& path) { this->path = path; in = NULL; SeekToFirst(); } virtual ~DatumFileCursor() { - } virtual void SeekToFirst(); virtual void Next(); virtual string key() { - if(!valid()){ - LOG(WARNING) << "not valid at key()"; - return ""; + if (!valid()) { + LOG(WARNING) << "not valid at key()"; + return ""; } return _key; } virtual string value() { - if(!valid()){ + if (!valid()) { LOG(WARNING) << "not valid at value()"; return ""; } @@ -224,7 +223,7 @@ class DatumFileCursor : public Cursor { class DatumFileTransaction : public Transaction { public: - explicit DatumFileTransaction(std::ofstream* out){ + explicit DatumFileTransaction(std::ofstream* out) { this->out = out; } @@ -235,7 +234,6 @@ class DatumFileTransaction : public Transaction { } private: - std::ofstream* out; DISABLE_COPY_AND_ASSIGN(DatumFileTransaction); }; @@ -245,17 +243,17 @@ class DatumFileDB : public DB { public: DatumFileDB() { out = NULL; } virtual ~DatumFileDB() { Close(); } - virtual void Open(const string& source, Mode mode){ + virtual void Open(const string& source, Mode mode) { path = source; this->can_write = mode != db::READ; } virtual void Close() { - if(out){ - out->close(); - out = NULL; + if (out) { + out->close(); + out = NULL; } } - virtual DatumFileCursor* NewCursor(){return new DatumFileCursor(this->path);} + virtual DatumFileCursor* NewCursor() {return new DatumFileCursor(this->path);} virtual Transaction* NewTransaction(); private: diff --git a/src/caffe/util/db.cpp b/src/caffe/util/db.cpp index 6b5faafc4c8..25358a292cc 100644 --- a/src/caffe/util/db.cpp +++ b/src/caffe/util/db.cpp @@ -61,75 +61,84 @@ void LMDBTransaction::Put(const string& key, const string& value) { void DatumFileCursor::SeekToFirst() { - if(in && in->is_open()){ - in->close(); + if (in && in->is_open()) { + in->close(); } LOG(INFO) << "reset ifstream" << path; - in = new std::ifstream(path.c_str(), std::ifstream::in|std::ifstream::binary); + in = new std::ifstream(path.c_str(), + std::ifstream::in|std::ifstream::binary); Next(); } void DatumFileCursor::Next() { valid_ = false; - if(!in->is_open()){ - LOG(WARNING) << "file not open!" << path; + if (!in->is_open()) { + LOG(WARNING) << "file not open!" << path; } uint32_t record_size, key_size, value_size; in->read(reinterpret_cast(&record_size), sizeof record_size); - if(in->gcount() != (sizeof record_size) || record_size > MAX_BUF){ - if(!in->eof()){ - LOG(WARNING) << "record_size read error: gcount\t" << in->gcount() << "\trecord_size\t" << record_size; - } - return; + if (in->gcount() != (sizeof record_size) || record_size > MAX_BUF) { + if (!in->eof()) { + LOG(WARNING) << "record_size read error: gcount\t" + << in->gcount() << "\trecord_size\t" << record_size; + } + return; } in->read(reinterpret_cast(&key_size), sizeof key_size); - if(in->gcount() != sizeof key_size || key_size > MAX_BUF){ - LOG(WARNING) << "key_size read error: gcount\t" << in->gcount() << "\tkey_size\t" << key_size; - return; + if (in->gcount() != sizeof key_size || key_size > MAX_BUF) { + LOG(WARNING) << "key_size read error: gcount\t" + << in->gcount() << "\tkey_size\t" << key_size; + return; } _key.resize(key_size); in->read(&_key[0], key_size); - if(in->gcount() != key_size){ - LOG(WARNING) << "key read error: gcount\t" << in->gcount() << "\tkey_size\t" << key_size; - return; + if (in->gcount() != key_size) { + LOG(WARNING) << "key read error: gcount\t" + << in->gcount() << "\tkey_size\t" << key_size; + return; } in->read(reinterpret_cast(&value_size), sizeof value_size); - if(in->gcount() != sizeof value_size || value_size > MAX_BUF){ - LOG(WARNING) << "value_size read error: gcount\t" << in->gcount() << "\tvalue_size\t" << value_size; - return; + if (in->gcount() != sizeof value_size || value_size > MAX_BUF) { + LOG(WARNING) << "value_size read error: gcount\t" + << in->gcount() << "\tvalue_size\t" << value_size; + return; } _value.resize(value_size); in->read(&_value[0], value_size); - if(in->gcount() != value_size){ - LOG(WARNING) << "value read error: gcount\t" << in->gcount() << "\tvalue_size\t" << value_size; - return; + if (in->gcount() != value_size) { + LOG(WARNING) << "value read error: gcount\t" + << in->gcount() << "\tvalue_size\t" << value_size; + return; } - valid_=true; + valid_ = true; } -void DatumFileTransaction::Put(const string& key, const string& value){ - try{ +void DatumFileTransaction::Put(const string& key, const string& value) { + try { uint32_t key_size = key.size(), value_size = value.size(); - uint32_t record_size = key_size + value_size + sizeof key_size + sizeof value_size; + uint32_t record_size = key_size + value_size + + sizeof key_size + sizeof value_size; out->write(reinterpret_cast(&record_size), sizeof record_size); out->write(reinterpret_cast(&key_size), sizeof key_size); out->write(key.data(), key_size); out->write(reinterpret_cast(&value_size), sizeof value_size); out->write(value.data(), value_size); - }catch(std::ios_base::failure& e){ - LOG(WARNING) << "exception: "<< e.what() << "rdstate: " << out->rdstate() << '\n'; + } catch(std::ios_base::failure& e) { + LOG(WARNING) << "exception: " + << e.what() << "rdstate: " << out->rdstate() << '\n'; } } -Transaction* DatumFileDB::NewTransaction(){ - if(!this->out){ +Transaction* DatumFileDB::NewTransaction() { + if (!this->out) { out = new std::ofstream(); - out->open(this->path.c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary); + out->open(this->path.c_str(), + std::ofstream::out | std::ofstream::trunc | std::ofstream::binary); out->exceptions(out->exceptions() | std::ios::failbit); LOG(INFO) << "out created!" << path << std::endl; } return new DatumFileTransaction(this->out); -}; +} DB* GetDB(DataParameter::DB backend) { switch (backend) { From f764b946371342e4d2c9d205282cf7c26568f062 Mon Sep 17 00:00:00 2001 From: immars Date: Fri, 27 Mar 2015 12:38:12 +0800 Subject: [PATCH 4/6] fix for code review; CHECK on error conditions --- include/caffe/util/db.hpp | 56 ++++++++++----------- src/caffe/util/db.cpp | 101 ++++++++++++++++++-------------------- 2 files changed, 75 insertions(+), 82 deletions(-) diff --git a/include/caffe/util/db.hpp b/include/caffe/util/db.hpp index 9544452fe0f..884c9ff098f 100644 --- a/include/caffe/util/db.hpp +++ b/include/caffe/util/db.hpp @@ -182,84 +182,82 @@ class LMDB : public DB { }; -#define MAX_BUF 10485760 // max entry size +#define MAX_BUF 104857600 // max entry size class DatumFileCursor : public Cursor { public: explicit DatumFileCursor(const string& path) { - this->path = path; - in = NULL; + this->path_ = path; + in_ = NULL; SeekToFirst(); } virtual ~DatumFileCursor() { + if (in_ && in_->is_open()) { + in_->close(); + } } virtual void SeekToFirst(); virtual void Next(); virtual string key() { - if (!valid()) { - LOG(WARNING) << "not valid at key()"; - return ""; - } - return _key; + CHECK(valid()) << "not valid state at key()"; + return key_; } virtual string value() { - if (!valid()) { - LOG(WARNING) << "not valid at value()"; - return ""; - } - return _value; + CHECK(valid()) << "not valid state at value()"; + return value_; } virtual bool valid() { return valid_; } private: - string path; - std::ifstream* in; + string path_; + std::ifstream* in_; bool valid_; - string _key, _value; + string key_, value_; }; class DatumFileTransaction : public Transaction { public: explicit DatumFileTransaction(std::ofstream* out) { - this->out = out; + this->out_ = out; } virtual void Put(const string& key, const string& value); virtual void Commit() { - out->flush(); + out_->flush(); } private: - std::ofstream* out; + std::ofstream* out_; DISABLE_COPY_AND_ASSIGN(DatumFileTransaction); }; class DatumFileDB : public DB { public: - DatumFileDB() { out = NULL; } + DatumFileDB() { out_ = NULL; can_write_ = false;} virtual ~DatumFileDB() { Close(); } virtual void Open(const string& source, Mode mode) { - path = source; - this->can_write = mode != db::READ; + path_ = source; + this->can_write_ = mode != db::READ; } virtual void Close() { - if (out) { - out->close(); - out = NULL; + if (out_) { + out_->close(); + out_ = NULL; } } - virtual DatumFileCursor* NewCursor() {return new DatumFileCursor(this->path);} + virtual DatumFileCursor* NewCursor() { return new DatumFileCursor(this->path_); } virtual Transaction* NewTransaction(); private: - string path; - std::ofstream* out; - bool can_write; + string path_; + std::ofstream* out_; + + bool can_write_; }; DB* GetDB(DataParameter::DB backend); diff --git a/src/caffe/util/db.cpp b/src/caffe/util/db.cpp index 25358a292cc..f2fce44af92 100644 --- a/src/caffe/util/db.cpp +++ b/src/caffe/util/db.cpp @@ -61,55 +61,50 @@ void LMDBTransaction::Put(const string& key, const string& value) { void DatumFileCursor::SeekToFirst() { - if (in && in->is_open()) { - in->close(); + if (in_ && in_->is_open()) { + in_->close(); } - LOG(INFO) << "reset ifstream" << path; - in = new std::ifstream(path.c_str(), + LOG(INFO) << "reset ifstream " << path_; + in_ = new std::ifstream(path_.c_str(), std::ifstream::in|std::ifstream::binary); Next(); } void DatumFileCursor::Next() { valid_ = false; - if (!in->is_open()) { - LOG(WARNING) << "file not open!" << path; - } - uint32_t record_size, key_size, value_size; - in->read(reinterpret_cast(&record_size), sizeof record_size); - if (in->gcount() != (sizeof record_size) || record_size > MAX_BUF) { - if (!in->eof()) { - LOG(WARNING) << "record_size read error: gcount\t" - << in->gcount() << "\trecord_size\t" << record_size; - } - return; - } - in->read(reinterpret_cast(&key_size), sizeof key_size); - if (in->gcount() != sizeof key_size || key_size > MAX_BUF) { - LOG(WARNING) << "key_size read error: gcount\t" - << in->gcount() << "\tkey_size\t" << key_size; - return; - } - _key.resize(key_size); - in->read(&_key[0], key_size); - if (in->gcount() != key_size) { - LOG(WARNING) << "key read error: gcount\t" - << in->gcount() << "\tkey_size\t" << key_size; - return; - } - in->read(reinterpret_cast(&value_size), sizeof value_size); - if (in->gcount() != sizeof value_size || value_size > MAX_BUF) { - LOG(WARNING) << "value_size read error: gcount\t" - << in->gcount() << "\tvalue_size\t" << value_size; - return; - } - _value.resize(value_size); - in->read(&_value[0], value_size); - if (in->gcount() != value_size) { - LOG(WARNING) << "value read error: gcount\t" - << in->gcount() << "\tvalue_size\t" << value_size; + CHECK(in_->is_open()) << "file is not open!" << path_; + + uint32_t record_size = 0, key_size = 0, value_size = 0; + in_->read(reinterpret_cast(&record_size), sizeof record_size); + if (in_->gcount() != (sizeof record_size) || record_size > MAX_BUF) { + CHECK(in_->eof() && record_size <= MAX_BUF) + <<"record_size read error: gcount\t" + << in_->gcount() << "\trecord_size\t" << record_size; return; } + + in_->read(reinterpret_cast(&key_size), sizeof key_size); + CHECK(in_->gcount() == sizeof key_size && key_size <= MAX_BUF) + << "key_size read error: gcount\t" + << in_->gcount() << "\tkey_size\t" << key_size; + + key_.resize(key_size); + in_->read(&key_[0], key_size); + CHECK(in_->gcount() == key_size) + << "key read error: gcount\t" + << in_->gcount() << "\tkey_size\t" << key_size; + + in_->read(reinterpret_cast(&value_size), sizeof value_size); + CHECK(in_->gcount() == sizeof value_size && value_size <= MAX_BUF) + << "value_size read error: gcount\t" + << in_->gcount() << "\tvalue_size\t" << value_size; + + value_.resize(value_size); + in_->read(&value_[0], value_size); + CHECK(in_->gcount() == value_size) + << "value read error: gcount\t" + << in_->gcount() << "\tvalue_size\t" << value_size; + valid_ = true; } @@ -118,26 +113,26 @@ void DatumFileTransaction::Put(const string& key, const string& value) { uint32_t key_size = key.size(), value_size = value.size(); uint32_t record_size = key_size + value_size + sizeof key_size + sizeof value_size; - out->write(reinterpret_cast(&record_size), sizeof record_size); - out->write(reinterpret_cast(&key_size), sizeof key_size); - out->write(key.data(), key_size); - out->write(reinterpret_cast(&value_size), sizeof value_size); - out->write(value.data(), value_size); + out_->write(reinterpret_cast(&record_size), sizeof record_size); + out_->write(reinterpret_cast(&key_size), sizeof key_size); + out_->write(key.data(), key_size); + out_->write(reinterpret_cast(&value_size), sizeof value_size); + out_->write(value.data(), value_size); } catch(std::ios_base::failure& e) { - LOG(WARNING) << "exception: " - << e.what() << "rdstate: " << out->rdstate() << '\n'; + LOG(FATAL) << "Exception: " + << e.what() << " rdstate: " << out_->rdstate() << '\n'; } } Transaction* DatumFileDB::NewTransaction() { - if (!this->out) { - out = new std::ofstream(); - out->open(this->path.c_str(), + if (!this->out_) { + out_ = new std::ofstream(); + out_->open(this->path_.c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary); - out->exceptions(out->exceptions() | std::ios::failbit); - LOG(INFO) << "out created!" << path << std::endl; + out_->exceptions(out_->exceptions() | std::ios::failbit); + LOG(INFO) << "Output created: " << path_ << std::endl; } - return new DatumFileTransaction(this->out); + return new DatumFileTransaction(this->out_); } DB* GetDB(DataParameter::DB backend) { From ccb212c3158d8b5aba64cdd2bde6b1caa286b202 Mon Sep 17 00:00:00 2001 From: immars Date: Fri, 27 Mar 2015 14:24:01 +0800 Subject: [PATCH 5/6] lint --- include/caffe/util/db.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/caffe/util/db.hpp b/include/caffe/util/db.hpp index 884c9ff098f..5f1b6632cfa 100644 --- a/include/caffe/util/db.hpp +++ b/include/caffe/util/db.hpp @@ -250,7 +250,9 @@ class DatumFileDB : public DB { out_ = NULL; } } - virtual DatumFileCursor* NewCursor() { return new DatumFileCursor(this->path_); } + virtual DatumFileCursor* NewCursor() { + return new DatumFileCursor(this->path_); + } virtual Transaction* NewTransaction(); private: From d03d6007729e4df1f19d069e3419e358e6676467 Mon Sep 17 00:00:00 2001 From: immars Date: Fri, 27 Mar 2015 17:44:35 +0800 Subject: [PATCH 6/6] destructor fix --- include/caffe/util/db.hpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/caffe/util/db.hpp b/include/caffe/util/db.hpp index 5f1b6632cfa..48a4fac6c05 100644 --- a/include/caffe/util/db.hpp +++ b/include/caffe/util/db.hpp @@ -191,8 +191,10 @@ class DatumFileCursor : public Cursor { SeekToFirst(); } virtual ~DatumFileCursor() { - if (in_ && in_->is_open()) { + if (in_ != NULL && in_->is_open()) { in_->close(); + delete in_; + in_ = NULL; } } virtual void SeekToFirst(); @@ -245,8 +247,9 @@ class DatumFileDB : public DB { this->can_write_ = mode != db::READ; } virtual void Close() { - if (out_) { + if (out_ != NULL) { out_->close(); + delete out_; out_ = NULL; } }