全流程:目标确认-数据集采集-数据清理-数据标注-模型选择-模型训练
一、项目背景
用于对家里的宠物进行监控,例如家中的鸟、乌龟、鱼、猫、狗等。(我们只需要训练模型来达到识别的目的,具体的监控其他功能是属于算法应用层,进行模型的能力调用实现)
二、目标对象
宠物:猫、狗(这里我们以这两类举例)
三、数据采集
在正常工作中我们一般会遇到两种情况:
1.甲方(客户)将现场数据给我们定制场景进行开发
2.由项目组提供,在网络上进行爬取,目标数据,这里我给大家分享一个爬虫脚本下来使用(只需要修改最下方keywords就可以开始使用)
import os
import time
import requests
import urllib.parse
from bs4 import BeautifulSoup
import re
ImageCount = 0
def GetPageURL(URLStr):
# 获取一个页面的所有图片的URL+下页的URL
if not URLStr:
print('现在是最后一页啦!爬取结束')
return [], ''
try:
header = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36"
}
response = requests.get(URLStr, headers=header)
response.encoding = 'utf-8'
html = response.text
except Exception as e:
print("err=", str(e))
return [], ''
# 使用正则表达式提取图片URL
ImageURL = re.findall(r'"objURL":"(.*?)",', html, re.S)
# 使用BeautifulSoup解析HTML,提取下一页的URL
soup = BeautifulSoup(html, 'html.parser')
NextPageURLS = soup.find('a', class_='n', text='下一页')
if NextPageURLS:
NextPageURL = 'http://image.baidu' + NextPageURLS['href']
else:
NextPageURL = ''
return ImageURL, NextPageURL
def DownLoadImage(pic_urls, ImageFilePath):
"""给出图片链接列表, 下载所有图片"""
global ImageCount
for i, pic_url in enumerate(pic_urls):
try:
time.sleep(5)
pic = requests.get(pic_url, timeout=15)
ImageCount += 1
string = os.path.join(ImageFilePath, f"{ImageCount}.jpg")
with open(string, 'wb') as f:
f.write(pic.content)
print(f'已下载第{ImageCount}张图片: {pic_url}')
except Exception as e:
print(f'下载第{ImageCount}张图片失败: {e}')
ImageCount -= 1
continue
def CreateDirectory(path):
"""创建目录,如果不存在的话"""
if not os.path.exists(path):
os.makedirs(path)
def CrawlPicture(keyword):
# 获取用户的桌面路径,并创建保存图片的目录
picture_path = 'picture'
#picture_path = os.path.join(desktop_path, 'picture')
CreateDirectory(picture_path)
# 创建以关键字命名的子文件夹
keyword_path = os.path.join(picture_path, keyword)
CreateDirectory(keyword_path)
# 初始化爬取标志
CrawlFlag = True
NextPageURL = f"https://image.baidu/search/flip?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word={urllib.parse.quote(keyword, safe='/')}"
while CrawlFlag:
ImageURL, NextPageURL = GetPageURL(NextPageURL)
if ImageURL:
DownLoadImage(list(set(ImageURL)), keyword_path)
if not NextPageURL:
CrawlFlag = False
if __name__ == '__main__':
keywords = ['', '', '']
for keyword in keywords:
CrawlPicture(keyword)
四、数据清洗
如果是自己爬取数据通常会遇到很多问题:
1.只需要jpg格式图片
2.数据集非目标数据较多,但是用脚本清理难度大,需要人工清洗
3.图片太小或者图片太大
4.重复照片数量大
5.下载文件打不开
6.需要将名字统一起来方便后续管理(名字一般全用英文命名)
7.建议在问题多的情况,建议根据所需问题来通过gpt等方式去寻找方法解决。
五、数据标注
1.准备好标注数据集,按种类分给标注团队
2.标注文档(包括标注区域,方式;例如猫、狗我只要求标注脸、用矩形框标注)
3.标注返回格式(例如txt、xml、json)
六、训练数据集、测试集准备
1.准备好训练数据集(一般8:2,训练数据:80%,验证数据20%,数据中包括标注文件以及对应的图片)
2.测试集准备(我自己的习惯一个目标使用100到200张图片进行测试,测试图片不能用训练数据中的图片,会影响测试结果)
3.数据集准备参考我的博客(数据集准备到训练)yolov5-从数据标注到模型训练技术分享_yolov5数据标注和训练-CSDN博客
七、模型框架选择
1.根据项目要求(例如在本次项目中,我们须应用到手机端,则我们选择的是yolov5框架,方便后续应用适配)
2.根据训练配置,显卡资源(算力差的情况,且推理较慢我们这里选择了yolov5s的模型,模型小、训练速度也快)
八、模型训练
直接见我的博客(很细、从环境到训练到测试)
yolov5-从数据标注到模型训练技术分享_yolov5数据标注和训练-CSDN博客
发布者:admin,转转请注明出处:http://www.yc00.com/web/1741054722a4298575.html
评论列表(0条)