Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_migraphx
Commits
11760f5c
Commit
11760f5c
authored
Nov 15, 2023
by
liucong
Browse files
修改resnet50工程格式
parent
1c515e55
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
75 additions
and
138 deletions
+75
-138
Doc/Tutorial_Cpp.md
Doc/Tutorial_Cpp.md
+3
-5
Doc/Tutorial_Python.md
Doc/Tutorial_Python.md
+9
-9
Python/Classifier.py
Python/Classifier.py
+7
-15
Python/Classifier_OffloadFalse.py
Python/Classifier_OffloadFalse.py
+9
-17
Src/Classifier.cpp
Src/Classifier.cpp
+47
-92
No files found.
Doc/Tutorial_Cpp.md
View file @
11760f5c
...
@@ -29,7 +29,7 @@
...
@@ -29,7 +29,7 @@
本示例代码主要采用了OpenCV实现了预处理操作:
本示例代码主要采用了OpenCV实现了预处理操作:
```
c++
```
c++
ErrorCode
Classifier
::
Classify
(
const
std
::
vector
<
cv
::
Mat
>
&
srcImages
,
std
::
vector
<
std
::
vector
<
ResultOfPrediction
>>
&
predictions
)
cv
::
Mat
Classifier
::
Preprocess
(
const
std
::
vector
<
cv
::
Mat
>
&
srcImages
)
{
{
...
...
...
@@ -142,8 +142,7 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
...
@@ -142,8 +142,7 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
inputData
[
inputName
]
=
migraphx
::
argument
{
inputShape
,
(
float
*
)
inputBlob
.
data
};
inputData
[
inputName
]
=
migraphx
::
argument
{
inputShape
,
(
float
*
)
inputBlob
.
data
};
// 推理
// 推理
std
::
vector
<
std
::
string
>
outputNames
=
{
"resnetv24_dense0_fwd"
};
// 设置返回的输出节点
std
::
vector
<
migraphx
::
argument
>
results
=
net
.
eval
(
inputData
);
std
::
vector
<
migraphx
::
argument
>
results
=
net
.
eval
(
inputData
,
outputNames
);
// 获取输出节点的属性
// 获取输出节点的属性
migraphx
::
argument
result
=
results
[
0
];
// 获取第一个输出节点的数据
migraphx
::
argument
result
=
results
[
0
];
// 获取第一个输出节点的数据
...
@@ -163,8 +162,7 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
...
@@ -163,8 +162,7 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
hipMemcpy
(
inputBuffer_Device
,
inputData
.
data
(),
inputShape
.
bytes
(),
hipMemcpyHostToDevice
);
hipMemcpy
(
inputBuffer_Device
,
inputData
.
data
(),
inputShape
.
bytes
(),
hipMemcpyHostToDevice
);
// 推理
// 推理
std
::
vector
<
std
::
string
>
outputNames
=
{
"resnetv24_dense0_fwd"
};
// 设置返回的输出节点
std
::
vector
<
migraphx
::
argument
>
results
=
net
.
eval
(
programParameters
);
std
::
vector
<
migraphx
::
argument
>
results
=
net
.
eval
(
programParameters
,
outputNames
);
// 获取输出节点的属性
// 获取输出节点的属性
migraphx
::
argument
result
=
results
[
0
];
// 获取第一个输出节点的数据
migraphx
::
argument
result
=
results
[
0
];
// 获取第一个输出节点的数据
...
...
Doc/Tutorial_Python.md
View file @
11760f5c
...
@@ -120,10 +120,10 @@ if __name__ == '__main__':
...
@@ -120,10 +120,10 @@ if __name__ == '__main__':
results
=
model
.
run
({
inputName
:
image
})
# 推理结果,list类型
results
=
model
.
run
({
inputName
:
image
})
# 推理结果,list类型
# 获取输出节点属性
# 获取输出节点属性
result
=
results
[
0
]
# 获取第一个输出节点的数据,migraphx.argument类型
result
=
results
[
0
]
# 获取第一个输出节点的数据,migraphx.argument类型
outputShape
=
result
.
get_shape
()
# 输出节点的shape,migraphx.shape类型
outputShape
=
result
.
get_shape
()
# 输出节点的shape,migraphx.shape类型
outputSize
=
outputShape
.
lens
()
# 每一维大小,维度顺序为(N,C,H,W),list类型
outputSize
=
outputShape
.
lens
()
# 每一维大小,维度顺序为(N,C,H,W),list类型
numberOfOutput
=
outputShape
.
elements
()
# 输出节点元素的个数
numberOfOutput
=
outputShape
.
elements
()
# 输出节点元素的个数
# 获取分类结果
# 获取分类结果
print
(
np
.
array
(
result
))
print
(
np
.
array
(
result
))
...
@@ -147,16 +147,16 @@ if __name__ == '__main__':
...
@@ -147,16 +147,16 @@ if __name__ == '__main__':
# 预处理并转换为NCHW
# 预处理并转换为NCHW
pathOfImage
=
"../Resource/Images/ImageNet_01.jpg"
pathOfImage
=
"../Resource/Images/ImageNet_01.jpg"
image
=
Preprocessing
(
pathOfImage
)
image
=
Preprocessing
(
pathOfImage
)
modelData
[
inputName
]
=
migraphx
.
to_gpu
(
migraphx
.
argument
(
image
))
modelData
[
inputName
]
=
migraphx
.
to_gpu
(
migraphx
.
argument
(
image
))
# 推理
# 推理
results
=
model
.
run
(
modelData
)
results
=
model
.
run
(
modelData
)
# 获取输出节点属性
# 获取输出节点属性
result
=
migraphx
.
from_gpu
(
results
[
0
])
# 获取第一个输出节点的数据,migraphx.argument类型
result
=
migraphx
.
from_gpu
(
results
[
0
])
# 获取第一个输出节点的数据,migraphx.argument类型
outputShape
=
result
.
get_shape
()
# 输出节点的shape,migraphx.shape类型
outputShape
=
result
.
get_shape
()
# 输出节点的shape,migraphx.shape类型
outputSize
=
outputShape
.
lens
()
# 每一维大小,维度顺序为(N,C,H,W),list类型
outputSize
=
outputShape
.
lens
()
# 每一维大小,维度顺序为(N,C,H,W),list类型
numberOfOutput
=
outputShape
.
elements
()
# 输出节点元素的个数
numberOfOutput
=
outputShape
.
elements
()
# 输出节点元素的个数
# 获取分类结果
# 获取分类结果
print
(
np
.
array
(
result
))
print
(
np
.
array
(
result
))
...
...
Python/Classifier.py
View file @
11760f5c
...
@@ -54,18 +54,10 @@ if __name__ == '__main__':
...
@@ -54,18 +54,10 @@ if __name__ == '__main__':
model
=
migraphx
.
parse_onnx
(
"../Resource/Models/resnet50-v2-7.onnx"
)
model
=
migraphx
.
parse_onnx
(
"../Resource/Models/resnet50-v2-7.onnx"
)
# 获取模型输入/输出节点信息
# 获取模型输入/输出节点信息
print
(
"inputs:"
)
inputs
=
model
.
get_inputs
()
inputs
=
model
.
get_inputs
()
for
key
,
value
in
inputs
.
items
():
print
(
"{}:{}"
.
format
(
key
,
value
))
print
(
"outputs:"
)
outputs
=
model
.
get_outputs
()
outputs
=
model
.
get_outputs
()
for
key
,
value
in
outputs
.
items
():
inputName
=
model
.
get_parameter_names
()[
0
]
print
(
"{}:{}"
.
format
(
key
,
value
))
inputShape
=
inputs
[
inputName
].
lens
()
inputName
=
"data"
inputShape
=
inputs
[
inputName
].
lens
()
# INT8量化
# INT8量化
if
use_int8
:
if
use_int8
:
...
@@ -83,17 +75,17 @@ if __name__ == '__main__':
...
@@ -83,17 +75,17 @@ if __name__ == '__main__':
model
.
compile
(
t
=
migraphx
.
get_target
(
"gpu"
),
device_id
=
0
)
# device_id: 设置GPU设备,默认为0号设备
model
.
compile
(
t
=
migraphx
.
get_target
(
"gpu"
),
device_id
=
0
)
# device_id: 设置GPU设备,默认为0号设备
# 预处理并转换为NCHW
# 预处理并转换为NCHW
pathOfImage
=
"../Resource/Images/ImageNet_01.jpg"
pathOfImage
=
"../Resource/Images/ImageNet_01.jpg"
image
=
Preprocessing
(
pathOfImage
)
image
=
Preprocessing
(
pathOfImage
)
# 推理
# 推理
results
=
model
.
run
({
inputName
:
image
})
# 推理结果,list类型
results
=
model
.
run
({
inputName
:
image
})
# 推理结果,list类型
# 获取输出节点属性
# 获取输出节点属性
result
=
results
[
0
]
# 获取第一个输出节点的数据,migraphx.argument类型
result
=
results
[
0
]
# 获取第一个输出节点的数据,migraphx.argument类型
outputShape
=
result
.
get_shape
()
# 输出节点的shape,migraphx.shape类型
outputShape
=
result
.
get_shape
()
# 输出节点的shape,migraphx.shape类型
outputSize
=
outputShape
.
lens
()
# 每一维大小,维度顺序为(N,C,H,W),list类型
outputSize
=
outputShape
.
lens
()
# 每一维大小,维度顺序为(N,C,H,W),list类型
numberOfOutput
=
outputShape
.
elements
()
# 输出节点元素的个数
numberOfOutput
=
outputShape
.
elements
()
# 输出节点元素的个数
# 获取分类结果
# 获取分类结果
print
(
np
.
array
(
result
))
print
(
np
.
array
(
result
))
\ No newline at end of file
Python/Classifier_OffloadFalse.py
View file @
11760f5c
...
@@ -9,7 +9,7 @@ import numpy as np
...
@@ -9,7 +9,7 @@ import numpy as np
import
migraphx
import
migraphx
def
AllocateOutputMemory
(
model
):
def
AllocateOutputMemory
(
model
):
outputData
=
{}
outputData
=
{}
for
key
in
model
.
get_outputs
().
keys
():
for
key
in
model
.
get_outputs
().
keys
():
outputData
[
key
]
=
migraphx
.
allocate_gpu
(
s
=
model
.
get_outputs
()[
key
])
outputData
[
key
]
=
migraphx
.
allocate_gpu
(
s
=
model
.
get_outputs
()[
key
])
...
@@ -61,18 +61,10 @@ if __name__ == '__main__':
...
@@ -61,18 +61,10 @@ if __name__ == '__main__':
model
=
migraphx
.
parse_onnx
(
"../Resource/Models/resnet50-v2-7.onnx"
)
model
=
migraphx
.
parse_onnx
(
"../Resource/Models/resnet50-v2-7.onnx"
)
# 获取模型输入/输出节点信息
# 获取模型输入/输出节点信息
print
(
"inputs:"
)
inputs
=
model
.
get_inputs
()
inputs
=
model
.
get_inputs
()
for
key
,
value
in
inputs
.
items
():
print
(
"{}:{}"
.
format
(
key
,
value
))
print
(
"outputs:"
)
outputs
=
model
.
get_outputs
()
outputs
=
model
.
get_outputs
()
for
key
,
value
in
outputs
.
items
():
print
(
"{}:{}"
.
format
(
key
,
value
))
inputName
=
model
.
get_parameter_names
()[
0
]
inputName
=
model
.
get_parameter_names
()[
0
]
inputShape
=
inputs
[
inputName
].
lens
()
inputShape
=
inputs
[
inputName
].
lens
()
# INT8量化
# INT8量化
if
use_int8
:
if
use_int8
:
...
@@ -90,22 +82,22 @@ if __name__ == '__main__':
...
@@ -90,22 +82,22 @@ if __name__ == '__main__':
model
.
compile
(
t
=
migraphx
.
get_target
(
"gpu"
),
offload_copy
=
False
,
device_id
=
0
)
# device_id: 设置GPU设备,默认为0号设备
model
.
compile
(
t
=
migraphx
.
get_target
(
"gpu"
),
offload_copy
=
False
,
device_id
=
0
)
# device_id: 设置GPU设备,默认为0号设备
# 为输出节点分配device内存,用于保存输出数据
# 为输出节点分配device内存,用于保存输出数据
modelData
=
AllocateOutputMemory
(
model
)
modelData
=
AllocateOutputMemory
(
model
)
# 预处理并转换为NCHW
# 预处理并转换为NCHW
pathOfImage
=
"../Resource/Images/ImageNet_01.jpg"
pathOfImage
=
"../Resource/Images/ImageNet_01.jpg"
image
=
Preprocessing
(
pathOfImage
)
image
=
Preprocessing
(
pathOfImage
)
modelData
[
inputName
]
=
migraphx
.
to_gpu
(
migraphx
.
argument
(
image
))
modelData
[
inputName
]
=
migraphx
.
to_gpu
(
migraphx
.
argument
(
image
))
# 推理
# 推理
results
=
model
.
run
(
modelData
)
results
=
model
.
run
(
modelData
)
# 获取输出节点属性
# 获取输出节点属性
result
=
migraphx
.
from_gpu
(
results
[
0
])
# 获取第一个输出节点的数据,migraphx.argument类型
result
=
migraphx
.
from_gpu
(
results
[
0
])
# 获取第一个输出节点的数据,migraphx.argument类型
outputShape
=
result
.
get_shape
()
# 输出节点的shape,migraphx.shape类型
outputShape
=
result
.
get_shape
()
# 输出节点的shape,migraphx.shape类型
outputSize
=
outputShape
.
lens
()
# 每一维大小,维度顺序为(N,C,H,W),list类型
outputSize
=
outputShape
.
lens
()
# 每一维大小,维度顺序为(N,C,H,W),list类型
numberOfOutput
=
outputShape
.
elements
()
# 输出节点元素的个数
numberOfOutput
=
outputShape
.
elements
()
# 输出节点元素的个数
# 获取分类结果
# 获取分类结果
print
(
np
.
array
(
result
))
print
(
np
.
array
(
result
))
\ No newline at end of file
Src/Classifier.cpp
View file @
11760f5c
...
@@ -14,6 +14,7 @@ namespace migraphxSamples
...
@@ -14,6 +14,7 @@ namespace migraphxSamples
Classifier
::
Classifier
()
Classifier
::
Classifier
()
{
{
}
}
Classifier
::~
Classifier
()
Classifier
::~
Classifier
()
...
@@ -21,11 +22,51 @@ Classifier::~Classifier()
...
@@ -21,11 +22,51 @@ Classifier::~Classifier()
configurationFile
.
release
();
configurationFile
.
release
();
}
}
cv
::
Mat
Classifier
::
Preprocess
(
const
std
::
vector
<
cv
::
Mat
>
&
srcImages
)
{
// 数据预处理
std
::
vector
<
cv
::
Mat
>
image
;
for
(
int
i
=
0
;
i
<
srcImages
.
size
();
++
i
)
{
//BGR转换为RGB
cv
::
Mat
imgRGB
;
cv
::
cvtColor
(
srcImages
[
i
],
imgRGB
,
cv
::
COLOR_BGR2RGB
);
// 调整大小,使短边为256,保持长宽比
cv
::
Mat
shrink
;
float
ratio
=
(
float
)
256
/
min
(
imgRGB
.
cols
,
imgRGB
.
rows
);
if
(
imgRGB
.
rows
>
imgRGB
.
cols
)
{
cv
::
resize
(
imgRGB
,
shrink
,
cv
::
Size
(
256
,
int
(
ratio
*
imgRGB
.
rows
)),
0
,
0
);
}
else
{
cv
::
resize
(
imgRGB
,
shrink
,
cv
::
Size
(
int
(
ratio
*
imgRGB
.
cols
),
256
),
0
,
0
);
}
// 裁剪中心窗口为224*224
int
start_x
=
shrink
.
cols
/
2
-
224
/
2
;
int
start_y
=
shrink
.
rows
/
2
-
224
/
2
;
cv
::
Rect
rect
(
start_x
,
start_y
,
224
,
224
);
cv
::
Mat
images
=
shrink
(
rect
);
image
.
push_back
(
images
);
}
// normalize并转换为NCHW
cv
::
Mat
inputBlob
;
Image2BlobParams
image2BlobParams
;
image2BlobParams
.
scalefactor
=
cv
::
Scalar
(
1
/
58.395
,
1
/
57.12
,
1
/
57.375
);
image2BlobParams
.
mean
=
cv
::
Scalar
(
123.675
,
116.28
,
103.53
);
image2BlobParams
.
swapRB
=
false
;
blobFromImagesWithParams
(
image
,
inputBlob
,
image2BlobParams
);
return
inputBlob
;
}
ErrorCode
Classifier
::
Initialize
(
InitializationParameterOfClassifier
initializationParameterOfClassifier
)
ErrorCode
Classifier
::
Initialize
(
InitializationParameterOfClassifier
initializationParameterOfClassifier
)
{
{
// 读取配置文件
// 读取配置文件
std
::
string
configFilePath
=
initializationParameterOfClassifier
.
configFilePath
;
std
::
string
configFilePath
=
initializationParameterOfClassifier
.
configFilePath
;
if
(
Exists
(
configFilePath
)
==
false
)
if
(
!
Exists
(
configFilePath
))
{
{
LOG_ERROR
(
stdout
,
"no configuration file!
\n
"
);
LOG_ERROR
(
stdout
,
"no configuration file!
\n
"
);
return
CONFIG_FILE_NOT_EXIST
;
return
CONFIG_FILE_NOT_EXIST
;
...
@@ -45,7 +86,7 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat
...
@@ -45,7 +86,7 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat
useoffloadcopy
=
(
bool
)(
int
)
netNode
[
"Useoffloadcopy"
];
useoffloadcopy
=
(
bool
)(
int
)
netNode
[
"Useoffloadcopy"
];
// 加载模型
// 加载模型
if
(
Exists
(
modelPath
)
==
false
)
if
(
!
Exists
(
modelPath
))
{
{
LOG_ERROR
(
stdout
,
"%s not exist!
\n
"
,
modelPath
.
c_str
());
LOG_ERROR
(
stdout
,
"%s not exist!
\n
"
,
modelPath
.
c_str
());
return
MODEL_NOT_EXIST
;
return
MODEL_NOT_EXIST
;
...
@@ -54,18 +95,8 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat
...
@@ -54,18 +95,8 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat
LOG_INFO
(
stdout
,
"succeed to load model: %s
\n
"
,
GetFileName
(
modelPath
).
c_str
());
LOG_INFO
(
stdout
,
"succeed to load model: %s
\n
"
,
GetFileName
(
modelPath
).
c_str
());
// 获取模型输入/输出节点信息
// 获取模型输入/输出节点信息
std
::
cout
<<
"inputs:"
<<
std
::
endl
;
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
inputs
=
net
.
get_inputs
();
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
inputs
=
net
.
get_inputs
();
for
(
auto
i
:
inputs
)
{
std
::
cout
<<
i
.
first
<<
":"
<<
i
.
second
<<
std
::
endl
;
}
std
::
cout
<<
"outputs:"
<<
std
::
endl
;
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
outputs
=
net
.
get_outputs
();
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
outputs
=
net
.
get_outputs
();
for
(
auto
i
:
outputs
)
{
std
::
cout
<<
i
.
first
<<
":"
<<
i
.
second
<<
std
::
endl
;
}
inputName
=
inputs
.
begin
()
->
first
;
inputName
=
inputs
.
begin
()
->
first
;
inputShape
=
inputs
.
begin
()
->
second
;
inputShape
=
inputs
.
begin
()
->
second
;
outputName
=
outputs
.
begin
()
->
first
;
outputName
=
outputs
.
begin
()
->
first
;
...
@@ -91,41 +122,7 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat
...
@@ -91,41 +122,7 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat
}
}
// 数据预处理
// 数据预处理
std
::
vector
<
cv
::
Mat
>
image
;
cv
::
Mat
inputBlob
=
Preprocess
(
srcImages
);
for
(
int
i
=
0
;
i
<
srcImages
.
size
();
++
i
)
{
//BGR转换为RGB
cv
::
Mat
imgRGB
;
cv
::
cvtColor
(
srcImages
[
i
],
imgRGB
,
cv
::
COLOR_BGR2RGB
);
// 调整大小,使短边为256,保持长宽比
cv
::
Mat
shrink
;
float
ratio
=
(
float
)
256
/
min
(
imgRGB
.
cols
,
imgRGB
.
rows
);
if
(
imgRGB
.
rows
>
imgRGB
.
cols
)
{
cv
::
resize
(
imgRGB
,
shrink
,
cv
::
Size
(
256
,
int
(
ratio
*
imgRGB
.
rows
)),
0
,
0
);
}
else
{
cv
::
resize
(
imgRGB
,
shrink
,
cv
::
Size
(
int
(
ratio
*
imgRGB
.
cols
),
256
),
0
,
0
);
}
// 裁剪中心窗口为224*224
int
start_x
=
shrink
.
cols
/
2
-
224
/
2
;
int
start_y
=
shrink
.
rows
/
2
-
224
/
2
;
cv
::
Rect
rect
(
start_x
,
start_y
,
224
,
224
);
cv
::
Mat
images
=
shrink
(
rect
);
image
.
push_back
(
images
);
}
// normalize并转换为NCHW
cv
::
Mat
inputBlob
;
Image2BlobParams
image2BlobParams
;
image2BlobParams
.
scalefactor
=
cv
::
Scalar
(
1
/
58.395
,
1
/
57.12
,
1
/
57.375
);
image2BlobParams
.
mean
=
cv
::
Scalar
(
123.675
,
116.28
,
103.53
);
image2BlobParams
.
swapRB
=
false
;
blobFromImagesWithParams
(
image
,
inputBlob
,
image2BlobParams
);
std
::
unordered_map
<
std
::
string
,
migraphx
::
argument
>
inputData
;
std
::
unordered_map
<
std
::
string
,
migraphx
::
argument
>
inputData
;
inputData
[
inputName
]
=
migraphx
::
argument
{
inputShape
,
(
float
*
)
inputBlob
.
data
};
inputData
[
inputName
]
=
migraphx
::
argument
{
inputShape
,
(
float
*
)
inputBlob
.
data
};
std
::
vector
<
std
::
unordered_map
<
std
::
string
,
migraphx
::
argument
>>
calibrationData
=
{
inputData
};
std
::
vector
<
std
::
unordered_map
<
std
::
string
,
migraphx
::
argument
>>
calibrationData
=
{
inputData
};
...
@@ -190,47 +187,6 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat
...
@@ -190,47 +187,6 @@ ErrorCode Classifier::Initialize(InitializationParameterOfClassifier initializat
LOG_INFO
(
stdout
,
"Useoffloadcopy:%d
\n
"
,(
int
)
useoffloadcopy
);
LOG_INFO
(
stdout
,
"Useoffloadcopy:%d
\n
"
,(
int
)
useoffloadcopy
);
return
SUCCESS
;
return
SUCCESS
;
}
cv
::
Mat
Classifier
::
Preprocess
(
const
std
::
vector
<
cv
::
Mat
>
&
srcImages
)
{
// 数据预处理
std
::
vector
<
cv
::
Mat
>
image
;
for
(
int
i
=
0
;
i
<
srcImages
.
size
();
++
i
)
{
//BGR转换为RGB
cv
::
Mat
imgRGB
;
cv
::
cvtColor
(
srcImages
[
i
],
imgRGB
,
cv
::
COLOR_BGR2RGB
);
// 调整大小,使短边为256,保持长宽比
cv
::
Mat
shrink
;
float
ratio
=
(
float
)
256
/
min
(
imgRGB
.
cols
,
imgRGB
.
rows
);
if
(
imgRGB
.
rows
>
imgRGB
.
cols
)
{
cv
::
resize
(
imgRGB
,
shrink
,
cv
::
Size
(
256
,
int
(
ratio
*
imgRGB
.
rows
)),
0
,
0
);
}
else
{
cv
::
resize
(
imgRGB
,
shrink
,
cv
::
Size
(
int
(
ratio
*
imgRGB
.
cols
),
256
),
0
,
0
);
}
// 裁剪中心窗口为224*224
int
start_x
=
shrink
.
cols
/
2
-
224
/
2
;
int
start_y
=
shrink
.
rows
/
2
-
224
/
2
;
cv
::
Rect
rect
(
start_x
,
start_y
,
224
,
224
);
cv
::
Mat
images
=
shrink
(
rect
);
image
.
push_back
(
images
);
}
// normalize并转换为NCHW
cv
::
Mat
inputBlob
;
Image2BlobParams
image2BlobParams
;
image2BlobParams
.
scalefactor
=
cv
::
Scalar
(
1
/
58.395
,
1
/
57.12
,
1
/
57.375
);
image2BlobParams
.
mean
=
cv
::
Scalar
(
123.675
,
116.28
,
103.53
);
image2BlobParams
.
swapRB
=
false
;
blobFromImagesWithParams
(
image
,
inputBlob
,
image2BlobParams
);
return
inputBlob
;
}
}
ErrorCode
Classifier
::
Classify
(
const
std
::
vector
<
cv
::
Mat
>
&
srcImages
,
std
::
vector
<
std
::
vector
<
ResultOfPrediction
>>
&
predictions
)
ErrorCode
Classifier
::
Classify
(
const
std
::
vector
<
cv
::
Mat
>
&
srcImages
,
std
::
vector
<
std
::
vector
<
ResultOfPrediction
>>
&
predictions
)
...
@@ -241,6 +197,7 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
...
@@ -241,6 +197,7 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
return
IMAGE_ERROR
;
return
IMAGE_ERROR
;
}
}
// 数据预处理
cv
::
Mat
inputBlob
=
Preprocess
(
srcImages
);
cv
::
Mat
inputBlob
=
Preprocess
(
srcImages
);
// 当offload为true时,不需要内存拷贝
// 当offload为true时,不需要内存拷贝
...
@@ -250,8 +207,7 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
...
@@ -250,8 +207,7 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
inputData
[
inputName
]
=
migraphx
::
argument
{
inputShape
,
(
float
*
)
inputBlob
.
data
};
inputData
[
inputName
]
=
migraphx
::
argument
{
inputShape
,
(
float
*
)
inputBlob
.
data
};
// 推理
// 推理
std
::
vector
<
std
::
string
>
outputNames
=
{
"resnetv24_dense0_fwd"
};
// 设置返回的输出节点
std
::
vector
<
migraphx
::
argument
>
results
=
net
.
eval
(
inputData
);
std
::
vector
<
migraphx
::
argument
>
results
=
net
.
eval
(
inputData
,
outputNames
);
// 获取输出节点的属性
// 获取输出节点的属性
migraphx
::
argument
result
=
results
[
0
];
// 获取第一个输出节点的数据
migraphx
::
argument
result
=
results
[
0
];
// 获取第一个输出节点的数据
...
@@ -294,8 +250,7 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
...
@@ -294,8 +250,7 @@ ErrorCode Classifier::Classify(const std::vector<cv::Mat> &srcImages,std::vector
hipMemcpy
(
inputBuffer_Device
,
inputData
.
data
(),
inputShape
.
bytes
(),
hipMemcpyHostToDevice
);
hipMemcpy
(
inputBuffer_Device
,
inputData
.
data
(),
inputShape
.
bytes
(),
hipMemcpyHostToDevice
);
// 推理
// 推理
std
::
vector
<
std
::
string
>
outputNames
=
{
"resnetv24_dense0_fwd"
};
// 设置返回的输出节点
std
::
vector
<
migraphx
::
argument
>
results
=
net
.
eval
(
programParameters
);
std
::
vector
<
migraphx
::
argument
>
results
=
net
.
eval
(
programParameters
,
outputNames
);
// 获取输出节点的属性
// 获取输出节点的属性
migraphx
::
argument
result
=
results
[
0
];
// 获取第一个输出节点的数据
migraphx
::
argument
result
=
results
[
0
];
// 获取第一个输出节点的数据
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment