Commit 40dd18f7 authored by liucong's avatar liucong
Browse files

修改部分代码和文档

parent bd09ccc2
import numpy as np import numpy as np
from PIL import Image
import cv2 import cv2
import migraphx import migraphx
def Preprocess(pil_img, newW, newH): def Preprocessing(pil_img, newW, newH):
assert newW > 0 and newH > 0, 'Scale is too small' assert newW > 0 and newH > 0, 'Scale is too small'
pil_img = pil_img.resize((newW, newH)) img_nd = cv2.resize(pil_img, (newW, newH)) # 将图像尺寸修改为256x256
img_nd = np.array(pil_img) img_nd = cv2.cvtColor(img_nd, cv2.COLOR_BGR2RGB) # BGR转换为RGB
if len(img_nd.shape) == 2: if len(img_nd.shape) == 2:
img_nd = np.expand_dims(img_nd, axis=2) img_nd = np.expand_dims(img_nd, axis=2)
# HWC to CHW img_trans = img_nd.transpose((2, 0, 1)) # HWC转换为CHW
img_print = pil_img if img_trans.max() > 1: # 保证数据处于0-1之间的浮点数
img_trans = img_nd.transpose((2, 0, 1)) img_trans = img_trans / 255.0
if img_trans.max() > 1: img_trans = np.expand_dims(img_trans, 0) # CHW扩展为NCHW
img_trans = img_trans / 255 img = img_trans.astype(np.float32) # 转换成浮点型数据
img_trans = np.expand_dims(img_trans, 0) return img
return img_trans, img_print
def Sigmoid(x): def Sigmoid(x):
return 1 / (1 + np.exp(-x)) return 1 / (1 + np.exp(-x))
...@@ -25,7 +23,7 @@ def Sigmoid(x): ...@@ -25,7 +23,7 @@ def Sigmoid(x):
if __name__ == '__main__': if __name__ == '__main__':
# 加载模型 # 加载模型
model = migraphx.parse_onnx("./model/unet_13_256.onnx") model = migraphx.parse_onnx("../../Resource/Models/Segmentation/unet_13_256.onnx")
inputName = model.get_parameter_names() inputName = model.get_parameter_names()
inputShape = model.get_parameter_shapes() inputShape = model.get_parameter_shapes()
print("inputName:{0} \ninputShape:{1}".format(inputName, inputShape)) print("inputName:{0} \ninputShape:{1}".format(inputName, inputShape))
...@@ -34,10 +32,8 @@ if __name__ == '__main__': ...@@ -34,10 +32,8 @@ if __name__ == '__main__':
model.compile(migraphx.get_target("gpu"), device_id=0) # device_id: 设置GPU设备,默认为0号设备 model.compile(migraphx.get_target("gpu"), device_id=0) # device_id: 设置GPU设备,默认为0号设备
# 图像预处理 # 图像预处理
img = Image.open("./car.jpeg") img = cv2.imread("../../Resource/Images/car1.jpeg")
img, imPrint = Preprocess(img, 256, 256) input_img = Preprocessing(img, 256, 256)
input_img = np.zeros((1,3,256,256),dtype='float32')
np.lib.stride_tricks.as_strided(input_img, shape=img.shape, strides=input_img.strides)[:] = img
# 模型推理 # 模型推理
mask = model.run({'inputs':input_img}) mask = model.run({'inputs':input_img})
......
opencv-python opencv-python
numpy numpy
pillow \ No newline at end of file
\ No newline at end of file
...@@ -86,7 +86,7 @@ python Unet.py ...@@ -86,7 +86,7 @@ python Unet.py
输出结果为: 输出结果为:
<img src="C:/Users/柳聪/Desktop/ni/unet_migraphx/Doc/Images/Unet_03.jpg" style="zoom:100%;" align=middle> <img src="./Doc/Images/Unet_03.jpg" style="zoom:100%;" align=middle>
### C++版本推理 ### C++版本推理
...@@ -105,7 +105,7 @@ cd ./build/ ...@@ -105,7 +105,7 @@ cd ./build/
会在当前目录中生成分割结果图像Result.jpg 会在当前目录中生成分割结果图像Result.jpg
<img src="C:/Users/柳聪/Desktop/ni/unet_migraphx/Doc/Images/Unet_02.jpg" style="zoom:100%;" align=middle> <img src="./Doc/Images/Unet_02.jpg" style="zoom:100%;" align=middle>
## 历史版本 ## 历史版本
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment