forked from olgamoskvyak/wbia-plugin-pie-v2
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathauto_train.py
More file actions
93 lines (71 loc) · 2.95 KB
/
Copy pathauto_train.py
File metadata and controls
93 lines (71 loc) · 2.95 KB
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
import subprocess
import logging
import argparse
import os
from os import listdir
from os.path import isfile, join
from pathlib import Path
STATUS_TRAINING = 'training'
STATUS_COMPLETE = 'complete'
def iter_train(args):
config_dir = args.config_dir
config_paths = [join(config_dir, f) for f in listdir(config_dir)
if isfile(join(config_dir, f)) and f.endswith('.yaml')]
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%m/%d %H:%M:%S'
)
logging.info(f'Found {len(config_paths)} config files in {config_dir}: {config_paths}.')
while get_next_untrained_config_path(config_dir):
config_path = get_next_untrained_config_path(config_dir)
try_train(config_path)
logging.info(f'Done with all training.')
def get_next_untrained_config_path(config_dir):
config_paths = [join(config_dir, f) for f in listdir(config_dir)
if isfile(join(config_dir, f)) and f.endswith('.yaml')]
config_paths.sort()
for config_path in config_paths:
if not trained_already(config_path):
return config_path
return None
def try_train(config_path):
if (trained_already(config_path)):
logging.info(f'Training results already found for {config_path}, continuing.')
return
training_command_args = ['python', 'train.py', '--cfg', config_path]
try:
logging.info(f'Beginning to train using {config_path}')
# add training touchfile
Path(status_touchfile_path(config_path, STATUS_TRAINING)).touch()
subprocess.run(training_command_args)
# remove training touchfile; add complete touchfile
os.remove(status_touchfile_path(config_path, STATUS_TRAINING))
Path(status_touchfile_path(config_path, STATUS_COMPLETE)).touch()
logging.info(f'Done training using {config_path}')
except:
logging.error(f'Hit an exception on {config_path} with args {training_command_args}')
# TODO: write this! This method will discern if we've already trained a
def trained_already(config_path):
return (
os.path.isfile(status_touchfile_path(config_path, STATUS_TRAINING)) or
os.path.isfile(status_touchfile_path(config_path, STATUS_COMPLETE))
)
# this exists to help me standardize these as much as anything
def status_touchfile_path(config_path, status):
tfile_path = f'{config_path}.{status}'
return tfile_path
if __name__ == '__main__':
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('--config_dir', type=str, default='configs', help='path to config directory')
# below should let us pass overwrite args to the training commands
parser.add_argument(
'opts',
default=None,
nargs=argparse.REMAINDER,
help='Modify config options using the command-line',
)
args = parser.parse_args()
iter_train(args)