基于百度AIStudio飞桨paddleRS-develop版道路模型开发训练

2024-09-06 19:44

本文主要是介绍基于百度AIStudio飞桨paddleRS-develop版道路模型开发训练,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

基于百度AIStudio飞桨paddleRS-develop版道路模型开发训练

参考地址:https://aistudio.baidu.com/projectdetail/8271882

基于python35+paddle120+env环境
预测可视化结果:
在这里插入图片描述

(一)安装环境:
先上传本地下载的源代码PaddleRS-develop.zip
解压PaddleRS-develop.zip到目录PaddleRS
然后分别执行下面安装命令!pip install

!unzip -q /home/aistudio/data/data191076/PaddleRS-develop.zip && mv PaddleRS-develop PaddleRS
!pip install matplotlib==3.4 scikit-image pycocotools -t /home/aistudio/external-libraries
!pip install  opencv-contrib-python -t /home/aistudio/external-libraries
!pip install -r PaddleRS/requirements.txt  -t /home/aistudio/external-libraries
!pip install -e PaddleRS/  -t /home/aistudio/external-libraries
!pip install paddleslim==2.6.0  -t /home/aistudio/external-libraries

添加环境组件

# 因为`sys.path`可能没有及时更新,这里选择手动更新
import sys
sys.path.append('/home/aistudio/external-libraries')
sys.path.append('/home/aistudio/PaddleRS')

(二)数据预处理tran_dataPre.py

%run tran_dataPre.py

(三)开始模型训练

%run trans.py

(四) tran_dataPre.py内容如下所示:

#先解压数据集
#!unzip -oq -d /home/aistudio/massroad /home/aistudio/data/data56961/mass_road.zip# 划分训练集/验证集/测试集,并生成文件名列表import random
import os.path as osp
from os import listdirimport cv2# 随机数生成器种子
RNG_SEED = 56961
# 调节此参数控制训练集数据的占比
TRAIN_RATIO = 0.9
# 数据集路径
DATA_DIR = '/home/aistudio/massroad'# 分割类别
CLASSES = ('background','road',
)def write_rel_paths(phase, names, out_dir, prefix):"""将文件相对路径存储在txt格式文件中"""with open(osp.join(out_dir, phase+'.txt'), 'w') as f:for name in names:f.write(' '.join([osp.join(prefix, 'input', name),osp.join(prefix, 'output', name)]))f.write('\n')random.seed(RNG_SEED)train_prefix = osp.join('road_segmentation_ideal', 'training')
test_prefix = osp.join('road_segmentation_ideal', 'testing')
train_names = listdir(osp.join(DATA_DIR, train_prefix, 'output'))
train_names = list(filter(lambda n: n.endswith('.png'), train_names))
test_names = listdir(osp.join(DATA_DIR, test_prefix, 'output'))
test_names = list(filter(lambda n: n.endswith('.png'), test_names))
# 对文件名进行排序,以确保多次运行结果一致
train_names.sort()
test_names.sort()
random.shuffle(train_names)
len_train = int(len(train_names)*TRAIN_RATIO)
write_rel_paths('train', train_names[:len_train], DATA_DIR, train_prefix)
write_rel_paths('val', train_names[len_train:], DATA_DIR, train_prefix)
write_rel_paths('test', test_names, DATA_DIR, test_prefix)# 写入类别信息
with open(osp.join(DATA_DIR, 'labels.txt'), 'w') as f:for cls in CLASSES:f.write(cls+'\n')print("数据集划分已完成。")# 将GT中的255改写为1,便于训练import os.path as osp
from glob import globimport cv2
from tqdm import tqdm# 数据集路径
# DATA_DIR = '/home/aistudio/massroad'train_prefix = osp.join('road_segmentation_ideal', 'training')
test_prefix = osp.join('road_segmentation_ideal', 'testing')train_paths = glob(osp.join(DATA_DIR, train_prefix, 'output', '*.png'))
test_paths = glob(osp.join(DATA_DIR, test_prefix, 'output', '*.png'))
for path in tqdm(train_paths+test_paths):im = cv2.imread(path, cv2.IMREAD_GRAYSCALE)im[im>0] = 1# 原地改写cv2.imwrite(path, im)

(五) trans.py内容如下所示:

