Skip to content

Commit dffbcd3

Browse files
renamed private members of utils and sam_inference
1 parent a9fce07 commit dffbcd3

File tree

4 files changed

+85
-85
lines changed

4 files changed

+85
-85
lines changed

include/sam_inference.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ class SAM
3333
std::vector<std::string> classes{};
3434

3535
private:
36-
Ort::Env env;
37-
std::unique_ptr<Ort::Session> session;
38-
bool cudaEnable;
39-
Ort::RunOptions options;
40-
std::vector<const char *> inputNodeNames;
41-
std::vector<const char *> outputNodeNames;
42-
43-
SEG::MODEL_TYPE modelType;
44-
std::vector<int> imgSize;
45-
float rectConfidenceThreshold;
46-
float iouThreshold;
36+
Ort::Env _env;
37+
std::unique_ptr<Ort::Session> _session;
38+
bool _cudaEnable;
39+
Ort::RunOptions _options;
40+
std::vector<const char *> _inputNodeNames;
41+
std::vector<const char *> _outputNodeNames;
42+
43+
SEG::MODEL_TYPE _modelType;
44+
std::vector<int> _imgSize;
45+
float _rectConfidenceThreshold;
46+
float _iouThreshold;
4747
};
4848

4949
#endif // SAMINFERENCE_H

include/utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class Utils
5151
}
5252

5353
private:
54-
float resizeScales;
55-
float resizeScalesBbox; // letterbox scale
54+
float _resizeScales;
55+
float _resizeScalesBbox; // letterbox scale
5656
};
5757

5858
#endif // UTILS_H

src/sam_inference.cpp

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ SAM::SAM() {}
99

