glenn-jocher commited on
Commit
19e2482
1 Parent(s): 81d3201

PyTorch version to screen and cleanup (#1325)

Browse files

* Create flatten_recursive() helper function

* cleanup

* print torch version

Files changed (4) hide show
  1. models/yolo.py +3 -4
  2. test.py +2 -1
  3. utils/datasets.py +8 -0
  4. utils/torch_utils.py +5 -6
models/yolo.py CHANGED
@@ -1,11 +1,10 @@
1
  import argparse
2
  import logging
 
3
  import sys
4
  from copy import deepcopy
5
  from pathlib import Path
6
 
7
- import math
8
-
9
  sys.path.append('./') # to run '$ python *.py' files in subdirectories
10
  logger = logging.getLogger(__name__)
11
 
@@ -74,7 +73,7 @@ class Model(nn.Module):
74
 
75
  # Define model
76
  if nc and nc != self.yaml['nc']:
77
- print('Overriding model.yaml nc=%g with nc=%g' % (self.yaml['nc'], nc))
78
  self.yaml['nc'] = nc # override yaml value
79
  self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist, ch_out
80
  # print([x.shape for x in self.forward(torch.zeros(1, ch, 64, 64))])
@@ -93,7 +92,7 @@ class Model(nn.Module):
93
  # Init weights, biases
94
  initialize_weights(self)
95
  self.info()
96
- print('')
97
 
98
  def forward(self, x, augment=False, profile=False):
99
  if augment:
 
1
  import argparse
2
  import logging
3
+ import math
4
  import sys
5
  from copy import deepcopy
6
  from pathlib import Path
7
 
 
 
8
  sys.path.append('./') # to run '$ python *.py' files in subdirectories
9
  logger = logging.getLogger(__name__)
10
 
 
73
 
74
  # Define model
75
  if nc and nc != self.yaml['nc']:
76
+ logger.info('Overriding model.yaml nc=%g with nc=%g' % (self.yaml['nc'], nc))
77
  self.yaml['nc'] = nc # override yaml value
78
  self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist, ch_out
79
  # print([x.shape for x in self.forward(torch.zeros(1, ch, 64, 64))])
 
92
  # Init weights, biases
93
  initialize_weights(self)
94
  self.info()
95
+ logger.info('')
96
 
97
  def forward(self, x, augment=False, profile=False):
98
  if augment:
test.py CHANGED
@@ -262,7 +262,8 @@ def test(data,
262
  print('ERROR: pycocotools unable to run: %s' % e)
263
 
264
  # Return results
265
- print('Results saved to %s' % save_dir)
 
266
  model.float() # for training
267
  maps = np.zeros(nc) + map
268
  for i, c in enumerate(ap_class):
 
262
  print('ERROR: pycocotools unable to run: %s' % e)
263
 
264
  # Return results
265
+ if not training:
266
+ print('Results saved to %s' % save_dir)
267
  model.float() # for training
268
  maps = np.zeros(nc) + map
269
  for i, c in enumerate(ap_class):
utils/datasets.py CHANGED
@@ -946,3 +946,11 @@ def create_folder(path='./new'):
946
  if os.path.exists(path):
947
  shutil.rmtree(path) # delete output folder
948
  os.makedirs(path) # make new output folder
 
 
 
 
 
 
 
 
 
946
  if os.path.exists(path):
947
  shutil.rmtree(path) # delete output folder
948
  os.makedirs(path) # make new output folder
949
+
950
+
951
+ def flatten_recursive(path='../coco128'):
952
+ # Flatten a recursive directory by bringing all files to top level
953
+ new_path = Path(path + '_flat')
954
+ create_folder(new_path)
955
+ for file in tqdm(glob.glob(str(Path(path)) + '/**/*.*', recursive=True)):
956
+ shutil.copyfile(file, new_path / Path(file).name)
utils/torch_utils.py CHANGED
@@ -1,9 +1,9 @@
1
  import logging
 
2
  import os
3
  import time
4
  from copy import deepcopy
5
 
6
- import math
7
  import torch
8
  import torch.backends.cudnn as cudnn
9
  import torch.nn as nn
@@ -39,14 +39,13 @@ def select_device(device='', batch_size=None):
39
  if ng > 1 and batch_size: # check that batch_size is compatible with device_count
40
  assert batch_size % ng == 0, 'batch-size %g not multiple of GPU count %g' % (batch_size, ng)
41
  x = [torch.cuda.get_device_properties(i) for i in range(ng)]
42
- s = 'Using CUDA '
43
  for i in range(0, ng):
44
  if i == 1:
45
  s = ' ' * len(s)
46
- logger.info("%sdevice%g _CudaDeviceProperties(name='%s', total_memory=%dMB)" %
47
- (s, i, x[i].name, x[i].total_memory / c))
48
  else:
49
- logger.info('Using CPU')
50
 
51
  logger.info('') # skip a line
52
  return torch.device('cuda:0' if cuda else 'cpu')
@@ -143,7 +142,7 @@ def model_info(model, verbose=False):
143
  from thop import profile
144
  flops = profile(deepcopy(model), inputs=(torch.zeros(1, 3, 64, 64),), verbose=False)[0] / 1E9 * 2
145
  fs = ', %.1f GFLOPS' % (flops * 100) # 640x640 FLOPS
146
- except:
147
  fs = ''
148
 
149
  logger.info(
 
1
  import logging
2
+ import math
3
  import os
4
  import time
5
  from copy import deepcopy
6
 
 
7
  import torch
8
  import torch.backends.cudnn as cudnn
9
  import torch.nn as nn
 
39
  if ng > 1 and batch_size: # check that batch_size is compatible with device_count
40
  assert batch_size % ng == 0, 'batch-size %g not multiple of GPU count %g' % (batch_size, ng)
41
  x = [torch.cuda.get_device_properties(i) for i in range(ng)]
42
+ s = f'Using torch {torch.__version__} '
43
  for i in range(0, ng):
44
  if i == 1:
45
  s = ' ' * len(s)
46
+ logger.info("%sCUDA:%g (%s, %dMB)" % (s, i, x[i].name, x[i].total_memory / c))
 
47
  else:
48
+ logger.info(f'Using torch {torch.__version__} CPU')
49
 
50
  logger.info('') # skip a line
51
  return torch.device('cuda:0' if cuda else 'cpu')
 
142
  from thop import profile
143
  flops = profile(deepcopy(model), inputs=(torch.zeros(1, 3, 64, 64),), verbose=False)[0] / 1E9 * 2
144
  fs = ', %.1f GFLOPS' % (flops * 100) # 640x640 FLOPS
145
+ except ImportError:
146
  fs = ''
147
 
148
  logger.info(