源码地址
https://github.com/AlibabaResearch/efficientteacher
创建虚拟环境
conda create -n SSOD python=3.11 -y
conda activate SSOD
安装依赖
pip install -r requirements.txt
数据集划分
这里提供一种思路:具体的数据集划分按照自己的需求进行,假设现在有1000张图片数据用于训练。
1、首先划分出全监督的数据集:取出10%作为全监督训练的数据,即100张。在100张的基础上划分出train、val、test,按照7:2:1的比例划分。在YOLOv5模型基础上训练出一个pt文件,命名为:pre_v5.pt
Dataset:
data_name: 'crowdhuman'
train: dataset_crowdhuman/train.txt # 70 images
val: dataset_crowdhuman/val.txt # 20 images
test: dataset_crowdhuman/test.txt # 10 images
nc: 2 # 类别数量
np: 0 # number of keypoints
names: ['head', 'person']
img_size: 640
batch_size: 64
workers: 8
2、进行半监督学习的训练,拿出剩余的90%数据参与到新的训练中,该数据没有标签,只有图片。
Dataset:
data_name: 'crowdhuman'
train: dataset_crowdhuman/train.txt # 70 images
val: dataset_crowdhuman/val.txt # 20 images
test: dataset_crowdhuman/test.txt # 10 images
target: dataset_crowdhuman/unlabeled_train.txt # 900 images
nc: 2 # number of classes
np: 0 # number of keypoints
names: ['head', 'person']
img_size: 640
batch_size: 32
workers: 8
在这里给出两个yaml文件,分别用于全监督和半监督学习。
# 全监督学习的YAML文件
# Parameters
project: 'sup_runs'
name: 'yolov5n_crowdhuman_1_percent'
adam: False
epochs: 500
weights: 'efficient-yolov5n.pt' # 初始化权重用YOLOv5官方的
prune_finetune: False
linear_lr: True
cache: True # Requires large memory
hyp:
lr0: 0.01
lrf: 0.01
hsv_h: 0.015
hsv_s: 0.7
hsv_v: 0.4
scale: 0.9
translate: 0.1
fliplr: 0.5
mosaic: 1.0
mixup: 0.1
warmup_epochs: 3
no_aug_epochs: 30
Model:
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.25 # layer channel multiple
Backbone:
name: 'YoloV5'
activation: 'SiLU'
Neck:
name: 'YoloV5'
in_channels: [256, 512, 1024]
out_channels: [256, 512, 1024]
activation: 'SiLU'
Head:
name: 'YoloV5'
activation: 'SiLU'
anchors: [[10,13, 16,30, 33,23],[30,61, 62,45, 59,119],[116,90, 156,198, 373,326]] # P5/32]
Loss:
type: 'ComputeLoss'
cls: 0.3
obj: 0.7
box: 0.05
anchor_t: 4.0
Dataset:
data_name: 'crowdhuman'
train: dataset_crowdhuman/train.txt # 70 images
val: dataset_crowdhuman/val.txt # 20 images
test: dataset_crowdhuman/test.txt # 10 images
nc: 2 # number of classes
np: 0 # number of keypoints
names: ['head', 'person']
img_size: 640
batch_size: 64
workers: 8
# 半监督学习的YAML文件
# Parameters
project: 'ssod_runs'
name: 'yolov5n_crowdhuamn_1per_exp1_ssod'
adam: False
epochs: 300
weights: 'sup_runs/yolov5n_crowdhuman_1_percent/weights/best.pt'
prune_finetune: False
linear_lr: True
cache: True
hyp:
lr0: 0.01
lrf: 1.0
hsv_h: 0.015
hsv_s: 0.7
hsv_v: 0.4
scale: 0.9
translate: 0.1
fliplr: 0.5
mosaic: 1.0
mixup: 0.1
warmup_epochs: 0
no_aug_epochs: 0
burn_epochs: 0
Model:
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.25 # layer channel multiple
Backbone:
name: 'YoloV5'
activation: 'SiLU'
Neck:
name: 'YoloV5'
in_channels: [256, 512, 1024]
out_channels: [256, 512, 1024]
activation: 'SiLU'
Head:
name: 'YoloV5'
activation: 'SiLU'
anchors: [[10,13, 16,30, 33,23],[30,61, 62,45, 59,119],[116,90, 156,198, 373,326]] # P5/32]
Loss:
type: 'ComputeLoss'
cls: 0.3
obj: 0.7
box: 0.05
anchor_t: 4.0
Dataset:
data_name: 'crowdhuman'
train: dataset_crowdhuman/train.txt # 70 images
val: dataset_crowdhuman/val.txt # 20 images
test: dataset_crowdhuman/test.txt # 10 images
target: dataset_crowdhuman/unlabeled_1_percent.txt # 900 images
nc: 2 # number of classes
np: 0 # number of keypoints
names: ['head', 'person']
img_size: 640
batch_size: 32
workers: 8
SSOD:
train_domain: True
nms_conf_thres: 0.1
nms_iou_thres: 0.7
teacher_loss_weight: 0.5
cls_loss_weight: 0.3
box_loss_weight: 0.05
obj_loss_weight: 0.7
loss_type: 'ComputeStudentMatchLoss'
ignore_thres_low: 0.4
ignore_thres_high: 0.45
uncertain_aug: True
use_ota: False
multi_label: False
ignore_obj: False
pseudo_label_with_obj: True
pseudo_label_with_bbox: True
pseudo_label_with_cls: False
with_da_loss: False
da_loss_weights: 0.01
epoch_adaptor: True
resample_high_percent: 0.25
resample_low_percent: 0.99
ema_rate: 0.999
cosine_ema: True
imitate_teacher: False
# dynamic_thres: True
ssod_hyp:
with_gt: False
mosaic: 1.0
cutout: 0.5
autoaugment: 0.5
scale: 0.8
degrees: 0.0
shear: 0.0
训练自己的数据集
#### 1. 转换权重. (yolo => efficient)
python scripts/mula_convertor/convert_pt_to_efficient.py
#### 2. 有监督训练
python train.py --cfg configs/sup/custom/yolov5n_1per.yaml
#### 3. 半监督训练
python train.py --cfg configs/ssod/custom/yolov5n_1per_ssod_exp1.yaml
#### 4. 测试全监督模型.
python val.py --data dataset_crowdhuman/data.yaml --weights sup_runs/yolov5n_crowdhuman_1_percent/weights/best.pt --task test --name yolov5n_crowdhuman_1_percent
#### 5. 测试半监督模型.
python val.py --cfg configs/ssod/custom/yolov5n_1per_ssod_exp1.yaml --weights ssod_runs/yolov5n_crowdhuamn_1per_exp1_ssod/weights/best.pt --task test --name yolov5n_crowdhuamn_1per_exp1_ssod --val-ssod
#### 6. 推理.
python detect.py --weights sup_runs/yolov5n_crowdhuman_1_percent/weights/best.pt --source dataset_crowdhuman/images/test/284193,bf20000e03d9239.jpg --name yolov5n_crowdhuman_1_percent
#### 7. 转换权重. (efficient => yolo)
python scripts/mula_convertor/convert_pt_to_efficient.py