From bf3d2d5e30e4b5c3a44d86aa5a9a2d739df55bd7 Mon Sep 17 00:00:00 2001
From: xuepengqiang <506321815@qq.com>
Date: 星期一, 06 一月 2020 14:01:13 +0800
Subject: [PATCH] add reid

---
 test.cpp         |   83 +++++++++++++
 reid_feature.cpp |  122 ++++++++++++++++++++
 Makefile         |   93 +++++++++++++++
 reid_feature.h   |   32 +++++
 4 files changed, 330 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..4eec39b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,93 @@
+GPU=1
+CUDNN=1
+DEBUG=1
+
+ARCH= -gencode arch=compute_30,code=sm_30 \
+      -gencode arch=compute_35,code=sm_35 \
+      -gencode arch=compute_50,code=[sm_50,compute_50] \
+      -gencode arch=compute_52,code=[sm_52,compute_52]
+#      -gencode arch=compute_20,code=[sm_20,sm_21] \ This one is deprecated?
+
+# This is what I use, uncomment if you know your arch and want to specify
+# ARCH= -gencode arch=compute_52,code=compute_52
+
+VPATH=.
+EXEC=reid
+OBJDIR=./obj/
+
+CC=gcc
+CPP=g++
+NVCC=nvcc 
+AR=ar
+ARFLAGS=rcs
+OPTS=-Ofast
+LDFLAGS= -lm -pthread 
+CFLAGS=-Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC
+
+CFLAGS+= -I/home/disk1/s_opt/opencv/include/opencv4
+LDFLAGS+= -L/home/disk1/s_opt/opencv/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_imgproc
+
+
+COMMON = -std=c++11
+#flag = -Wl,-rpath=/home/disk1/s_opt/libtorch/lib
+#COMMON += -D_GLIBCXX_USE_CXX11_ABI=0 -I/home/disk1/s_opt/libtorch/include -I/home/disk1/s_opt/libtorch/include/torch/csrc/api/include
+#LDFLAGS+= -L/home/disk1/s_opt/libtorch/lib -ltorch -lc10 -lc10_cuda -lcudart -lgomp -lnvToolsExt
+
+flag = -Wl,-rpath=/home/disk1/s_opt/libtorch_CPP11/libtorch/lib
+COMMON += -I/home/disk1/s_opt/libtorch_CPP11/libtorch/include -I/home/disk1/s_opt/libtorch_CPP11/libtorch/include/torch/csrc/api/include
+LDFLAGS+= -L/home/disk1/s_opt/libtorch_CPP11/libtorch/lib -ltorch -lc10 -lc10_cuda -lcudart -lgomp -lnvToolsExt
+
+#COMMON = -std=c++11
+#COMMON= -I/home/disk1/data/s_software/CPP11_torch/libtorch/include  -std=c++11
+#COMMON= -D_GLIBCXX_USE_CXX11_ABI=0 -I/home/disk1/data/s_software/CPP11_torch/libtorch/include  -std=c++11
+#LDFLAGS+= -L/home/disk1/data/s_software/CPP11_torch/libtorch/lib -ltorch -lc10 -lc10_cuda -lcudart -lgomp -lnvToolsExt
+
+
+ifeq ($(DEBUG), 1) 
+OPTS=-O0 -g
+endif
+
+CFLAGS+=$(OPTS)
+
+
+COMMON+= -DGPU -I/usr/local/cuda/include/
+
+ifeq ($(CUDNN), 1) 
+COMMON+= -DCUDNN 
+CFLAGS+= -DCUDNN
+LDFLAGS+= -lcudnn
+endif
+
+OBJ=test.o reid_feature.o
+
+ifeq ($(GPU), 1) 
+LDFLAGS+= -lstdc++ 
+OBJ+=
+endif
+
+OBJS = $(addprefix $(OBJDIR), $(OBJ))
+DEPS = $(wildcard */*.h) Makefile 
+
+all: obj $(EXEC)
+#all: obj  results $(SLIB) $(ALIB) $(EXEC)
+
+
+$(EXEC): $(OBJS) 
+	$(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(ALIB)  $(flag)
+
+$(OBJDIR)%.o: %.cpp $(DEPS)
+	$(CPP) $(COMMON) $(CFLAGS) -c $< -o $@
+
+$(OBJDIR)%.o: %.c $(DEPS)
+	$(CC) $(COMMON) $(CFLAGS) -c $< -o $@
+
+$(OBJDIR)%.o: %.cu $(DEPS)
+	$(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@
+
+obj:
+	mkdir -p obj
+
+.PHONY: clean
+
+clean:
+	rm -rf $(OBJS) $(EXEC) $(EXECOBJ) $(OBJDIR)/*
diff --git a/reid_feature.cpp b/reid_feature.cpp
new file mode 100644
index 0000000..4bb847e
--- /dev/null
+++ b/reid_feature.cpp
@@ -0,0 +1,122 @@
+//
+// Created by Scheaven on 2020/1/3.
+//
+
+#include "reid_feature.h"
+#include <cuda_runtime_api.h>
+#include <torch/torch.h>
+
+bool ReID_Feature::ReID_init(int gpu_id)
+{
+    if(gpu_id == -1){
+        this->module = torch::jit::load(MODEL_PATH);
+        this->module.to(torch::kCPU);
+        this->module.eval();
+        this->is_gpu = false;
+    }else if(torch::cuda::is_available() && torch::cuda::device_count() >= gpu_id)
+    {
+        cudaSetDevice(gpu_id);
+        cout << "model loading::" << HUMAN_FEATS << endl;
+        this->module = torch::jit::load(MODEL_PATH, torch::Device(torch::DeviceType::CUDA,gpu_id));
+        this->module.to(torch::kCUDA);
+        this->module.eval();
+        this->is_gpu = true;
+    }else{
+        return false;
+    }
+    return true;
+}
+
+int ReID_Feature::ReID_size()
+{
+    int size = 2048;
+    return size;
+
+}
+
+bool ReID_Feature::ReID_extractor(unsigned char *pBuf, float *pFeature)
+{
+    auto input_tensor = torch::from_blob(pBuf, {1, 256, 128, 3});
+    input_tensor = input_tensor.permute({0, 3, 1, 2});
+    input_tensor[0][0] = input_tensor[0][0].sub_(0.485).div_(0.229);
+    input_tensor[0][1] = input_tensor[0][1].sub_(0.456).div_(0.224);
+    input_tensor[0][2] = input_tensor[0][2].sub_(0.406).div_(0.225);
+    if(this->is_gpu)
+        input_tensor = input_tensor.to(at::kCUDA);
+    torch::Tensor human_feat =this->module.forward({input_tensor}).toTensor();
+
+//    for (int k = 0; k < 20; ++k) {
+//        cout << "--extractor---human_feats------" <<human_feat[0][k+2000]<< endl;
+//    }
+    torch::Tensor query_feat;
+    if(this->is_gpu)
+        query_feat = human_feat.cpu();
+    else
+        query_feat = human_feat;
+
+    auto foo_one = query_feat.accessor<float,2>();
+
+    ReID_Utils RET;
+
+    float f_size = -0.727412;
+    for (int64_t i = 0; i < foo_one.size(0); i++) {
+        auto a1 = foo_one[i];
+        for (int64_t j = 0; j < a1.size(0); j++) {
+            pFeature[j] = a1[j];
+        }
+    }
+
+//    cout << "---- end 11-------" << pFeature[0] << endl;
+    return true;
+}
+
+float ReID_Feature::ReID_Compare(float *pFeature1, float *pFeature2)
+{
+    torch::Tensor query_feat = torch::zeros({1,2048});
+    torch::Tensor gallery_feat = torch::zeros({1,2048});
+
+    for (int i = 0; i < 2048; i++)
+    {
+        query_feat[0][i] = pFeature1[i];
+        gallery_feat[0][i] = pFeature2[i];
+    }
+
+    if(this->is_gpu)
+    {
+        query_feat = query_feat.cuda();
+        gallery_feat = gallery_feat.cuda();
+    }
+
+//    cout << "-----------------after-----------" << endl;
+//    cout << query_feat<< endl;
+
+//    for (int k = 0; k < 20; ++k) {
+//        cout << "-query_feat----1111111111------" <<query_feat[0][k+2000]<< endl;
+//    }
+//
+//    cout << "-----------------asdf-----------" << endl;
+////    cout << gallery_feat[0][0]<< endl;
+//    for (int k = 0; k < 20; ++k) {
+//        cout << "-gallery_feat----22222222------" <<gallery_feat[0][k+2000]<< endl;
+//    }
+
+    torch::Tensor a_similarity = torch::cosine_similarity(query_feat, gallery_feat);
+
+    if(this->is_gpu)
+        a_similarity = a_similarity.cpu();
+
+    auto foo_one = a_similarity.accessor<float,1>();
+//    cout << ":::::::::-" << endl;
+
+    float f_distance = foo_one[0];
+
+    return f_distance;
+
+}
+
+void ReID_Feature::ReID_Release()
+{
+        prinf("release");
+//    this->module = nullptr;//鍔犺浇妯″瀷
+//    return true;
+}
\ No newline at end of file
diff --git a/reid_feature.h b/reid_feature.h
new file mode 100644
index 0000000..782ffda
--- /dev/null
+++ b/reid_feature.h
@@ -0,0 +1,32 @@
+//
+// Created by Scheaven on 2020/1/3.
+//
+
+#ifndef INC_03_REID_STRONG_BASELINE_REID_FEATURE_H
+#define INC_03_REID_STRONG_BASELINE_REID_FEATURE_H
+#include <torch/script.h>
+#include <opencv2/opencv.hpp>
+#include <fstream>
+#include <string>
+#include <iomanip>
+#include <stdlib.h>
+#include <vector>
+#include <iostream>
+
+
+struct ReID_Feature {
+private:
+    bool is_gpu;
+    //    auto feat;
+    torch::jit::script::Module module;
+public:
+    bool ReID_init(int gpu_id);
+    int ReID_size();
+//    static unsigned char * extractor(unsigned char *pBuf, unsigned char *pFeature);
+    bool ReID_extractor(unsigned char *pBuf, float * pFeature);
+    float ReID_Compare(float *pFeature1, float *pFeature2);
+    void ReID_Release();
+};
+
+
+#endif //INC_03_REID_STRONG_BASELINE_REID_FEATURE_H
diff --git a/test.cpp b/test.cpp
new file mode 100644
index 0000000..d3f2eda
--- /dev/null
+++ b/test.cpp
@@ -0,0 +1,83 @@
+//
+// Created by Scheaven on 2019/12/27.
+//
+#include <torch/script.h> // One-stop header.
+#include <iostream>
+#include <memory>
+#include <vector>
+#include <string>
+#include <opencv2/core/core.hpp>
+#include <opencv2/highgui/highgui.hpp>
+#include "opencv2/imgproc/imgproc.hpp"
+#include "opencv2/opencv.hpp"
+#include "opencv2/videoio.hpp"
+#include "reid_feature.h"
+
+
+using namespace std;
+//using namespace cv;
+
+int main(int argc, const char* argv[]) {
+//    if (argc != 2) {
+//        std::cerr << "usage: reid-app <image path>\n";;
+//        return -1;
+//    }
+
+//    torch::jit::script::Module module;
+//    char cam_id = 'A';
+//    ReID_Tracker Tracker;
+
+
+
+    /*鍒濆鍖�*/
+    int gpu_id = 0;
+    ReID_Feature R_Feater;
+    bool n_flog = R_Feater.ReID_init(0);
+//    ReID_Feature R_Feater(gpu_id);
+
+    /*opencv鍔犺浇鍥剧墖淇℃伅*/
+    cv::Mat human_img = cv::imread("./03.jpg");
+    cv::Mat human_img2 = cv::imread("./01.jpg");
+    if (human_img.data == nullptr)
+    {
+        cerr<<"===鍥剧墖鏂囦欢涓嶅瓨鍦�"<<endl;
+        return 0;
+    }else
+    {
+        //cv::namedWindow("Display", CV_WINDOW_AUTOSIZE);
+        try {
+            float pFeature1[2048];
+            /*杞箟鍥剧墖淇℃伅鏍煎紡*/
+            cv::cvtColor(human_img, human_img, cv::COLOR_RGB2BGR);
+            human_img.convertTo(human_img, CV_32FC3, 1.0f / 255.0f);
+            bool ex_flag1 = R_Feater.ReID_extractor(human_img.data, pFeature1);
+//            for (int k = 0; k < 20; ++k) {
+//                cout << "-----11111111111------" <<pFeature1[k+2000]<< endl;
+//            }
+
+            float pFeature2[2048];
+            cv::cvtColor(human_img2, human_img2, cv::COLOR_RGB2BGR);
+            human_img2.convertTo(human_img2, CV_32FC3, 1.0f / 255.0f);
+            bool ex_flag2 = R_Feater.ReID_extractor(human_img2.data, pFeature2);
+//            for (int k = 0; k < 20; ++k) {
+//                cout << "-----2222222222------" <<pFeature2[k+2000]<< endl;
+//            }
+
+            /*璁$畻鐩镐技搴�*/
+            cout << "--attention_distance human-" << endl;
+            float result = R_Feater.ReID_Compare(pFeature1, pFeature2);
+
+//            Tracker.storager(1,human_img);
+        }
+        catch (const c10::Error& e) {
+            std::cerr << "error loading the model\n";
+            return -1;
+        }
+
+        std::cout << "ok\n";
+        //cout<<  human_img  <<endl;
+        //cv::imshow("0909",human_img);
+        //cv::waitKey(0);
+    }
+
+}

--
Gitblit v1.8.0