🔄Update: [Lab-5] 基于YOLOv8的工业缺陷检测实验
This commit is contained in:
@@ -5,3 +5,4 @@ model
|
||||
runs
|
||||
datasets
|
||||
yolov8n.pt
|
||||
dataset
|
||||
@@ -0,0 +1,20 @@
|
||||
from ultralytics import YOLO
|
||||
import cv2
|
||||
|
||||
# 加载最佳模型
|
||||
model = YOLO('runs/detect/yolov8n_pcb_defect_exp14/weights/best.pt')
|
||||
|
||||
# 在单张图像上推理
|
||||
results = model('../dataset/images/val/patches_296.jpg', save=True, conf=0.5)
|
||||
|
||||
# 可视化结果(使用OpenCV)
|
||||
for result in results:
|
||||
plotted_img = result.plot() # 绘制检测结果
|
||||
cv2.imshow('Detection Result', plotted_img)
|
||||
cv2.waitKey(0)
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
# 支持文件夹、视频、摄像头推理
|
||||
# results = model('path/to/video.mp4', save=True, show=True) # 视频文件
|
||||
# results = model(0, show=True) # 摄像头(0为默认摄像头)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
path: ../dataset # 数据集根目录
|
||||
train: images/train # 训练图像路径
|
||||
val: images/val # 验证图像路径
|
||||
|
||||
# 类别数
|
||||
nc: 6
|
||||
# 类别名称
|
||||
names: ['missing_hole', 'mouse_bite', 'open_circuit', 'short', 'spur', 'spurious_copper']
|
||||
@@ -0,0 +1,16 @@
|
||||
from ultralytics import YOLO
|
||||
|
||||
# 加载预训练模型
|
||||
model = YOLO('yolov8n.pt') # 可选模型: yolov8n, yolov8s, yolov8m, yolov8l, yolov8x
|
||||
|
||||
# 训练模型
|
||||
results = model.train(
|
||||
data='pcb_defect.yaml', # 数据集配置文件路径
|
||||
epochs=20, # 训练轮次
|
||||
imgsz=640, # 输入图像大小
|
||||
batch=16, # 批次大小(根据GPU内存调整)
|
||||
name='yolov8n_pcb_defect_exp1', # 实验名称
|
||||
optimizer='AdamW', # 优化器
|
||||
lr0=0.001, # 初始学习率
|
||||
device='cpu' # 使用设备(如GPU:device=0)
|
||||
)
|
||||
@@ -0,0 +1,61 @@
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
import pickle
|
||||
import os
|
||||
from os import listdir, getcwd
|
||||
from os.path import join
|
||||
import glob
|
||||
|
||||
classes = ['crazing', 'inclusion', 'patches', 'pitted_surface', 'rolled-in_scale', 'scratches'] # xml文件中标记的种类
|
||||
|
||||
|
||||
def convert(size, box):
|
||||
dw = 1.0 / size[0]
|
||||
dh = 1.0 / size[1]
|
||||
x = (box[0] + box[1]) / 2.0
|
||||
y = (box[2] + box[3]) / 2.0
|
||||
w = box[1] - box[0]
|
||||
h = box[3] - box[2]
|
||||
x = x * dw
|
||||
w = w * dw
|
||||
y = y * dh
|
||||
h = h * dh
|
||||
return (x, y, w, h)
|
||||
|
||||
|
||||
def convert_annotation(image_name):
|
||||
try:
|
||||
in_file = open('../dataset/annotations/val/' + image_name[:-3] + 'xml', encoding='utf-8') # 原来的xml文件路径
|
||||
except FileNotFoundError:
|
||||
print(f"Warning: XML file for {image_name} not found, skipping.")
|
||||
return
|
||||
|
||||
out_file = open('../dataset/labels/val/' + image_name[:-3] + 'txt', 'w', encoding='utf-8') # 转换后的txt文件存放路径
|
||||
tree = ET.parse(in_file)
|
||||
root = tree.getroot()
|
||||
size = root.find('size')
|
||||
w = int(size.find('width').text)
|
||||
h = int(size.find('height').text)
|
||||
|
||||
for obj in root.iter('object'):
|
||||
difficult = obj.find('difficult').text
|
||||
cls = obj.find('name').text
|
||||
if cls not in classes or int(difficult) == 1:
|
||||
continue
|
||||
cls_id = classes.index(cls)
|
||||
xmlbox = obj.find('bndbox')
|
||||
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
|
||||
float(xmlbox.find('ymax').text))
|
||||
bb = convert((w, h), b)
|
||||
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
|
||||
in_file.close()
|
||||
out_file.close()
|
||||
|
||||
|
||||
wd = getcwd()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
for image_path in glob.glob("../dataset/images/val/*.jpg"): # xml对应的图片的路径
|
||||
image_name = image_path.split('\\')[-1]
|
||||
convert_annotation(image_name)
|
||||
@@ -0,0 +1,9 @@
|
||||
from ultralytics import YOLO
|
||||
|
||||
# 加载最佳模型
|
||||
best_model = YOLO('runs/detect/yolov8n_pcb_defect_exp14/weights/best.pt')
|
||||
|
||||
# 在测试集上评估
|
||||
metrics = best_model.val() # 程序会自动计算mAP等指标
|
||||
print(f"mAP@0.5: {metrics.box.map50}")
|
||||
print(f"mAP@0.5:0.95: {metrics.box.map}")
|
||||
Reference in New Issue
Block a user