Commit 76d4e7b9 authored by liucong's avatar liucong
Browse files

修改python代码中的预处理过程

parent 0b73611d
......@@ -14,23 +14,27 @@
​ 1.尺度变换,将图像resize到256x256大小
​ 2.归一化,将数据归一化到[0.0, 1.0]之间
​ 2.数据排布,将数据从HWC转换为CHW,再将维度转换为NCHW
​ 3.数据排布,将数据从HWC转换为CHW,再将维度转换为NCHW
​ 3.归一化,将数据归一化到[0.0, 1.0]之间
本示例代码通过如下方式实现预处理操作:
```python
def Preprocessing(pil_img, newW, newH):
assert newW > 0 and newH > 0, 'Scale is too small'
img_nd = cv2.resize(pil_img, (newW, newH)) # 将图像尺寸修改为256x256
img_nd = cv2.cvtColor(img_nd, cv2.COLOR_BGR2RGB) # BGR转换为RGB
img_trans = img_nd.transpose((2, 0, 1)) # HWC转换为CHW
if img_trans.max() > 1: # 保证数据处于0-1之间的浮点数
img_trans = img_trans / 255.0
img_trans = np.expand_dims(img_trans, 0) # CHW转换为NCHW
img = img_trans.astype(np.float32) # 转换成浮点型数据
img_nd = cv2.cvtColor(pil_img, cv2.COLOR_BGR2RGB) # BGR转换为RGB
img_nd = cv2.resize(img_nd, (newW, newH)) # 将图像尺寸修改为256x256
if len(img_nd.shape) == 2:
img_nd = np.expand_dims(img_nd, axis=2)
img_trans = img_nd.transpose((2, 0, 1)) # HWC转换为CHW
img_trans = np.expand_dims(img_trans, 0) # CHW扩展为NCHW
img_trans = np.ascontiguousarray(img_trans) # 保证内存连续存储
img_trans = img_trans.astype(np.float32) # 转换成浮点型数据
if img_trans.max() > 1:
img = img_trans / 255.0 # 保证数据处于0-1之间的浮点数
return img
```
......
......@@ -4,17 +4,19 @@ import migraphx
def Preprocessing(pil_img, newW, newH):
assert newW > 0 and newH > 0, 'Scale is too small'
img_nd = cv2.resize(pil_img, (newW, newH)) # 将图像尺寸修改为256x256
img_nd = cv2.cvtColor(img_nd, cv2.COLOR_BGR2RGB) # BGR转换为RGB
img_nd = cv2.cvtColor(pil_img, cv2.COLOR_BGR2RGB) # BGR转换为RGB
img_nd = cv2.resize(img_nd, (newW, newH)) # 将图像尺寸修改为256x256
if len(img_nd.shape) == 2:
img_nd = np.expand_dims(img_nd, axis=2)
img_trans = img_nd.transpose((2, 0, 1)) # HWC转换为CHW
if img_trans.max() > 1: # 保证数据处于0-1之间的浮点数
img_trans = img_trans / 255.0
img_trans = np.expand_dims(img_trans, 0) # CHW扩展为NCHW
img = img_trans.astype(np.float32) # 转换成浮点型数据
img_trans = img_nd.transpose((2, 0, 1)) # HWC转换为CHW
img_trans = np.expand_dims(img_trans, 0) # CHW扩展为NCHW
img_trans = np.ascontiguousarray(img_trans) # 保证内存连续存储
img_trans = img_trans.astype(np.float32) # 转换成浮点型数据
if img_trans.max() > 1:
img = img_trans / 255.0 # 保证数据处于0-1之间的浮点数
return img
def Sigmoid(x):
......
......@@ -84,7 +84,7 @@ pip install -r requirements.txt
python Unet.py
```
输出结果为:
会在当前目录中生成分割图像
<img src="./Doc/Images/Unet_03.jpg" style="zoom:100%;" align=middle>
......@@ -103,7 +103,7 @@ cd ./build/
./MIGraphX_Samples 0
```
会在当前目录中生成分割结果图像Result.jpg
会在当前目录中生成分割图像
<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