# 导入需要用到的库import random
import os.path as ospimport cv2
import numpy as np
import paddle
import paddlers as pdrs
from paddlers import transforms as T
from matplotlib import pyplot as plt
from PIL import Imageimport sys
sys.path.append('/home/aistudio/external-libraries')
sys.path.append('/home/aistudio/PaddleRS')# 定义全局变量# 随机种子
SEED = 56961
# 数据集存放目录
DATA_DIR = '/home/aistudio/massroad/'
# 训练集`file_list`文件路径
TRAIN_FILE_LIST_PATH = '/home/aistudio/massroad/train.txt'
# 验证集`file_list`文件路径
VAL_FILE_LIST_PATH = '/home/aistudio/massroad/val.txt'
# 测试集`file_list`文件路径
TEST_FILE_LIST_PATH = '/home/aistudio/massroad/test.txt'
# 数据集类别信息文件路径
LABEL_LIST_PATH = '/home/aistudio/massroad/labels.txt'
# 实验目录,保存输出的模型权重和结果
EXP_DIR =  '/home/aistudio/exp/'# 固定随机种子,尽可能使实验结果可复现random.seed(SEED)
np.random.seed(SEED)
paddle.seed(SEED)# 构建数据集# 定义训练和验证时使用的数据变换(数据增强、预处理等)
train_transforms = T.Compose([T.DecodeImg(),# 随机裁剪T.RandomCrop(crop_size=512),# 以50%的概率实施随机水平翻转T.RandomHorizontalFlip(prob=0.5),# 以50%的概率实施随机垂直翻转T.RandomVerticalFlip(prob=0.5),# 将数据归一化到[-1,1]T.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),T.ArrangeSegmenter('train')
])eval_transforms = T.Compose([T.DecodeImg(),T.Resize(target_size=1500),# 验证阶段与训练阶段的数据归一化方式必须相同T.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),T.ArrangeSegmenter('eval')
])# 分别构建训练和验证所用的数据集
train_dataset = pdrs.datasets.SegDataset(data_dir=DATA_DIR,file_list=TRAIN_FILE_LIST_PATH,label_list=LABEL_LIST_PATH,transforms=train_transforms,num_workers=4,shuffle=True
)val_dataset = pdrs.datasets.SegDataset(data_dir=DATA_DIR,file_list=VAL_FILE_LIST_PATH,label_list=LABEL_LIST_PATH,transforms=eval_transforms,num_workers=0,shuffle=False
)# 构建DeepLab V3+模型,使用ResNet-50作为backbone
model = pdrs.tasks.seg.DeepLabV3P(in_channels=3,num_classes=len(train_dataset.labels),backbone='ResNet50_vd'
)
model.initialize_net(pretrain_weights='CITYSCAPES',save_dir=osp.join(EXP_DIR, 'pretrain'),resume_checkpoint=None,is_backbone_weights=False
)# 构建优化器
optimizer = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.net.parameters()
)# 执行模型训练
model.train(num_epochs=100,train_dataset=train_dataset,train_batch_size=8,eval_dataset=val_dataset,optimizer=optimizer,save_interval_epochs=10,# 每多少次迭代记录一次日志log_interval_steps=30,save_dir=EXP_DIR,# 是否使用early stopping策略,当精度不再改善时提前终止训练early_stop=False,# 是否启用VisualDL日志功能use_vdl=True,# 指定从某个检查点继续训练resume_checkpoint=None
)

(六)训练生成过程信息

