forked from lyuwenyu/RT-DETR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hubconf.py
174 lines (132 loc) · 5.5 KB
/
hubconf.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""Copyright(c) 2024 lyuwenyu. All Rights Reserved.
"""
import os
import sys
from pathlib import Path
from urllib.parse import urlparse
ROOT = Path(__file__).absolute().parent / 'rtdetrv2_pytorch'
sys.path.append(str(ROOT))
from src.core import YAMLConfig
import torch
import torch.nn as nn
dependencies = ['torch', 'torchvision',]
def _load_checkpoint(path: str, map_location='cpu'):
scheme = urlparse(str(path)).scheme
if not scheme:
state = torch.load(path, map_location=map_location)
else:
state = torch.hub.load_state_dict_from_url(path, map_location=map_location)
return state
def _build_model(args, ):
"""main
"""
cfg = YAMLConfig(args.config)
if args.resume:
checkpoint = _load_checkpoint(args.resume, map_location='cpu')
if 'ema' in checkpoint:
state = checkpoint['ema']['module']
else:
state = checkpoint['model']
# NOTE load train mode state
cfg.model.load_state_dict(state)
class Model(nn.Module):
def __init__(self, ) -> None:
super().__init__()
self.model = cfg.model.deploy()
self.postprocessor = cfg.postprocessor.deploy()
def forward(self, images, orig_target_sizes):
outputs = self.model(images)
outputs = self.postprocessor(outputs, orig_target_sizes)
return outputs
return Model()
CONFIG = {
# rtdetr
'rtdetr_r18vd': {
'config': ROOT / 'configs/rtdetr/rtdetr_r18vd_6x_coco.yml',
'resume': 'https://github.com/lyuwenyu/storage/releases/download/v0.1/rtdetr_r18vd_dec3_6x_coco_from_paddle.pth',
},
'rtdetr_r34vd': {
'config': ROOT / 'configs/rtdetr/rtdetr_r34vd_6x_coco.yml',
'resume': 'https://github.com/lyuwenyu/storage/releases/download/v0.1/rtdetr_r34vd_dec4_6x_coco_from_paddle.pth',
},
'rtdetr_r50vd_m': {
'config': ROOT / 'configs/rtdetr/rtdetr_r50vd_m_6x_coco.yml',
'resume': 'https://github.com/lyuwenyu/storage/releases/download/v0.1/rtdetr_r50vd_m_6x_coco_from_paddle.pth',
},
'rtdetr_r50vd': {
'config': ROOT / 'configs/rtdetr/rtdetr_r50vd_6x_coco.yml',
'resume': 'https://github.com/lyuwenyu/storage/releases/download/v0.1/rtdetr_r50vd_6x_coco_from_paddle.pth',
},
'rtdetr_r101vd': {
'config': ROOT / 'configs/rtdetr/rtdetr_r101vd_6x_coco.yml',
'resume': 'https://github.com/lyuwenyu/storage/releases/download/v0.1/rtdetr_r101vd_6x_coco_from_paddle.pth',
},
# rtdetrv2
'rtdetrv2_r18vd': {
'config': ROOT / 'configs/rtdetrv2/rtdetrv2_r18vd_120e_coco.yml',
'resume': 'https://github.com/lyuwenyu/storage/releases/download/v0.2/rtdetrv2_r18vd_120e_coco_rerun_48.1.pth',
},
'rtdetrv2_r34vd': {
'config': ROOT / 'configs/rtdetrv2/rtdetrv2_r34vd_120e_coco.yml',
'resume': 'https://github.com/lyuwenyu/storage/releases/download/v0.1/rtdetrv2_r34vd_120e_coco_ema.pth',
},
'rtdetrv2_r50vd_m': {
'config': ROOT / 'configs/rtdetrv2/rtdetrv2_r50vd_m_7x_coco.yml',
'resume': 'https://github.com/lyuwenyu/storage/releases/download/v0.1/rtdetrv2_r50vd_m_7x_coco_ema.pth',
},
'rtdetrv2_r50vd': {
'config': ROOT / 'configs/rtdetrv2/rtdetrv2_r50vd_6x_coco.yml',
'resume': 'https://github.com/lyuwenyu/storage/releases/download/v0.1/rtdetrv2_r50vd_6x_coco_ema.pth',
},
'rtdetrv2_r101vd': {
'config': ROOT / 'configs/rtdetrv2/rtdetrv2_r101vd_6x_coco.yml',
'resume': 'https://github.com/lyuwenyu/storage/releases/download/v0.1/rtdetrv2_r101vd_6x_coco_from_paddle.pth',
},
}
# rtdetr
def rtdetr_r18vd(pretrained=True,):
args = type('Args', (), CONFIG['rtdetr_r18vd'])()
args.resume = args.resume if pretrained else ''
return _build_model(args, )
def rtdetr_r34vd(pretrained=True,):
args = type('Args', (), CONFIG['rtdetr_r34vd'])()
args.resume = args.resume if pretrained else ''
return _build_model(args, )
def rtdetr_r50vd_m(pretrained=True):
args = type('Args', (), CONFIG['rtdetr_r50vd_m'])()
args.resume = args.resume if pretrained else ''
return _build_model(args, )
def rtdetr_r50vd(pretrained=True):
args = type('Args', (), CONFIG['rtdetr_r50vd'])()
args.resume = args.resume if pretrained else ''
return _build_model(args, )
def rtdetr_r101vd(pretrained=True):
args = type('Args', (), CONFIG['rtdetr_r101vd'])()
args.resume = args.resume if pretrained else ''
return _build_model(args, )
# rtdetrv2
def rtdetrv2_r18vd(pretrained=True,):
args = type('Args', (), CONFIG['rtdetrv2_r18vd'])()
args.resume = args.resume if pretrained else ''
return _build_model(args, )
def rtdetrv2_r34vd(pretrained=True,):
args = type('Args', (), CONFIG['rtdetrv2_r34vd'])()
args.resume = args.resume if pretrained else ''
return _build_model(args, )
def rtdetrv2_r50vd_m(pretrained=True):
args = type('Args', (), CONFIG['rtdetrv2_r50vd_m'])()
args.resume = args.resume if pretrained else ''
return _build_model(args, )
def rtdetrv2_r50vd(pretrained=True):
args = type('Args', (), CONFIG['rtdetrv2_r50vd'])()
args.resume = args.resume if pretrained else ''
return _build_model(args, )
def rtdetrv2_r101vd(pretrained=True):
args = type('Args', (), CONFIG['rtdetrv2_r101vd'])()
args.resume = args.resume if pretrained else ''
return _build_model(args, )
rtdetrv2_s = rtdetrv2_r18vd
rtdetrv2_m_r34 = rtdetrv2_r34vd
rtdetrv2_m_r50 = rtdetrv2_r50vd_m
rtdetrv2_l = rtdetrv2_r50vd
rtdetrv2_x = rtdetrv2_r101vd