Compare commits
6
Commits
2f0c97c9bd
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8b4f10ee2 | ||
|
|
0e04debb7b | ||
|
|
47cfade856 | ||
|
|
0a07cffe2a | ||
|
|
b5a0d5f1f3 | ||
|
|
8f9aa11e99 |
+5
-1
@@ -1,4 +1,8 @@
|
|||||||
.idea
|
.idea
|
||||||
*/img
|
*/img
|
||||||
*/data
|
*/data
|
||||||
model
|
model
|
||||||
|
runs
|
||||||
|
datasets
|
||||||
|
yolov8n.pt
|
||||||
|
dataset
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
from ultralytics import YOLO
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
# 加载最佳模型
|
||||||
|
model = YOLO('runs/detect/yolov8n_coco128_exp1/weights/best.pt')
|
||||||
|
|
||||||
|
# 在图像上进行推理
|
||||||
|
source = 'datasets/img3.png' # 替换成你自己的测试图片路径
|
||||||
|
results = model(source, save=True, conf=0.5) # conf为置信度阈值
|
||||||
|
|
||||||
|
# 可视化结果(可选,使用OpenCV)
|
||||||
|
# 读取带标注的结果图像
|
||||||
|
result_image = results[0].plot() # 获取第一张图片的绘制结果
|
||||||
|
cv2.imshow('Detection Result', result_image)
|
||||||
|
cv2.waitKey(0)
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
|
# 也可以在视频或摄像头上进行推理
|
||||||
|
# results = model('path/to/video.mp4', save=True, show=True)
|
||||||
|
# results = model(0, show=True) # 0代表默认摄像头
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
from ultralytics import YOLO
|
||||||
|
|
||||||
|
# 1. 加载一个预训练模型(小规模模型,适合实验)
|
||||||
|
# 可选模型: YOLOv8n, YOLOv8s, YOLOv8m, YOLOv8l, YOLOv8x (nano, small, medium, large, xlarge)
|
||||||
|
model = YOLO('yolov8n.pt')
|
||||||
|
|
||||||
|
# 2. 训练模型
|
||||||
|
results = model.train(
|
||||||
|
data='coco128.yaml', # 数据集配置文件,YOLOv8会自动识别并下载
|
||||||
|
epochs=50, # 训练轮次,建议50-100轮以观察效果
|
||||||
|
imgsz=640, # 输入图像大小
|
||||||
|
batch=16, # 批次大小,根据GPU内存调整(CPU可设为-1)
|
||||||
|
name='yolov8n_coco128_exp1', # 实验名称,用于保存结果
|
||||||
|
device='cpu' # 使用CPU训练,如果有GPU且环境配置好,可改为 device=0 或 'cuda'
|
||||||
|
)
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
from ultralytics import YOLO
|
||||||
|
|
||||||
|
# 加载训练得到的最佳模型
|
||||||
|
best_model = YOLO('runs/detect/yolov8n_coco128_exp1/weights/best.pt')
|
||||||
|
# 在验证集上评估模型
|
||||||
|
metrics = best_model.val()
|
||||||
|
# 程序会自动打印出mAP、 Precision、 Recall等关键指标
|
||||||
|
|
||||||
|
# 加载训练得到的最佳模型
|
||||||
|
best_model = YOLO('runs/detect/yolov8n_coco128_exp1/weights/best.pt')
|
||||||
|
|
||||||
|
# 在验证集上评估模型
|
||||||
|
metrics = best_model.val()
|
||||||
|
# 程序会自动打印出mAP、 Precision、 Recall等关键指标。
|
||||||
@@ -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}")
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
`Lab-1/src/rgb_img.py`
|
`Lab-1/src/rgb_img.py`
|
||||||
|
|
||||||
## 基于PyTorch的Minist数据集分类
|
## **实验二** 基于PyTorch的Minist数据集分类
|
||||||
|
|
||||||
### 数据预览测试和数据装载;
|
### 数据预览测试和数据装载;
|
||||||
|
|
||||||
@@ -51,3 +51,7 @@
|
|||||||
### 通过代码自动下载模型并直接调用;对当前迁移过来的模型进行全连接层的调整;模型训练及测试结果。
|
### 通过代码自动下载模型并直接调用;对当前迁移过来的模型进行全连接层的调整;模型训练及测试结果。
|
||||||
|
|
||||||
`Lab-3/src/train.py`
|
`Lab-3/src/train.py`
|
||||||
|
|
||||||
|
## **实验四** 基于YOLO的目标检测
|
||||||
|
|
||||||
|
## **实验五** 基于YOLOv8的工业缺陷检测实验
|
||||||
|
|||||||
Reference in New Issue
Block a user