Output exceeds the size limit. Open the full output data in a text editor
2024-09-05 14:16:51 [INFO]	Loading pretrained model from /home/aistudio/exp/pretrain/model.pdparams
2024-09-05 14:16:53 [WARNING]	[SKIP] Shape of parameters head.decoder.conv.weight do not match. (pretrained: [19, 256, 1, 1] vs actual: [2, 256, 1, 1])
2024-09-05 14:16:53 [WARNING]	[SKIP] Shape of parameters head.decoder.conv.bias do not match. (pretrained: [19] vs actual: [2])
2024-09-05 14:16:53 [INFO]	There are 358/360 variables loaded into DeepLabV3P.
2024-09-05 14:17:46 [INFO]	[TRAIN] Epoch=1/100, Step=30/90, loss=0.133503, lr=0.001000, time_each_step=1.77s, eta=4:24:32
2024-09-05 14:18:25 [INFO]	[TRAIN] Epoch=1/100, Step=60/90, loss=0.181917, lr=0.001000, time_each_step=1.31s, eta=3:14:53
2024-09-05 14:19:02 [INFO]	[TRAIN] Epoch=1/100, Step=90/90, loss=0.112567, lr=0.001000, time_each_step=1.22s, eta=3:2:6
2024-09-05 14:19:03 [INFO]	[TRAIN] Epoch 1 finished, loss=0.15933047160506247 .
2024-09-05 14:19:44 [INFO]	[TRAIN] Epoch=2/100, Step=30/90, loss=0.141528, lr=0.001000, time_each_step=1.36s, eta=3:22:2
2024-09-05 14:20:20 [INFO]	[TRAIN] Epoch=2/100, Step=60/90, loss=0.165187, lr=0.001000, time_each_step=1.22s, eta=3:0:42
2024-09-05 14:20:57 [INFO]	[TRAIN] Epoch=2/100, Step=90/90, loss=0.145009, lr=0.001000, time_each_step=1.22s, eta=2:59:1
2024-09-05 14:20:58 [INFO]	[TRAIN] Epoch 2 finished, loss=0.1168842613697052 .
2024-09-05 14:21:39 [INFO]	[TRAIN] Epoch=3/100, Step=30/90, loss=0.126603, lr=0.001000, time_each_step=1.38s, eta=3:22:13
2024-09-05 14:22:16 [INFO]	[TRAIN] Epoch=3/100, Step=60/90, loss=0.117296, lr=0.001000, time_each_step=1.22s, eta=2:58:14
2024-09-05 14:22:53 [INFO]	[TRAIN] Epoch=3/100, Step=90/90, loss=0.072859, lr=0.001000, time_each_step=1.23s, eta=2:58:46
2024-09-05 14:22:53 [INFO]	[TRAIN] Epoch 3 finished, loss=0.10787189056475957 .
2024-09-05 14:23:34 [INFO]	[TRAIN] Epoch=4/100, Step=30/90, loss=0.081685, lr=0.001000, time_each_step=1.37s, eta=3:18:39
2024-09-05 14:24:11 [INFO]	[TRAIN] Epoch=4/100, Step=60/90, loss=0.087735, lr=0.001000, time_each_step=1.23s, eta=2:57:28
2024-09-05 14:24:48 [INFO]	[TRAIN] Epoch=4/100, Step=90/90, loss=0.084795, lr=0.001000, time_each_step=1.22s, eta=2:55:44
2024-09-05 14:24:49 [INFO]	[TRAIN] Epoch 4 finished, loss=0.10476481277081702 .
2024-09-05 14:25:30 [INFO]	[TRAIN] Epoch=5/100, Step=30/90, loss=0.098625, lr=0.001000, time_each_step=1.37s, eta=3:16:59
2024-09-05 14:26:07 [INFO]	[TRAIN] Epoch=5/100, Step=60/90, loss=0.078188, lr=0.001000, time_each_step=1.24s, eta=2:57:12
2024-09-05 14:26:43 [INFO]	[TRAIN] Epoch=5/100, Step=90/90, loss=0.098015, lr=0.001000, time_each_step=1.21s, eta=2:52:11
2024-09-05 14:26:44 [INFO]	[TRAIN] Epoch 5 finished, loss=0.10311256903741095 .
2024-09-05 14:27:25 [INFO]	[TRAIN] Epoch=6/100, Step=30/90, loss=0.109136, lr=0.001000, time_each_step=1.38s, eta=3:16:8
...
2024-09-05 15:39:38 [INFO]	Start to evaluate (total_samples=81, total_steps=81)...
2024-09-05 15:40:14 [INFO]	[EVAL] Finished, Epoch=40, miou=0.716638, category_iou=[0.96831487 0.46496069], oacc=0.969164, category_acc=[0.97447995 0.81316509], kappa=0.619485, category_F1-score=[0.98390241 0.63477565] .
2024-09-05 15:40:14 [INFO]	Current evaluated best model on eval_dataset is epoch_10, miou=0.7255623401044613
2024-09-05 15:40:18 [INFO]	Model saved in /home/aistudio/exp/epoch_40.

(七) 测试集预测结果:

