Getting Started with YOLOv5

Model checking

key parameter

weights: trained model file

image-20230803192833654

source: the detection target, which can be a single picture, folder, screen or camera, etc.

image-20230803192903522

conf-thres: Confidence threshold value, the lower the value, the more boxes, the higher the value, the fewer boxes.

iou-thres: IOU value, the lower the number, the fewer frames, the lower the number, the more frames.

torch.hub detection method

Install Jupyter

Enter the following code on the command line

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple jupyterlab
conda install ipykernel

image-20230803193551027

activate environment

python -m ipykernel install --name yolov5

image-20230803200817611

Create a new Jupyter file

hub_detect.ipynb

image-20230803193329230

import torch

#Model
model = torch.hub.load("./","yolov5s",source= "local")

#Images
img = "./data/images/zidane.jpg"

# Inference
results = model(img)

# Results
results.show()

Pay attention to the operating environment, download yolov5

image-20230803200936030

Dataset construction

The video is placed in the data02 folder

Extract video frames

import cv2
import matplotlib.pyplot as plt
#%%
# 打开视频文件
video = cv2.VideoCapture("./BVN.mp4")
# 读取一帧
ret, frame = video.read()

plt.imshow(frame)

plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

video = cv2.VideoCapture("./BVN.mp4")
num = 0         # 计数器
save_step = 30  # 间隔帧
while True:
    ret, frame = video.read()
    if not ret:
        break
    num += 1
    if num % save_step == 0:
        cv2.imwrite("./images/" + str(num) + ".jpg", frame)

Install the standard tool labelimg

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

Use labelimg

  • Terminal inputlabelimg

  • Open the folder that needs to be markedimages

image-20230803225027177

  • Set save path

image-20230803225658417

  • Click the logo in the picture below and change the save format to YOLO

image-20230803225332505

  • Turn on auto-save

image-20230803225752233

  • To start labeling, right-clickCreate RectBox

image-20230803225938948

  • Frame two characters and name them

image-20230803230054984

  • Switch to the next picture and continue with the standard (shortcut keys A for next picture, D for next picture, and W to create a RectBox)

  • After the standard is completed, the folder contents are as shown in the figure

image-20230803232028650

Model training

Adjust file naming

The naming is strictly consistent and cannot be changed.

image-20230803232326057

File adjustments

image-20230803232806766

image-20230803232740636

Set up yaml file

  1. Copy the coco128.yaml file and name it bvn.yaml

image-20230803233606669

  1. Modify bvn.yaml

image-20230803234334745

  1. Modify train.py

Change the file name to bvn

workers set to 1

image-20230803233922856

image-20230804002329930

  1. Run train.py to train the model

You may need to download something in the middle, which is quite slow, so wait patiently (you can hang a ladder)

If an error occurs, please see the error resolution below.

image-20230804010009222

Error reporting and resolution

The page file is too small to complete the operation

During the training process, the error message shown in the figure below occurred, and pycharm crashed.

image-20230804000941294

1. Change virtual memory
  1. You should be able to enter advanced system settings, so I won’t talk about the process.

image-20230804002758867

  1. Set virtual memory size

image-20230804003012617

2. Reduce the occupied content size
  1. Create a new fixNvPe.py program
# Simple script to disable ASLR and make .nv_fatb sections read-only
# Requires: pefile  ( python -m pip install pefile )
# Usage:  fixNvPe.py --input path/to/*.dll
 
import argparse
import pefile
import glob
import os
import shutil
 
def main(args):
    failures = []
    for file in glob.glob( args.input, recursive=args.recursive ):
        print(f"\n---\nChecking {
      
      file}...")
        pe = pefile.PE(file, fast_load=True)
        nvbSect = [ section for section in pe.sections if section.Name.decode().startswith(".nv_fatb")]
        if len(nvbSect) == 1:
            sect = nvbSect[0]
            size = sect.Misc_VirtualSize
            aslr = pe.OPTIONAL_HEADER.IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
            writable = 0 != ( sect.Characteristics & pefile.SECTION_CHARACTERISTICS['IMAGE_SCN_MEM_WRITE'] )
            print(f"Found NV FatBin! Size: {
      
      size/1024/1024:0.2f}MB  ASLR: {
      
      aslr}  Writable: {
      
      writable}")
            if (writable or aslr) and size > 0:
                print("- Modifying DLL")
                if args.backup:
                    bakFile = f"{
      
      file}_bak"
                    print(f"- Backing up [{
      
      file}] -> [{
      
      bakFile}]")
                    if os.path.exists( bakFile ):
                        print( f"- Warning: Backup file already exists ({
      
      bakFile}), not modifying file! Delete the 'bak' to allow modification")
                        failures.append( file )
                        continue
                    try:
                        shutil.copy2( file, bakFile)
                    except Exception as e:
                        print( f"- Failed to create backup! [{
      
      str(e)}], not modifying file!")
                        failures.append( file )
                        continue
                # Disable ASLR for DLL, and disable writing for section
                pe.OPTIONAL_HEADER.DllCharacteristics &= ~pefile.DLL_CHARACTERISTICS['IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE']
                sect.Characteristics = sect.Characteristics & ~pefile.SECTION_CHARACTERISTICS['IMAGE_SCN_MEM_WRITE']
                try:
                    newFile = f"{
      
      file}_mod"
                    print( f"- Writing modified DLL to [{
      
      newFile}]")
                    pe.write( newFile )
                    pe.close()
                    print( f"- Moving modified DLL to [{
      
      file}]")
                    os.remove( file )
                    shutil.move( newFile, file )
                except Exception as e:
                    print( f"- Failed to write modified DLL! [{
      
      str(e)}]")
                    failures.append( file )
                    continue
 
    print("\n\nDone!")
    if len(failures) > 0:
        print("***WARNING**** These files needed modification but failed: ")
        for failure in failures:
            print( f" - {
      
      failure}")
 
 
 
 
 
 
 
def parseArgs():
    parser = argparse.ArgumentParser( description="Disable ASLR and make .nv_fatb sections read-only", formatter_class=argparse.ArgumentDefaultsHelpFormatter )
    parser.add_argument('--input', help="Glob to parse", default="*.dll")
    parser.add_argument('--backup', help="Backup modified files", default=True, required=False)
    parser.add_argument('--recursive', '-r', default=False, action='store_true', help="Recurse into subdirectories")
 
    return parser.parse_args()
 
 
###############################
# program entry point
#
if __name__ == "__main__":
    args = parseArgs()
    main( args )
  1. installpefile
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pefile

image-20230804001137635

  1. Run the fixNvPe.py file in the terminal
python fixNvPe.py --input E:\kaifa\Anaconda3\envs\yolov5\lib\site-packages\torch\lib\cudnn_adv_infer64_8.dll

The path behind input is where the error is reported, the path given later

When the following figure appears, the execution is completed.

image-20230804001547119

RuntimeError: CUDA out of memory.

Solution:

Change to a smaller model

image-20230804004234793

AttributeError: ‘FreeTypeFont’ object has no attribute ‘getsize’

image-20230804004739957

This was because a new version of Pillow (10) was installed , pip install tf-models-officialwhich removed the getsize feature, and downgrading to Pillow 9.5 resolved the issue

Solution:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Pillow==9.5

View log

Terminal inputtensorboard --logdir runs

image-20230804010221749

Training effect detection

Enter the following command on the command line

python detect.py  --weights runs/train/exp/weights/best.pt --source data02/BVN.mp4 --view-img

image-20230804011029138

image-20230804011220424

image-20230804011303814

Guess you like

Origin blog.csdn.net/qq_61228493/article/details/132113992