释放双眼,带上耳机,听听看~!
本文介绍了机器学习模型训练与预测的实践方法,包括模型训练结果的展示和解压缩数据集的步骤,以及使用训练好的模型进行预测并得到预测结果的过程。
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第32天,点击查看活动详情
一、模型训练结果
下为后台任务训练visualdl图
二、模型预测
1.应用模型
- 后台任务下载训练结果
- 上传训练模型为数据集并解压缩
# 解压后台训练好的模型
!unzip -qoa data/data166847/best_model.zip
2.解压缩test数据集
# 解压测试数据
!unzip -qoa -O GBK data/data163113/齿轮检测A榜评测数据.zip -d data
!mv data/齿轮检测A榜评测数据 data/test
3.预测结果
%cd ~
import glob
img_file=[]
img_file=glob.glob("data/test/val/*.jpg")
print(len(img_file))
/home/aistudio
600
import paddlex as pdx
model = pdx.load_model('/home/aistudio/best_model')
result=[]
# 不要批量预测,显存容易爆
for img in img_file:
item = model.predict(img)
result.append(item)
print(result[0])
[{'category_id': 0, 'category': 'hp_cm', 'bbox': [277.3127746582031, 398.80572509765625, 116.0015869140625, 123.09124755859375], 'score': 0.2651117742061615}, {'category_id': 0, 'category': 'hp_cm', 'bbox': [270.89251708984375, 242.0045928955078, 131.30450439453125, 115.49867248535156], 'score': 0.18997494876384735}, {'category_id': 0, 'category': 'hp_cm', 'bbox': [266.58856201171875, 69.13514709472656, 127.87396240234375, 108.75114440917969], 'score': 0.09731187671422958}, {'category_id': 0, 'category': 'hp_cm', 'bbox': [271.2421875, 556.1539306640625, 118.46600341796875, 108.4219970703125], 'score': 0.09247241914272308}, {'category_id': 0, 'category': 'hp_cm', 'bbox': [270.625244140625, 232.81887817382812, 91.84698486328125, 79.2542724609375], 'score': 0.03366655856370926}, {'category_id': 0, 'category': 'hp_cm', 'bbox': [343.02398681640625, 0.6909008026123047, 73.678466796875, 42.58038520812988], 'score': 0.025596577674150467}, {'category_id': 0, 'category': 'hp_cm', 'bbox': [275.1397705078125, 367.2425231933594, 76.25787353515625, 66.8385009765625], 'score': 0.015586825087666512}, {'category_id': 0, 'category': 'hp_cm', 'bbox': [267.201416015625, 514.007568359375, 45.40545654296875, 66.0347900390625], 'score': 0.011892958544194698}, {'category_id': 0, 'category': 'hp_cm', 'bbox': [268.7194519042969, 714.6714477539062, 44.30950927734375, 56.0875244140625], 'score': 0.011540604755282402}, {'category_id': 0, 'category': 'hp_cm', 'bbox': [261.60333251953125, 220.31338500976562, 59.601318359375, 71.15499877929688], 'score': 0.010460827499628067}, {'category_id': 1, 'category': 'hp_cd', 'bbox': [266.58856201171875, 69.13514709472656, 127.87396240234375, 108.75114440917969], 'score': 0.352761834859848}, {'category_id': 1, 'category': 'hp_cd', 'bbox': [771.477783203125, 8.217887878417969, 362.9459228515625, 212.39063262939453], 'score': 0.1544414609670639}, {'category_id': 1, 'category': 'hp_cd', 'bbox': [412.41827392578125, 0.0, 687.5846557617188, 373.23834228515625], 'score': 0.1034211814403534}, {'category_id': 1, 'category': 'hp_cd', 'bbox': [270.89251708984375, 242.0045928955078, 131.30450439453125, 115.49867248535156], 'score': 0.10121399909257889}, {'category_id': 1, 'category': 'hp_cd', 'bbox': [271.2421875, 556.1539306640625, 118.46600341796875, 108.4219970703125], 'score': 0.09936320781707764}, {'category_id': 1, 'category': 'hp_cd', 'bbox': [229.65936279296875, 71.80953979492188, 937.6921997070312, 630.4786682128906], 'score': 0.06198175996541977}, {'category_id': 1, 'category': 'hp_cd', 'bbox': [277.3127746582031, 398.80572509765625, 116.0015869140625, 123.09124755859375], 'score': 0.0524945892393589}, {'category_id': 1, 'category': 'hp_cd', 'bbox': [343.02398681640625, 0.6909008026123047, 73.678466796875, 42.58038520812988], 'score': 0.04534951224923134}, {'category_id': 1, 'category': 'hp_cd', 'bbox': [268.7194519042969, 714.6714477539062, 44.30950927734375, 56.0875244140625], 'score': 0.01778482086956501}, {'category_id': 1, 'category': 'hp_cd', 'bbox': [226.39495849609375, 376.0963439941406, 914.5798950195312, 617.7748718261719], 'score': 0.016559403389692307}, {'category_id': 1, 'category': 'hp_cd', 'bbox': [270.625244140625, 232.81887817382812, 91.84698486328125, 79.2542724609375], 'score': 0.015641620382666588}, {'category_id': 2, 'category': 'kp', 'bbox': [266.58856201171875, 69.13514709472656, 127.87396240234375, 108.75114440917969], 'score': 0.021044017747044563}, {'category_id': 2, 'category': 'kp', 'bbox': [939.2327270507812, 18.061260223388672, 80.750244140625, 52.013065338134766], 'score': 0.01880461722612381}, {'category_id': 2, 'category': 'kp', 'bbox': [879.6025390625, 987.5177001953125, 61.5970458984375, 51.7886962890625], 'score': 0.017387624830007553}, {'category_id': 2, 'category': 'kp', 'bbox': [277.3127746582031, 398.80572509765625, 116.0015869140625, 123.09124755859375], 'score': 0.014427728950977325}]
print(len(result))
600
4.按格式保存并提交
import json
import os
result_json = []
for i in range(len(result)):
for item in result[i]:
dt = {}
dt['name'] = os.path.basename(img_file[i])
dt['category_id'] = item['category_id']
dt['bbox'] = item['bbox']
dt['score'] = item['score']
result_json.append(dt)
# 生成上传文件
with open('./result.json','w') as f:
json.dump(result_json, f)
三、改进空间
- 由于采用后台训练模式,可增大训练轮次,不用担心训练中断;
- 可结合visualdl 训练图来调整训练策略。
本网站的内容主要来自互联网上的各种资源,仅供参考和信息分享之用,不代表本网站拥有相关版权或知识产权。如您认为内容侵犯您的权益,请联系我们,我们将尽快采取行动,包括删除或更正。