基于caffe的鉴黄图片分类c++代码

基于caffe的鉴黄图片分类c++代码

icity.cpp:

#include 
#include 
#include 
#include 

#include "caffe/caffe.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "sys/time.h"

#include 
#include 
#include 
#include 
#include
#include
#include
#include

using caffe::Blob;
using caffe::Caffe;
using caffe::Net;
using caffe::Layer;
using caffe::vector;

int copyfile(std::string original,std::string destination){
	int length=0;
	char originalname[100];
	length=original.copy(originalname, original.size());
	originalname[length]='\0';
	
	int length2=0;
	char destinationname[100];
	length2=destination.copy(destinationname, destination.size());
	destinationname[length2]='\0';
	
	char c = '\0';  
    int in = -1, out = -1;  
      
    //以只读方式打开数据文件  
    in = open(originalname, O_RDONLY);  
    //以只写方式创建文件,如果文件不存在就创建一个新的文件  
    //文件属主具有读和写的权限  
    out = open(destinationname, O_WRONLY|O_CREAT,  S_IRUSR| S_IWUSR);  
    while(read(in, &c, 1) == 1)//读一个字节的数据  
        write(out, &c, 1);//写一个字节的数据  
  
    //关闭文件描述符  
    close(in);  
    close(out);  
    return 0;  
	
}

int main(int argc, char** argv) {
	std::string FLAGS_weights;
	std::string net_config;
	int model_tag=0;
	for(int model=0;model<=1;model++){
		std::cout<<"print model"< net(net_config, caffe::TEST);
	
	net.CopyTrainedLayersFrom(FLAGS_weights);
	vector* > input_blobs = net.input_blobs();
	vector* > output_blobs = net.output_blobs();

	//define the map from labels to output
	int num_outputs = output_blobs[0]->count();

	//turn to new
	dlib::directory test("/opt/jiangjiangchu/jianhuang/test_picture/");
	std::vector dirs= test.get_dirs();
	sort(dirs.begin(),dirs.end());
	int num_class= dirs.size();//统计下有多少个文件夹可以读图片
	std::string fname;
	std::string filename;
	int ttotal=0;
	int tcorr = 0;
	for(int i=0;i files = dirs[i].get_files();
		for(int j=0; j mat_vec;
			split(src, mat_vec);

			float* dst_data = input_blobs[0]->mutable_cpu_data();
			float* src_data;
			//
			for (int k = 0; k < mat_vec.size(); ++k)
			{
				for (int y = 0; y(y);
					memcpy(dst_data, src_data, sizeof(float)*mat_vec[k].cols);
					dst_data += mat_vec[k].cols;
				}
			}

			vector test_score_output_id;
			vector test_score;
			float loss = 0;
			//计算
			const vector*>& result =net.Forward(input_blobs);
			int my_class=0;
			for (int j = 0; j < result.size(); ++j) {
				int maxind=0;
				float maxval=-100;
				const float* result_vec = result[j]->cpu_data();
				for(int k=0; kcount(); k++)
				{
					//打印每个值
					std::cout< maxval)
					{
						maxval = result_vec[k];
						maxind = k;
					}
				}
				//std::cout<<"model :"<

make

makefile:

CXX=g++
jianhuang: icity.cpp
        $(CXX) -Ofast  -std=c++11 -DCPU_ONLY -DUSE_MKL -I./dlib-19.3   -I./include -I/opt/jiangjiangchu/intelcaffe/mkl/include icity.cpp    -L./lib -lcaffe -lopencv_highgui -lopencv_imgproc -lopencv_core -lprotobuf ./lib/libgflags.a  -lboost_system    -lhdf5 -lhdf5_hl -L./lib/  -lcaffe -lopencv_highgui -lopencv_core -lmkldnn ./dlib-19.3/examples/build/dlib_build/libdlib.a  -o  jianhuang 

./run.sh >out.log 2>&1

run.sh:

export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH
export PATH=./lib:$PATH
./jianhuang



------------------  附 -------------------------------------------------------

linux 下C++没有copyfile类似的函数,上面是加入的自己的复制函数,下面调用系统调用system(),复制图片分类:

#include 
#include 
#include 
#include 

#include "caffe/caffe.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "sys/time.h"

#include 
#include 
#include 
#include 
#include
#include

using caffe::Blob;
using caffe::Caffe;
using caffe::Net;
using caffe::Layer;
using caffe::vector;

//int copyfile(std::string original,std::string destination){
//	char c = '\0';  
 //   int in = -1, out = -1;  
      
    //以只读方式打开数据文件  
 //   in = open(original, std::O_RDONLY);  
    //以只写方式创建文件,如果文件不存在就创建一个新的文件  
    //文件属主具有读和写的权限  
 //   out = open(destination, std::O_WRONLY|std::O_CREAT, std::S_IRUSR|std::S_IWUSR);  
 //   while(read(in, &c, 1) == 1)//读一个字节的数据  
 //       write(out, &c, 1);//写一个字节的数据  
  
    //关闭文件描述符  
  //  close(in);  
  //  close(out);  
  //  return 0;  
	
//}

int main(int argc, char** argv) {
	std::string FLAGS_weights;
	std::string net_config;
	int model_tag=0;
	for(int model=0;model<=1;model++){
		std::cout<<"print model"< net(net_config, caffe::TEST);
	
	net.CopyTrainedLayersFrom(FLAGS_weights);
	vector* > input_blobs = net.input_blobs();
	vector* > output_blobs = net.output_blobs();

	//define the map from labels to output
	int num_outputs = output_blobs[0]->count();

	//turn to new
	dlib::directory test("/opt/jiangjiangchu/jianhuang/test_picture/");
	std::vector dirs= test.get_dirs();
	sort(dirs.begin(),dirs.end());
	int num_class= dirs.size();//统计下有多少个文件夹可以读图片
	std::string fname;
	std::string filename;
	int ttotal=0;
	int tcorr = 0;
	for(int i=0;i files = dirs[i].get_files();
		for(int j=0; j mat_vec;
			split(src, mat_vec);

			float* dst_data = input_blobs[0]->mutable_cpu_data();
			float* src_data;
			//
			for (int k = 0; k < mat_vec.size(); ++k)
			{
				for (int y = 0; y(y);
					memcpy(dst_data, src_data, sizeof(float)*mat_vec[k].cols);
					dst_data += mat_vec[k].cols;
				}
			}

			vector test_score_output_id;
			vector test_score;
			float loss = 0;
			//计算
			const vector*>& result =net.Forward(input_blobs);
			int my_class=0;
			for (int j = 0; j < result.size(); ++j) {
				int maxind=0;
				float maxval=-100;
				const float* result_vec = result[j]->cpu_data();
				for(int k=0; kcount(); k++)
				{
					//打印每个值
					std::cout< maxval)
					{
						maxval = result_vec[k];
						maxind = k;
					}
				}
				std::cout<<"model :"<



你可能感兴趣的:(C++,机器学习深度学习)