c++ 文件的参数
Parameters for a c++ file
我正在尝试从命令行为 Caffe执行一个C++
程序。 我需要传递参数列表来执行程序以创建数据库格式。该程序未在可用于执行程序的参数列表中提供示例。在这里你可以看到程序
// This program converts a triplet list to DB format as Triplet_Datum proto buffers
#include <algorithm>
#include <fstream> // NOLINT(readability/streams)
#include <string>
#include <utility>
#include <vector>
#include "boost/scoped_ptr.hpp"
#include "gflags/gflags.h"
#include "glog/logging.h"
#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"
using namespace caffe; // NOLINT(build/namespaces)
using std::pair;
using boost::scoped_ptr;
DEFINE_bool(gray, false,
"When this option is on, treat images as grayscale ones");
DEFINE_bool(shuffle, false,
"Randomly shuffle the order of images and their labels");
DEFINE_string(backend, "lmdb",
"The backend {lmdb, leveldb} for storing the result");
DEFINE_int32(resize_width, 0, "Width images are resized to");
DEFINE_int32(resize_height, 0, "Height images are resized to");
DEFINE_bool(check_size, false,
"When this option is on, check that all the datum have the same size");
DEFINE_bool(encoded, false,
"When this option is on, the encoded image will be save in datum");
DEFINE_string(encode_type, "",
"Optional: What type should we encode the image as ('png','jpg',...).");
DEFINE_string(triplet_list_name,"",
"Required: the triplet list file, in which each line stores the anchor/positive/negative images, respectively, being separated by t or a blank.");
DEFINE_string(db_save_name,"",
"Required: the file name that stores the created DB proto buffers.");
int main(int argc, char** argv) {
#ifdef USE_OPENCV
::google::InitGoogleLogging(argv[0]);
// Print output to stderr (while still logging)
FLAGS_alsologtostderr = 1;
#ifndef GFLAGS_GFLAGS_H_
namespace gflags = google;
#endif
gflags::SetUsageMessage("Convert a set of images in triplet format to the leveldb/lmdbn"
"format used as input for Caffe.n"
"Usage:n"
" convert_triplet_dataset [FLAGS]n");
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (argc < 1) {
gflags::ShowUsageWithFlagsRestrict(argv[0], "tools/convert_triplet_datum");
return 1;
}
const bool is_color = !FLAGS_gray;
const bool check_size = FLAGS_check_size;
const bool encoded = FLAGS_encoded;
const string encode_type = FLAGS_encode_type;
const std::string triplet_list_name = FLAGS_triplet_list_name;
const std::string db_save_name = FLAGS_db_save_name;
CHECK( triplet_list_name.size() > 0 ) << "the triplet_list_name param should be specified!";
CHECK( db_save_name.size() > 0 ) << "the db_save_name param should be specified!";
std::ifstream infile( triplet_list_name.c_str() );
std::vector< std::vector< std::string > > lines;
std::string line;
//size_t pos;
std::string anchor_img_name;
std::string pos_img_name;
std::string neg_img_name;
//std::vector< std::string > triple_pair(3);
while( infile >> anchor_img_name >> pos_img_name >> neg_img_name ){
std::vector< std::string > triple_pair;
triple_pair.push_back( anchor_img_name );
triple_pair.push_back( pos_img_name );
triple_pair.push_back( neg_img_name );
lines.push_back( triple_pair );
}
infile.close();
if (FLAGS_shuffle) {
// randomly shuffle data
LOG(INFO) << "Shuffling data";
shuffle(lines.begin(), lines.end());
}
LOG(INFO) << "A total of " << lines.size() << " images.";
if (encode_type.size() && !encoded)
LOG(INFO) << "encode_type specified, assuming encoded=true.";
int resize_height = std::max<int>(0, FLAGS_resize_height);
int resize_width = std::max<int>(0, FLAGS_resize_width);
// Create new DB
scoped_ptr<db::DB> db(db::GetDB(FLAGS_backend));
db->Open( db_save_name.c_str(), db::NEW);
scoped_ptr<db::Transaction> txn(db->NewTransaction());
// Storing to db
//std::string root_folder(argv[1]);
TripletDatum triplet_datum;
int count = 0;
int data_size = 0;
bool data_size_initialized = false;
for (int line_id = 0; line_id < lines.size(); ++line_id) {
bool status;
std::string enc = encode_type;
if (encoded && !enc.size()) {
// Guess the encoding type from the file name
string fn = lines[line_id][0];
size_t p = fn.rfind('.');
if ( p == fn.npos )
LOG(WARNING) << "Failed to guess the encoding of '" << fn << "'";
enc = fn.substr(p);
std::transform(enc.begin(), enc.end(), enc.begin(), ::tolower);
}
status = ReadTripletImagesToTripletDatum( lines[line_id], resize_height, resize_width, is_color, enc, &triplet_datum);
if (status == false) continue;
if (check_size) {
if (!data_size_initialized) {
data_size = triplet_datum.channels() * triplet_datum.height() * triplet_datum.width();
data_size_initialized = true;
} else {
const std::string& data = triplet_datum.data_anchor();
CHECK_EQ(data.size(), data_size) << "Incorrect data field size "
<< data.size();
}
}
// sequential
string key_str = caffe::format_int(line_id, 8) + "_" + lines[line_id][0];
// Put in db
std::string out;
CHECK( triplet_datum.SerializeToString( &out ) );
txn->Put( key_str, out );
if (++count % 1000 == 0) {
// Commit db
txn->Commit();
txn.reset(db->NewTransaction());
LOG(INFO) << "Processed " << count << " files.";
}
}
// write the last batch
if (count % 1000 != 0) {
txn->Commit();
LOG(INFO) << "Processed " << count << " files.";
}
#else
LOG(FATAL) << "This tool requires OpenCV; compile with USE_OPENCV.";
#endif // USE_OPENCV
return 0;
}
我正在尝试使用以下命令执行命令./.build_release/tools/convert_triplet_db_dataset tools/triplet.txt 1 0 0 0
但没有成功。有人可以让我命令的确切格式吗? 我有以下错误
convert_triplet_db_dataset.cpp:77] Check failed: triplet_list_name.size() > 0 the triplet_list_name param should be specified!
命令行是使用"gflags"定义的。您可以在此处查找文档。基本上,定义参数时使用以下语法:
布尔:
例如,将"gray"参数定义为 false,您将运行(注意双破折号(:
./.build_release/tools/convert_triplet_db_dataset --nogray
或者将其设置为 true:
./.build_release/tools/convert_triplet_db_dataset --gray
字符串或字符串列表:
定义字符串输入可以使用单短划线、双短划线以及带或不带=
号来完成。
./.build_release/tools/convert_triplet_db_dataset --triplet_list_name="listname"
./.build_release/tools/convert_triplet_db_dataset -triplet_list_name="listname"
./.build_release/tools/convert_triplet_db_dataset --triplet_list_name "listname"
./.build_release/tools/convert_triplet_db_dataset -triplet_list_name "listname"
您可以根据需要指定任意数量的输入,以便同时定义--gray
和--triplet_list_name="listname"
。
您可以通过将--helpfull
作为可执行文件的唯一选项传递来获取标志的完整列表。
相关文章:
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 读取大文件(>2GB)(文本文件包含以太网数据)并通过不同参数随机访问数据的最佳方法是什么?
- 是否可以在命令行中将输入参数传递给可执行文件
- 如何使用输入参数作为文件的传递?
- 隐蔽的 gcc 参数到 CMake 文件
- 生成文件文件名参数
- 在实现文件中使用模板参数声明方法
- 提供变量作为 MATLAB 系统命令的输入参数,以便C++可执行文件
- 如何在 c++ 窗口中将参数和返回的退出值传递到批处理文件/从批处理文件获取返回的退出值
- 编写一个将 LLVM IR 文件作为命令行参数的程序
- 通过命令行参数获取llvm ir文件时面临问题
- 如何使输入文本文件成为构造函数参数?c++
- (C/C++)fscanf_s从txt文件以字符形式读取数组时缺少整数参数错误
- 如何在不使用文件扩展名的情况下使用命令行参数打开C++中的文本文件?
- 读取用户必须提供一些参数的文件
- 如何使用 QProcess 设置参数以运行类文件?
- 可变参数模板在目标文件中有重复的符号?
- c++ 文件的参数
- accept(..) 似乎正在修改我给它的文件描述符参数
- C++ 找不到命令行参数(文件)