/**************************************************************************************************************************
文件说明:【1】这个程序用于将图像数据集合(a set of images)和与这个图像相关的注释(annotations)转换为caffe专用的数据库LMDB/LEVELDB格式【2】使用方法:convert_annoset [FLAGS] ROOTFOLDER/ LISTFILE DB_NAME[1]ROOTFOLDER/----------------图像数据所在的文件夹[2]LISTFILE-------------------输入图像数据的文件列表,其每一行为subfolder1/file1.JPEG 7[3][FLAGS]--------------------可选参数,是否使用shuffle,颜色空间,编码等【3】对于【分类任务】,文件的格式如下所示:imgfolder1/img1.JPEG 7【4】对于【检测任务】,文件的格式如下所示:imgfolder1/img1.JPEG annofolder1/anno1.xml
检测任务的使用举例:代码需要进行三处修改:【1】DEFINE_string(anno_type,   "detection","The type of annotation {classification, detection}.");【2】DEFINE_string(label_map_file,"E://caffeInstall2013SSDCUDA//caffe-ssd-microsoft//data//ssd//labelmap_voc.prototxt","A file with LabelMap protobuf message.");【3】主函数中代码修改:argv[1] = "E://caffeInstall2013SSDCUDA//caffe-ssd-microsoft//data//ssd//";argv[2] = "E://caffeInstall2013SSDCUDA//caffe-ssd-microsoft//data//ssd//test.txt";argv[3] = "E://caffeInstall2013SSDCUDA//caffe-ssd-microsoft//examples//ssd//ssd_test_lmdb";
开发环境:Win10+cuda7.5+cuDnnV5.0+ccaffe_windows_ssd+OpenCv+VS2013
时间地点:陕西师范大学 文津楼 2017.8.10
作    者:九 月
***************************************************************************************************************************/
#include <algorithm>
#include <fstream>  // NOLINT(readability/streams)
#include <map>
#include <string>
#include <utility>
#include <vector>#include "boost/scoped_ptr.hpp"
#include "boost/variant.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_string(anno_type,   "detection","The type of annotation {classification, detection}.");
DEFINE_string(label_type,  "xml",      "The type of annotation file format.");
DEFINE_string(label_map_file,"E://caffeInstall2013SSDCUDA//caffe-ssd-microsoft//data//ssd//labelmap_voc.prototxt",      "A file with LabelMap protobuf message.");
DEFINE_bool(  check_label,  false,    "When this option is on, check that there is no duplicated name/label.");
DEFINE_int32( min_dim,      0,         "Minimum dimension images are resized to (keep same aspect ratio)");
DEFINE_int32( max_dim,      0,         "Maximum dimension images are resized to (keep same aspect ratio)");
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',...).");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;
#endifgflags::SetUsageMessage("Convert a set of images and annotations to the ""leveldb/lmdb format used as input for Caffe.\n""Usage:\n""    convert_annoset [FLAGS] ROOTFOLDER/ LISTFILE DB_NAME\n");argv[1] = "E://caffeInstall2013SSDCUDA//caffe-ssd-microsoft//data//ssd//";argv[2] = "E://caffeInstall2013SSDCUDA//caffe-ssd-microsoft//data//ssd//test.txt";argv[3] = "E://caffeInstall2013SSDCUDA//caffe-ssd-microsoft//examples//ssd//ssd_test_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;const string anno_type   = FLAGS_anno_type;AnnotatedDatum_AnnotationType type;const string label_type     = FLAGS_label_type;const string label_map_file = FLAGS_label_map_file;const bool check_label     = FLAGS_check_label;std::map<std::string, int> name_to_label;std::ifstream infile(argv[2]);std::vector<std::pair<std::string, boost::variant<int, std::string> > > lines;std::string filename;int         label;std::string labelname;if (anno_type == "classification") {while (infile >> filename >> label) {lines.push_back(std::make_pair(filename, label));}} else if (anno_type == "detection") {type = AnnotatedDatum_AnnotationType_BBOX;LabelMap label_map;CHECK(ReadProtoFromTextFile(label_map_file, &label_map))<< "Failed to read label map file.";CHECK(MapNameToLabel(label_map, check_label, &name_to_label))<< "Failed to convert name to label.";while (infile >> filename >> labelname) {lines.push_back(std::make_pair(filename, labelname));}}if (FLAGS_shuffle) {// randomly shuffle dataLOG(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 min_dim       = std::max<int>(0, FLAGS_min_dim);int max_dim       = std::max<int>(0, FLAGS_max_dim);int resize_height = std::max<int>(0, FLAGS_resize_height);int resize_width  = std::max<int>(0, FLAGS_resize_width);// Create new DBscoped_ptr<db::DB> db(db::GetDB(FLAGS_backend));db->Open(argv[3], db::NEW);scoped_ptr<db::Transaction> txn(db->NewTransaction());// Storing to dbstd::string root_folder(argv[1]);AnnotatedDatum anno_datum;Datum* datum = anno_datum.mutable_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 = true;std::string enc = encode_type;if (encoded && !enc.size()) {// Guess the encoding type from the file namestring 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);}filename = root_folder + lines[line_id].first;if (anno_type == "classification") {label = boost::get<int>(lines[line_id].second);status = ReadImageToDatum(filename, label, resize_height, resize_width,min_dim, max_dim, is_color, enc, datum);} else if (anno_type == "detection") {labelname = root_folder + boost::get<std::string>(lines[line_id].second);status = ReadRichImageToAnnotatedDatum(filename, labelname, resize_height,resize_width, min_dim, max_dim, is_color, enc, type, label_type,name_to_label, &anno_datum);anno_datum.set_type(AnnotatedDatum_AnnotationType_BBOX);}if (status == false) {LOG(WARNING) << "Failed to read " << lines[line_id].first;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();}}// sequentialstring key_str = caffe::format_int(line_id, 8) + "_" + lines[line_id].first;// Put in dbstring out;CHECK(anno_datum.SerializeToString(&out));txn->Put(key_str, out);if (++count % 1000 == 0) {// Commit dbtxn->Commit();txn.reset(db->NewTransaction());LOG(INFO) << "Processed " << count << " files.";}}// write the last batchif (count % 1000 != 0) {txn->Commit();LOG(INFO) << "Processed " << count << " files.";}std::system("pause");
#elseLOG(FATAL) << "This tool requires OpenCV; compile with USE_OPENCV.";
#endif  // USE_OPENCVreturn 0;
}

