本文主要介绍PaddlePaddle入门的模型训练及预测,重点在于掌握搭建模型及调用模型进行预测,网络结构以及预处理等调优不在本文关注的范畴。
运行环境:PaddlePaddle 1.7.2
数据集:
代码如下:
import paddle.fluid as fluid
import numpy as np
import cv2
import glob
import os
#define model
def FlowerNet(x, num_classes):
x = fluid.layers.conv2d(x, num_filters=32, filter_size=5, stride=1, padding=2, act='relu')
x = fluid.layers.pool2d(x, pool_size=2, pool_stride=2, pool_type='max')
x = fluid.layers.conv2d(x, num_filters=, filter_size=5, stride=1, padding=2, act='relu')
x = fluid.layers.pool2d(x, pool_size=2, pool_stride=2, pool_type='max')
x = fluid.layers.conv2d(x, num_filters=128, filter_size=3, stride=1, padding=1, act='relu')
x = fluid.layers.pool2d(x, pool_size=2, pool_stride=2, pool_type='max')
x = fluid.layers.conv2d(x, num_filters=128, filter_size=3, stride=1, padding=1, act='relu')
x = fluid.layers.pool2d(x, pool_size=2, pool_stride=2, pool_type='max')
nodes = x.shape[1] * x.shape[2] * x.shape[3]
x = fluid.layers.reshape(x, [-1, nodes])
x = fluid.layers.fc(x, size=1024, act='relu')
x = fluid.layers.fc(x, size=512, act='relu')
x = fluid.layers.fc(x, size=num_classes)
return x
#read data
path = 'D:/code/tf/dataset/flower_photos'
img_paths = []
labels = []
for idx, cls in enumerate(os.listdir(path)):
cls_path = os.path.join(path, cls)
cls_files = glob.glob(os.path.join(cls_path, '*'))
img_paths.extend(cls_files)
labels.extend([idx] * len(cls_files))
#shuffle data
dataset_size = len(img_paths)
idx = np.arange(dataset_size)
np.random.shuffle(idx)
img_paths = np.asarray(img_paths)[idx]
labels = np.asarray(labels)[idx].astype('int')
#split train and test dataset
ratio = 0.8
train_img_paths = img_paths[:int(ratio*dataset_size)]
train_labels = labels[:int(ratio*dataset_size)]
test_img_paths = img_paths[int(ratio*dataset_size):]
test_labels = labels[int(ratio*dataset_size):]
#creat reader
size = (100, 100)
def preprocess(img_path):
img = cv2.imread(img_path)
img = cv2.resize(img, size)
img = np.transpose(img, [2, 0, 1]).astype('float32')
return img
def train_reader():
for i in range(len(train_img_paths)):
img = preprocess(train_img_paths[i])
yield img, train_labels[i]
def test_reader():
for i in range(len(test_img_paths)):
img = preprocess(test_img_paths[i])
yield img, test_labels[i]
batch_size =
train_reader = fluid.io.batch(fluid.io.shuffle(train_reader, buf_size=int(ratio*dataset_size)), batch_size=batch_size)
test_reader = fluid.io.batch(test_reader, batch_size=batch_size)
#train and test
image = fluid.layers.data(name='image', shape=[3, size[0], size[1]], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int')
predict = FlowerNet(image, num_classes=5)
loss = fluid.layers.softmax_with_cross_entropy(predict, label)
avg_loss = fluid.layers.mean(loss)
acc = fluid.layers.accuracy(predict, label)
optimizer = fluid.optimizer.AdamOptimizer(learning_rate=1e-3, regularization=fluid.regularizer.L2DecayRegularizer(1e-4))
opt = optimizer.minimize(avg_loss)
train_program = fluid.default_main_program()
test_program = train_program.clone(for_test=True)
use_cuda = True
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
feeder = fluid.DataFeeder(place=place, feed_list=[image, label])
epoch_num = 50
for i in range(epoch_num):
print("Epoch: {:d}".format(i+1))
#train
train_loss_epoch = []
train_acc_epoch = []
for batch_id, batch_data in enumerate(train_reader()):
train_loss, train_acc = exe.run(train_program, feed=feeder.feed(batch_data), fetch_list=[avg_loss, acc])
train_loss_epoch.append(train_loss[0])
train_acc_epoch.append(train_acc[0])
print('\tTrain---> loss: {:.3f}, acc: {:.3f}'.format(np.mean(train_loss_epoch), np.mean(train_acc_epoch)))
#test
test_loss_epoch = []
test_acc_epoch = []
for batch_id, batch_data in enumerate(test_reader()):
test_loss, test_acc = exe.run(test_program, feed=feeder.feed(batch_data), fetch_list=[avg_loss, acc])
test_loss_epoch.append(test_loss[0])
test_acc_epoch.append(test_acc[0])
print('\tTest---> loss: {:.3f}, acc: {:.3f}'.format(np.mean(test_loss_epoch), np.mean(test_acc_epoch)))
#save model
save_path = "model/"
if not os.path.exists(save_path):
os.makedirs(save_path)
fluid.io.save_inference_model(save_path, feeded_var_names=[image.name], target_vars=[predict], executor=exe)
模型训练后,测试准确率为60%左右。
输入一张图片,调用模型预测其类别,代码如下:
import paddle.fluid as fluid
import numpy as np
import cv2
import os
dataset_path = 'D:/code/tf/dataset/flower_photos'
cls_list = []
for cls in os.listdir(dataset_path):
cls_list.append(cls)
model_path = 'model/'
img_path = 'D:/code/tf/dataset/flower_photos/daisy/5794835_d15905c7c8_n.jpg'
size = (100, 100)
def read_image(path):
img = cv2.imread(img_path)
img = cv2.resize(img, size)
img = np.transpose(img, [2, 0, 1]).astype('float32')
img = np.expand_dims(img, axis=0)
return img
use_cuda = True
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
[infer_program, feed_var_names, target_vars] = fluid.io.load_inference_model(dirname=model_path, executor=exe)
img = read_image(img_path)
result = exe.run(program=infer_program, feed={feed_var_names[0] :img}, fetch_list=target_vars)
max_id = np.argmax(result[0][0])
print('预测结果为:{:s}, 概率为:{:.3f}'.format(cls_list[max_id], result[0][0][max_id]))
运行结果如下(概率未进行softmax处理):
预测结果为:daisy, 概率为:31.949
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- fupindai.com 版权所有 赣ICP备2024042792号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务