glenn-jocher
commited on
Commit
·
883924d
1
Parent(s):
c5d2331
classifier, export, torch seed updates
Browse files- models/export.py +11 -4
- utils/general.py +2 -3
- utils/torch_utils.py +10 -12
models/export.py
CHANGED
@@ -6,6 +6,7 @@ Usage:
|
|
6 |
|
7 |
import argparse
|
8 |
import sys
|
|
|
9 |
|
10 |
sys.path.append('./') # to run '$ python *.py' files in subdirectories
|
11 |
|
@@ -15,7 +16,7 @@ import torch.nn as nn
|
|
15 |
import models
|
16 |
from models.experimental import attempt_load
|
17 |
from utils.activations import Hardswish
|
18 |
-
from utils.general import set_logging
|
19 |
|
20 |
if __name__ == '__main__':
|
21 |
parser = argparse.ArgumentParser()
|
@@ -26,16 +27,22 @@ if __name__ == '__main__':
|
|
26 |
opt.img_size *= 2 if len(opt.img_size) == 1 else 1 # expand
|
27 |
print(opt)
|
28 |
set_logging()
|
|
|
29 |
|
30 |
# Input
|
31 |
img = torch.zeros((opt.batch_size, 3, *opt.img_size)) # image size(1,3,320,192) iDetection
|
32 |
|
33 |
# Load PyTorch model
|
34 |
model = attempt_load(opt.weights, map_location=torch.device('cpu')) # load FP32 model
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
# Update model
|
37 |
for k, m in model.named_modules():
|
38 |
-
m._non_persistent_buffers_set = set() # pytorch 1.6.0
|
39 |
if isinstance(m, models.common.Conv) and isinstance(m.act, nn.Hardswish):
|
40 |
m.act = Hardswish() # assign activation
|
41 |
# if isinstance(m, models.yolo.Detect):
|
@@ -76,7 +83,7 @@ if __name__ == '__main__':
|
|
76 |
|
77 |
print('\nStarting CoreML export with coremltools %s...' % ct.__version__)
|
78 |
# convert model from torchscript and apply pixel scaling as per detect.py
|
79 |
-
model = ct.convert(ts, inputs=[ct.ImageType(name='
|
80 |
f = opt.weights.replace('.pt', '.mlmodel') # filename
|
81 |
model.save(f)
|
82 |
print('CoreML export success, saved as %s' % f)
|
@@ -84,4 +91,4 @@ if __name__ == '__main__':
|
|
84 |
print('CoreML export failure: %s' % e)
|
85 |
|
86 |
# Finish
|
87 |
-
print('\nExport complete. Visualize with https://github.com/lutzroeder/netron.')
|
|
|
6 |
|
7 |
import argparse
|
8 |
import sys
|
9 |
+
import time
|
10 |
|
11 |
sys.path.append('./') # to run '$ python *.py' files in subdirectories
|
12 |
|
|
|
16 |
import models
|
17 |
from models.experimental import attempt_load
|
18 |
from utils.activations import Hardswish
|
19 |
+
from utils.general import set_logging, check_img_size
|
20 |
|
21 |
if __name__ == '__main__':
|
22 |
parser = argparse.ArgumentParser()
|
|
|
27 |
opt.img_size *= 2 if len(opt.img_size) == 1 else 1 # expand
|
28 |
print(opt)
|
29 |
set_logging()
|
30 |
+
t = time.time()
|
31 |
|
32 |
# Input
|
33 |
img = torch.zeros((opt.batch_size, 3, *opt.img_size)) # image size(1,3,320,192) iDetection
|
34 |
|
35 |
# Load PyTorch model
|
36 |
model = attempt_load(opt.weights, map_location=torch.device('cpu')) # load FP32 model
|
37 |
+
labels = model.names
|
38 |
+
|
39 |
+
# Checks
|
40 |
+
gs = int(max(model.stride)) # grid size (max stride)
|
41 |
+
opt.img_size = [check_img_size(x, gs) for x in opt.img_size] # verify img_size are gs-multiples
|
42 |
|
43 |
# Update model
|
44 |
for k, m in model.named_modules():
|
45 |
+
m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatibility
|
46 |
if isinstance(m, models.common.Conv) and isinstance(m.act, nn.Hardswish):
|
47 |
m.act = Hardswish() # assign activation
|
48 |
# if isinstance(m, models.yolo.Detect):
|
|
|
83 |
|
84 |
print('\nStarting CoreML export with coremltools %s...' % ct.__version__)
|
85 |
# convert model from torchscript and apply pixel scaling as per detect.py
|
86 |
+
model = ct.convert(ts, inputs=[ct.ImageType(name='image', shape=img.shape, scale=1 / 255.0, bias=[0, 0, 0])])
|
87 |
f = opt.weights.replace('.pt', '.mlmodel') # filename
|
88 |
model.save(f)
|
89 |
print('CoreML export success, saved as %s' % f)
|
|
|
91 |
print('CoreML export failure: %s' % e)
|
92 |
|
93 |
# Finish
|
94 |
+
print('\nExport complete (%.2fs). Visualize with https://github.com/lutzroeder/netron.' % (time.time() - t))
|
utils/general.py
CHANGED
@@ -23,8 +23,7 @@ from scipy.signal import butter, filtfilt
|
|
23 |
from tqdm import tqdm
|
24 |
|
25 |
from utils.google_utils import gsutil_getsize
|
26 |
-
from utils.torch_utils import
|
27 |
-
from utils.torch_utils import is_parallel
|
28 |
|
29 |
# Set printoptions
|
30 |
torch.set_printoptions(linewidth=320, precision=5, profile='long')
|
@@ -56,7 +55,7 @@ def set_logging(rank=-1):
|
|
56 |
def init_seeds(seed=0):
|
57 |
random.seed(seed)
|
58 |
np.random.seed(seed)
|
59 |
-
init_torch_seeds(seed
|
60 |
|
61 |
|
62 |
def get_latest_run(search_dir='./runs'):
|
|
|
23 |
from tqdm import tqdm
|
24 |
|
25 |
from utils.google_utils import gsutil_getsize
|
26 |
+
from utils.torch_utils import is_parallel, init_torch_seeds
|
|
|
27 |
|
28 |
# Set printoptions
|
29 |
torch.set_printoptions(linewidth=320, precision=5, profile='long')
|
|
|
55 |
def init_seeds(seed=0):
|
56 |
random.seed(seed)
|
57 |
np.random.seed(seed)
|
58 |
+
init_torch_seeds(seed)
|
59 |
|
60 |
|
61 |
def get_latest_run(search_dir='./runs'):
|
utils/torch_utils.py
CHANGED
@@ -8,12 +8,11 @@ import torch
|
|
8 |
import torch.backends.cudnn as cudnn
|
9 |
import torch.nn as nn
|
10 |
import torch.nn.functional as F
|
11 |
-
import torchvision.models as models
|
12 |
|
13 |
logger = logging.getLogger(__name__)
|
14 |
|
15 |
|
16 |
-
def
|
17 |
torch.manual_seed(seed)
|
18 |
|
19 |
# Speed-reproducibility tradeoff https://pytorch.org/docs/stable/notes/randomness.html
|
@@ -152,16 +151,15 @@ def model_info(model, verbose=False):
|
|
152 |
|
153 |
def load_classifier(name='resnet101', n=2):
|
154 |
# Loads a pretrained model reshaped to n-class output
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
print(x + ' =', eval(x))
|
165 |
|
166 |
# Reshape output to n classes
|
167 |
filters = model.fc.weight.shape[1]
|
|
|
8 |
import torch.backends.cudnn as cudnn
|
9 |
import torch.nn as nn
|
10 |
import torch.nn.functional as F
|
|
|
11 |
|
12 |
logger = logging.getLogger(__name__)
|
13 |
|
14 |
|
15 |
+
def init_torch_seeds(seed=0):
|
16 |
torch.manual_seed(seed)
|
17 |
|
18 |
# Speed-reproducibility tradeoff https://pytorch.org/docs/stable/notes/randomness.html
|
|
|
151 |
|
152 |
def load_classifier(name='resnet101', n=2):
|
153 |
# Loads a pretrained model reshaped to n-class output
|
154 |
+
import torchvision
|
155 |
+
model = torchvision.models.__dict__[name](pretrained=True)
|
156 |
+
|
157 |
+
# ResNet model properties
|
158 |
+
# input_size = [3, 224, 224]
|
159 |
+
# input_space = 'RGB'
|
160 |
+
# input_range = [0, 1]
|
161 |
+
# mean = [0.485, 0.456, 0.406]
|
162 |
+
# std = [0.229, 0.224, 0.225]
|
|
|
163 |
|
164 |
# Reshape output to n classes
|
165 |
filters = model.fc.weight.shape[1]
|