用了30分钟,终于生成了训练阶段所需要的LMDB数据库文件,如下所示:

【深度学习】【caffe实用工具6】笔记28 windows下SSD网络中的convert_annoset工具的使用相关推荐

  1. 深度学习核心技术精讲100篇(二)-图网络中的社群及社群发现算法

    前言 本篇博文主要讲解Graph中社群的概念,然后介绍了一种简单的社群发现算法Louvain Algorithm,最后提供可重叠的社群发现,提出BigCLAM算法,用来识别节点从属关系. 01 Gra ...

  2. 吴恩达【优化深度神经网络】笔记01——深度学习的实用层面

    文章目录 引言 一.训练集/验证集/测试集(Train/Dev/Test sets) 1. 数据集选择 2. 补充:交叉验证(cross validation) 二.偏差和方差(Bias/Varian ...

  3. 吴恩达《优化深度神经网络》精炼笔记(1)-- 深度学习的实用层面

    AI有道 不可错过的AI技术公众号 关注 吴恩达的深度学习专项课程的第一门课<神经网络与深度学习>的所有精炼笔记我已经整理完毕.迷路的小伙伴请见文章末尾的推荐阅读: 在接下来的几次笔记中, ...

  4. 吴恩达深度学习笔记5-Course2-Week1【深度学习的实用层面】

    改善深层神经网络:超参数调试.正则化以及优化 深度学习的实用层面 一.训练.验证.测试集 样本数据分成以下三个部分: 训练集(train set): 用于对模型进行训练. 验证集(hold-out c ...

  5. 深度学习硬啃计划与笔记

    标题:爱看不看,你怎么还不死 提示:直观的图表有助于理论的理解与加深记忆,学习的重心是理解算法,由浅入深从来不是问题. 6.22 上午 复习Logistic回归和梯度下降 6.22 下午 复习Logi ...

  6. Python 深度学习架构实用指南:第一、二部分

    原文:Hands-On Deep Learning Architectures with Python 协议:CC BY-NC-SA 4.0 译者:飞龙 本文来自[ApacheCN 深度学习 译文集] ...

  7. 深度学习总结——CS231n课程深度学习(机器视觉相关)笔记整理

    深度学习笔记整理 说明 基本知识点一:模型的设置(基本) 1. 激活函数的设置 2. 损失函数的设置 (1) 分类问题 (2) 属性问题 (3) 回归问题 3. 正则化方式的设置 (1) 损失函数添加 ...

  8. 网络存储 linux 访问,Linux基础教程学习笔记28——使用Samba访问网络存储

    Linux基础教程学习笔记28--使用Samba访问网络存储 SMB用于Windows和类Linux系统直接的文件共享 安装samba client包: [root@linuxidc~]# yum i ...

  9. caffe模型文件解析_深度学习 Caffe 初始化流程理解(数据流建立)

    深度学习 Caffe 初始化流程理解(数据流建立) 之前在简书的文章,搬迁过来 ^-^ 本文是作者原创,如有理解错误,恳请大家指出,如需引用,请注明出处. #Caffe FeatureMap数据流的建 ...

最新文章

  1. #6279. 数列分块入门 3(区间修改,查询权值前驱)
  2. Spark SQL 1.x之SQL Context使用
  3. 清北学堂十一培训酱油记
  4. python文件备份_基于 Python 的文件备份
  5. window10 安装python
  6. GaussDB T 强体验:通过 DBeaver/RazorSQL/DbVisualizer工具连接数据库(附测试账号)
  7. 关于在Servlet的路径问题
  8. python程序运行键_python实现按任意键继续执行程序
  9. 利用微查询和数据锐化进行大数据探索
  10. 你在被窝里刷手机岁月静好,一个名叫 Flink 的 ​“神秘引擎” 却在远方和时间赛跑...
  11. 七月的尾巴,我不是狮子座
  12. html网页打开慢的解决方法,真实案例讲述导致网站打开速度慢的原因及解决方法...
  13. notepad++ paste data vertically
  14. Django新手入门教程(2)测试服务器是否可用
  15. 验证input和textarea的输入是否有效,也就是不为空,也不都是空格
  16. 正则 显示0-100的数字(可以是小数也可以是整数,不能是01,02可以是0.1)
  17. C/C++植物大战僵尸之CE找基址+修改器制作(基础版)
  18. 移动端证件识别SDK
  19. 题解专栏(七):kotori和气球
  20. Unc0ver-v5.0.1版本发布,报错原因以及广告问题

热门文章

  1. 理查德·斯托曼一直是对的
  2. 云计算入门必懂的60条术语
  3. 制作Ubuntu镜像并在虚拟机上安装
  4. 嘉宾预告(二) | 安全左中右 · 2022 XDR网络安全运营新理念峰会
  5. 离散数学 --- 图论基础 --- 图的同构,通路与回路,可达性与最短通路
  6. TestFlight 提交提示“处理你的请求时出错。请稍后再试”
  7. android开发基础5-intent(明日科技教程)
  8. 【沟通的艺术】 国王的演讲
  9. win10/win11更新后没有声音,音频服务未响应
  10. 混改之后的中国联通能否把握5G打场翻身仗?