1010
SAM::~SAM() {
1111
// Clean up input/output node names
12-
for (auto &name : inputNodeNames) {
12+
for (auto &name : _inputNodeNames) {
1313
delete[] name;
1414
}
15-
for (auto &name : outputNodeNames) {
15+
for (auto &name : _outputNodeNames) {
1616
delete[] name;
1717
}
1818
}
@@ -28,19 +28,19 @@ template <> struct TypeToTensorType<half> {
2828

2929
const char *SAM::CreateSession(SEG::DL_INIT_PARAM &iParams) {
3030
const char *Ret = RET_OK;
31-
if (session) {
32-
session.reset(); // Release previous session
31+
if (_session) {
32+
_session.reset(); // Release previous _session
3333

3434
// Clear node names
35-
for (auto &name : inputNodeNames) {
35+
for (auto &name : _inputNodeNames) {
3636
delete[] name;
3737
}
38-
inputNodeNames.clear();
38+
_inputNodeNames.clear();
3939

40-
for (auto &name : outputNodeNames) {
40+
for (auto &name : _outputNodeNames) {
4141
delete[] name;
4242
}
43-
outputNodeNames.clear();
43+
_outputNodeNames.clear();
4444
}
4545
std::regex pattern("[\u4e00-\u9fa5]");
4646
bool result = std::regex_search(iParams.modelPath, pattern);
@@ -51,55 +51,55 @@ const char *SAM::CreateSession(SEG::DL_INIT_PARAM &iParams) {
5151
return Ret;
5252
}
5353
try {
54-
rectConfidenceThreshold = iParams.rectConfidenceThreshold;
55-
iouThreshold = iParams.iouThreshold;
56-
imgSize = iParams.imgSize;
57-
modelType = iParams.modelType;
58-
cudaEnable = iParams.cudaEnable;
59-
env = Ort::Env(ORT_LOGGING_LEVEL_WARNING, "Sam");
60-
Ort::SessionOptions sessionOption;
54+
_rectConfidenceThreshold = iParams.rectConfidenceThreshold;
55+
_iouThreshold = iParams.iouThreshold;
56+
_imgSize = iParams.imgSize;
57+
_modelType = iParams.modelType;
58+
_cudaEnable = iParams.cudaEnable;
59+
_env = Ort::Env(ORT_LOGGING_LEVEL_WARNING, "Sam");
60+
Ort::SessionOptions _sessionOption;
6161
if (iParams.cudaEnable) {
6262
OrtCUDAProviderOptions cudaOption;
6363
cudaOption.device_id = 0;
64-
sessionOption.AppendExecutionProvider_CUDA(cudaOption);
64+
_sessionOption.AppendExecutionProvider_CUDA(cudaOption);
6565
}
6666

67-
sessionOption.SetGraphOptimizationLevel(
67+
_sessionOption.SetGraphOptimizationLevel(
6868
GraphOptimizationLevel::ORT_ENABLE_ALL);
69-
sessionOption.SetIntraOpNumThreads(iParams.intraOpNumThreads);
70-
sessionOption.SetLogSeverityLevel(iParams.logSeverityLevel);
69+
_sessionOption.SetIntraOpNumThreads(iParams.intraOpNumThreads);
70+
_sessionOption.SetLogSeverityLevel(iParams.logSeverityLevel);
7171

7272
const char *modelPath = iParams.modelPath.c_str();
7373

74-
session = std::make_unique<Ort::Session>(env, modelPath, sessionOption);
74+
_session = std::make_unique<Ort::Session>(_env, modelPath, _sessionOption);
7575
Ort::AllocatorWithDefaultOptions allocator;
76-
size_t inputNodesNum = session->GetInputCount();
76+
size_t inputNodesNum = _session->GetInputCount();
7777
for (size_t i = 0; i < inputNodesNum; i++) {
7878
Ort::AllocatedStringPtr input_node_name =
79-
session->GetInputNameAllocated(i, allocator);
79+
_session->GetInputNameAllocated(i, allocator);
8080
char *temp_buf = new char[50];
8181
strcpy(temp_buf, input_node_name.get());
82-
inputNodeNames.push_back(temp_buf);
82+
_inputNodeNames.push_back(temp_buf);
8383
}
84-
size_t OutputNodesNum = session->GetOutputCount();
84+
size_t OutputNodesNum = _session->GetOutputCount();
8585
for (size_t i = 0; i < OutputNodesNum; i++) {
8686
Ort::AllocatedStringPtr output_node_name =
87-
session->GetOutputNameAllocated(i, allocator);
87+
_session->GetOutputNameAllocated(i, allocator);
8888
char *temp_buf = new char[50];
8989
strcpy(temp_buf, output_node_name.get());
90-
outputNodeNames.push_back(temp_buf);
90+
_outputNodeNames.push_back(temp_buf);
9191
}
92-
options = Ort::RunOptions{nullptr};
92+
_options = Ort::RunOptions{nullptr};
9393

9494
auto input_shape =
95-
session->GetInputTypeInfo(0).GetTensorTypeAndShapeInfo().GetShape();
95+
_session->GetInputTypeInfo(0).GetTensorTypeAndShapeInfo().GetShape();
9696
auto output_shape =
97-
session->GetOutputTypeInfo(0).GetTensorTypeAndShapeInfo().GetShape();
98-
auto output_type = session->GetOutputTypeInfo(0)
97+
_session->GetOutputTypeInfo(0).GetTensorTypeAndShapeInfo().GetShape();
98+
auto output_type = _session->GetOutputTypeInfo(0)
9999
.GetTensorTypeAndShapeInfo()
100100
.GetElementType();
101101

102-
WarmUpSession(modelType);
102+
WarmUpSession(_modelType);
103103
return RET_OK;
104104
} catch (const std::exception &e) {
105105
const char *str1 = "[SAM]:";
@@ -109,30 +109,30 @@ const char *SAM::CreateSession(SEG::DL_INIT_PARAM &iParams) {
109109
std::strcpy(merged, str_result.c_str());
110110
std::cout << merged << std::endl;
111111
delete[] merged;
112-
return "[SAM]:Create session failed.";
112+
return "[SAM]:Create _session failed.";
113113
}
114114
}
115115

116116
const char *SAM::RunSession(const cv::Mat &iImg,
117117
std::vector<SEG::DL_RESULT> &oResult,
118-
SEG::MODEL_TYPE modelType, SEG::DL_RESULT &result) {
118+
SEG::MODEL_TYPE _modelType, SEG::DL_RESULT &result) {
119119
#ifdef benchmark
120120
clock_t starttime_1 = clock();
121121
#endif // benchmark
122122
Utils utilities;
123123
const char *Ret = RET_OK;
124124
cv::Mat processedImg;
125-
utilities.PreProcess(iImg, imgSize, processedImg);
125+
utilities.PreProcess(iImg, _imgSize, processedImg);
126126
float *blob = new float[processedImg.total() * 3];
127127
utilities.BlobFromImage(processedImg, blob);
128128
std::vector<int64_t> inputNodeDims;
129-
if (modelType == SEG::SAM_SEGMENT_ENCODER) {
130-
inputNodeDims = {1, 3, imgSize.at(0), imgSize.at(1)};
131-
} else if (modelType == SEG::SAM_SEGMENT_DECODER) {
129+
if (_modelType == SEG::SAM_SEGMENT_ENCODER) {
130+
inputNodeDims = {1, 3, _imgSize.at(0), _imgSize.at(1)};
131+
} else if (_modelType == SEG::SAM_SEGMENT_DECODER) {
132132
// Input size or SAM decoder model is 256x64x64 for the decoder
133133
inputNodeDims = {1, 256, 64, 64};
134134
}
135-
TensorProcess(starttime_1, iImg, blob, inputNodeDims, modelType, oResult,
135+
TensorProcess(starttime_1, iImg, blob, inputNodeDims, _modelType, oResult,
136136
utilities, result);
137137

138138
return Ret;
@@ -141,26 +141,26 @@ const char *SAM::RunSession(const cv::Mat &iImg,
141141
template <typename N>
142142
const char *SAM::TensorProcess(clock_t &starttime_1, const cv::Mat &iImg,
143143
N &blob, std::vector<int64_t> &inputNodeDims,
144-
SEG::MODEL_TYPE modelType,
144+
SEG::MODEL_TYPE _modelType,
145145
std::vector<SEG::DL_RESULT> &oResult,
146146
Utils &utilities, SEG::DL_RESULT &result) {
147147

148-
switch (modelType) {
148+
switch (_modelType) {
149149
case SEG::SAM_SEGMENT_ENCODER:
150150
// case OTHER_SAM_MODEL:
151151
{
152152

153153
Ort::Value inputTensor =
154154
Ort::Value::CreateTensor<typename std::remove_pointer<N>::type>(
155155
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU),
156-
blob, 3 * imgSize.at(0) * imgSize.at(1), inputNodeDims.data(),
156+
blob, 3 * _imgSize.at(0) * _imgSize.at(1), inputNodeDims.data(),
157157
inputNodeDims.size());
158158
#ifdef benchmark
159159
clock_t starttime_2 = clock();
160160
#endif // benchmark
161161
auto outputTensor =
162-
session->Run(options, inputNodeNames.data(), &inputTensor, 1,
163-
outputNodeNames.data(), outputNodeNames.size());
162+
_session->Run(_options, _inputNodeNames.data(), &inputTensor, 1,
163+
_outputNodeNames.data(), _outputNodeNames.size());
164164
#ifdef benchmark
165165
clock_t starttime_3 = clock();
166166
#endif // benchmark
@@ -186,7 +186,7 @@ const char *SAM::TensorProcess(clock_t &starttime_1, const cv::Mat &iImg,
186186
(double)(starttime_3 - starttime_2) / CLOCKS_PER_SEC * 1000;
187187
double post_process_time =
188188
(double)(starttime_4 - starttime_3) / CLOCKS_PER_SEC * 1000;
189-
if (cudaEnable) {
189+
if (_cudaEnable) {
190190
std::cout << "[SAM(CUDA)]: " << pre_process_time << "ms pre-process, "
191191
<< process_time << "ms inference, " << post_process_time
192192
<< "ms post-process." << std::endl;
@@ -269,7 +269,7 @@ const char *SAM::TensorProcess(clock_t &starttime_1, const cv::Mat &iImg,
269269
std::vector<float> hasMaskInput = {0.0f}; // No mask provided
270270
std::vector<int64_t> hasMaskInputDims = {1};
271271

272-
utilities.ScaleBboxPoints(iImg, imgSize, pointCoords, pointCoordsScaled);
272+
utilities.ScaleBboxPoints(iImg, _imgSize, pointCoords, pointCoordsScaled);
273273

274274
std::vector<Ort::Value> inputTensors = utilities.PrepareInputTensor(
275275
decoderInputTensor, pointCoordsScaled, pointCoordsDims, pointLabels,
@@ -279,15 +279,15 @@ const char *SAM::TensorProcess(clock_t &starttime_1, const cv::Mat &iImg,
279279
#ifdef benchmark
280280
starttime_2 = clock();
281281
#endif // benchmark
282-
auto output_tensors = session->Run(
283-
options, inputNodeNames.data(), inputTensors.data(),
284-
inputTensors.size(), outputNodeNames.data(), outputNodeNames.size());
282+
auto output_tensors = _session->Run(
283+
_options, _inputNodeNames.data(), inputTensors.data(),
284+
inputTensors.size(), _outputNodeNames.data(), _outputNodeNames.size());
285285

286286
#ifdef benchmark
287287
starttime_3 = clock();
288288
#endif // benchmark
289289

290-
utilities.PostProcess(output_tensors, iImg, imgSize, result);
290+
utilities.PostProcess(output_tensors, iImg, _imgSize, result);
291291
}
292292
// Add the result to oResult
293293
oResult.push_back(result);
@@ -302,7 +302,7 @@ const char *SAM::TensorProcess(clock_t &starttime_1, const cv::Mat &iImg,
302302
(double)(starttime_3 - starttime_2) / CLOCKS_PER_SEC * 1000;
303303
double post_process_time =
304304
(double)(starttime_4 - starttime_3) / CLOCKS_PER_SEC * 1000;
305-
if (cudaEnable) {
305+
if (_cudaEnable) {
306306
std::cout << "[SAM(CUDA)]: " << pre_process_time << "ms pre-process, "
307307
<< process_time << "ms inference, " << post_process_time
308308
<< "ms post-process." << std::endl;
@@ -321,31 +321,31 @@ const char *SAM::TensorProcess(clock_t &starttime_1, const cv::Mat &iImg,
321321
return RET_OK;
322322
}
323323

324-
char *SAM::WarmUpSession(SEG::MODEL_TYPE modelType) {
324+
char *SAM::WarmUpSession(SEG::MODEL_TYPE _modelType) {
325325
clock_t starttime_1 = clock();
326326
Utils utilities;
327-
cv::Mat iImg = cv::Mat(cv::Size(imgSize.at(0), imgSize.at(1)), CV_8UC3);
327+
cv::Mat iImg = cv::Mat(cv::Size(_imgSize.at(0), _imgSize.at(1)), CV_8UC3);
328328
cv::Mat processedImg;
329-
utilities.PreProcess(iImg, imgSize, processedImg);
329+
utilities.PreProcess(iImg, _imgSize, processedImg);
330330

331331
float *blob = new float[iImg.total() * 3];
332332
utilities.BlobFromImage(processedImg, blob);
333-
std::vector<int64_t> SAM_input_node_dims = {1, 3, imgSize.at(0),
334-
imgSize.at(1)};
335-
switch (modelType) {
333+
std::vector<int64_t> SAM_input_node_dims = {1, 3, _imgSize.at(0),
334+
_imgSize.at(1)};
335+
switch (_modelType) {
336336
case SEG::SAM_SEGMENT_ENCODER: {
337337
Ort::Value input_tensor = Ort::Value::CreateTensor<float>(
338338
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU), blob,
339-
3 * imgSize.at(0) * imgSize.at(1), SAM_input_node_dims.data(),
339+
3 * _imgSize.at(0) * _imgSize.at(1), SAM_input_node_dims.data(),
340340
SAM_input_node_dims.size());
341341
auto output_tensors =
342-
session->Run(options, inputNodeNames.data(), &input_tensor, 1,
343-
outputNodeNames.data(), outputNodeNames.size());
342+
_session->Run(_options, _inputNodeNames.data(), &input_tensor, 1,
343+
_outputNodeNames.data(), _outputNodeNames.size());
344344
delete[] blob;
345345
clock_t starttime_4 = clock();
346346
double post_process_time =
347347
(double)(starttime_4 - starttime_1) / CLOCKS_PER_SEC * 1000;
348-
if (cudaEnable) {
348+
if (_cudaEnable) {
349349
std::cout << "[SAM(CUDA)]: " << "Cuda warm-up cost " << post_process_time
350350
<< " ms. " << std::endl;
351351
}
@@ -387,7 +387,7 @@ char *SAM::WarmUpSession(SEG::MODEL_TYPE modelType) {
387387

388388
std::vector<float> pointCoordsScaled;
389389

390-
utilities.ScaleBboxPoints(iImg, imgSize, pointCoords, pointCoordsScaled);
390+
utilities.ScaleBboxPoints(iImg, _imgSize, pointCoords, pointCoordsScaled);
391391

392392
// Labels for the points
393393
std::vector<float> pointLabels = {1.0f}; // All points are foreground
@@ -403,17 +403,17 @@ char *SAM::WarmUpSession(SEG::MODEL_TYPE modelType) {
403403
pointLabelsDims, maskInput, maskInputDims, hasMaskInput,
404404
hasMaskInputDims);
405405

406-
auto output_tensors = session->Run(
407-
options, inputNodeNames.data(), inputTensors.data(),
408-
inputTensors.size(), outputNodeNames.data(), outputNodeNames.size());
406+
auto output_tensors = _session->Run(
407+
_options, _inputNodeNames.data(), inputTensors.data(),
408+
inputTensors.size(), _outputNodeNames.data(), _outputNodeNames.size());
409409
}
410410

411-
outputNodeNames.size();
411+
_outputNodeNames.size();
412412
delete[] blob;
413413
clock_t starttime_4 = clock();
414414
double post_process_time =
415415
(double)(starttime_4 - starttime_1) / CLOCKS_PER_SEC * 1000;
416-
if (cudaEnable) {
416+
if (_cudaEnable) {
417417
std::cout << "[SAM(CUDA)]: " << "Cuda warm-up cost " << post_process_time
418418
<< " ms. " << std::endl;
419419
}

src/utils.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ char *Utils::PreProcess(const cv::Mat &iImg, std::vector<int> iImgSize, cv::Mat
2626

2727
if (iImg.cols >= iImg.rows)
2828
{
29-
resizeScales = iImg.cols / (float)iImgSize.at(0);
30-
cv::resize(oImg, oImg, cv::Size(iImgSize.at(0), int(iImg.rows / resizeScales)));
29+
_resizeScales = iImg.cols / (float)iImgSize.at(0);
30+
cv::resize(oImg, oImg, cv::Size(iImgSize.at(0), int(iImg.rows / _resizeScales)));
3131
}
3232
else
3333
{
34-
resizeScales = iImg.rows / (float)iImgSize.at(1);
35-
cv::resize(oImg, oImg, cv::Size(int(iImg.cols / resizeScales), iImgSize.at(1)));
34+
_resizeScales = iImg.rows / (float)iImgSize.at(1);
35+
cv::resize(oImg, oImg, cv::Size(int(iImg.cols / _resizeScales), iImgSize.at(1)));
3636
}
3737
//cv::Mat tempImg = cv::Mat::zeros(iImgSize.at(0), iImgSize.at(1), CV_8UC3);
3838
cv::Mat tempImg = cv::Mat::zeros(iImgSize.at(1), iImgSize.at(0), CV_8UC3);
@@ -52,12 +52,12 @@ void Utils::ScaleBboxPoints(const cv::Mat &iImg, std::vector<int> imgSize, std::
5252
if (iImg.cols >= iImg.rows)
5353
{
5454
scale = imgSize[0] / (float)iImg.cols;
55-
resizeScalesBbox = iImg.cols / (float)imgSize[0];
55+
_resizeScalesBbox = iImg.cols / (float)imgSize[0];
5656
}
5757
else
5858
{
5959
scale = imgSize[1] / (float)iImg.rows;
60-
resizeScalesBbox = iImg.rows / (float)imgSize[1];
60+
_resizeScalesBbox = iImg.rows / (float)imgSize[1];
6161
}
6262

6363
// Top-Left placement (matching PreProcess)

0 commit comments

Comments
 (0)