Deploying yolov5 in linux server virtual environment & training your own data set

Posted by gethinw on Sun, 20 Feb 2022 01:49:09 +0100

1, Virtual environment

1. Download conda (skip if conda already exists)

wget -c

2. Add permissions

chmod 777

3. Operation


(1) There will be two prompts in the middle. Press enter for the first one, and enter yes for the second one
(2) After the word installation finished appears, you will be prompted to ask whether you want the installer to initialize miniconda3 by running CONDA init? [yes | no])
Enter no

4. Editing bashrc file

vim ~/.bashrc

Add export PATH = "your minicanda3 path / bin:"$PATH at the end

5. Start bashrc

source ~/.bashrc

6. Verification


7. Create Python 3 7 virtual environment

conda create -n virtual environment name python==3.7

Some common commands for virtual environments:

The next time you want to enter this virtual environment: conda activate virtual environment name
Switch to another virtual environment: conda source activate other virtual environment names
Exit (end) virtual environment: conda deactivate
Check which virtual environments are available: conda env list


2, yolov5 configuration

1.github download yolob5 project

git clone

2. Download pt file

Run download or download it yourself and put it in the weights path (for convenience, the figure below shows what it looks like in windows)

3. Run yolov5

(1) (in virtual environment) enter the directory of yolov5 and execute

pip install -r requirements.txt

(2) Execute


(3) You can see two result pictures in the path (figure in the previous step). Finish


3, Train your own dataset

What I need to do is train a person only (not the kind that changes the configuration file to actually recognize 80 but only show one)

  1. Create the following folder in mytrain folder, all_images put all pictures, all_xml to all xml files. Then put the official code of yolov5 downloaded by GitHub in the mytrain folder

  1. Label the picture with labelImg. If you already have labelImg, you can skip this step
    (1) windows+r open cmd

    (2) Download labelImg
pip install -i labelImg

(3) Open labelImg

  1. Open labelImg and mark the picture (open dir is all_images, save dir is all_xml)

4. Create python file (used to turn the xml just generated into txt) code is as follows, and run

import os
import random
trainval_percent = 0.1  
train_percent = 0.9    
xmlfilepath = 'all_images'
txtsavepath = 'ImageSets'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv) #Returns tv a number of items from all list s
train = random.sample(trainval, tr)
if not os.path.exists('ImageSets/'):
ftrainval = open('ImageSets/trainval.txt', 'w')
ftest = open('ImageSets/test.txt', 'w')
ftrain = open('ImageSets/train.txt', 'w')
fval = open('ImageSets/val.txt', 'w')
for i in list:
    name = total_xml[i][:-4] + '\n'
    if i in trainval:
        if i in train:

5. The structure of the document is as follows

6. Create a file, the code is as follows. And run

import xml.etree.ElementTree as ET
import pickle
import os
import shutil
from os import listdir, getcwd
from os.path import join
sets = ['train', 'trainval'] 
classes = ['person']#Change to your own category
def convert(size, box):
    dw = 1. / size[0]
    dh = 1. / 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_id):
    in_file = open('E:/mytrain/mycoco/all_xml/%s.xml' % (image_id),encoding='utf-8',errors='replace')#Change to your own path
    out_file = open('E:/mytrain/mycoco/all_labels/%s.txt' % (image_id), 'w' )#Change to your own path
    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:
        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),
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
wd = getcwd()
for image_set in sets:
    if not os.path.exists('all_labels/'):
    image_ids = open('ImageSets/%s.txt' % (image_set)).read().strip().split()
    image_list_file = open('images_%s.txt' % (image_set), 'w')
    for image_id in image_ids:
        image_list_file.write('%s.jpg\n' % (image_id))
#If the label is already in txt format, comment out this line and store all txt in all_labels folder.

def copy_file(new_path,path_txt,search_path):#Parameter 1: the location where the new file is stored parameter 2: the path of the train and val training data established in the previous step txt file parameter 3: the location of the searched file
    if not os.path.exists(new_path):
    with open(path_txt, 'r') as lines:
        filenames_to_copy = set(line.rstrip() for line in lines)
        # print('filenames_to_copy:',filenames_to_copy)
        # print(len(filenames_to_copy))
    for root, _, filenames in os.walk(search_path):
        # print('root',root)
        # print(_)
        # print(filenames)
        for filename in filenames:
            if filename in filenames_to_copy:
                shutil.copy(os.path.join(root, filename), new_path)

#Divide the path to the new training file and copy it to yolo according to the training format
copy_file('E:/mytrain/mycoco/images/train/','E:/mytrain/mycoco/images_train.txt','E:/mytrain/mycoco/all_image')#Change to your own path
copy_file('E:/mytrain/mycoco/images/val/','E:/AI/peopleDetection/mytrain/mycoco/images_trainval.txt','E:/mytrain/mycoco/all_image')#Change to your own path
copy_file('E:/mytrain/mycoco/labels/train/','E:/mytrain/mycoco/labels_train.txt','E:/mytrain/mycoco/all_labels')#Change to your own path
copy_file('E:/mytrain/mycoco/labels/val/','E:/mytrain/mycoco/labels_trainval.txt','E:/mytrain/mycoco/all_labels')#Change to your own path

Check whether there are pictures in the train and val folders of the images folder.

If not, you can refer to train in ImageSets Txt and val.txt put the corresponding pictures into the folder

7. Change train yaml file used in PY

8. Modify yolov5s In the category of person, I only recognize 1:1

9. Run train py


There will be an error of wandb here. Operate according to the prompt or comment out the part of wandb in the code. The following is the solution to not annotate wandb:

pip install wandb
wandb init

Open browser login , complete the registration (scientific Internet access may be required)
After registration, it looks like this

APIkey is in the setting in the upper right corner

Go back to linux, fill in the APIkey, and enter the user-defined wandb project name (just choose one, no other operation is required)

10. If you want to detect your own video (pictures also apply), the video is called normal video MP4, the weight file uses yolov5 pt

 python --source normal video.mp4 --weights --conf 0.4

According to the prompt, look for the detected file in runs/detect/exp5 (everyone's results may be different)


Virtual environment reference:
Training reference:

Topics: Linux yolov5