# 构建测试集
test_dataset = pdrs.datasets.SegDataset(data_dir=DATA_DIR,file_list=TEST_FILE_LIST_PATH,label_list=LABEL_LIST_PATH,transforms=eval_transforms,num_workers=0,shuffle=False
)# 为模型加载历史最佳权重
state_dict = paddle.load(osp.join(EXP_DIR, 'best_model/model.pdparams'))
model.net.set_state_dict(state_dict)# 执行测试
test_result = model.evaluate(test_dataset)
print("测试集上指标:IoU为{:.2f},Acc为{:.2f},Kappa系数为{:.2f}, F1为{:.2f}".format(test_result['category_iou'][1], test_result['category_acc'][1],test_result['kappa'],test_result['category_F1-score'][1])
)
2024-09-05 20:07:40 [INFO]	13 samples in file /home/aistudio/massroad/test.txt
2024-09-05 20:07:41 [INFO]	Start to evaluate (total_samples=13, total_steps=13)...
测试集上指标:IoU为0.47,Acc为0.82,Kappa系数为0.62, F1为0.64

(八)预测结果可视化情况:

# 预测结果可视化
# 重复运行本单元可以查看不同结果def read_image(path):im = cv2.imread(path)return im[...,::-1]def show_images_in_row(ims, fig, title='', quantize=False):n = len(ims)fig.suptitle(title)axs = fig.subplots(nrows=1, ncols=n)for idx, (im, ax) in enumerate(zip(ims, axs)):# 去掉刻度线和边框ax.spines['top'].set_visible(False)ax.spines['right'].set_visible(False)ax.spines['bottom'].set_visible(False)ax.spines['left'].set_visible(False)ax.get_xaxis().set_ticks([])ax.get_yaxis().set_ticks([])if isinstance(im, str):im = read_image(im)if quantize:im = (im*255).astype('uint8')if im.ndim == 2:im = np.tile(im[...,np.newaxis], [1,1,3])ax.imshow(im)# 需要展示的样本个数
num_imgs_to_show = 4
# 随机抽取样本
chosen_indices = random.choices(range(len(test_dataset)), k=num_imgs_to_show)# 参考 https://stackoverflow.com/a/68209152
fig = plt.figure(constrained_layout=True)
fig.suptitle("Test Results")subfigs = fig.subfigures(nrows=3, ncols=1)# 读取输入影像并显示
im_paths = [test_dataset.file_list[idx]['image'] for idx in chosen_indices]
show_images_in_row(im_paths, subfigs[0], title='Image')# 获取模型预测输出
with paddle.no_grad():model.net.eval()preds = []for idx in chosen_indices:input, mask = test_dataset[idx]input = paddle.to_tensor(input["image"]).unsqueeze(0)logits, *_ = model.net(input)pred = paddle.argmax(logits[0], axis=0)preds.append(pred.numpy())
show_images_in_row(preds, subfigs[1], title='Pred', quantize=True)# 读取真值标签并显示
im_paths = [test_dataset.file_list[idx]['mask'] for idx in chosen_indices]
show_images_in_row(im_paths, subfigs[2], title='GT', quantize=True)# 渲染结果
fig.canvas.draw()
Image.frombytes('RGB', fig.canvas.get_width_height(), fig.canvas.tostring_rgb())

在这里插入图片描述
(九) 导出静态模型
训练后保存的模型为动态模型,布署发布模型为静态模型,因此需要导出操作

import matplotlib.pyplot as plt
import random
import cv2
import numpy as np
import paddle
import paddlers as pdrs
from PIL import Imageimport os
from paddlers.tasks import load_modelmodel_path =  './exp/best_model'img_14="i:/cwgis_ai/cup/mass_road/road_segmentation_ideal/testing/input/img-14.png"
img_10="i:/cwgis_ai/cup/mass_road/road_segmentation_ideal/testing/input/img-10.png"#save_dir="./models/road_infer_model_100"
save_dir="./models/road_infer_model_100_custom"# export model OK
# Set environment variables
os.environ['PADDLEX_EXPORT_STAGE'] = 'True'
os.environ['PADDLESEG_EXPORT_STAGE'] = 'True'# Load model from directory
model = load_model(model_path)#fixed_input_shape = None
#fixed_input_shape = [1500,1500]
fixed_input_shape = [17761,25006]      #[w,h]# Do dynamic-to-static cast   动态到静态的转换
# XXX: Invoke a protected (single underscore) method outside of subclasses.
model.export_inference_model(save_dir, fixed_input_shape)

(十) 预测单张图片代码

