工训备赛日志(一)——利用PaddleX与Paddle inference在windows环境下实现简单垃圾分类
此节分为以下几个部分
一、数据集准备
二、利用PaddleX-gui训练模型
三、利用Paddle inference部署
四、结果测试
一、数据集准备
笔者准备了电池、瓶子、易拉罐、陶瓷、香烟、水果、水果皮、蔬菜、蔬菜皮共计九种类型的垃圾图片,并将其放在以各自名字命名的文件夹下。
数据集下载:https://download.csdn.net/download/lurenjia1256/74085906
Mydate
--Battery
--Bottle
--Cans
--Ceramics
--Cigarette
--Fruit
--FruitSkin
--Vegetable
--VegetableSkin
二、利用PaddleX-gui训练模型
新建数据集,导入之前建立好的数据包,确定划分比例(笔者为7:2:1)
新建项目,选择垃圾分类数据集,配置参数。
配置好参数后启动训练。(如果电脑配置略低,可以调低批大小,以防训练失败。)
训练之后,我们导出模型,
之后我们打开文件夹中的inference_model,将里面的model.pdiparams,model.pdmodel文件取出。
至此,训练部分结束。
三、利用Paddle inference部署
#导入库
import cv2
import numpy as np
from paddle.inference import Config
from paddle.inference import create_predictor
#图像预处理函数
def resize_short(img, target_size):
""" resize_short """
percent = float(target_size) / min(img.shape[0], img.shape[1])
resized_width = int(round(img.shape[1] * percent))
resized_height = int(round(img.shape[0] * percent))
resized = cv2.resize(img, (resized_width, resized_height))
return resized
def crop_image(img, target_size, center):
""" crop_image """
height, width = img.shape[:2]
size = target_size
if center == True:
w_start = (width - size) / 2
h_start = (height - size) / 2
else:
w_start = np.random.randint(0, width - size + 1)
h_start = np.random.randint(0, height - size + 1)
w_end = w_start + size
h_end = h_start + size
img = img[int(h_start):int(h_end), int(w_start):int(w_end), :]
return img
def preprocess(img):
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
img = resize_short(img, 224)
img = crop_image(img, 224, True)
# bgr-> rgb && hwc->chw
img = img[:, :, ::-1].astype('float32').transpose((2, 0, 1)) / 255
img_mean = np.array(mean).reshape((3, 1, 1))
img_std = np.array(std).reshape((3, 1, 1))
img -= img_mean
img /= img_std
return img[np.newaxis, :]
def predict_config(model_file, params_file):
# 根据预测部署的实际情况,设置Config
config = Config()
# 读取模型文件
config.set_prog_file(model_file)
config.set_params_file(params_file)
# Config默认是使用CPU预测,若要使用GPU预测,需要手动开启,设置运行的GPU卡号和分配的初始显存。
config.enable_use_gpu(500, 0)
# 可以设置开启IR优化、开启内存优化。
config.switch_ir_optim()
config.enable_memory_optim()
predictor = create_predictor(config)
return predictor
def predict(image, predictor):
img = preprocess(image)
input_names = predictor.get_input_names()
input_tensor = predictor.get_input_handle(input_names[0])
input_tensor.reshape(img.shape)
input_tensor.copy_from_cpu(img.copy())
# 执行Predictor
predictor.run()
# 获取输出
output_names = predictor.get_output_names()
output_tensor = predictor.get_output_handle(output_names[0])
output_data = output_tensor.copy_to_cpu()
print("output_names", output_names)
print("output_tensor", output_tensor)
print("output_data", output_data)
return output_data
# 展示结果
def post_res(label_dict, res):
res = res.tolist()
print(res)
# print(type(res))
# print(max(res))
target_index = res[0].index(max(res[0]))
print("结果是:" + " " + label_dict[target_index])
#主函数
if __name__ == '__main__':
label_dict = {0:"Battery", 1:"Bottle", 2:"Cans", 3:"Ceramics",4:"Cigarette",5:"Fruit",6:"FruitSkin",7:"Vegetable",8:"VegetableSkin"}#标签所对应识别的种类,序号取决于数据集文件夹的排列顺序
model_file = ".\model.pdmodel" #导出的.pdmodel文件路径
params_file = ".\model.pdiparams" #导出的.pdiparams文件路径
image = cv2.imread("O-Cigarette0.jpg") #测试图片路径
predictor = predict_config(model_file, params_file)
res = predict(image, predictor)
post_res(label_dict, res)
cv2.imshow("image", image)
cv2.waitKey()