AI比赛编程语句整理
字体下载
https://ultralytics.com/assets/Arial.Unicode.ttf
SDK模型下载
pip install modelscope
from modelscope import snapshot_download
Model_dir = snapshot_download(‘yolo_master/YOLO11’)
在terminal拷贝模型文件(进入模型下载目录、查看文件、拷贝文件)
cd .cache/modelscope/models/yolo_master/YOLO11
ls -a
cp yolo11l.pt /mnt/workspace/Practice
coco128.yaml文件创建
train: '../datasets/car/train/images' # train images (relative to 'path') 128 images
val: '../datasets/car/val/images' # val images (relative to 'path') 128 images
test: # test images (optional)
Classes
nc: 3
names: [ '逍客','1号轮辋','2号轮辋']
库安装
pip install ultralytics
pip install openvino
拷贝字体文件
cp Arial.Unicode.ttf /root/.config/Ultralytics
加载、训练模型
from ultralytics import YOLO
model = YOLO("yolo11s.pt")
model.train(data="coco128.yaml", epochs=200, imgsz=640, workers=8)
通过AI再标注图片
from ultralytics import YOLO
model = YOLO("best.pt")
model.export(format="onnx")
制作model.yaml文件
type: yolo11
name: yolo11n
display_name: best
model_path: bestcar.onnx
nms_threshold: 0.45
confidence_threshold: 0.25
classes:
- 逍客
- 1号轮辋
- 2号轮辋
模型转换OPENVINO
from ultralytics import YOLO
model = YOLO("best.pt")
model.export(format="openvino")
部署OPENVINO
from ultralytics import YOLO
from pathlib import Path
import torch,time
import openvino as ov
import cv2
model_path = Path(r"./best-Copy1_openvino_model/best-Copy1.xml")
image_path = Path( r"./00116.jpg")
用Ultralytics工具包API实现yolov11模型推理程序
yolo11 = YOLO("best-Copy1.pt", task="detect")
result = yolo11(image_path, show=True)
result[0].show()
使用OpenVINO实现推理计算
core = ov.Core()
device = "CPU"
config = {"PERFORMANCE_HINT": "LATENCY"}
编译模型到CPU上
compiled_model = core.compile_model(model_path, device, config)
compiled_model = core.compile_model(model_path, device, config)
替代yolov11的原生推理计算方法
def ov_infer(*args):
result = compiled_model(args)
return torch.from_numpy(result[0])
yolo11.predictor.inference = ov_infer
执行基于OpenVINO的推理计算, 并复用YOLOV11的原生前后处理程序
img = cv2.imread(image_path)
result = yolo11.predict(img)
result[0].show()
time.sleep(6)
模型运行
from ultralytics import YOLO
model = YOLO("best2.pt")
results = model("0.jpg")
results[0].show()
将数据集随机划分为训练集和验证集,测试集
import os
import random
import shutil
from tqdm import tqdm
image_path = r"./Images//" # 源图片文件夹路径
mask_path = r"./Labels//" # 标签文件夹路径
train_images = r"./datasets2/images/train" # 划分后训练集图片的保存路径
train_labels = r"./datasets2/labels/train"
val_images = r"./datasets2/images/val"
val_labels = r"./datasets2/labels/val"
if not os.path.exists(train_images):
os.makedirs(train_images)
if not os.path.exists(train_labels):
os.makedirs(train_labels)
if not os.path.exists(val_images):
os.makedirs(val_images)
if not os.path.exists(val_labels):
os.makedirs(val_labels)
train_rate = 0.8 # 自定义抽取图片的比例,比方说100张抽10张,那就是0.1
val_rate = 0.2
test_rate = 0.0
求训练集
pathDir = os.listdir(image_path) # 取图片的原始路径
print('数据集总共有图片:', len(pathDir))
print(
'划分比例如下:训练集:{},验证集:{},测试集:{}'.format(int(len(pathDir) train_rate), int(len(pathDir) val_rate),
int(len(pathDir) test_rate)
)) # ,测试集:{} int(len(pathDir) test_rate)
picknumber = int(len(pathDir) * train_rate) # 按照rate比例从文件夹中取一定数量图片
train_sample = random.sample(pathDir, picknumber) # 随机选取picknumber数量的样本图片
print('训练集的大小为:', len(train_sample))
复制为训练集
for name in tqdm(train_sample):
if os.path.isfile(image_path + name):
shutil.copy(image_path + name, train_images + "//" + name)
shutil.copy(mask_path + name[:-3] + "txt", train_labels + "//" + name[:-3] + "txt")
求出原数据集不含训练集
all_images = os.listdir(image_path)
val_sample = []
for file in all_images:
if file not in train_sample:
val_sample.append(file)
print('验证集的大小为:', len(val_sample))
复制为验证集
for file in tqdm(val_sample):
shutil.copy(image_path + file, val_images + "/" + file)
shutil.copy(mask_path + file[:-3] + "txt", val_labels + "/" + file[:-3] + "txt")