Nanobit glenn-jocher commited on
Commit
c0d3f80
·
unverified ·
1 Parent(s): 3665c0f

Add verbose option to pytorch hub models (#2926)

Browse files

* Add verbose and update print to logging

* Fix positonal param

* Revert auto formatting changes

* Update hubconf.py

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

Files changed (3) hide show
  1. hubconf.py +28 -25
  2. models/yolo.py +11 -11
  3. utils/general.py +2 -2
hubconf.py CHANGED
@@ -16,10 +16,9 @@ from utils.torch_utils import select_device
16
 
17
  dependencies = ['torch', 'yaml']
18
  check_requirements(Path(__file__).parent / 'requirements.txt', exclude=('pycocotools', 'thop'))
19
- set_logging()
20
 
21
 
22
- def create(name, pretrained, channels, classes, autoshape):
23
  """Creates a specified YOLOv5 model
24
 
25
  Arguments:
@@ -32,6 +31,8 @@ def create(name, pretrained, channels, classes, autoshape):
32
  pytorch model
33
  """
34
  try:
 
 
35
  cfg = list((Path(__file__).parent / 'models').rglob(f'{name}.yaml'))[0] # model.yaml path
36
  model = Model(cfg, channels, classes)
37
  if pretrained:
@@ -55,7 +56,7 @@ def create(name, pretrained, channels, classes, autoshape):
55
  raise Exception(s) from e
56
 
57
 
58
- def custom(path_or_model='path/to/model.pt', autoshape=True):
59
  """YOLOv5-custom model https://github.com/ultralytics/yolov5
60
 
61
  Arguments (3 options):
@@ -66,6 +67,8 @@ def custom(path_or_model='path/to/model.pt', autoshape=True):
66
  Returns:
67
  pytorch model
68
  """
 
 
69
  model = torch.load(path_or_model) if isinstance(path_or_model, str) else path_or_model # load checkpoint
70
  if isinstance(model, dict):
71
  model = model['ema' if model.get('ema') else 'model'] # load model
@@ -79,49 +82,49 @@ def custom(path_or_model='path/to/model.pt', autoshape=True):
79
  return hub_model.to(device)
80
 
81
 
82
- def yolov5s(pretrained=True, channels=3, classes=80, autoshape=True):
83
  # YOLOv5-small model https://github.com/ultralytics/yolov5
84
- return create('yolov5s', pretrained, channels, classes, autoshape)
85
 
86
 
87
- def yolov5m(pretrained=True, channels=3, classes=80, autoshape=True):
88
  # YOLOv5-medium model https://github.com/ultralytics/yolov5
89
- return create('yolov5m', pretrained, channels, classes, autoshape)
90
 
91
 
92
- def yolov5l(pretrained=True, channels=3, classes=80, autoshape=True):
93
  # YOLOv5-large model https://github.com/ultralytics/yolov5
94
- return create('yolov5l', pretrained, channels, classes, autoshape)
95
 
96
 
97
- def yolov5x(pretrained=True, channels=3, classes=80, autoshape=True):
98
  # YOLOv5-xlarge model https://github.com/ultralytics/yolov5
99
- return create('yolov5x', pretrained, channels, classes, autoshape)
100
 
101
 
102
- def yolov5s6(pretrained=True, channels=3, classes=80, autoshape=True):
103
- # YOLOv5-small model https://github.com/ultralytics/yolov5
104
- return create('yolov5s6', pretrained, channels, classes, autoshape)
105
 
106
 
107
- def yolov5m6(pretrained=True, channels=3, classes=80, autoshape=True):
108
- # YOLOv5-medium model https://github.com/ultralytics/yolov5
109
- return create('yolov5m6', pretrained, channels, classes, autoshape)
110
 
111
 
112
- def yolov5l6(pretrained=True, channels=3, classes=80, autoshape=True):
113
- # YOLOv5-large model https://github.com/ultralytics/yolov5
114
- return create('yolov5l6', pretrained, channels, classes, autoshape)
115
 
116
 
117
- def yolov5x6(pretrained=True, channels=3, classes=80, autoshape=True):
118
- # YOLOv5-xlarge model https://github.com/ultralytics/yolov5
119
- return create('yolov5x6', pretrained, channels, classes, autoshape)
120
 
121
 
122
  if __name__ == '__main__':
123
- model = create(name='yolov5s', pretrained=True, channels=3, classes=80, autoshape=True) # pretrained example
124
- # model = custom(path_or_model='path/to/model.pt') # custom example
125
 
126
  # Verify inference
127
  import cv2
 
16
 
17
  dependencies = ['torch', 'yaml']
18
  check_requirements(Path(__file__).parent / 'requirements.txt', exclude=('pycocotools', 'thop'))
 
19
 
20
 
21
+ def create(name, pretrained, channels, classes, autoshape, verbose):
22
  """Creates a specified YOLOv5 model
23
 
24
  Arguments:
 
31
  pytorch model
32
  """
33
  try:
34
+ set_logging(verbose=verbose)
35
+
36
  cfg = list((Path(__file__).parent / 'models').rglob(f'{name}.yaml'))[0] # model.yaml path
37
  model = Model(cfg, channels, classes)
38
  if pretrained:
 
56
  raise Exception(s) from e
57
 
58
 
59
+ def custom(path_or_model='path/to/model.pt', autoshape=True, verbose=True):
60
  """YOLOv5-custom model https://github.com/ultralytics/yolov5
61
 
62
  Arguments (3 options):
 
67
  Returns:
68
  pytorch model
69
  """
70
+ set_logging(verbose=verbose)
71
+
72
  model = torch.load(path_or_model) if isinstance(path_or_model, str) else path_or_model # load checkpoint
73
  if isinstance(model, dict):
74
  model = model['ema' if model.get('ema') else 'model'] # load model
 
82
  return hub_model.to(device)
83
 
84
 
85
+ def yolov5s(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True):
86
  # YOLOv5-small model https://github.com/ultralytics/yolov5
87
+ return create('yolov5s', pretrained, channels, classes, autoshape, verbose)
88
 
89
 
90
+ def yolov5m(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True):
91
  # YOLOv5-medium model https://github.com/ultralytics/yolov5
92
+ return create('yolov5m', pretrained, channels, classes, autoshape, verbose)
93
 
94
 
95
+ def yolov5l(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True):
96
  # YOLOv5-large model https://github.com/ultralytics/yolov5
97
+ return create('yolov5l', pretrained, channels, classes, autoshape, verbose)
98
 
99
 
100
+ def yolov5x(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True):
101
  # YOLOv5-xlarge model https://github.com/ultralytics/yolov5
102
+ return create('yolov5x', pretrained, channels, classes, autoshape, verbose)
103
 
104
 
105
+ def yolov5s6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True):
106
+ # YOLOv5-small-P6 model https://github.com/ultralytics/yolov5
107
+ return create('yolov5s6', pretrained, channels, classes, autoshape, verbose)
108
 
