我想要使得输入数据和模型权重的数据精度都设置为fp16,应该如何操作。
下面是我的数据读入和模型定义的代码
模型定义:
def cnn_model(image):
conv1 = fluid.layers.conv2d(input=image, num_filters=32, filter_size=5, stride=2, act='relu',data_format='NHWC')
conv2 = fluid.layers.conv2d(input=conv1, num_filters=32, filter_size=5, stride=2)
bn0 = fluid.layers.batch_norm(input=conv2,act='relu')
conv3 = fluid.layers.conv2d(input=bn0, num_filters=64, filter_size=5, stride=2, act='relu')
conv4 = fluid.layers.conv2d(input=conv3, num_filters=64, filter_size=3, stride=2)
bn1 = fluid.layers.batch_norm(input=conv4,act='relu')
conv5 = fluid.layers.conv2d(input=bn1, num_filters=128, filter_size=3, stride=1)
# conv6 = fluid.layers.conv2d(input=conv5, num_filters=64, filter_size=3, stride=1)
bn2 = fluid.layers.batch_norm(input=conv5,act='relu')
# conv7 = fluid.layers.conv2d(input=conv6, num_filters=64, filter_size=3, stride=1, act='relu')
fc1 = fluid.layers.fc(input=conv5, size=128, act=None)
drop_fc1 = fluid.layers.dropout(fc1, dropout_prob=0.1)
fc2 = fluid.layers.fc(input=drop_fc1, size=64, act=None)
drop_fc2 = fluid.layers.dropout(fc2, dropout_prob=0.1)
predict = fluid.layers.fc(input=drop_fc2, size=1)
return predict
数据读入:
image = fluid.layers.data(name='image', shape=[3, crop_size, crop_size], dtype='float16')
label = fluid.layers.data(name='label', shape=[1], dtype='float16')
def train_mapper(sample):
img_path, label, crop_size, resize_size = sample
try:
img = Image.open(img_path)
# 统一图片大小
img = img.resize((resize_size, resize_size), Image.ANTIALIAS)
# 把图片转换成numpy值
img = np.array(img).astype(np.float32)
img = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
# 转换成CHW
img = img.transpose((2, 0, 1))
# 转换成BGR
img = img[(2, 1, 0), :, :] / 255.0
img=img.astype(np.float16)
return img, label
except:
print("%s 该图片错误,请删除该图片并重新创建图像数据列表" % img_path)
# 获取训练的reader
def train_reader(train_list_path, crop_size, resize_size):
father_path = os.path.dirname(train_list_path)
def reader():
with open(train_list_path, 'r') as f:
lines = f.readlines()
# 打乱图像列表
np.random.shuffle(lines)
# 开始获取每张图像和标签
for line in lines:
img, label = line.split('\t')
img = os.path.join(father_path, img)
yield img, label, crop_size, resize_size
return paddle.reader.xmap_readers(train_mapper, reader, cpu_count(), 102400)
模型导入:
def load_model():
valid_places = (
Place(TargetType.kFPGA, PrecisionType.kFP16, DataLayoutType.kNHWC),
Place(TargetType.kHost, PrecisionType.kFloat),
Place(TargetType.kARM, PrecisionType.kFloat),
)
config = CxxConfig()
config.set_model_file("/home/root/SC/yolov3/line_model/v9/models")
config.set_param_file("/home/root/SC/yolov3/line_model/v9/params")
config.set_valid_places(valid_places)
predictor = CreatePaddlePredictor(config)
return predictor
def predict(predictor, img):
i = predictor.get_input(0)
i.set_data(img)
predictor.run()
out = predictor.get_output(0)
result = out.data()[0][0]
return result