Object Detection Based on YOLOv5
1. Prepare original model files under windows
Prepare the YOLOv5 development kit and yolov5n.pt file
Download the YOLOv5 development toolkit and the yolov5n.pt file. Once the download is complete, extract the toolkit and place the yolov5n.pt
file in the yolov5-master
directory.
Configure the conda environment
Anaconda needs to be installed in advance.
Open a new Anaconda Prompt
terminal and execute conda env list
to view the current environment.
(base) C:\Users\Carbon> conda env list
# conda environments:
#
base * C:\Users\Carbon\anaconda3
Create a new conda virtual environment and install version 3.9.0 of python. duotpu
is the name you chose.
(base) C:\Users\Carbon> conda create --name duotpu python=3.9.0
Check the current environment again after success.
(base) C:\Users\Carbon> conda env list
# conda environments:
#
base * C:\Users\Carbon\anaconda3
duotpu C:\Users\Carbon\anaconda3\envs\duotpu
Activate the newly installed 3.9.0 environment.
(base) C:\Users\Carbon> conda activate duotpu
Confirm that the activation is successful.
(duotpu) C:\Users\Carbon> conda env list
# conda environments:
#
base C:\Users\Carbon\anaconda3
duotpu * C:\Users\Carbon\anaconda3\envs\duotpu
Then, you can use the following command to install PyTorch version 1.12.1. Please choose the specific installation command based on your requirements. The subsequent process only requires the use of CPU.
# CUDA 10.2
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=10.2 -c pytorch
# CUDA 11.3
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch
# CUDA 11.6
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.6 -c pytorch
# CPU Only
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cpuonly -c pytorch
Then cd
the terminal path to the yolov5-master
path of the development kit and enter pip install -r requirements.txt
to install other dependencies.
(duotpu) C:\Users\Carbon> cd Duo-TPU\yolov5-master
(duotpu) C:\Users\Carbon\Duo-TPU\yolov5-master> pip install -r requirements.txt
Generate original model file
Create a new main.py
file in the yolov5-master
directory and write the following code in the file:
import torch
from models.experimental import attempt_download
model = torch.load(attempt_download("./yolov5n.pt"),
map_location=torch.device('cpu'))['model'].float()
model.eval()
model.model[-1].export = True
torch.jit.trace(model, torch.rand(1, 3, 640, 640), strict=False).save('./yolov5n_jit.pt')
Then find the yolov5-master/models/yolo.py
file, comment the code from lines 99 to 115, and add the code return x
on line 116, as shown below:
In addition, this file also needs to be modified:
C:\Users\Carbon\anaconda3\envs\duotpu\Lib\site-packages\torch\nn\modules\upsampling.py