109
 
110
+ def yolov5m6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True):
111
+ # YOLOv5-medium-P6 model https://github.com/ultralytics/yolov5
112
+ return create('yolov5m6', pretrained, channels, classes, autoshape, verbose)
113
 
114
 
115
+ def yolov5l6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True):
116
+ # YOLOv5-large-P6 model https://github.com/ultralytics/yolov5
117
+ return create('yolov5l6', pretrained, channels, classes, autoshape, verbose)
118
 
119
 
120
+ def yolov5x6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True):
121
+ # YOLOv5-xlarge-P6 model https://github.com/ultralytics/yolov5
122
+ return create('yolov5x6', pretrained, channels, classes, autoshape, verbose)
123
 
124
 
125
  if __name__ == '__main__':
126
+ model = create(name='yolov5s', pretrained=True, channels=3, classes=80, autoshape=True, verbose=True) # pretrained
127
+ # model = custom(path_or_model='path/to/model.pt') # custom
128
 
129
  # Verify inference
130
  import cv2
models/yolo.py CHANGED
@@ -84,7 +84,7 @@ class Model(nn.Module):
84
  self.yaml['anchors'] = round(anchors) # override yaml value
85
  self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist
86
  self.names = [str(i) for i in range(self.yaml['nc'])] # default names
87
- # print([x.shape for x in self.forward(torch.zeros(1, ch, 64, 64))])
88
 
89
  # Build strides, anchors
90
  m = self.model[-1] # Detect()
@@ -95,7 +95,7 @@ class Model(nn.Module):
95
  check_anchor_order(m)
96
  self.stride = m.stride
97
  self._initialize_biases() # only run once
98
- # print('Strides: %s' % m.stride.tolist())
99
 
100
  # Init weights, biases
101
  initialize_weights(self)
@@ -134,13 +134,13 @@ class Model(nn.Module):
134
  for _ in range(10):
135
  _ = m(x)
136
  dt.append((time_synchronized() - t) * 100)
137
- print('%10.1f%10.0f%10.1fms %-40s' % (o, m.np, dt[-1], m.type))
138
 
139
  x = m(x) # run
140
  y.append(x if m.i in self.save else None) # save output
141
 
142
  if profile:
143
- print('%.1fms total' % sum(dt))
144
  return x
145
 
146
  def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency
@@ -157,15 +157,15 @@ class Model(nn.Module):
157
  m = self.model[-1] # Detect() module
158
  for mi in m.m: # from
159
  b = mi.bias.detach().view(m.na, -1).T # conv.bias(255) to (3,85)
160
- print(('%6g Conv2d.bias:' + '%10.3g' * 6) % (mi.weight.shape[1], *b[:5].mean(1).tolist(), b[5:].mean()))
161
 
162
  # def _print_weights(self):
163
  # for m in self.model.modules():
164
  # if type(m) is Bottleneck:
165
- # print('%10.3g' % (m.w.detach().sigmoid() * 2)) # shortcut weights
166
 
167
  def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers
168
- print('Fusing layers... ')
169
  for m in self.model.modules():
170
  if type(m) is Conv and hasattr(m, 'bn'):
171
  m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv
@@ -177,19 +177,19 @@ class Model(nn.Module):
177
  def nms(self, mode=True): # add or remove NMS module
178
  present = type(self.model[-1]) is NMS # last layer is NMS
179
  if mode and not present:
180
- print('Adding NMS... ')
181
  m = NMS() # module
182
  m.f = -1 # from
183
  m.i = self.model[-1].i + 1 # index
184
  self.model.add_module(name='%s' % m.i, module=m) # add
185
  self.eval()
186
  elif not mode and present:
187
- print('Removing NMS... ')
188
  self.model = self.model[:-1] # remove
189
  return self
190
 
191
  def autoshape(self): # add autoShape module
192
- print('Adding autoShape... ')
193
  m = autoShape(self) # wrap model
194
  copy_attr(m, self, include=('yaml', 'nc', 'hyp', 'names', 'stride'), exclude=()) # copy attributes
195
  return m
@@ -272,6 +272,6 @@ if __name__ == '__main__':
272
  # Tensorboard (not working https://github.com/ultralytics/yolov5/issues/2898)
273
  # from torch.utils.tensorboard import SummaryWriter
274
  # tb_writer = SummaryWriter('.')
275
- # print("Run 'tensorboard --logdir=models' to view tensorboard at http://localhost:6006/")
276
  # tb_writer.add_graph(torch.jit.trace(model, img, strict=False), []) # add model graph
277
  # tb_writer.add_image('test', img[0], dataformats='CWH') # add model to tensorboard
 
84
  self.yaml['anchors'] = round(anchors) # override yaml value
85
  self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist
86
  self.names = [str(i) for i in range(self.yaml['nc'])] # default names
87
+ # logger.info([x.shape for x in self.forward(torch.zeros(1, ch, 64, 64))])
88
 
89
  # Build strides, anchors
90
  m = self.model[-1] # Detect()
 
95
  check_anchor_order(m)
96
  self.stride = m.stride
97
  self._initialize_biases() # only run once
98
+ # logger.info('Strides: %s' % m.stride.tolist())
99
 
100
  # Init weights, biases
101
  initialize_weights(self)
 
134
  for _ in range(10):
135
  _ = m(x)
136
  dt.append((time_synchronized() - t) * 100)
137
+ logger.info('%10.1f%10.0f%10.1fms %-40s' % (o, m.np, dt[-1], m.type))
138
 
139
  x = m(x) # run
140
  y.append(x if m.i in self.save else None) # save output
141
 
142
  if profile:
143
+ logger.info('%.1fms total' % sum(dt))
144
  return x
145
 
146
  def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency
 
157
  m = self.model[-1] # Detect() module
158
  for mi in m.m: # from
159
  b = mi.bias.detach().view(m.na, -1).T # conv.bias(255) to (3,85)
160
+ logger.info(('%6g Conv2d.bias:' + '%10.3g' * 6) % (mi.weight.shape[1], *b[:5].mean(1).tolist(), b[5:].mean()))
161
 
162
  # def _print_weights(self):
163
  # for m in self.model.modules():
164
  # if type(m) is Bottleneck:
165
+ # logger.info('%10.3g' % (m.w.detach().sigmoid() * 2)) # shortcut weights
166
 
167
  def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers
168
+ logger.info('Fusing layers... ')
169
  for m in self.model.modules():
170
  if type(m) is Conv and hasattr(m, 'bn'):
171
  m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv
 
177
  def nms(self, mode=True): # add or remove NMS module
178
  present = type(self.model[-1]) is NMS # last layer is NMS
179
  if mode and not present:
180
+ logger.info('Adding NMS... ')
181
  m = NMS() # module
182
  m.f = -1 # from
183
  m.i = self.model[-1].i + 1 # index
184
  self.model.add_module(name='%s' % m.i, module=m) # add
185
  self.eval()
186
  elif not mode and present:
187
+ logger.info('Removing NMS... ')
188
  self.model = self.model[:-1] # remove
189
  return self
190
 
191
  def autoshape(self): # add autoShape module
192
+ logger.info('Adding autoShape... ')
193
  m = autoShape(self) # wrap model
194
  copy_attr(m, self, include=('yaml', 'nc', 'hyp', 'names', 'stride'), exclude=()) # copy attributes
195
  return m
 
272
  # Tensorboard (not working https://github.com/ultralytics/yolov5/issues/2898)
273
  # from torch.utils.tensorboard import SummaryWriter
274
  # tb_writer = SummaryWriter('.')
275
+ # logger.info("Run 'tensorboard --logdir=models' to view tensorboard at http://localhost:6006/")
276
  # tb_writer.add_graph(torch.jit.trace(model, img, strict=False), []) # add model graph
277
  # tb_writer.add_image('test', img[0], dataformats='CWH') # add model to tensorboard
utils/general.py CHANGED
@@ -32,10 +32,10 @@ cv2.setNumThreads(0) # prevent OpenCV from multithreading (incompatible with Py
32
  os.environ['NUMEXPR_MAX_THREADS'] = str(min(os.cpu_count(), 8)) # NumExpr max threads
33
 
34
 
35
- def set_logging(rank=-1):
36
  logging.basicConfig(
37
  format="%(message)s",
38
- level=logging.INFO if rank in [-1, 0] else logging.WARN)
39
 
40
 
41
  def init_seeds(seed=0):
 
32
  os.environ['NUMEXPR_MAX_THREADS'] = str(min(os.cpu_count(), 8)) # NumExpr max threads
33
 
34
 
35
+ def set_logging(rank=-1, verbose=True):
36
  logging.basicConfig(
37
  format="%(message)s",
38
+ level=logging.INFO if (verbose and rank in [-1, 0]) else logging.WARN)
39
 
40
 
41
  def init_seeds(seed=0):