Model checking
key parameter
weights: trained model file
source: the detection target, which can be a single picture, folder, screen or camera, etc.
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
activate environment
python -m ipykernel install --name yolov5
Create a new Jupyter file
hub_detect.ipynb
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
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 input
labelimg
-
Open the folder that needs to be marked
images
- Set save path
- Click the logo in the picture below and change the save format to YOLO
- Turn on auto-save
- To start labeling, right-click
Create RectBox
- Frame two characters and name them
-
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
Model training
Adjust file naming
The naming is strictly consistent and cannot be changed.
File adjustments
Set up yaml file
- Copy the coco128.yaml file and name it bvn.yaml
- Modify bvn.yaml
- Modify train.py
Change the file name to bvn
workers set to 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.
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.
1. Change virtual memory
- You should be able to enter advanced system settings, so I won’t talk about the process.
- Set virtual memory size
2. Reduce the occupied content size
- 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 )
- installpefile
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pefile
- 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.
RuntimeError: CUDA out of memory.
Solution:
Change to a smaller model
AttributeError: ‘FreeTypeFont’ object has no attribute ‘getsize’
This was because a new version of Pillow (10) was installed , pip install tf-models-official
which 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
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