c++ - C++文件的参数

标签 c++ caffe

我正在尝试从 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/lmdb\n"
        "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”定义的。您可以查找文档 here .基本上,当您定义参数时,您使用以下语法:

bool 值:

例如,将“灰色”参数定义为 false,您将运行(注意双破折号):

./.build_release/tools/convert_triplet_db_dataset --nogray

或设置为真:

./.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 作为可执行文件的唯一选项来获取完整的标志列表。

关于c++ - C++文件的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45457781/

相关文章:

machine-learning - 如何使用 HDF5 层根据迭代次数计算纪元?

deep-learning - 如何在 caffe 中创建用于对象检测的数据集?

c++ - 在 ubuntu 16.04 中安装 Caffe 时遇到困难

c++ - 当我在条件语句中使用相同的值时,为什么我的插入排序算法返回不同的值?

如果派生中没有数据成员,C++是否仍然需要虚拟析构函数?

c++ - 链接器错误 : undefined reference to function from header file

opencv - 安装caffe时找不到lopencv_core

c++ - 关闭应用程序时如何抑制 "There are still active COM objects in this application"错误?

c++ - 解释字符串修剪功能

c++ - "InitGoogleLogging"是做什么的?