附录Ⅲ. 前处理和配置文件注意要点
根据input_config.ini文件中training_input_formats 和 input_formats设置的不同,下面举例前处理文件的写法:
1.1. training_input_formats和input_formats均为RGB或者均为BGR¶
(1) input config配置信息设置
training_input_formats | input_formats | 板上运行时数据对齐方式 |
---|---|---|
RGB/BGR | RGB/BGR | 不用对齐 |
RGB/BGR | RGBA/BGRA | 不用对齐 |
(2) 前处理脚本示例
import cv2
import numpy as np
def get_image(img_path, resizeH=224, resizeW=224, norm=True, meanB=103.53, meanG=116.28, meanR=123.68, std=57.375, rgb=False, nchw=False):
img = cv2.imread(img_path)
if img is None:
raise FileNotFoundError('No such image: {}'.format(img_path))
img_norm = img_norm.astype('float32')
img_norm = cv2.resize(img, (resizeW, resizeH), interpolation=cv2.INTER_LINEAR)
if norm:
img_norm = (img_norm - [meanB, meanG, meanR]) / std
img_norm = img_norm.astype('float32')
else:
img_norm = np.round(img_norm).astype('uint8')
if rgb:
img_norm = cv2.cvtColor(img_norm, cv2.COLOR_BGR2RGB)
if nchw:
img_norm = np.transpose(img_norm, axes=(2, 0, 1))
return np.expand_dims(img_norm, 0)
def image_preprocess(img_path, norm=True):
return get_image(img_path, norm=norm)
Note
-
示例中每个channel上有相同的std值,故参数只给了一个值,如果在每个channel上有不同的std值,需要在对应channel上除以std,参数应给 std= [57.375, 57.12, 58.395]。
-
input_config.ini文件中mean每个通道对应的std_value值,以英文冒号( : )分隔,顺序为RGB。
1.2. training_input_formats为GRAY和input_formats为GRAY¶
(1) input config配置信息设置
training_input_formats | input_formats | 板上运行时数据对齐方式 |
---|---|---|
GRAY | GRAY | H = ALIGN_UP(H, yuv420_v_pitch_alignment ) input_height_alignment默认为1 |
W = ALIGN_UP(W, yuv420_h_pitch_alignment ) input_width_alignment默认为1 |
(2) 前处理脚本示例
import cv2
import numpy as np
def get_image(img_path, resizeH=28, resizeW=28, norm=True, meanR=33.318, std=1.0, rgb=False, nchw=False):
img = cv2.imread(img_path, flags=-1)
if img is None:
raise FileNotFoundError('No such image: {}'.format(img_path))
try:
img_dim = img.shape[2]
except IndexError:
img_dim = 1
if img_dim == 3:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
elif img_dim == 4:
img = cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY)
img_norm = img_norm.astype('float32')
img_norm = cv2.resize(img, (resizeW, resizeH), interpolation=cv2.INTER_LINEAR)
if norm:
img_norm = (img_norm - meanR) / std
img_norm = np.expand_dims(img_norm, axis=2)
img_norm = img_norm.astype('float32')
else:
img_norm = np.expand_dims(img_norm, axis=2)
img_norm = np.round(img_norm).astype('uint8')
if rgb:
img_norm = cv2.cvtColor(img_norm, cv2.COLOR_BGR2RGB)
if nchw:
# NCHW
img_norm = np.transpose(img_norm, axes=(2, 0, 1))
return np.expand_dims(img_norm, 0)
def image_preprocess(img_path, norm=True):
return get_image(img_path, norm=norm)
Note
- 灰度模型,指输入是单通道图片的模型,即输入C维度上为1的模型。
1.3. training_input_formats为RGB或BGR或GRAY和input_formats为YUV_NV12¶
(1) input config配置信息设置
training_input_formats | input_formats | 板上运行时数据对齐方式 |
---|---|---|
RGB/BGR/GRAY | YUV_NV12 | H = ALIGN_UP(H, yuv420_v_pitch_alignment ) input_height_alignment默认为2 |
W = ALIGN_UP(W, yuv420_h_pitch_alignment )input_width_alignment默认为2 |
(2) 前处理脚本示例
import cv2
import numpy as np
def get_image(img_path, resizeH=224, resizeW=224, norm=True, meanB=127.5, meanG=127.5, meanR=127.5, std=128.0, rgb=False, nchw=False):
img = cv2.imread(img_path)
if img is None:
raise FileNotFoundError('No such image: {}'.format(img_path))
img_norm = img_norm.astype('float32')
img_norm = cv2.resize(img, (resizeW, resizeH), interpolation=cv2.INTER_LINEAR)
if norm:
img_norm = (img_norm - [meanB, meanG, meanR]) / std
img_norm = img_norm.astype('float32')
else:
img_norm = np.round(img_norm).astype('uint8')
if rgb:
img_norm = cv2.cvtColor(img_norm, cv2.COLOR_BGR2RGB)
if nchw:
img_norm = np.transpose(img_norm, axes=(2, 0, 1))
return np.expand_dims(img_norm, 0)
def image_preprocess(img_path, norm=True):
return get_image(img_path, norm=norm)
1.4. training_input_formats和input_formats均为RAWDATA_S16_NHWC或者均为RAWDATA_F32_NHWC¶
1.4.1. training_input_formats和input_formats均为RAWDATA_S16_NHWC¶
(1) input config配置信息设置
training_input_formats | input_formats | 板上运行时数据对齐方式 |
---|---|---|
RAWDATA_S16_NHWC | RAWDATA_S16_NHWC | 不用对齐 |
(2) 前处理脚本示例
import cv2
import numpy as np
def get_image(img_path, resizeH=224, resizeW=224, norm=True, rgb=False, nchw=False):
img = cv2.imread(img_path)
if img is None:
raise FileNotFoundError('No such image: {}'.format(img_path))
img_norm = cv2.resize(img, (resizeW, resizeH), interpolation=cv2.INTER_LINEAR)
img_norm = img_norm.astype('float32')
if rgb:
img_norm = cv2.cvtColor(img_norm, cv2.COLOR_BGR2RGB)
if nchw:
img_norm = np.transpose(img_norm, axes=(2, 0, 1))
return np.expand_dims(img_norm, 0)
def image_preprocess(img_path, norm=True):
return get_image(img_path, norm=norm)
Note
-
请不要设置input_config.ini中的mean_red、mean_green、mean_blue和std_value参数值,因为不需要前处理中的归一化操作。
-
编写图片处理函数时(如示例中的def get_image(……)),由于该函数必须包含归一化标记(norm),故只需构造函数时声明norm=True即可,函数中无需实现norm为True或false的操作。
-
使用simulator工具时,会读入原浮点数据,进行反量化和对齐排列后输入给定点模型。
-
在板上运行RAWDATA_S16_NHWC的网络时,不需要输入数据的定点化和对齐排列过程。
1.4.2. training_input_formats和input_formats均为RAWDATA_F32_NHWC¶
(1) input config配置信息设置
training_input_formats | input_formats | 板上运行时数据对齐方式 |
---|---|---|
RAWDATA_F32_NHWC | RAWDATA_F32_NHWC | 不用对齐 |
(2) 前处理脚本示例
import cv2
import numpy as np
def get_image(img_path, resizeH=224, resizeW=224, norm=True, rgb=False, nchw=False):
img = cv2.imread(img_path)
if img is None:
raise FileNotFoundError('No such image: {}'.format(img_path))
img_norm = cv2.resize(img, (resizeW, resizeH), interpolation=cv2.INTER_LINEAR)
img_norm = img_norm.astype('float32')
if rgb:
img_norm = cv2.cvtColor(img_norm, cv2.COLOR_BGR2RGB)
if nchw:
img_norm = np.transpose(img_norm, axes=(2, 0, 1))
return np.expand_dims(img_norm, 0)
def image_preprocess(img_path, norm=True):
return get_image(img_path, norm=norm)
Note
-
请不要设置input_config.ini中的mean_red、mean_green、mean_blue和std_value参数值,因为不需要前处理中的归一化操作。
-
编写图片处理函数时(如示例中的def get_image(……)),由于该函数必须包含归一化标记(norm),故只需构造函数时声明norm=True即可,函数中无需实现norm为True或false的操作。
-
使用simulator工具时,simulator.py会读入原浮点数据,由模型内部的align算子自动对齐数据,不需要人为操作。
-
在板上运行RAWDATA_F32_NHWC的网络时,不需要输入数据的定点化和对齐排列过程。特殊模型转换要点:RAWDATA_F32_NHWC模型转换要点