forked from open-mmlab/mmyolo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyolov7_l_anchor_free.py
143 lines (123 loc) · 4.39 KB
/
yolov7_l_anchor_free.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
_base_ = './configs/yolov7/yolov7_l_syncbn_fast_8x16b-300e_coco.py'
data_root = '/root/mmyolo/autodl-tmp/defect_yolo'
work_dir = '/root/mmyolo/autodl-tmp/work_dirs/yolov7_m_anchor_free_car_defect'
train_batch_size_per_gpu = 16
train_num_workers = 4 # 推荐使用 train_num_workers = nGPU x 4
val_batch_size_per_gpu = 8
val_num_workers = 4
max_epochs = 200
img_scale = (1280,1280)
save_epoch_intervals = 5
num_det_layers = 3
norm_cfg = dict(type='BN', momentum=0.03, eps=0.001)
strides = [8, 16, 32]
loss_cls_weight = 0.5
loss_bbox_weight = 7.5
loss_dfl_weight = 1.5 / 4
tal_topk = 10 # Number of bbox selected in each level
tal_alpha = 0.5 # A Hyper-parameter related to alignment_metrics
tal_beta = 6.0 # A Hyper-parameter related to alignment_metrics
class_name = ('paint_defect','shape_defect') # 根据 class_with_id.txt 类别信息,设置 class_name
num_classes = len(class_name)
metainfo = dict(
classes=class_name,
palette=[(220, 20, 60),(110, 50, 60)] # 画图时候的颜色,随便设置即可
)
train_cfg = dict(
type='EpochBasedTrainLoop',
max_epochs=max_epochs,
val_interval=save_epoch_intervals,
dynamic_intervals=[(270, 1)])
model_test_cfg = dict(
# The config of multi-label for multi-class prediction.
multi_label=True,
# The number of boxes before NMS
nms_pre=30000,
score_thr=0.001, # Threshold to filter out boxes.
nms=dict(type='nms', iou_threshold=0.7), # NMS type and threshold
max_per_img=300) # Max number of detections of each image
model = dict(
bbox_head=dict(
_delete_=True,
type='YOLOv8Head',
head_module=dict(
type='YOLOv8HeadModule',
num_classes=num_classes,
in_channels=[256, 512, 1024],
widen_factor=1.0,
reg_max=16,
norm_cfg=norm_cfg,
act_cfg=dict(type='SiLU', inplace=True),
featmap_strides=strides),
prior_generator=dict(
type='mmdet.MlvlPointGenerator', offset=0.5, strides=strides),
bbox_coder=dict(type='DistancePointBBoxCoder'),
# scaled based on number of detection layers
loss_cls=dict(
type='mmdet.CrossEntropyLoss',
use_sigmoid=True,
reduction='none',
loss_weight=loss_cls_weight),
loss_bbox=dict(
type='IoULoss',
iou_mode='ciou',
bbox_format='xyxy',
reduction='sum',
loss_weight=loss_bbox_weight,
return_iou=False),
loss_dfl=dict(
type='mmdet.DistributionFocalLoss',
reduction='mean',
loss_weight=loss_dfl_weight)),
train_cfg=dict(
_delete_=True,
assigner=dict(
type='BatchTaskAlignedAssigner',
num_classes=num_classes,
use_ciou=True,
topk=tal_topk,
alpha=tal_alpha,
beta=tal_beta,
eps=1e-9)),
test_cfg=model_test_cfg)
train_dataloader = dict(
batch_size=train_batch_size_per_gpu,
num_workers=train_num_workers,
dataset=dict(
_delete_=True,
type='RepeatDataset',
# 数据量太少的话,可以使用 RepeatDataset ,在每个 epoch 内重复当前数据集 n 次,这里设置 5 是重复 5 次
times=5,
dataset=dict(
type=_base_.dataset_type,
data_root=data_root,
metainfo=metainfo,
ann_file='annotation/trainval.json',
data_prefix=dict(img='image/'),
filter_cfg=dict(filter_empty_gt=False, min_size=32),
pipeline=_base_.train_pipeline)))
val_dataloader = dict(
batch_size=val_batch_size_per_gpu,
num_workers=val_num_workers,
dataset=dict(
metainfo=metainfo,
data_root=data_root,
ann_file='annotation/trainval.json',
data_prefix=dict(img='image/')))
test_dataloader = val_dataloader
val_evaluator = dict(ann_file=data_root + '/annotation/trainval.json')
test_evaluator = val_evaluator
default_hooks = dict(
logger=dict(interval=1),
visualization=dict(draw=True, interval=1))
visualizer = dict(vis_backends = [dict(type='WandbVisBackend')])
optim_wrapper = dict(
type='OptimWrapper',
optimizer=dict(
type='SGD',
lr=0.01,
momentum=0.937,
weight_decay=0.0005,
nesterov=True,
batch_size_per_gpu=train_batch_size_per_gpu),
constructor='YOLOv7OptimWrapperConstructor')