зеркало из https://github.com/microsoft/caffe.git
Merge pull request #3361 from BonsaiAI/avoid-snprintf
replace snprintf with a C++98 equivalent
This commit is contained in:
Коммит
c7ee2615a8
|
@ -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();
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
#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_
|
|
@ -4,6 +4,7 @@
|
|||
#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"
|
||||
|
@ -448,11 +449,8 @@ void Solver<Dtype>::CheckSnapshotWritePermissions() {
|
|||
|
||||
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>
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
|
||||
#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"
|
||||
|
||||
|
@ -99,8 +100,6 @@ int main(int argc, char** argv) {
|
|||
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;
|
||||
|
||||
|
@ -131,13 +130,12 @@ int main(int argc, char** argv) {
|
|||
}
|
||||
}
|
||||
// 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
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
#include <stdio.h> // for snprintf
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
@ -10,6 +9,7 @@
|
|||
#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"
|
||||
|
||||
|
@ -135,8 +135,6 @@ int feature_extraction_pipeline(int argc, char** argv) {
|
|||
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) {
|
||||
|
@ -158,11 +156,11 @@ int feature_extraction_pipeline(int argc, char** argv) {
|
|||
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();
|
||||
|
@ -186,4 +184,3 @@ int feature_extraction_pipeline(int argc, char** argv) {
|
|||
LOG(ERROR)<< "Successfully extracted the features!";
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче