xuepengqiang
2020-01-06 bf3d2d5e30e4b5c3a44d86aa5a9a2d739df55bd7
add reid
4个文件已添加
330 ■■■■■ 已修改文件
Makefile 93 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
reid_feature.cpp 122 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
reid_feature.h 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test.cpp 83 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Makefile
New file
@@ -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)/*
reid_feature.cpp
New file
@@ -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;
}
reid_feature.h
New file
@@ -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
test.cpp
New file
@@ -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);
    }
}