yolov8实战初学体验·1

深度学习初学1

我可能是为数不多的踩坑者,555尊的很烦差点放弃,我个人认为自己的环境配置是没有问题的,但是就是爆显存了,不知道有没有小伙伴能跟我分享交流~~

回归正传,我自己正在做的项目是一个鸟类识别加分类的项目,当然目前还在没有做完,自己的环境配置是cuda13.1+pytorch2.0+python3.9。

其实我是觉得我自己不配分享环境配置的经历的因为到最后我也没有在我自己的电脑上跑成

网络模型yolov8,最后还是选择在服务器上跑,当然最后是跑成了,因此我建议小伙伴们当在自

己电脑上跑不动的时候不用沮丧,当然你得确保肯定是环境配置出了问题,

比如报错:cuda显存不足,或者是程序访问到不该访问的地方等等很离谱的情况,而不是找不到某某文件的时候,就可以放心大胆地去学习如何在服务器上跑成自己的数据集了。(当然本文中我也会简单介绍一些指令)。

1.关键的几个流程

其实想要跑通网络模型,对于初学者来说就是

  1. 准备好数据集
  2. 对网络模型的调参和指令

1.数据集的准备

前提我默认你已经具备了科学上网的条件,于是我会建议你去两个网站上选取数据集:
1.https://www.kaggle.com /kaggle数据集)
Kaggle是一个数据建模和数据分析竞赛平台。企业和研究者可在其上发布数据,统计学者和数据挖掘专家可在其上进行竞赛以产生最好的模型。
在这里插入图片描述

2.https://universe.roboflow.com/(roboflow数据集)
 Roboflow是一款专为YOLOv8设计的自动化训练数据工具,它为YOLOv8提供了一种更便捷、更快速的方式来准备训练数据。其实我没有以为他是对yolov专门的数据集提供网址
roboflow
 这两个数据集都比较好,因为在我们深度学习中数据集不仅仅是单纯的图片,还需要我们自己对我们想要识别的物品进行打框(俗称打标)而这两个网站就提供了已经打标好的数据集,只需要一行代码就能将其下载下来,十分方便捏~~

有同学会问了那万一我想用我自己的数据集该怎么办呢 ?不准问

其实很好办,我会提供给你两部分代码:

import os, shutil
from sklearn.model_selection import train_test_split

val_size = 0.1
test_size = 0.2
postfix = 'jpg'
imgpath = 'VOCdevkit/JPEGImages'
txtpath = 'VOCdevkit/txt'

os.makedirs('../datasets/images/train', exist_ok=True)
os.makedirs('../datasets/images/val', exist_ok=True)
os.makedirs('../datasets/images/test', exist_ok=True)
os.makedirs('../datasets/labels/train', exist_ok=True)
os.makedirs('../datasets/labels/val', exist_ok=True)
os.makedirs('../datasets/labels/test', exist_ok=True)

listdir = [i for i in os.listdir(txtpath) if 'txt' in i]
train, test = train_test_split(listdir, test_size=test_size, shuffle=True, random_state=0)
train, val = train_test_split(train, test_size=val_size, shuffle=True, random_state=0)

for i in train:
    shutil.copy('{}/{}.{}'.format(imgpath, i[:-4], postfix), 'images/train/{}.{}'.format(i[:-4], postfix))
    shutil.copy('{}/{}'.format(txtpath, i), 'labels/train/{}'.format(i))

for i in val:
    shutil.copy('{}/{}.{}'.format(imgpath, i[:-4], postfix), 'images/val/{}.{}'.format(i[:-4], postfix))
    shutil.copy('{}/{}'.format(txtpath, i), 'labels/val/{}'.format(i))
for i in test:
    shutil.copy('{}/{}.{}'.format(imgpath, i[:-4], postfix), 'images/test/{}.{}'.format(i[:-4], postfix))
    shutil.copy('{}/{}'.format(txtpath, i), 'labels/test/{}'.format(i))
import xml.etree.ElementTree as ET
import os, cv2
import numpy as np
from os import listdir
from os.path import join
classes = []
def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = (box[0] + box[1]) / 2.0 - 1
    y = (box[2] + box[3]) / 2.0 - 1
    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(xmlpath, xmlname):
    with open(xmlpath, "r", encoding='utf-8') as in_file:
        txtname = xmlname[:-4] + '.txt'
        txtfile = os.path.join(txtpath, txtname)
        tree = ET.parse(in_file)
        root = tree.getroot()
        filename = root.find('filename')
        img = cv2.imdecode(np.fromfile('{}/{}.{}'.format(imgpath, xmlname[:-4], postfix), np.uint8), cv2.IMREAD_COLOR)
        h, w = img.shape[:2]
        res = []
        for obj in root.iter('object'):
            cls = obj.find('name').text
            if cls not in classes:
                classes.append(cls)
            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)
            res.append(str(cls_id) + " " + " ".join([str(a) for a in bb]))
        if len(res) != 0:
            with open(txtfile, 'w+') as f:
                f.write('\n'.join(res))


if __name__ == "__main__":
    postfix = 'jpg'
    imgpath = 'VOCdevkit/JPEGImages'
    xmlpath = 'VOCdevkit/Annotations'
    txtpath = 'VOCdevkit/txt'
    
    if not os.path.exists(txtpath):
        os.makedirs(txtpath, exist_ok=True)
    
    list = os.listdir(xmlpath)
    error_file_list = []
    for i in range(0, len(list)):
        try:
            path = os.path.join(xmlpath, list[i])
            if ('.xml' in path) or ('.XML' in path):
                convert_annotation(path, list[i])
                print(f'file {list[i]} convert success.')
            else:
                print(f'file {list[i]} is not xml format.')
        except Exception as e:
            print(f'file {list[i]} convert error.')
            print(f'error message:\n{e}')
            error_file_list.append(list[i])
    print(f'this file convert failure\n{error_file_list}')
    print(f'Dataset Classes:{classes}')

先睡一会之后再更,有点遭不住了,有人看我就更新555

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值