import matplotlib.pyplot as plt
import random
import cv2
import numpy as np
import paddle
import paddlers as pdrs
from PIL import Imageimport os
from paddlers.tasks import load_model# 因为`sys.path`可能没有及时更新,这里选择手动更新
import sys
sys.path.append('/home/aistudio/external-libraries')
sys.path.append('/home/aistudio/PaddleRS')img_14="./massroad/road_segmentation_ideal/testing/input/img-14.png"
img_10="./massroad/road_segmentation_ideal/testing/input/img-10.png"
img_5="./massroad/road_segmentation_ideal/testing/input/img-5.png"customImg="./customImage/DeepLearning_Image.png"    #file tif to png #model_dir="./models/road_infer_model_100"
#model_dir="./models/road_infer_model_100_None"
model_dir="./models/road_infer_model_100_custom"#model = pdrs.deploy.Predictor(model_dir)
model = pdrs.deploy.Predictor(model_dir,use_gpu=True)# 读取输入影像并显示
im_paths = [customImg]
im_lis = []
for name in im_paths:print(name)img = cv2.imread(name)      print(img.shape) #img = paddle.to_tensor(img) #.unsqueeze(0)   #标量输入im_lis.append(img)
# 获取模型预测输出img_file=img_10
preds = []
results = model.predict(im_lis)
#print(results)label_map=results[0]["label_map"]
#print(label_map)
label_map[label_map>0] = 255
cv2.imwrite('./outImage/label_map_custom.png', label_map)score_map=results[0]["score_map"]
#cv2.imwrite('./outImage/score_map.png', score_map[0])
print(score_map)print("预测完成")

本blog地址:https://blog.csdn.net/hsg77

这篇关于基于百度AIStudio飞桨paddleRS-develop版道路模型开发训练的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1142946

相关文章

Java中的Opencv简介与开发环境部署方法

《Java中的Opencv简介与开发环境部署方法》OpenCV是一个开源的计算机视觉和图像处理库,提供了丰富的图像处理算法和工具,它支持多种图像处理和计算机视觉算法,可以用于物体识别与跟踪、图像分割与... 目录1.Opencv简介Opencv的应用2.Java使用OpenCV进行图像操作opencv安装j

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

Python基于火山引擎豆包大模型搭建QQ机器人详细教程(2024年最新)

《Python基于火山引擎豆包大模型搭建QQ机器人详细教程(2024年最新)》:本文主要介绍Python基于火山引擎豆包大模型搭建QQ机器人详细的相关资料,包括开通模型、配置APIKEY鉴权和SD... 目录豆包大模型概述开通模型付费安装 SDK 环境配置 API KEY 鉴权Ark 模型接口Prompt

在 VSCode 中配置 C++ 开发环境的详细教程

《在VSCode中配置C++开发环境的详细教程》本文详细介绍了如何在VisualStudioCode(VSCode)中配置C++开发环境,包括安装必要的工具、配置编译器、设置调试环境等步骤,通... 目录如何在 VSCode 中配置 C++ 开发环境:详细教程1. 什么是 VSCode?2. 安装 VSCo

C#图表开发之Chart详解

《C#图表开发之Chart详解》C#中的Chart控件用于开发图表功能,具有Series和ChartArea两个重要属性,Series属性是SeriesCollection类型,包含多个Series对... 目录OverviChina编程ewSeries类总结OverviewC#中,开发图表功能的控件是Char

鸿蒙开发搭建flutter适配的开发环境

《鸿蒙开发搭建flutter适配的开发环境》文章详细介绍了在Windows系统上如何创建和运行鸿蒙Flutter项目,包括使用flutterdoctor检测环境、创建项目、编译HAP包以及在真机上运... 目录环境搭建创建运行项目打包项目总结环境搭建1.安装 DevEco Studio NEXT IDE

Python开发围棋游戏的实例代码(实现全部功能)

《Python开发围棋游戏的实例代码(实现全部功能)》围棋是一种古老而复杂的策略棋类游戏,起源于中国,已有超过2500年的历史,本文介绍了如何用Python开发一个简单的围棋游戏,实例代码涵盖了游戏的... 目录1. 围棋游戏概述1.1 游戏规则1.2 游戏设计思路2. 环境准备3. 创建棋盘3.1 棋盘类

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

百度/小米/滴滴/京东,中台架构比较

小米中台建设实践 01 小米的三大中台建设:业务+数据+技术 业务中台--从业务说起 在中台建设中,需要规范化的服务接口、一致整合化的数据、容器化的技术组件以及弹性的基础设施。并结合业务情况,判定是否真的需要中台。 小米参考了业界优秀的案例包括移动中台、数据中台、业务中台、技术中台等,再结合其业务发展历程及业务现状,整理了中台架构的核心方法论,一是企业如何共享服务,二是如何为业务提供便利。

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template