glenn-jocher
commited on
Commit
•
bf6f415
1
Parent(s):
0fef3f6
hyperparameter printout update
Browse files- test.py +0 -1
- train.py +9 -10
- utils/utils.py +6 -5
test.py
CHANGED
@@ -19,7 +19,6 @@ def test(data,
|
|
19 |
dataloader=None,
|
20 |
save_dir='',
|
21 |
merge=False):
|
22 |
-
|
23 |
# Initialize/load model and set device
|
24 |
training = model is not None
|
25 |
if training: # called by train.py
|
|
|
19 |
dataloader=None,
|
20 |
save_dir='',
|
21 |
merge=False):
|
|
|
22 |
# Initialize/load model and set device
|
23 |
training = model is not None
|
24 |
if training: # called by train.py
|
train.py
CHANGED
@@ -20,9 +20,8 @@ except:
|
|
20 |
print('Apex recommended for faster mixed precision training: https://github.com/NVIDIA/apex')
|
21 |
mixed_precision = False # not installed
|
22 |
|
23 |
-
|
24 |
# Hyperparameters
|
25 |
-
hyp = {'optimizer': 'SGD',
|
26 |
'lr0': 0.01, # initial learning rate (SGD=1E-2, Adam=1E-3)
|
27 |
'momentum': 0.937, # SGD momentum/Adam beta1
|
28 |
'weight_decay': 5e-4, # optimizer weight decay
|
@@ -44,6 +43,7 @@ hyp = {'optimizer': 'SGD', # ['adam', 'SGD', None] if none, default is SGD
|
|
44 |
|
45 |
|
46 |
def train(hyp):
|
|
|
47 |
log_dir = tb_writer.log_dir # run directory
|
48 |
wdir = str(Path(log_dir) / 'weights') + os.sep # weights directory
|
49 |
|
@@ -90,7 +90,7 @@ def train(hyp):
|
|
90 |
pg0.append(v) # all else
|
91 |
|
92 |
if hyp['optimizer'] == 'adam': # https://pytorch.org/docs/stable/_modules/torch/optim/lr_scheduler.html#OneCycleLR
|
93 |
-
optimizer = optim.Adam(pg0, lr=hyp['lr0'], betas=(hyp['momentum'], 0.999))
|
94 |
else:
|
95 |
optimizer = optim.SGD(pg0, lr=hyp['lr0'], momentum=hyp['momentum'], nesterov=True)
|
96 |
|
@@ -176,7 +176,7 @@ def train(hyp):
|
|
176 |
yaml.dump(hyp, f, sort_keys=False)
|
177 |
with open(Path(log_dir) / 'opt.yaml', 'w') as f:
|
178 |
yaml.dump(vars(opt), f, sort_keys=False)
|
179 |
-
|
180 |
# Class frequency
|
181 |
labels = np.concatenate(dataset.labels, 0)
|
182 |
c = torch.tensor(labels[:, 0]) # classes
|
@@ -365,7 +365,8 @@ if __name__ == '__main__':
|
|
365 |
parser.add_argument('--batch-size', type=int, default=16)
|
366 |
parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='train,test sizes')
|
367 |
parser.add_argument('--rect', action='store_true', help='rectangular training')
|
368 |
-
parser.add_argument('--resume', nargs='?', const
|
|
|
369 |
parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
|
370 |
parser.add_argument('--notest', action='store_true', help='only test final epoch')
|
371 |
parser.add_argument('--noautoanchor', action='store_true', help='disable autoanchor check')
|
@@ -378,14 +379,14 @@ if __name__ == '__main__':
|
|
378 |
parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')
|
379 |
parser.add_argument('--single-cls', action='store_true', help='train as single-class dataset')
|
380 |
opt = parser.parse_args()
|
381 |
-
|
382 |
last = get_latest_run() if opt.resume == 'get_last' else opt.resume # resume from most recent run
|
383 |
if last and not opt.weights:
|
384 |
print(f'Resuming training from {last}')
|
385 |
opt.weights = last if opt.resume and not opt.weights else opt.weights
|
386 |
opt.cfg = check_file(opt.cfg) # check file
|
387 |
opt.data = check_file(opt.data) # check file
|
388 |
-
opt.hyp = check_file(opt.hyp) if opt.hyp else ''
|
389 |
print(opt)
|
390 |
opt.img_size.extend([opt.img_size[-1]] * (2 - len(opt.img_size))) # extend to 2 sizes (train, test)
|
391 |
device = torch_utils.select_device(opt.device, apex=mixed_precision, batch_size=opt.batch_size)
|
@@ -394,14 +395,12 @@ if __name__ == '__main__':
|
|
394 |
|
395 |
# Train
|
396 |
if not opt.evolve:
|
|
|
397 |
tb_writer = SummaryWriter(comment=opt.name)
|
398 |
if opt.hyp: # update hyps
|
399 |
with open(opt.hyp) as f:
|
400 |
hyp.update(yaml.load(f, Loader=yaml.FullLoader))
|
401 |
|
402 |
-
print(f'Beginning training with {hyp}\n\n')
|
403 |
-
print('Start Tensorboard with "tensorboard --logdir=runs", view at http://localhost:6006/')
|
404 |
-
|
405 |
train(hyp)
|
406 |
|
407 |
# Evolve hyperparameters (optional)
|
|
|
20 |
print('Apex recommended for faster mixed precision training: https://github.com/NVIDIA/apex')
|
21 |
mixed_precision = False # not installed
|
22 |
|
|
|
23 |
# Hyperparameters
|
24 |
+
hyp = {'optimizer': 'SGD', # ['adam', 'SGD', None] if none, default is SGD
|
25 |
'lr0': 0.01, # initial learning rate (SGD=1E-2, Adam=1E-3)
|
26 |
'momentum': 0.937, # SGD momentum/Adam beta1
|
27 |
'weight_decay': 5e-4, # optimizer weight decay
|
|
|
43 |
|
44 |
|
45 |
def train(hyp):
|
46 |
+
print(f'Hyperparameters {hyp}')
|
47 |
log_dir = tb_writer.log_dir # run directory
|
48 |
wdir = str(Path(log_dir) / 'weights') + os.sep # weights directory
|
49 |
|
|
|
90 |
pg0.append(v) # all else
|
91 |
|
92 |
if hyp['optimizer'] == 'adam': # https://pytorch.org/docs/stable/_modules/torch/optim/lr_scheduler.html#OneCycleLR
|
93 |
+
optimizer = optim.Adam(pg0, lr=hyp['lr0'], betas=(hyp['momentum'], 0.999)) # adjust beta1 to momentum
|
94 |
else:
|
95 |
optimizer = optim.SGD(pg0, lr=hyp['lr0'], momentum=hyp['momentum'], nesterov=True)
|
96 |
|
|
|
176 |
yaml.dump(hyp, f, sort_keys=False)
|
177 |
with open(Path(log_dir) / 'opt.yaml', 'w') as f:
|
178 |
yaml.dump(vars(opt), f, sort_keys=False)
|
179 |
+
|
180 |
# Class frequency
|
181 |
labels = np.concatenate(dataset.labels, 0)
|
182 |
c = torch.tensor(labels[:, 0]) # classes
|
|
|
365 |
parser.add_argument('--batch-size', type=int, default=16)
|
366 |
parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='train,test sizes')
|
367 |
parser.add_argument('--rect', action='store_true', help='rectangular training')
|
368 |
+
parser.add_argument('--resume', nargs='?', const='get_last', default=False,
|
369 |
+
help='resume from given path/to/last.pt, or most recent run if blank.')
|
370 |
parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
|
371 |
parser.add_argument('--notest', action='store_true', help='only test final epoch')
|
372 |
parser.add_argument('--noautoanchor', action='store_true', help='disable autoanchor check')
|
|
|
379 |
parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')
|
380 |
parser.add_argument('--single-cls', action='store_true', help='train as single-class dataset')
|
381 |
opt = parser.parse_args()
|
382 |
+
|
383 |
last = get_latest_run() if opt.resume == 'get_last' else opt.resume # resume from most recent run
|
384 |
if last and not opt.weights:
|
385 |
print(f'Resuming training from {last}')
|
386 |
opt.weights = last if opt.resume and not opt.weights else opt.weights
|
387 |
opt.cfg = check_file(opt.cfg) # check file
|
388 |
opt.data = check_file(opt.data) # check file
|
389 |
+
opt.hyp = check_file(opt.hyp) if opt.hyp else '' # check file
|
390 |
print(opt)
|
391 |
opt.img_size.extend([opt.img_size[-1]] * (2 - len(opt.img_size))) # extend to 2 sizes (train, test)
|
392 |
device = torch_utils.select_device(opt.device, apex=mixed_precision, batch_size=opt.batch_size)
|
|
|
395 |
|
396 |
# Train
|
397 |
if not opt.evolve:
|
398 |
+
print('Start Tensorboard with "tensorboard --logdir=runs", view at http://localhost:6006/')
|
399 |
tb_writer = SummaryWriter(comment=opt.name)
|
400 |
if opt.hyp: # update hyps
|
401 |
with open(opt.hyp) as f:
|
402 |
hyp.update(yaml.load(f, Loader=yaml.FullLoader))
|
403 |
|
|
|
|
|
|
|
404 |
train(hyp)
|
405 |
|
406 |
# Evolve hyperparameters (optional)
|
utils/utils.py
CHANGED
@@ -37,10 +37,10 @@ def init_seeds(seed=0):
|
|
37 |
torch_utils.init_seeds(seed=seed)
|
38 |
|
39 |
|
40 |
-
def get_latest_run(search_dir
|
41 |
# Return path to most recent 'last.pt' in /runs (i.e. to --resume from)
|
42 |
-
last_list =
|
43 |
-
return max(last_list, key
|
44 |
|
45 |
|
46 |
def check_git_status():
|
@@ -1113,7 +1113,7 @@ def plot_study_txt(f='study.txt', x=None): # from utils.utils import *; plot_st
|
|
1113 |
plt.savefig(f.replace('.txt', '.png'), dpi=200)
|
1114 |
|
1115 |
|
1116 |
-
def plot_labels(labels, save_dir=
|
1117 |
# plot dataset labels
|
1118 |
c, b = labels[:, 0], labels[:, 1:].transpose() # classees, boxes
|
1119 |
|
@@ -1180,7 +1180,8 @@ def plot_results_overlay(start=0, stop=0): # from utils.utils import *; plot_re
|
|
1180 |
fig.savefig(f.replace('.txt', '.png'), dpi=200)
|
1181 |
|
1182 |
|
1183 |
-
def plot_results(start=0, stop=0, bucket='', id=(), labels=(),
|
|
|
1184 |
# Plot training 'results*.txt' as seen in https://github.com/ultralytics/yolov5#reproduce-our-training
|
1185 |
fig, ax = plt.subplots(2, 5, figsize=(12, 6))
|
1186 |
ax = ax.ravel()
|
|
|
37 |
torch_utils.init_seeds(seed=seed)
|
38 |
|
39 |
|
40 |
+
def get_latest_run(search_dir='./runs'):
|
41 |
# Return path to most recent 'last.pt' in /runs (i.e. to --resume from)
|
42 |
+
last_list = glob.glob(f'{search_dir}/**/last*.pt', recursive=True)
|
43 |
+
return max(last_list, key=os.path.getctime)
|
44 |
|
45 |
|
46 |
def check_git_status():
|
|
|
1113 |
plt.savefig(f.replace('.txt', '.png'), dpi=200)
|
1114 |
|
1115 |
|
1116 |
+
def plot_labels(labels, save_dir=''):
|
1117 |
# plot dataset labels
|
1118 |
c, b = labels[:, 0], labels[:, 1:].transpose() # classees, boxes
|
1119 |
|
|
|
1180 |
fig.savefig(f.replace('.txt', '.png'), dpi=200)
|
1181 |
|
1182 |
|
1183 |
+
def plot_results(start=0, stop=0, bucket='', id=(), labels=(),
|
1184 |
+
save_dir=''): # from utils.utils import *; plot_results()
|
1185 |
# Plot training 'results*.txt' as seen in https://github.com/ultralytics/yolov5#reproduce-our-training
|
1186 |
fig, ax = plt.subplots(2, 5, figsize=(12, 6))
|
1187 |
ax = ax.ravel()
|