admin
2025-04-25 c3c0d2742db73d8a91cdc28218a58e4863086095
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include"clasi.h"
 
#define checkRuntime3(op)  __check_cuda_runtime3((op), #op, __FILE__, __LINE__)
 
bool __check_cuda_runtime3(cudaError_t code, const char* op, const char* file, int line) {
    if (code != cudaSuccess) {
        const char* err_name = cudaGetErrorName(code);
        const char* err_message = cudaGetErrorString(code);
        printf("runtime error %s:%d  %s failed. \n  code = %s, message = %s\n", file, line, op, err_name, err_message);
        return false;
    }
    return true;
}
 
 
 
inline const char* severity_string3(nvinfer1::ILogger::Severity t) {
    switch (t) {
    case nvinfer1::ILogger::Severity::kINTERNAL_ERROR: return "internal_error";
    case nvinfer1::ILogger::Severity::kERROR:   return "error";
    case nvinfer1::ILogger::Severity::kWARNING: return "warning";
    case nvinfer1::ILogger::Severity::kINFO:    return "info";
    case nvinfer1::ILogger::Severity::kVERBOSE: return "verbose";
    default: return "unknow";
    }
}
 
 
class Logger3 : public ILogger
{
    void log(Severity severity, const char* msg)  noexcept
    {
        // suppress info-level messages
        if (severity != Severity::kINFO)
            printf("%s: %s\n", severity_string3(severity), msg);
    }
};
 
 
 
bool clasification::initConfig(const char* modelpath) {
    std::ifstream file(modelpath, std::ios::binary);
    char* trtModelStream = NULL;
    int size = 0;
    if (file.good()) {
        file.seekg(0, file.end);
        size = file.tellg();
        file.seekg(0, file.beg);
        trtModelStream = new char[size];
        assert(trtModelStream);
        file.read(trtModelStream, size);
        file.close();
    }
    else {
        return false;
    }
 
    // ��ʼ����������
    Logger3 logger;
    this->runtime = createInferRuntime(logger);
    assert(this->runtime != nullptr);
    this->engine = runtime->deserializeCudaEngine(trtModelStream, size);
    assert(this->engine != nullptr);
    this->context = engine->createExecutionContext();
    assert(this->context != nullptr);
    delete[] trtModelStream;
 
    auto input_dims = engine->getBindingDimensions(0);
    input_batch = input_dims.d[0];
    input_channel = input_dims.d[1];
    input_height = input_dims.d[2];
    input_width = input_dims.d[3];
    input_numel = input_batch * input_channel * input_height * input_width;
 
    image_area = input_width * input_height;
 
    printf("input size=%d*%d*%d*%d\n", input_batch, input_channel, input_height, input_width);
    auto output_dims = engine->getBindingDimensions(1);
    output_classs = output_dims.d[0];
    num_classes = output_dims.d[1];
    printf("output size=%d*%d\n", output_classs, num_classes);
 
    output_numel = output_classs * num_classes;
 
    checkRuntime3(cudaMallocHost(&input_data_host, input_numel * sizeof(float)));
    checkRuntime3(cudaMalloc(&input_data_device, input_numel * sizeof(float)));
 
    checkRuntime3(cudaMallocHost(&output_data_host, sizeof(float) * output_numel));
    checkRuntime3(cudaMalloc(&output_data_device, sizeof(float) * output_numel));
 
 
    checkRuntime3(cudaStreamCreate(&stream));
 
 
    return true;
 
 
}
 
 
int clasification::detect(cv::Mat& image) {
    cv::Mat blob;
    cv::resize(image,blob, cv::Size(input_width, input_height));
    unsigned char* pimage = blob.data;
    float* host_b = input_data_host + image_area * 0;
    float* host_g = input_data_host + image_area * 1;
    float* host_r = input_data_host + image_area * 2;
    for (int i = 0; i < image_area; ++i, pimage += 3) {
        *host_r++ = (pimage[0] / 255.0f - 0.406) / 0.225;
        *host_g++ = (pimage[1] / 255.0f - 0.456) / 0.224;
        *host_b++ = (pimage[2] / 255.0f - 0.485) / 0.229;
    }
 
    checkRuntime3(cudaMemcpyAsync(input_data_device, input_data_host, input_numel * sizeof(float), cudaMemcpyHostToDevice, stream));
    float* bindings[] = { input_data_device, output_data_device };
    bool success = context->enqueueV2((void**)bindings, stream, nullptr);
    checkRuntime3(cudaMemcpyAsync(output_data_host, output_data_device, sizeof(float) * output_numel, cudaMemcpyDeviceToHost, stream));
    checkRuntime3(cudaStreamSynchronize(stream));
 
    float* prob = output_data_host;
    int predict_label = std::max_element(prob, prob + num_classes) - prob;
    //float confidence = prob[predict_label]; 
    //printf("predict: confidence = %f, label = %d\n", confidence, predict_label);
 
    return predict_label;
 
 
}
 
 
clasification::~clasification() {
 
    // ͬ���������ͷ���Դ
 
    checkRuntime3(cudaStreamSynchronize(stream));
    //if (stream != nullptr)
    //{
        checkRuntime3(cudaStreamDestroy(stream));
    //}
 
    if (!context) {
        context->destroy();
    }
    if (!engine) {
        engine->destroy();
    }
    if (!runtime) {
        runtime->destroy();
    }
 
    if (!output_data_host) {
        delete[] output_data_host;
 
    }
 
    if (!output_data_device) {
        checkRuntime3(cudaFree(output_data_device));
    }
 
    if (!input_data_device) {
        checkRuntime3(cudaFree(input_data_device));
    }
 
 
    if (!input_data_host) {
        delete[] input_data_host;
    }
 
}