未验证 提交 2b1e334d 编写于 作者: 朱本福 提交者: GitHub

Add files via upload

上级 548fde32
#pragma once
#ifndef interface
#define CINTERFACE
#define interface struct
#endif
//-------------------------------------------------------------------------------------------------
#ifndef IMPORT
#define IMPORT __declspec(dllimport)
#endif
//-------------------------------------------------------------------------------------------------
#ifndef EXPORT
#define EXPORT __declspec(dllexport)
#endif
#include <vector>
#include <string>
typedef std::pair<std::string, float> Prediction;
typedef std::pair<int, float> PredictionIdx;
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
interface ICNNPredict
{
virtual bool IsCPUMode() = 0;
virtual std::vector<Prediction> Classify(const std::string& file, int N = 5) = 0;
virtual std::vector<Prediction> Classify(const unsigned char* pJPGBuffer, int len, int N = 5) = 0;
virtual std::vector<Prediction> Classify(const cv::Mat& img, int N = 5) = 0;
virtual std::vector<std::vector<PredictionIdx> > BatchClassify(const std::vector<cv::Mat>& imgs, int N = 5) = 0;
virtual std::vector<std::vector<float> > BatchPredict(const std::vector<cv::Mat>& img)=0;
virtual std::vector<Prediction> CropClassify(const cv::Mat& img, int std_size, int crop_num = 1, int N = 5) = 0;
virtual std::vector<PredictionIdx> ClassifyRtnIdx(const cv::Mat& img, int N = 5) = 0;
//virtual std::vector<float> ExtractFeature(const cv::Mat& img, const std::string& strLayerName = "") = 0;
virtual std::vector<float> GetLayerFeatureMaps(const std::string& strLayerName, std::vector<int>& outshape) = 0;
virtual int GetFeatureDim() = 0;
virtual std::vector< std::vector<float> > GetLastBlockFeature(const cv::Mat& img) = 0;
virtual std::vector<float> GetOutputFeatureMap(const cv::Mat& img, std::vector<int>& outshape) = 0;
virtual std::vector<std::string> GetLabels() = 0;
virtual void SetMean(const std::string& mean_file) = 0;
virtual std::vector<float> Predict(const cv::Mat& img) = 0;
virtual void GetInputImageSize(int &w, int &h) = 0;
//advanced operations
virtual float Pruning(float weight_t, const char* saveas_name=0)=0;
virtual cv::Mat EstimateReceptiveField(const cv::Mat& img, const std::string& layerName, int x, int y, int idxNeuron = -1,bool islstm=false,int* width_parts=0) = 0;
virtual void GetLayerFeatureMapSize(int w, int h, const std::string& layerName,int& w1, int& h1)=0;
virtual void Release()=0;
};
extern "C" EXPORT ICNNPredict* CreatePredictInstance(const char* model_folder, bool use_gpu);
========================================================================
DYNAMIC LINK LIBRARY : libClassification Project Overview
========================================================================
AppWizard has created this libClassification DLL for you.
This file contains a summary of what you will find in each of the files that
make up your libClassification application.
libClassification.vcxproj
This is the main project file for VC++ projects generated using an Application Wizard.
It contains information about the version of Visual C++ that generated the file, and
information about the platforms, configurations, and project features selected with the
Application Wizard.
libClassification.vcxproj.filters
This is the filters file for VC++ projects generated using an Application Wizard.
It contains information about the association between the files in your project
and the filters. This association is used in the IDE to show grouping of files with
similar extensions under a specific node (for e.g. ".cpp" files are associated with the
"Source Files" filter).
libClassification.cpp
This is the main DLL source file.
When created, this DLL does not export any symbols. As a result, it
will not produce a .lib file when it is built. If you wish this project
to be a project dependency of some other project, you will either need to
add code to export some symbols from the DLL so that an export library
will be produced, or you can set the Ignore Input Library property to Yes
on the General propert page of the Linker folder in the project's Property
Pages dialog box.
/////////////////////////////////////////////////////////////////////////////
Other standard files:
StdAfx.h, StdAfx.cpp
These files are used to build a precompiled header (PCH) file
named libClassification.pch and a precompiled types file named StdAfx.obj.
/////////////////////////////////////////////////////////////////////////////
Other notes:
AppWizard uses "TODO:" comments to indicate parts of the source code you
should add to or customize.
/////////////////////////////////////////////////////////////////////////////
No such lib:
libjpeg.lib
libpng.lib
zlib.lib
////////////////////////////////////////////////////////////////////////
#ifndef CAFFE_GPU_H_
#define CAFFE_GPU_H_
#include <caffe/caffe.hpp>
// c++
#include <string>
#include <vector>
#include <fstream>
#include "tform_fcn.h"
// opencv
#include <opencv2/opencv.hpp>
// boost
//#include "boost/make_shared.hpp"
//#define CPU_ONLY
#define INTER_FAST
using namespace caffe;
typedef struct FaceRect {
float x1;
float y1;
float x2;
float y2;
float score; /**< Larger score should mean higher confidence. */
} FaceRect;
typedef struct FacePts {
float x[5], y[5];
} FacePts;
typedef struct FaceInfo {
FaceRect bbox;
cv::Vec4f regression;
FacePts facePts;
double roll;
double pitch;
double yaw;
} FaceInfo;
class _declspec(dllexport) MTCNN {
public:
MTCNN(const string& proto_model_dir);
void Detect(const cv::Mat& img, std::vector<FaceInfo> &faceInfo, int minSize, double* threshold, double factor);
private:
bool CvMatToDatumSignalChannel(const cv::Mat& cv_mat, Datum* datum);
void Preprocess(const cv::Mat& img,
std::vector<cv::Mat>* input_channels);
void WrapInputLayer(std::vector<cv::Mat>* input_channels,Blob<float>* input_layer,
const int height,const int width);
/*void SetMean();*/
void GenerateBoundingBox( Blob<float>* confidence,Blob<float>* reg,
float scale,float thresh,int image_width,int image_height);
void ClassifyFace(const std::vector<FaceInfo>& regressed_rects,cv::Mat &sample_single,
boost::shared_ptr<Net<float> >& net,double thresh,char netName);
void ClassifyFace_MulImage(const std::vector<FaceInfo> &regressed_rects, cv::Mat &sample_single,
boost::shared_ptr<Net<float> >& net, double thresh, char netName);
std::vector<FaceInfo> NonMaximumSuppression(std::vector<FaceInfo>& bboxes,float thresh,char methodType);
void Bbox2Square(std::vector<FaceInfo>& bboxes);
void Padding(int img_w, int img_h);
std::vector<FaceInfo> BoxRegress(std::vector<FaceInfo> &faceInfo_, int stage);
void RegressPoint(const std::vector<FaceInfo>& faceInfo);
private:
boost::shared_ptr<Net<float> > PNet_;
boost::shared_ptr<Net<float> > RNet_;
boost::shared_ptr<Net<float> > ONet_;
// x1,y1,x2,t2 and score
std::vector<FaceInfo> condidate_rects_;
std::vector<FaceInfo> total_boxes_;
std::vector<FaceInfo> regressed_rects_;
std::vector<FaceInfo> regressed_pading_;
std::vector<cv::Mat> crop_img_;
int curr_feature_map_w_;
int curr_feature_map_h_;
int num_channels_;
};
//extern int outnum;
//extern real_t face_dev;
//_declspec(dllexport) void setoutnum(int);
//_declspec(dllexport) void set_facedev(float num);
//
////std::vector<facereslt> recface(cv::Mat mat, caffe::Net& net, int nummtcnn = 0);
////_declspec(dllexport) std::vector<facereslt> recface(cv::Mat mat, caffe::Net& net);
//_declspec(dllexport) std::vector<facereslt> recface(cv::Mat mat, caffe::Net* net, MTCNN *detector);
////_declspec(dllexport) std::vector<FaceInfo> return_face_rect(cv::Mat mat);
//_declspec(dllexport) std::vector<FaceInfo> return_face_rect(cv::Mat mat, MTCNN *detector);
//
//// н
//_declspec(dllexport) double cosine(const std::vector<double>& v1, const std::vector<double>& v2);
//_declspec(dllexport) int test(cv::Mat image, vector<FaceInfo>& faceinfo_, vector<facereslt>& result_);
#endif
\ No newline at end of file
此差异已折叠。
#ifndef __CLASSIFICATION__
#define __CLASSIFICATION__
#include <caffe/caffe.hpp>
// #include <opencv2/core/core.hpp>
// #include <opencv2/highgui/highgui.hpp>
// #include <opencv2/imgproc/imgproc.hpp>
#include "ICNNPredict.h"
using namespace caffe; // NOLINT(build/namespaces)
using std::string;
using std::ifstream;
class EXPORT Classifier : public ICNNPredict
{
public:
Classifier();
bool Init(const string& model_path, bool gpu_mode = true);
bool Init(const string& trained_file, const string& model_file,
const string&mean_file, const string&label_file,
bool gpu_mode);
void Release() { delete this; }
bool IsCPUMode();
std::vector<Prediction> Classify(const string& file, int N = 5);
std::vector<Prediction> Classify(const unsigned char* pJPGBuffer, int len, int N = 5);
std::vector<Prediction> Classify(const cv::Mat& img, int N = 5);
std::vector< std::vector<PredictionIdx> > BatchClassify(const std::vector<cv::Mat>& imgs, int N = 5);
std::vector<Prediction> CropClassify(const cv::Mat& img, int std_size, int crop_num = 1, int N = 5);
std::vector<PredictionIdx> ClassifyRtnIdx(const cv::Mat& img, int N = 5);
//std::vector<float> ExtractFeature(const cv::Mat& img,const string& strLayerName="");
std::vector<float> GetLayerFeatureMaps(const string& strLayerName, std::vector<int>& outshape);
int GetFeatureDim();
std::vector<std::string> GetLabels() { return labels_; }
std::vector< std::vector<float> > GetLastBlockFeature(const cv::Mat& img);
std::vector<float> GetOutputFeatureMap(const cv::Mat& img, std::vector<int>& outshape);
void SetMean(const string& mean_file);
std::vector<float> Predict(const cv::Mat& img);
std::vector<std::vector<float> > BatchPredict(const std::vector<cv::Mat>& img);
void WrapInputLayer(std::vector<cv::Mat>* input_channels);
void Preprocess(const cv::Mat& img,
std::vector<cv::Mat>* input_channels,bool resize_img=true);
void GetInputImageSize(int &w, int &h);
//advanced operations
float Pruning(float weight_t, const char* saveas_name = 0);
//Ұ
cv::Mat EstimateReceptiveField(const cv::Mat& img, const string& layerName,int x,int y, int idxNeuron = -1, bool islstm = false, int* width_parts = 0);
void GetLayerFeatureMapSize(int w, int h, const std::string& layerName, int& w1, int& h1);
private:
void Forward(const cv::Mat& img, const string& lastLayerName);
void BatchForward(const vector<cv::Mat>& imgs, const string& lastLayerName);
void PrepareInput(const cv::Mat& img);
void PrepareBatchInputs(const vector<cv::Mat>& imgs);
private:
std::shared_ptr<Net<float> > net_;
cv::Size input_geometry_;
int num_channels_;
cv::Mat mean_;
std::vector<float> channel_mean_;
std::vector<string> labels_;
std::vector< std::set<string> > synsetwords;
int FindMaxChannelLayer();
int FindLayerIndex(const string& strLayerName);
};
#endif
#include "classifierCaffe.h"
///////////////////////////////
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h> /*用到了time函数,所以要有这个头文件*/
#include <fstream>
#include <sstream>
#include <exception>
#include <vector>
#include <io.h>
#include <caffe/caffe.hpp>
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif // USE_OPENCV
#include <algorithm>
#include <iosfwd>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#ifdef USE_OPENCV
using namespace caffe; // NOLINT(build/namespaces)
using std::string;
extern void initTrainImage();
extern vector<string> imgNames;
//
//#define showSteps 0
//
using namespace std;
//char * configFile = "config.txt";
//
////读取config文件里的内容--
//char* trainSetPosPath = (char *)malloc(200 * sizeof(char));
//char* templateName = (char *)malloc(200 * sizeof(char));
//int frame_width = 640;
//int frame_height = 480;
//char *model_file = (char *)malloc(200 * sizeof(char)); //model_file = "LightenedCNN_C_deploy.prototxt";
//char *trained_file = (char *)malloc(200 * sizeof(char)); //trained_file = "LightenedCNN_C.caffemodel";
//int label_file = 256;
//void readConfig(char* configFile, char* trainSetPosPath){
// fstream f;
// char cstring[1000];
// int readS = 0;
// f.open(configFile, fstream::in);
// char param1[200]; strcpy(param1, "");
// char param2[200]; strcpy(param2, "");
// char param3[200]; strcpy(param3, "");
//
// //--读取第一行:--
// f.getline(cstring, sizeof(cstring));
// readS = sscanf(cstring, "%s %s %s", param1, param2, param3);
// strcpy(trainSetPosPath, param3);
//
// //--读取第2行:-- 对比人脸
// f.getline(cstring, sizeof(cstring));
// readS = sscanf(cstring, "%s %s %s", param1, param2, param3);
// strcpy(templateName, param3);
//
// //--读取第3行:-- 相机宽
// f.getline(cstring, sizeof(cstring));
// readS = sscanf(cstring, "%s %s %d", param1, param2, &frame_width);
//
// //--读取第4行:-- 相机高
// f.getline(cstring, sizeof(cstring));
// readS = sscanf(cstring, "%s %s %d", param1, param2, &frame_height);
//
// //--读取第5行:-- 训练模型
// f.getline(cstring, sizeof(cstring));
// readS = sscanf(cstring, "%s %s %s", param1, param2, param3);
// strcpy(model_file, param3);
//
// //--读取第6行:-- 训练权重
// f.getline(cstring, sizeof(cstring));
// readS = sscanf(cstring, "%s %s %s", param1, param2, param3);
// strcpy(trained_file, param3);
//
// //--读取第6行:-- 特征数量
// f.getline(cstring, sizeof(cstring));
// readS = sscanf(cstring, "%s %s %d", param1, param2, &label_file);
//}
//
////遍历config.txt里的根目录下的所有的文件,包括子目录。--
//// 其中子目录的名字就是label,子目录里的文件为label对于的训练测试样本---
//vector<string> imgNames;
//vector<string> imgLists;
//vector<int> imgLabels;
//int labelTemp = 0;
//
//void dfsFolder(string folderPath){
// _finddata_t FileInfo;
// string strfind = folderPath + "\\*";
// long long Handle = _findfirst(strfind.c_str(), &FileInfo);
// if (Handle == -1L)
// {
// cerr << "can not match the folder path" << endl;
// exit(-1);
// }
// do{
// //判断是否有子目录--
// if (FileInfo.attrib & _A_SUBDIR) {
// // cout<<FileInfo.name<<" "<<FileInfo.attrib<<endl;
// //这个语句很重要--
// if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0)) {
// string newPath = folderPath + "\\" + FileInfo.name;
// cout << FileInfo.name << " " << newPath << endl;
// //根目录下下的子目录名字就是label名,如果没有子目录则其为根目录下
// labelTemp = atoi(FileInfo.name);
// // printf("%d\n",labelTemp);
// dfsFolder(newPath);
// }
// }
// else {
// string finalName = folderPath + "\\" + FileInfo.name;
// //将所有的文件名写入一个txt文件--
// // cout << FileInfo.name << "\t";
// // printf("%d\t",label);
// // cout << folderPath << "\\" << FileInfo.name << " " <<endl;
// //将文件名字和label名字(子目录名字赋值给向量)--
// imgLabels.push_back(labelTemp);
// imgNames.push_back(finalName);
//
//
// std::stringstream ss;
// std::string str;
// ss << labelTemp;
// ss >> str;
//
// string finalList = FileInfo.name;
// imgLists.push_back(finalList);
//
// }
// } while (_findnext(Handle, &FileInfo) == 0);
// _findclose(Handle);
//
//}
//
//void initTrainImage(){
// readConfig(configFile, trainSetPosPath);
//
// string folderPath = trainSetPosPath;
// // string folderPath = "H:\\char\\poker_rec_char_equalist_test";
// // string folderPath = "C:\\planeSample\\recSampleData\\rec";
// // string folderPath = "C:\\Users\\Administrator\\Desktop\\LPR\\hu\\";
// dfsFolder(folderPath);
//
//}
////////////////////////////////////////////
Classifier::Classifier(const string& model_file,
const string& trained_file,
const string& mean_file,
const int& label_file) {
#ifdef CPU_ONLY
caffe::Caffe::set_mode(caffe::Caffe::CPU);
#else
Caffe::set_mode(Caffe::GPU);
#endif
/* Load the network. */
net_.reset(new caffe::Net<float>(model_file, caffe::TEST));
net_->CopyTrainedLayersFrom(trained_file);
CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly one input.";
//CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output.";
caffe::Blob<float>* input_layer = net_->input_blobs()[0];
num_channels_ = input_layer->channels();
CHECK(num_channels_ == 3 || num_channels_ == 1)
<< "Input layer should have 1 or 3 channels.";
input_geometry_ = cv::Size(input_layer->width(), input_layer->height());
/* Load the binaryproto mean file. */
if (mean_file != ""){
SetMean(mean_file);
}
vector<string> label_array;
for (int j = 0; j < label_file; j++)
{
label_array.push_back("0");
}
caffe::Blob<float>* output_layer = net_->output_blobs()[0];
CHECK_EQ(label_array.size(), output_layer->channels())
<< "Number of labels is different from the output layer dimension.";
labels_.push_back(label_array);
}
static bool PairCompare(const std::pair<float, int>& lhs,
const std::pair<float, int>& rhs) {
return lhs.first > rhs.first;
}
/* Return the indices of the top N values of vector v. */
static std::vector<int> Argmax(const std::vector<float>& v, int N) {
std::vector<std::pair<float, int> > pairs;
for (size_t i = 0; i < v.size(); ++i)
pairs.push_back(std::make_pair(v[i], static_cast<int>(i)));
std::partial_sort(pairs.begin(), pairs.begin() + N, pairs.end(), PairCompare);
std::vector<int> result;
for (int i = 0; i < N; ++i)
result.push_back(pairs[i].second);
return result;
}
/* Return the top N predictions. */
std::vector<float> Classifier::Classify(const cv::Mat& img) {
auto output = Predict(img);
vector<float> v1;
for (int i = 0; i < output.size(); i++){
v1.push_back((float)output[i]);
}
return v1;
}
/* Load the mean file in binaryproto format. */
void Classifier::SetMean(const string& mean_file) {
caffe::BlobProto blob_proto;
caffe::ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);
/* Convert from BlobProto to Blob<float> */
caffe::Blob<float> mean_blob;
mean_blob.FromProto(blob_proto);
CHECK_EQ(mean_blob.channels(), num_channels_)
<< "Number of channels of mean file doesn't match input layer.";
/* The format of the mean file is planar 32-bit float BGR or grayscale. */
std::vector<cv::Mat> channels;
float* data = mean_blob.mutable_cpu_data();
for (int i = 0; i < num_channels_; ++i) {
/* Extract an individual channel. */
cv::Mat channel(mean_blob.height(), mean_blob.width(), CV_32FC1, data);
channels.push_back(channel);
data += mean_blob.height() * mean_blob.width();
}
/* Merge the separate channels into a single image. */
cv::Mat mean;
cv::merge(channels, mean);
/* Compute the global mean pixel value and create a mean image
* filled with this value. */
cv::Scalar channel_mean = cv::mean(mean);
mean_ = cv::Mat(input_geometry_, mean.type(), channel_mean);
}
std::vector<float> Classifier::Predict(const cv::Mat& img) {
caffe::Blob<float>* input_layer = net_->input_blobs()[0];
input_layer->Reshape(1, num_channels_,
input_geometry_.height, input_geometry_.width);
/* Forward dimension change to all layers. */
net_->Reshape();
std::vector<cv::Mat> input_channels;
WrapInputLayer(&input_channels);
Preprocess(img, &input_channels);
net_->ForwardPrefilled();
/* Copy the output layer to a std::vector */
caffe::Blob<float>* output_layer1 = net_->output_blobs()[0];
const float* begin1 = output_layer1->cpu_data();
const float* end1 = begin1 + output_layer1->channels();
std::vector<float> prob1(begin1, end1);
return prob1;
}
/* Wrap the input layer of the network in separate cv::Mat objects
* (one per channel). This way we save one memcpy operation and we
* don't need to rely on cudaMemcpy2D. The last preprocessing
* operation will write the separate channels directly to the input
* layer. */
void Classifier::WrapInputLayer(std::vector<cv::Mat>* input_channels) {
caffe::Blob<float>* input_layer = net_->input_blobs()[0];
int width = input_layer->width();
int height = input_layer->height();
float* input_data = input_layer->mutable_cpu_data();
for (int i = 0; i < input_layer->channels(); ++i) {
cv::Mat channel(height, width, CV_32FC1, input_data);
input_channels->push_back(channel);
input_data += width * height;
}
}
void Classifier::Preprocess(const cv::Mat& img,
std::vector<cv::Mat>* input_channels) {
/* Convert the input image to the input image format of the network. */
cv::Mat sample;
if (img.channels() == 3 && num_channels_ == 1)
cv::cvtColor(img, sample, cv::COLOR_BGR2GRAY);
else if (img.channels() == 4 && num_channels_ == 1)
cv::cvtColor(img, sample, cv::COLOR_BGRA2GRAY);
else if (img.channels() == 4 && num_channels_ == 3)
cv::cvtColor(img, sample, cv::COLOR_BGRA2BGR);
else if (img.channels() == 1 && num_channels_ == 3)
cv::cvtColor(img, sample, cv::COLOR_GRAY2BGR);
else
sample = img;
cv::Mat sample_resized;
if (sample.size() != input_geometry_)
cv::resize(sample, sample_resized, input_geometry_);
else
sample_resized = sample;
cv::Mat sample_float;
if (num_channels_ == 3)
sample_resized.convertTo(sample_float, CV_32FC3);
else
sample_resized.convertTo(sample_float, CV_32FC1);
cv::Mat sample_normalized;
if (mean_.data != NULL)
cv::subtract(sample_float, mean_, sample_normalized);
else {
sample_normalized = sample_float;
int channels = sample_normalized.channels();
int nRows = sample_normalized.rows;
int nCols = sample_normalized.cols* channels;
if (sample_normalized.isContinuous())
{
nCols *= nRows;
nRows = 1;
}
int i, j;
float* p;
for (i = 0; i < nRows; ++i)
{
p = sample_normalized.ptr<float>(i);
for (j = 0; j < nCols; ++j)
{
//if (label_file == 256)
p[j] *= 0.00390625;
//else if (label_file == 512){
// p[j] -= 128;
// p[j] *= 0.0078125;
//}
// cout << p[j] << " ";
}
}
}
/* This operation will write the separate BGR planes directly to the
* input layer of the network because it is wrapped by the cv::Mat
* objects in input_channels. */
cv::split(sample_normalized, *input_channels);
CHECK(reinterpret_cast<float*>(input_channels->at(0).data)
== net_->input_blobs()[0]->cpu_data())
<< "Input channels are not wrapping the input layer of the network.";
}
double dotProduct(const vector<float>& v1, const vector<float>& v2)
{
assert(v1.size() == v2.size());
float ret = 0.0;
for (vector<float>::size_type i = 0; i != v1.size(); ++i)
{
ret += v1[i] * v2[i];
}
return ret;
}
double module(const vector<float>& v)
{
float ret = 0.0;
for (vector<float>::size_type i = 0; i != v.size(); ++i)
{
ret += v[i] * v[i];
}
return sqrt(ret);
}
// 夹角余弦
double cosine(const vector<float>& v1, const vector<float>& v2)
{
assert(v1.size() == v2.size());
return dotProduct(v1, v2) / (module(v1) * module(v2));
}
//float CalcSimilarity(float const fc1,
// float const fc2,
// long dim) {
// if (dim == -1) {
// dim = 2048;
// }
// return simd_dot(fc1, fc2, dim)/ (
// sqrt(simd_dot(fc1, fc1, dim))*
// sqrt(simd_dot(fc2, fc2, dim))
// );
//}
//
//
//float simd_dot(const float* x, const float* y, const long& len) {
// float inner_prod = 0.0f;
// __m128 X, Y; // 128-bit values
// __m128 acc = _mm_setzero_ps(); // set to (0, 0, 0, 0)
// float temp[4];
//
// long i;
// for (i = 0; i + 4 < len; i += 4) {
// X = _mm_loadu_ps(x + i); // load chunk of 4 floats
// Y = _mm_loadu_ps(y + i);
// acc = _mm_add_ps(acc, _mm_mul_ps(X, Y));
// }
// _mm_storeu_ps(&temp[0], acc); // store acc into an array
// inner_prod = temp[0] + temp[1] + temp[2] + temp[3];
//
// // add the remaining values
// for (; i < len; ++i) {
// inner_prod += x[i] * y[i];
// }
// return inner_prod;
//}
int main0(int argc, char** argv) {
::google::InitGoogleLogging(argv[0]);
initTrainImage();
string model_file = "face_deploy_mirror_normalize.prototxt";
string trained_file = "face_train_test_iter_530000.caffemodel"; //7:6
string mean_file = "";
int label_file = 512;
//string model_file = "LightenedCNN_C_deploy.prototxt";
//string trained_file = "LightenedCNN_C.caffemodel"; //7:6
//string mean_file = "";
//int label_file = 256;
std::cout << "the labels' channel:" << label_file << std::endl;
Classifier classifier(model_file, trained_file, mean_file, label_file);
string file = ".\\face_align\\1.jpg";
cv::Mat img = cv::imread(file, 1);
CHECK(!img.empty()) << "Unable to decode image " << file;
std::cout << "---------- Prediction for "<< file << " ----------" << std::endl;
vector<float> predictions = classifier.Classify(img);
int imgNum = imgNames.size();
for (int iNum = 0; iNum < imgNum; iNum++){
cout << endl << iNum << " " << imgNames[iNum].c_str() << endl;
//string file2 = "F:\\MTCNN-master\\vs2013_caffe_BN_multi_label\\water_meter_caffe_old\\face\\3.jpg";
cv::Mat img2 = cv::imread(imgNames[iNum].c_str(), 1);
//CHECK(!img2.empty()) << "Unable to decode image " << imgNames[iNum].c_str();
if (img2.empty()) continue;
//cv::normalize(img2,img2,255,0,CV_MINMAX);
vector<float> predictions2 = classifier.Classify(img2);
/* Print the top N predictions. */
for (size_t i = 0; i < predictions.size(); ++i) {
std::cout << predictions[i] << " ";
}
std::cout << std::endl << std::endl;
for (size_t i = 0; i < predictions2.size(); ++i) {
std::cout << predictions2[i] << " ";
}
double sim2 = cosine(predictions, predictions2);
cout << "相似度为2 :" << sim2 << endl;
}
return 1;
}
Classifier * init() {
initTrainImage();
string model_file = "face_deploy_mirror_normalize.prototxt";
string trained_file = "face_train_test_iter_530000.caffemodel"; //7:6
string mean_file = "";
int label_file = 512;
//string model_file = "LightenedCNN_C_deploy.prototxt";
//string trained_file = "LightenedCNN_C.caffemodel"; //7:6
//string mean_file = "";
//int label_file = 256;
std::cout << "the labels' channel:" << label_file << std::endl;
//Classifier classifier(model_file, trained_file, mean_file, label_file);
Classifier *classifier;
classifier = new Classifier(model_file, trained_file, mean_file, label_file);
return classifier;
}
void test_(Classifier *classify) {
Classifier *classifier = classify;
string file = ".\\face_align\\1.jpg";
cv::Mat img = cv::imread(file, 1);
CHECK(!img.empty()) << "Unable to decode image " << file;
//std::cout << "---------- Prediction for " << file << " ----------" << std::endl;
vector<float> predictions = classifier->Classify(img);
int imgNum = imgNames.size();
for (int iNum = 0; iNum < imgNum; iNum++) {
//cout << endl << iNum << " " << imgNames[iNum].c_str() << endl;
//string file2 = "F:\\MTCNN-master\\vs2013_caffe_BN_multi_label\\water_meter_caffe_old\\face\\3.jpg";
cv::Mat img2 = cv::imread(imgNames[iNum].c_str(), 1);
//CHECK(!img2.empty()) << "Unable to decode image " << imgNames[iNum].c_str();
if (img2.empty()) continue;
//cv::normalize(img2,img2,255,0,CV_MINMAX);
/*while (1) */{
vector<float> predictions2 = classifier->Classify(img2);
/* Print the top N predictions. */
/*for (size_t i = 0; i < predictions.size(); ++i) {
std::cout << predictions[i] << " ";
}
std::cout << std::endl << std::endl;
for (size_t i = 0; i < predictions2.size(); ++i) {
std::cout << predictions2[i] << " ";
}*/
double sim2 = cosine(predictions, predictions2);
//cout << "相似度为2 :" << sim2 << endl;
}
}
}
int main00(int argc, char **argv) {
::google::InitGoogleLogging(argv[0]);
Classifier *classifier = init();
Classifier *classifier2 = init();
//init_rec();
//init_detect();
for (int i = 0; i < 10; i++)
{
cout << i << endl;
std::thread first(test_, classifier);
std::thread second(test_, classifier2);
//std::thread third(test_);
first.join();
second.join();
//third.join();
}
return 1;
}
int main000(int argc, char **argv) {
Phase phase = TEST;
int gpuid = 0;
#ifdef CPU_ONLY
Caffe::set_mode(Caffe::CPU);
#else
Caffe::set_mode(Caffe::GPU);
Caffe::SetDevice(gpuid);
#endif
boost::shared_ptr<Net<float> >feature_net;
boost::shared_ptr<Net<float> >feature_net1;
//boost::thread_specific_ptr<Net<float> >feature_net;
string protopath = "face_deploy_mirror_normalize.prototxt";
string modelpath = "face_train_test_iter_530000.caffemodel"; //7:6
feature_net.reset(new Net<float>(protopath, phase));
feature_net->CopyTrainedLayersFrom(modelpath);
feature_net1.reset(new Net<float>(protopath, phase));
feature_net1->CopyTrainedLayersFrom(modelpath);
const int num = 2;
/*std::thread * thrcall = new thread[num];
thrcall[0].set_param(0, feature_net);
thrcall[0].start();
thrcall[1].set_param(1, feature_net1);
thrcall[1].start();
thrcall[0].join();
thrcall[1].join();*/
return 1;
}
#endif // USE_OPENCV
\ No newline at end of file
#ifndef _CLASSIFIERCAFFE_H_
#define _CLASSIFIERCAFFE_H_
#include <caffe/caffe.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/ml/ml.hpp>
#include <algorithm>
#include <iosfwd>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <thread>
#include "boost/make_shared.hpp"
//using namespace caffe; // NOLINT(build/namespaces)
using std::string;
using namespace std;
double cosine(const vector<double>& v1, const vector<double>& v2);
/* Pair (label, confidence) representing a prediction. */
// change: 2 classify (label1, confidence1) (label2, confidence2)
typedef std::pair<string, float> Prediction;
class _declspec(dllexport) Classifier {
public:
Classifier(const string& model_file,
const string& trained_file,
const string& mean_file,
const int& label_files);
std::vector<float> Classifier::Classify(const cv::Mat& img);
cv::Size input_geometry_;
private:
void SetMean(const string& mean_file);
std::vector<float> Predict(const cv::Mat& img);
void WrapInputLayer(std::vector<cv::Mat>* input_channels);
void Preprocess(const cv::Mat& img,
std::vector<cv::Mat>* input_channels);
private:
std::shared_ptr<caffe::Net<float> > net_;
int num_channels_;
cv::Mat mean_;
std::vector<vector<string>> labels_; //multi
};
#endif // _CLASSIFIERCAFFE_H_
\ No newline at end of file
path = testImg
startNum = 0
#include <stdio.h>
#include <math.h>
#include <omp.h>
#include <windows.h>
LARGE_INTEGER m_liPerfFreq;
LARGE_INTEGER m_liPerfStart;
LARGE_INTEGER liPerfNow;
double dfTim;
void getStartTime()
{
QueryPerformanceFrequency(&m_liPerfFreq);
QueryPerformanceCounter(&m_liPerfStart);
}
void getEndTime()
{
QueryPerformanceCounter(&liPerfNow);
dfTim=( ((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000.0f)/m_liPerfFreq.QuadPart);
}
void test()
{
static long num_steps = 1000000;
double step, pi;
double x, sum=0.0;
int i=0;
step = 1.0/(double)num_steps;
getStartTime();
//#pragma omp parallel for private(x) reduction(+:sum)
for (i=0; i<num_steps; i++)
{
x = (i+0.5)*step;
sum += 4.0/(1.0+x*x);
//printf("%f,%d\n",omp_get_thread_num());
}
getEndTime();
printf("%f\n",dfTim);
pi = step * sum;
printf ("pi=%f\n",pi);
}
\ No newline at end of file
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
此差异已折叠。
此差异已折叠。
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerCommandArguments>C:\plate_card_BLSTM\vs2013_caffe_BN_multi_label_kenel_w\model_platecar ..\model\test2.jpg</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
\ No newline at end of file
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
// stdafx.cpp : source file that includes just the standard includes
// libClassification.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
// TODO: reference additional headers your program requires here
#pragma once
// Including SDKDDKVer.h defines the highest available Windows platform.
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
#include <SDKDDKVer.h>
此差异已折叠。
#ifndef TFORM_FCN_H
#define TFORM_FCN_H
#include <opencv2/opencv.hpp>
class matlabexception{};
typedef struct tfm
{
cv::Mat forword;//ǰӳ
cv::Mat inv;//ӳ
} Tfm;
typedef struct opt
{
int order;//ǰӳ
int K;//ӳ
} options;
Tfm cp2tform_similarity(cv::Mat src, cv::Mat dst); //srcԴͼ κͼ
cv::Mat itransform(cv::Mat img,Tfm tm,int rows,int cols);
cv::Mat onetone(cv::Mat img, cv::Mat point, int size, int emh, int eh);
#endif
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册