[yolov7 nanny-level tutorial] win10+anaconda3+cuda11.3 train your own data set

Attached are two other tutorials:
① yolov7 performs data enhancement and data division: link
② yolov7 cuts out the recognition results: link

1.Install Anaconda

Download Anaconda3. Considering the download speed, you can go to Tsinghua Mirror to download. Download address: Tsinghua Mirror Station
Insert image description here
. Here I downloaded Anaconda3-2022.10-Windows-x86_64.exe.
For the specific installation process, you can read this article: https://blog.csdn. net/in546/article/details/117400839

2. Create a python virtual environment

Open the Anaconda Prompt (Anaconda3) you just downloaded
Please add image description

Create a virtual environment named yolov7try using python3.8:

conda create -n yolov7try python=3.8

After the creation is completed, view the created environment:

conda env list

Activate the environment:

conda activate yolov7try

Successful activation is like this

3. Install CUDA11.3

First check the currently installed CUDA version in Anaconda Prompt

nvcc -V

If it is other versions, then download the corresponding versions of pytorch and pytorchvision. Here I use CUDA11.3

Check the CUDA driver version in the upper right corner. Anything less than or equal to it can be installed.

nvidia-smi

Please add image description

CUDA download address: CUDA download address
Please add image description
Click to download, installation tutorial: https://blog.csdn.net/Zinnir/article/details/122766367
After successful installation, enter nvcc -V to view:
Please add image description

4. Configure Yolov7 environment

Yolov7 source code download address: yolov7
also downloaded the weights by the way:
Please add image description
I used yolov7.pt and placed it in the yolov7-main folder

Click on the requirements.txt file, delete the two lines torch and torchvision, and create a new requirements_gpu.txt file:
-i https://download.pytorch.org/whl/cu113
torch1.11.0+cu113
torchvision
0.12.0+cu113
Please add image description

Then in anaconda3, cd to the yolov7-main directory and enter the following commands:

pip install -r requirements.txt
pip install -r requirements_gpu.txt

If the speed is too slow, you can refer to this configuration Tsinghua source: https://blog.csdn.net/YPP0229/article/details/105630429/
After installation, use the pip list command to view the library just downloaded. If it displays this, it means the installation is successful. :
Please add image description

5. Prepare the data set

I downloaded a baggage data set from roboflow, download address: https://universe.roboflow.com/shudarshan-kongkham/baggage-jowci, I integrated it into the format supported by yolov7, as follows:
Please add image description
Baidu Netdisk: Link
extraction code: vuj9
needs to modify the following three txt files according to its own image path. Open pathget.py and
Insert image description here
modify it to test, train, and val in sequence. Run it three times to generate three txt files.
But I saw someone here talking about how to label your own data set. Let’s start talking about it.

6.labelimg marks your own data set

Create a new labelimg environment in anaconda prompt

conda create -n labelimg python=3.6

After the creation is completed, enter the following command:

pip install labelimg -i https://pypi.tuna.tsinghua.edu.cn/simple

After installation, enter labelimg and wait for the labelimg interface to appear. The installation is successful.
Insert image description here
How to use: https://blog.csdn.net/klaus_x/article/details/106854136
Switch to YOLO mode on the right side to generate a mark file in txt format.

Next, divide the training set, verification set and test set images, put all the images into the images folder, put the labels into the labels folder, and run the code as follows.
Insert image description here
You need to modify it to your own path here. The following is the generated divided data set and .txt file. The defect here is one of my defective data sets.

# 将图片和标注数据按比例切分为 训练集和测试集
import shutil
import random
import os
 
# 原始路径
image_original_path = "C:/Users/A/Desktop/datasets/images/"
label_original_path = "C:/Users/A/Desktop/datasets/labels/"
 
cur_path = os.getcwd()
 
# 训练集路径
train_image_path = os.path.join(cur_path, "datasets/defect/images/train/")
train_label_path = os.path.join(cur_path, "datasets/defect/labels/train/")
 
# 验证集路径
val_image_path = os.path.join(cur_path, "datasets/defect/images/val/")
val_label_path = os.path.join(cur_path, "datasets/defect/labels/val/")
 
# 测试集路径
test_image_path = os.path.join(cur_path, "datasets/defect/images/test/")
test_label_path = os.path.join(cur_path, "datasets/defect/labels/test/")
 
# 训练集目录
list_train = os.path.join(cur_path, "datasets/defect/train.txt")
list_val = os.path.join(cur_path, "datasets/defect/val.txt")
list_test = os.path.join(cur_path, "datasets/defect/test.txt")
 
train_percent = 0.6
val_percent = 0.2
test_percent = 0.2
 
 
def del_file(path):
    for i in os.listdir(path):
        file_data = path + "\\" + i
        os.remove(file_data)
 
 
