#include "caffe/proto/caffe.pb.h"
#include "caffe/util/db.hpp"
+#include "caffe/util/format.hpp"
using caffe::Datum;
using boost::scoped_ptr;
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();
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();
#include <string>
#include "caffe/proto/caffe.pb.h"
+#include "caffe/util/format.hpp"
#if defined(USE_LEVELDB) && defined(USE_LMDB)
char label;
char* pixels = new char[rows * cols];
int count = 0;
- const int kMaxKeyLength = 10;
- char key_cstr[kMaxKeyLength];
string value;
Datum datum;
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 {
#include "stdint.h"
#include "caffe/proto/caffe.pb.h"
+#include "caffe/util/format.hpp"
#include "caffe/util/math_functions.hpp"
#ifdef USE_LEVELDB
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;
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;
--- /dev/null
+#ifndef CAFFE_UTIL_FORMAT_H_
+#define CAFFE_UTIL_FORMAT_H_
+
+#include <iomanip> // NOLINT(readability/streams)
+#include <sstream> // NOLINT(readability/streams)
+#include <string>
+
+namespace caffe {
+
+inline std::string format_int(int n, int numberOfLeadingZeros = 0 ) {
+ std::ostringstream s;
+ s << std::setw(numberOfLeadingZeros) << std::setfill('0') << n;
+ return s.str();
+}
+
+}
+
+#endif // CAFFE_UTIL_FORMAT_H_
#include <vector>
#include "caffe/solver.hpp"
+#include "caffe/util/format.hpp"
#include "caffe/util/hdf5.hpp"
#include "caffe/util/io.hpp"
#include "caffe/util/upgrade_proto.hpp"
template <typename Dtype>
string Solver<Dtype>::SnapshotFilename(const string extension) {
- string filename(param_.snapshot_prefix());
- const int kBufferSize = 20;
- char iter_str_buffer[kBufferSize];
- snprintf(iter_str_buffer, kBufferSize, "_iter_%d", iter_);
- return filename + iter_str_buffer + extension;
+ return param_.snapshot_prefix() + "_iter_" + caffe::format_int(iter_)
+ + extension;
}
template <typename Dtype>
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/db.hpp"
+#include "caffe/util/format.hpp"
#include "caffe/util/io.hpp"
#include "caffe/util/rng.hpp"
std::string root_folder(argv[1]);
Datum datum;
int count = 0;
- const int kMaxKeyLength = 256;
- char key_cstr[kMaxKeyLength];
int data_size = 0;
bool data_size_initialized = false;
}
}
// sequential
- int length = snprintf(key_cstr, kMaxKeyLength, "%08d_%s", line_id,
- lines[line_id].first.c_str());
+ string key_str = caffe::format_int(line_id, 8) + "_" + lines[line_id].first;
// Put in db
string out;
CHECK(datum.SerializeToString(&out));
- txn->Put(string(key_cstr, length), out);
+ txn->Put(key_str, out);
if (++count % 1000 == 0) {
// Commit db
-#include <stdio.h> // for snprintf
#include <string>
#include <vector>
#include "caffe/net.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/db.hpp"
+#include "caffe/util/format.hpp"
#include "caffe/util/io.hpp"
#include "caffe/vision_layers.hpp"
LOG(ERROR)<< "Extacting Features";
Datum datum;
- const int kMaxKeyStrLength = 100;
- char key_str[kMaxKeyStrLength];
std::vector<Blob<float>*> input_vec;
std::vector<int> image_indices(num_features, 0);
for (int batch_index = 0; batch_index < num_mini_batches; ++batch_index) {
for (int d = 0; d < dim_features; ++d) {
datum.add_float_data(feature_blob_data[d]);
}
- int length = snprintf(key_str, kMaxKeyStrLength, "%010d",
- image_indices[i]);
+ string key_str = caffe::format_int(image_indices[i], 10);
+
string out;
CHECK(datum.SerializeToString(&out));
- txns.at(i)->Put(std::string(key_str, length), out);
+ txns.at(i)->Put(key_str, out);
++image_indices[i];
if (image_indices[i] % 1000 == 0) {
txns.at(i)->Commit();
LOG(ERROR)<< "Successfully extracted the features!";
return 0;
}
-