diff options
author | Ronghang Hu <huronghang@hotmail.com> | 2015-11-27 10:50:52 -0800 |
---|---|---|
committer | Ronghang Hu <huronghang@hotmail.com> | 2015-11-27 10:50:52 -0800 |
commit | c7ee2615a8df2dedb408ff9c2c842e5f4f42a310 (patch) | |
tree | d1e6f5bc0f0936f382a4b0b2065ea87dff823301 /examples | |
parent | 665649a50d81f65bc2d93e4956b17de83dadf8e3 (diff) | |
parent | b72b0318e2802785c17be1fe8ed1b6899961df19 (diff) | |
download | caffeonacl-c7ee2615a8df2dedb408ff9c2c842e5f4f42a310.tar.gz caffeonacl-c7ee2615a8df2dedb408ff9c2c842e5f4f42a310.tar.bz2 caffeonacl-c7ee2615a8df2dedb408ff9c2c842e5f4f42a310.zip |
Merge pull request #3361 from BonsaiAI/avoid-snprintf
replace snprintf with a C++98 equivalent
Diffstat (limited to 'examples')
-rw-r--r-- | examples/cifar10/convert_cifar_data.cpp | 13 | ||||
-rw-r--r-- | examples/mnist/convert_mnist_data.cpp | 12 | ||||
-rw-r--r-- | examples/siamese/convert_mnist_siamese_data.cpp | 7 |
3 files changed, 14 insertions, 18 deletions
diff --git a/examples/cifar10/convert_cifar_data.cpp b/examples/cifar10/convert_cifar_data.cpp index f4c42e4d..e1b89f42 100644 --- a/examples/cifar10/convert_cifar_data.cpp +++ b/examples/cifar10/convert_cifar_data.cpp @@ -16,6 +16,7 @@ #include "caffe/proto/caffe.pb.h" #include "caffe/util/db.hpp" +#include "caffe/util/format.hpp" using caffe::Datum; using boost::scoped_ptr; @@ -52,19 +53,18 @@ void convert_dataset(const string& input_folder, const string& output_folder, for (int fileid = 0; fileid < kCIFARTrainBatches; ++fileid) { // Open files LOG(INFO) << "Training Batch " << fileid + 1; - snprintf(str_buffer, kCIFARImageNBytes, "/data_batch_%d.bin", fileid + 1); - std::ifstream data_file((input_folder + str_buffer).c_str(), + string batchFileName = input_folder + "/data_batch_" + + caffe::format_int(fileid+1) + ".bin"; + std::ifstream data_file(batchFileName.c_str(), std::ios::in | std::ios::binary); CHECK(data_file) << "Unable to open train file #" << fileid + 1; for (int itemid = 0; itemid < kCIFARBatchSize; ++itemid) { read_image(&data_file, &label, str_buffer); datum.set_label(label); datum.set_data(str_buffer, kCIFARImageNBytes); - int length = snprintf(str_buffer, kCIFARImageNBytes, "%05d", - fileid * kCIFARBatchSize + itemid); string out; CHECK(datum.SerializeToString(&out)); - txn->Put(string(str_buffer, length), out); + txn->Put(caffe::format_int(fileid * kCIFARBatchSize + itemid, 5), out); } } txn->Commit(); @@ -82,10 +82,9 @@ void convert_dataset(const string& input_folder, const string& output_folder, read_image(&data_file, &label, str_buffer); datum.set_label(label); datum.set_data(str_buffer, kCIFARImageNBytes); - int length = snprintf(str_buffer, kCIFARImageNBytes, "%05d", itemid); string out; CHECK(datum.SerializeToString(&out)); - txn->Put(string(str_buffer, length), out); + txn->Put(caffe::format_int(itemid, 5), out); } txn->Commit(); test_db->Close(); diff --git a/examples/mnist/convert_mnist_data.cpp b/examples/mnist/convert_mnist_data.cpp index 8f29bafd..16d28093 100644 --- a/examples/mnist/convert_mnist_data.cpp +++ b/examples/mnist/convert_mnist_data.cpp @@ -23,6 +23,7 @@ #include <string> #include "caffe/proto/caffe.pb.h" +#include "caffe/util/format.hpp" #if defined(USE_LEVELDB) && defined(USE_LMDB) @@ -108,8 +109,6 @@ void convert_dataset(const char* image_filename, const char* label_filename, char label; char* pixels = new char[rows * cols]; int count = 0; - const int kMaxKeyLength = 10; - char key_cstr[kMaxKeyLength]; string value; Datum datum; @@ -123,18 +122,17 @@ void convert_dataset(const char* image_filename, const char* label_filename, label_file.read(&label, 1); datum.set_data(pixels, rows*cols); datum.set_label(label); - snprintf(key_cstr, kMaxKeyLength, "%08d", item_id); + string key_str = caffe::format_int(item_id, 8); datum.SerializeToString(&value); - string keystr(key_cstr); // Put in db if (db_backend == "leveldb") { // leveldb - batch->Put(keystr, value); + batch->Put(key_str, value); } else if (db_backend == "lmdb") { // lmdb mdb_data.mv_size = value.size(); mdb_data.mv_data = reinterpret_cast<void*>(&value[0]); - mdb_key.mv_size = keystr.size(); - mdb_key.mv_data = reinterpret_cast<void*>(&keystr[0]); + mdb_key.mv_size = key_str.size(); + mdb_key.mv_data = reinterpret_cast<void*>(&key_str[0]); CHECK_EQ(mdb_put(mdb_txn, mdb_dbi, &mdb_key, &mdb_data, 0), MDB_SUCCESS) << "mdb_put failed"; } else { diff --git a/examples/siamese/convert_mnist_siamese_data.cpp b/examples/siamese/convert_mnist_siamese_data.cpp index ad08036f..928b3fbf 100644 --- a/examples/siamese/convert_mnist_siamese_data.cpp +++ b/examples/siamese/convert_mnist_siamese_data.cpp @@ -13,6 +13,7 @@ #include "stdint.h" #include "caffe/proto/caffe.pb.h" +#include "caffe/util/format.hpp" #include "caffe/util/math_functions.hpp" #ifdef USE_LEVELDB @@ -75,8 +76,6 @@ void convert_dataset(const char* image_filename, const char* label_filename, char label_i; char label_j; char* pixels = new char[2 * rows * cols]; - const int kMaxKeyLength = 10; - char key[kMaxKeyLength]; std::string value; caffe::Datum datum; @@ -99,8 +98,8 @@ void convert_dataset(const char* image_filename, const char* label_filename, datum.set_label(0); } datum.SerializeToString(&value); - snprintf(key, kMaxKeyLength, "%08d", itemid); - db->Put(leveldb::WriteOptions(), std::string(key), value); + std::string key_str = caffe::format_int(itemid, 8); + db->Put(leveldb::WriteOptions(), key_str, value); } delete db; |