【深度学习】【caffe实用工具3】笔记25 Windows下caffe中将图像数据集合转换为DB(LMDB/LEVELDB)文件格式之convert_imageset

来源:互联网 发布:辉素网络带练真的假的 编辑:程序博客网 时间:2024/05/22 03:18
/*********************************************************************************************************************************文件说明:        【1】This program converts a set of images to a lmdb/leveldb by storing them  as Datum proto buffers.【2】这个程序用于将图像数据转化为DB(lmdb/leveldb)数据库文件【3】where ROOTFOLDER is the root folder that holds all the images, and LISTFILE should be a list of files as well as their      labels, in the format as subfolder1/file1.JPEG 7用    法:        【注意1】*.bat文件的使用方法:         convert_imageset [FLAGS] ROOTFOLDER/ LISTFILE DB_NAME 【1】convert_imageset:convert_imageset.exe文件所在的路径 【2】[FLAGS]:可选参数的配置 【3】ROOTFOLDER/:图片集合所在的文件夹所在的路径,如下所示:          argv[1] = "E://caffeInstall2013//caffe-master//data//myself//train//"; 【4】LISTFILE:图片文件列表文件所在的路径,如下所示:          argv[2] = "E://caffeInstall2013//caffe-master//data//myself//train.txt"; 【5】DB_NAME:生成的DB文件存放的路径以及生成DB数据库的数据库名字【注意2】直接在代码中进行配置,运行:         【1】首先根据自己要转换图片数据集合的图片规格等,对可选参数进行配置     【2】添加如下的代码:   argv[1] = "E://caffeInstall2013//caffe-master//data//myself//train//";   argv[2] = "E://caffeInstall2013//caffe-master//data//myself//train.txt";   argv[3] = "E://caffeInstall2013//caffe-master//examples//myself//myself_train_lmdb";【注意3】:*.bat文件形式的具体使用方法,可以参考下面的博客:               http://www.cnblogs.com/LiuSY/p/5761781.html操作步骤:        【1】首先,在目录E:\caffeInstall2013\caffe-master\data下建立自己的图像数据集合文件,例如train,val【2】其次,再建立这些图像文件的文件列表train.txt val.txt【3】然后,配置程序(就是本程序)【4】运行程序,生成DB数据库文件开发环境:        windows+cuda7.5+cuDnnV5+opencv+caffe1+vs2013时间地点:        陕西师范大学 文津楼 2017.8.9作    者:        九 月**********************************************************************************************************************************/#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;/**********************************************************************************************************************模块说明:         可选参数的定义参数说明:         【1】-gray: 是否以灰度图的方式打开图片。程序调用opencv库中的imread()函数来打开图片,默认为false 【2】-shuffle: 是否随机打乱图片顺序。默认为false 【3】-backend:需要转换成的db文件格式,可选为leveldb或lmdb,默认为lmdb 【4】-resize_width/resize_height: 改变图片的大小。在运行中,要求所有图片的尺寸一致,因此需要改变图片大小。                程序调用opencv库的resize()函数来对图片放大缩小,默认为0,不改变 【5】-check_size: 检查所有的数据是否有相同的尺寸。默认为false,不检查 【6】-encoded: 是否将原图片编码放入最终的数据中,默认为false 【7】-encode_type: 与前一个参数对应,将图片编码为哪一个格式:‘png','jpg'......***********************************************************************************************************************/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,  256,   "Width images are resized to");DEFINE_int32(resize_height, 256,   "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',...).");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 to the leveldb/lmdb\n"  "format used as input for Caffe.\n"  "Usage:\n"  "    convert_imageset [FLAGS] ROOTFOLDER/ LISTFILE DB_NAME\n"  "The ImageNet dataset for the training demo is at\n"  "http://www.image-net.org/download-images\n");    argv[1] = "E://caffeInstall2013//caffe-master//data//myself//train//";  argv[2] = "E://caffeInstall2013//caffe-master//data//myself//train.txt";  argv[3] = "E://caffeInstall2013//caffe-master//examples//myself//myself_train_lmdb";  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;  std::ifstream                               infile(argv[2]);  std::vector<std::pair<std::string, int> >  lines;  std::string                                 line;  size_t                                      pos;  int                                         label;  while (std::getline(infile, line))   {    pos = line.find_last_of(' ');    label = atoi(line.substr(pos + 1).c_str());    lines.push_back(std::make_pair(line.substr(0, pos), label));  }  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(argv[3], db::NEW);  scoped_ptr<db::Transaction> txn(db->NewTransaction());  // Storing to db  std::string root_folder(argv[1]);  Datum 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].first;      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 = ReadImageToDatum(root_folder + lines[line_id].first,lines[line_id].second, resize_height, resize_width, is_color,                              enc, &datum);    if (status == false) continue;    if (check_size) {      if (!data_size_initialized)   {        data_size = datum.channels() * datum.height() * datum.width();        data_size_initialized = true;      } else   {        const std::string& data = datum.data();        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].first;    // Put in db    string out;    CHECK(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.";  }  std::system("pause");#else  LOG(FATAL) << "This tool requires OpenCV; compile with USE_OPENCV.";#endif  // USE_OPENCV  return 0;}


convert.bat的格式为

convert_imageset.exe的位置+空格+FLAGS+空格+图片所在的位置+空格+你生成的list的位置+空格+将要生成的db格式要保存的位置

建议都使用绝对位置!!!

例子:

D:/deeptools/caffe-windows-master/bin/convert_imageset.exe --shuffle --resize_height=256 --resize_width=256 D:/deeptools/caffe-windows-master/data/re/ D:/deeptools/caffe-windows-master/examples/myfile/train.txt D:/deeptools/caffe-windows-master/examples/myfile/train_dbpause

其中FLAGS可以选择为:

(1)--shuffle  是否随机打乱图片顺序 【默认为false】

D:/deeptools/caffe-windows-master/bin/convert_imageset.exe --shuffle D:/deeptools/caffe-windows-master/data/mnist/train-images/ D:/deeptools/caffe-windows-master/examples/mymnist/train.txt D:/deeptools/caffe-windows-master/examples/mymnist/train_lmdbpause

 

      为什么要随机打乱图片顺序?

      待答。。。

(2)--gray 是否以灰度图片的方式打开【默认为false】

D:/deeptools/caffe-windows-master/bin/convert_imageset.exe --gray D:/deeptools/caffe-windows-master/data/mnist/train-images/ D:/deeptools/caffe-windows-master/examples/mymnist/train.txt D:/deeptools/caffe-windows-master/examples/mymnist/traingray_lmdbpause

 

(3)--resize_width

      --resize_height  改变图片大小(缩放)【默认为原图】

(4)--backend 需要转换成什么格式的db,可选为leveldb与lmdb格式【默认为lmdb】

D:/deeptools/caffe-windows-master/bin/convert_imageset.exe --backend=leveldb D:/deeptools/caffe-windows-master/data/mnist/train-images/ D:/deeptools/caffe-windows-master/examples/mymnist/train.txt D:/deeptools/caffe-windows-master/examples/mymnist/trainbackend_leveldbpause

结果:

 现在我们认真解读一下这个leveldb格式:

 http://www.2cto.com/kf/201607/527860.html 

待续。。。不知道里面的纠结是什么东西

(5)--check_size 检查所有的数据是否为同一个size【默认为false,不检查】

(6)--encoded 是否将原图编码放入最终的数据中【默认为false】

(7)--encode_type 与前边呼应,将图片改为哪种格式【png,jpg。。】

  貌似这个得需要opencv。。我没有安装opencv出错如下



阅读全文
0 0
原创粉丝点击