def mkdir():
    if not os.path.exists(train_image_path):
        os.makedirs(train_image_path)
    else:
        del_file(train_image_path)
    if not os.path.exists(train_label_path):
        os.makedirs(train_label_path)
    else:
        del_file(train_label_path)
 
    if not os.path.exists(val_image_path):
        os.makedirs(val_image_path)
    else:
        del_file(val_image_path)
    if not os.path.exists(val_label_path):
        os.makedirs(val_label_path)
    else:
        del_file(val_label_path)
 
    if not os.path.exists(test_image_path):
        os.makedirs(test_image_path)
    else:
        del_file(test_image_path)
    if not os.path.exists(test_label_path):
        os.makedirs(test_label_path)
    else:
        del_file(test_label_path)
 
 
def clearfile():
    if os.path.exists(list_train):
        os.remove(list_train)
    if os.path.exists(list_val):
        os.remove(list_val)
    if os.path.exists(list_test):
        os.remove(list_test)
 
 
def main():
    mkdir()
    clearfile()
 
    file_train = open(list_train, 'w')
    file_val = open(list_val, 'w')
    file_test = open(list_test, 'w')
 
    total_txt = os.listdir(label_original_path)
    num_txt = len(total_txt)
    list_all_txt = range(num_txt)
 
    num_train = int(num_txt * train_percent)
    num_val = int(num_txt * val_percent)
    num_test = num_txt - num_train - num_val
 
    train = random.sample(list_all_txt, num_train)
    # train从list_all_txt取出num_train个元素
    # 所以list_all_txt列表只剩下了这些元素
    val_test = [i for i in list_all_txt if not i in train]
    # 再从val_test取出num_val个元素,val_test剩下的元素就是test
    val = random.sample(val_test, num_val)
 
    print("训练集数目:{}, 验证集数目:{}, 测试集数目:{}".format(len(train), len(val), len(val_test) - len(val)))
    for i in list_all_txt:
        name = total_txt[i][:-4]
 
        srcImage = image_original_path + name + '.jpg'
        srcLabel = label_original_path + name + ".txt"
 
        if i in train:
            dst_train_Image = train_image_path + name + '.jpg'
            dst_train_Label = train_label_path + name + '.txt'
            shutil.copyfile(srcImage, dst_train_Image)
            shutil.copyfile(srcLabel, dst_train_Label)
            file_train.write(dst_train_Image + '\n')
        elif i in val:
            dst_val_Image = val_image_path + name + '.jpg'
            dst_val_Label = val_label_path + name + '.txt'
            shutil.copyfile(srcImage, dst_val_Image)
            shutil.copyfile(srcLabel, dst_val_Label)
            file_val.write(dst_val_Image + '\n')
        else:
            dst_test_Image = test_image_path + name + '.jpg'
            dst_test_Label = test_label_path + name + '.txt'
            shutil.copyfile(srcImage, dst_test_Image)
            shutil.copyfile(srcLabel, dst_test_Label)
            file_test.write(dst_test_Image + '\n')
 
    file_train.close()
    file_val.close()
    file_test.close()
 
 
if __name__ == "__main__":
    main()

Finally, place the datasets folder in yolov7-main
Insert image description here

7. Modification of some files

Then take suitcase recognition as an example, modify the following files:
yolov7.yaml in cfg\training, change it to whatever type it is.
Insert image description here
Create a new data.yaml in the data file, and change it to the path of the data set just now.

# train and val data as 1) directory: path\images\, 2) file: path\images.txt, or 3) list: [path1\images\, path2\images\]

train: D:\jmcode\2\yolov7-main\datasets\defect\train.txt
val: D:\jmcode\2\yolov7-main\datasets\defect\val.txt
test: D:\jmcode\2\yolov7-main\datasets\defect\test.txt
# number of classes
nc: 1

# class names
names: ['baggage']

Insert image description here

8.Training and testing

Next, you can start training.
cd to the yolov7-main folder and enter the following command to start training:
the batchsize I set here is 16 and the epoch is 100.

python train.py --workers 0 --device 0 --batch-size 16 --data data/data.yaml --img-size 320 320 --cfg cfg/training/yolov7.yaml --weights 'yolov7.pt' --name yolov7 --hyp data/hyp.scratch.p5.yaml --epochs 100

Add –cache-images to speed up training

python train.py --workers 0 --device 0 --batch-size 16 --data data/data.yaml --img-size 320 320 --cfg cfg/training/yolov7.yaml --weights 'yolov7.pt' --name yolov7 --hyp data/hyp.scratch.p5.yaml --epochs 100 --cache-images

Insert image description here
You can slowly wait for training. After training, test it:

python detect.py --weights .\runs\train\yolov7\weights\best.pt --source .\datasets\defect\testImages\

Insert image description here

Guess you like

Origin blog.csdn.net/qq_40481270/article/details/128022783