Caffe of the Neural network framework (convert picture to LEVELDB format)

Source: Internet
Author: User
Tags assert strcmp

Caffe of the Neural network framework (convert picture to LEVELDB format)

This should be the older version of the Caffe, directly take it should not be used, but you can refer to the next

Caffe in charge of the entire network input datalayer is read from the Leveldb, is a Google implementation of a very efficient KV database.

Therefore, we must first turn the data into LEVELDB format for training the network.

What I do here is to turn all the pictures of a folder into a leveldb format.

Tool using the command grid format: Convert_imagedata src_dir dst_dir attach_dir channel width height

Example:./convert_imagedata.bin/home/linger/imdata/collar_train//home/linger/linger/testfile/crop_train_db//home/ LINGER/LINGER/TESTFILE/CROP_TRAIN_ATTACHMENT/3 50 50

Source:

[CPP]View Plaincopy
  1. #include <google/protobuf/text_format.h>
  2. #include <glog/logging.h>
  3. #include <leveldb/db.h>
  4. #include <stdint.h>
  5. #include <fstream>//Nolint (Readability/streams)
  6. #include <string>
  7. #include <set>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <dirent.h>
  12. #include <sys/stat.h>
  13. #include <unistd.h>
  14. #include <sys/types.h>
  15. #include "Caffe/proto/caffe.pb.h"
  16. #include <opencv2/highgui/highgui.hpp>
  17. #include <opencv2/highgui/highgui_c.h>
  18. #include <opencv2/imgproc/imgproc.hpp>
  19. Using Std::string;
  20. Using namespace std;
  21. Set<string> All_class_name;
  22. Map<string,int> class2id;
  23. /**
  24. * Path: Directory
  25. * Files: vector for saving file names
  26. * R: Whether you need to traverse subdirectories
  27. * Return: File name, not including path
  28. */
  29. void List_dir (const char *path,vector<string>& files,bool R = false)
  30. {
  31. DIR *pdir;
  32. struct dirent *ent;
  33. Char childpath[512];
  34. Pdir = opendir (path);
  35. memset (Childpath, 0, sizeof (Childpath));
  36. While ((ent = Readdir (pdir)) = NULL)
  37. {
  38. if (Ent->d_type & Dt_dir)
  39. {
  40. if (strcmp (Ent->d_name, ".") = = 0 | | strcmp (ent->d_name, "..") = = 0)
  41. {
  42. continue;
  43. }
  44. if (r) //If you need to traverse subdirectories
  45. {
  46. sprintf (Childpath, "%s/%s", Path, ent->d_name);
  47. List_dir (Childpath,files);
  48. }
  49. }
  50. Else
  51. {
  52. Files.push_back (Ent->d_name);
  53. }
  54. }
  55. Sort (Files.begin (), Files.end ()); //Sort
  56. }
  57. String Get_classname (String path)
  58. {
  59. int index = path.find_last_of (' _ ');
  60. return path.substr (0, index);
  61. }
  62. int Get_labelid (String fileName)
  63. {
  64. String class_name_tmp = Get_classname (fileName);
  65. All_class_name.insert (CLASS_NAME_TMP);
  66. Map<string,int>::iterator name_iter_tmp = Class2id.find (class_name_tmp);
  67. if (name_iter_tmp = = Class2id.end ())
  68. {
  69. int id = class2id.size ();
  70. Class2id.insert (Name_iter_tmp, Std::make_pair (class_name_tmp, id));
  71. return ID;
  72. }
  73. Else
  74. {
  75. return name_iter_tmp->second;
  76. }
  77. }
  78. void Loadimg (string path,char* buffer)
  79. {
  80. Cv::mat img = cv::imread (path, cv_load_image_color);
  81. String Val;
  82. int rows = img.rows;
  83. int cols = Img.cols;
  84. int pos=0;
  85. For (int c = 0; c < 3; C + +)
  86. {
  87. For (int row = 0; row < rows; row++)
  88. {
  89. For (int col = 0; col < cols; col++)
  90. {
  91. Buffer[pos++]=img.at<cv::vec3b> (Row,col) [C];
  92. }
  93. }
  94. }
  95. }
  96. void Convert (String imgdir,string outputdb,string attachdir,int channel,int width,int height)
  97. {
  98. LEVELDB::D b* DB;
  99. Leveldb::options Options;
  100. Options.create_if_missing = true;
  101. Options.error_if_exists = true;
  102. Caffe::D Atum Datum;
  103. Datum.set_channels (channel);
  104. Datum.set_height (height);
  105. Datum.set_width (width);
  106. int image_size = channel*width*height;
  107. Char buffer[image_size];
  108. String value;
  109. CHECK (leveldb::D b::open (Options, Outputdb, &db). OK ());
  110. Vector<string> filenames;
  111. List_dir (Imgdir.c_str (), filenames);
  112. String img_log = attachdir+"Image_filename";
  113. Ofstream WriteFile (Img_log.c_str ());
  114. For (int i=0;i<filenames.size (); i++)
  115. {
  116. String Path= imgdir;
  117. Path.append (Filenames[i]); //Calculate the absolute path
  118. Loadimg (Path,buffer);
  119. int labelid = Get_labelid (Filenames[i]);
  120. Datum.add_label (Labelid);
  121. Datum.set_data (buffer,image_size);
  122. Datum. Serializetostring (&value);
  123. snprintf (buffer, image_size, "%05d", I);
  124. printf ("\nclassid:%d classname:%s abspath:%s", Labelid,get_classname (Filenames[i]). C_STR (), Path.c_str ());
  125. Db->put (Leveldb::writeoptions (), string (buffer), value);
  126. //printf ("%d%s\n", i,filenames[i].c_str ());
  127. ASSERT (Writefile.is_open ());
  128. writefile<<i<< "" <<filenames[i]<<"\ n";
  129. }
  130. Delete db;
  131. Writefile.close ();
  132. Img_log = attachdir+"Image_classname";
  133. Writefile.open (Img_log.c_str ());
  134. Set<string>::iterator iter = All_class_name.begin ();
  135. While (iter! = All_class_name.end ())
  136. {
  137. ASSERT (Writefile.is_open ());
  138. writefile<< (*iter) <<"\ n";
  139. //printf ("%s\n", (*iter). C_STR ());
  140. iter++;
  141. }
  142. Writefile.close ();
  143. }
  144. int main (int argc, char** argv)
  145. {
  146. if (argc < 6)
  147. {
  148. LOG (ERROR) << "Convert_imagedata src_dir dst_dir attach_dir channel width height";
  149. return 0;
  150. }
  151. ./convert_imagedata.bin/home/linger/imdata/collartest//home/linger/linger/testfile/dbtest//home/linger/ LINGER/TESTFILE/TEST_ATTACHMENT/3 250 250
  152. //./convert_imagedata.bin/home/linger/imdata/collar_train//home/linger/linger/testfile/crop_train_db//Home /LINGER/LINGER/TESTFILE/CROP_TRAIN_ATTACHMENT/3
  153. Google::initgooglelogging (Argv[0]);
  154. String src_dir = Argv[1];
  155. String src_dst = Argv[2];
  156. String attach_dir = Argv[3];
  157. int channel = Atoi (Argv[4]);
  158. int width = atoi (argv[5]);
  159. int height = atoi (argv[6]);
  160. //for Test
  161. /* 
  162. Src_dir = "/home/linger/imdata/collartest/";
  163. SRC_DST = "/home/linger/linger/testfile/dbtest/";
  164. Attach_dir = "/home/linger/linger/testfile/";
  165. Channel = 3;
  166. width = 250;
  167. Height = 250;
  168. */
  169. Convert (src_dir,src_dst,attach_dir,channel,width,height);
  170. }

Caffe of the Neural network framework (convert picture to LEVELDB format)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.