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

Source: Internet
Author: User
Tags assert strcmp


Caffe is responsible for the entire network input datalayer is read from the LEVELDB data, 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 directory into 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:

#include <google/protobuf/text_format.h> #include <glog/logging.h> #include <leveldb/db.h># Include <stdint.h> #include <fstream>//Nolint (readability/streams) #include <string> #include < set> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <dirent.h> #include <sys/stat.h> #include <unistd.h> #include <sys/types.h> #include "caffe/proto/caffe.pb.h" #include <opencv2/highgui/highgui.hpp> #include <opencv2/highgui/highgui_c.h> #include <opencv2/imgproc/ Imgproc.hpp>using std::string;using namespace Std;set<string> all_class_name;map<string,int> class2id;/** * Path: folder * files: vector for saving file name * R: Whether you need to traverse subfolders * Return: file name, not including path */void list_dir (const char *path,vector <string>& Files,bool r = False) {DIR *pdir;struct dirent *ent;char childpath[512];pD ir = opendir (path); Memset ( Childpath, 0, sizeof (Childpath)), while (ent = Readdir (pdir)) = NULL) {if (Ent->d_type &Amp Dt_dir) {if (strcmp (Ent->d_name, ".") = = 0 | | strcmp (Ent->d_name, "..") = = 0) {continue;} if (r)//assume the need to traverse subfolders {sprintf (Childpath, "%s/%s", Path, ent->d_name); List_dir (childpath,files);}} Else{files.push_back (Ent->d_name);}} Sort (Files.begin (), Files.end ());//Sort}string get_classname (string path) {int index = path.find_last_of ('_'); return PATH.SUBSTR (0, index);} int Get_labelid (string fileName) {string class_name_tmp = Get_classname (fileName); All_class_name.insert (class_name_ TMP); Map<string,int>::iterator name_iter_tmp = Class2id.find (class_name_tmp); if (name_iter_tmp = = Class2id.end ()) {int id = class2id.size (); Class2id.insert (Name_iter_tmp, Std::make_pair (class_name_tmp, id)); return ID;} Else{return Name_iter_tmp->second;}} void loadimg (string path,char* buffer) {cv::mat img = cv::imread (path, cv_load_image_color); string Val;int rows = img.rows ; int cols = Img.cols;int pos=0;for (int c = 0; c < 3; C + +) {for (int row = 0; row < rows; row++) {for (int col = 0; co L < cols; col++) {buffer[pos++]=img.at<cv::vec3b> (Row,col) [C];}}} void Convert (String imgdir,string outputdb,string attachdir,int channel,int width,int height) {leveldb::D b* db;leveldb: : Options options;options.create_if_missing = true;options.error_if_exists = True;caffe::D atum datum;datum.set_ Channels (channel);d atum.set_height (height);d atum.set_width (width); int image_size = Channel*width*height;char Buffer[image_size];string value; CHECK (leveldb::D b::open (Options, Outputdb, &db). OK ());vector<string> Filenames;list_dir (Imgdir.c_str (), filenames); string img_log = attachdir+ "Image_filename"; Ofstream WriteFile (Img_log.c_str ()); for (int i=0;i< Filenames.size (); i++) {string path= imgdir;path.append (Filenames[i]);//Calculate absolute path loadimg (path,buffer); int labelid = get_ Labelid (Filenames[i]);d Atum.add_label (labelid);d atum.set_data (buffer,image_size);d atum. Serializetostring (&value); snprintf (buffer, image_size, "%05d", i);p rintf ("\nclassid:%d classname:%s abspath:%s" , Labelid,get_classname (filenameS[i]). C_STR (), Path.c_str ());d B->put (Leveldb::writeoptions (), string (buffer), value),//printf ("%d%s\n", I, Filenames[i].c_str ()); Assert (Writefile.is_open ());writefile<<i<< "" <<filenames[i]<< "\ n" ;} Delete Db;writefile.close (); img_log = attachdir+ "Image_classname"; Writefile.open (Img_log.c_str ()); set<string >::iterator iter = All_class_name.begin (); while (iter! = All_class_name.end ()) {assert (Writefile.is_open ()); writefile<< (*iter) << "\ n";//printf ("%s\n", (*iter). C_STR ()); iter++;} Writefile.close ();} int main (int argc, char** argv) {if (ARGC < 6) {LOG (ERROR) << "Convert_imagedata src_dir dst_dir attach_dir Cha    Nnel width height "; return 0;} ./convert_imagedata.bin/home/linger/imdata/collartest//home/linger/linger/testfile/dbtest//home/linger/ LINGER/TESTFILE/TEST_ATTACHMENT/3 250//./convert_imagedata.bin/home/linger/imdata/collar_train//home/linger/ linger/testfile/crop_train_db//home/linger/linger/testfile/crop_train_attachment/3 50google::initgooglelogging (Argv[0]); string src_dir = argv[1];string Src_dst = argv[2];string Attach_dir = argv[3]; int channel = Atoi (argv[4]), int width = atoi (argv[5]), int height = atoi (argv[6]);//for test/*src_dir = "/home/linger/imdat a/collartest/"; src_dst ="/home/linger/linger/testfile/dbtest/"; attach_dir ="/home/linger/linger/testfile/"; Channel = 3;width = 250;height = 250; */convert (src_dir,src_dst,attach_dir,channel,width,height);}



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.