29
29
30
30
class Trainer ():
31
31
"""
32
- An experiment template in order to structure the training code and take
33
- care of saving, loading, logging, visualization stuffs. It's intended to
34
- be flexible and simple.
35
-
36
- So it only handles output directory (create directory for the output,
37
- create a checkpoint directory, dump the config in use and create
32
+ An experiment template in order to structure the training code and take
33
+ care of saving, loading, logging, visualization stuffs. It's intended to
34
+ be flexible and simple.
35
+
36
+ So it only handles output directory (create directory for the output,
37
+ create a checkpoint directory, dump the config in use and create
38
38
visualizer and logger) in a standard way without enforcing any
39
- input-output protocols to the model and dataloader. It leaves the main
40
- part for the user to implement their own (setup the model, criterion,
41
- optimizer, define a training step, define a validation function and
39
+ input-output protocols to the model and dataloader. It leaves the main
40
+ part for the user to implement their own (setup the model, criterion,
41
+ optimizer, define a training step, define a validation function and
42
42
customize all the text and visual logs).
43
- It does not save too much boilerplate code. The users still have to write
44
- the forward/backward/update mannually, but they are free to add
43
+ It does not save too much boilerplate code. The users still have to write
44
+ the forward/backward/update mannually, but they are free to add
45
45
non-standard behaviors if needed.
46
46
We have some conventions to follow.
47
- 1. Experiment should have ``model``, ``optimizer``, ``train_loader`` and
47
+ 1. Experiment should have ``model``, ``optimizer``, ``train_loader`` and
48
48
``valid_loader``, ``config`` and ``args`` attributes.
49
- 2. The config should have a ``training`` field, which has
50
- ``valid_interval``, ``save_interval`` and ``max_iteration`` keys. It is
51
- used as the trigger to invoke validation, checkpointing and stop of the
49
+ 2. The config should have a ``training`` field, which has
50
+ ``valid_interval``, ``save_interval`` and ``max_iteration`` keys. It is
51
+ used as the trigger to invoke validation, checkpointing and stop of the
52
52
experiment.
53
- 3. There are four methods, namely ``train_batch``, ``valid``,
53
+ 3. There are four methods, namely ``train_batch``, ``valid``,
54
54
``setup_model`` and ``setup_dataloader`` that should be implemented.
55
- Feel free to add/overwrite other methods and standalone functions if you
55
+ Feel free to add/overwrite other methods and standalone functions if you
56
56
need.
57
-
57
+
58
58
Parameters
59
59
----------
60
60
config: yacs.config.CfgNode
61
61
The configuration used for the experiment.
62
-
62
+
63
63
args: argparse.Namespace
64
64
The parsed command line arguments.
65
65
Examples
@@ -68,16 +68,16 @@ class Trainer():
68
68
>>> exp = Trainer(config, args)
69
69
>>> exp.setup()
70
70
>>> exp.run()
71
- >>>
71
+ >>>
72
72
>>> config = get_cfg_defaults()
73
73
>>> parser = default_argument_parser()
74
74
>>> args = parser.parse_args()
75
- >>> if args.config:
75
+ >>> if args.config:
76
76
>>> config.merge_from_file(args.config)
77
77
>>> if args.opts:
78
78
>>> config.merge_from_list(args.opts)
79
79
>>> config.freeze()
80
- >>>
80
+ >>>
81
81
>>> if args.nprocs > 1 and args.device == "gpu":
82
82
>>> dist.spawn(main_sp, args=(config, args), nprocs=args.nprocs)
83
83
>>> else:
@@ -114,7 +114,7 @@ def setup(self):
114
114
115
115
@property
116
116
def parallel (self ):
117
- """A flag indicating whether the experiment should run with
117
+ """A flag indicating whether the experiment should run with
118
118
multiprocessing.
119
119
"""
120
120
return self .args .device == "gpu" and self .args .nprocs > 1
@@ -144,9 +144,9 @@ def save(self, tag=None, infos: dict=None):
144
144
self .optimizer , infos )
145
145
146
146
def resume_or_scratch (self ):
147
- """Resume from latest checkpoint at checkpoints in the output
147
+ """Resume from latest checkpoint at checkpoints in the output
148
148
directory or load a specified checkpoint.
149
-
149
+
150
150
If ``args.checkpoint_path`` is not None, load the checkpoint, else
151
151
resume training.
152
152
"""
@@ -181,8 +181,7 @@ def train(self):
181
181
if from_scratch :
182
182
# save init model, i.e. 0 epoch
183
183
self .save (tag = 'init' , infos = None )
184
-
185
- self .lr_scheduler .step (self .iteration )
184
+ self .lr_scheduler .step (self .epoch )
186
185
if self .parallel :
187
186
self .train_loader .batch_sampler .set_epoch (self .epoch )
188
187
@@ -254,7 +253,7 @@ def setup_output_dir(self):
254
253
255
254
def setup_checkpointer (self ):
256
255
"""Create a directory used to save checkpoints into.
257
-
256
+
258
257
It is "checkpoints" inside the output directory.
259
258
"""
260
259
# checkpoint dir
@@ -277,13 +276,13 @@ def destory(self):
277
276
@mp_tools .rank_zero_only
278
277
def setup_visualizer (self ):
279
278
"""Initialize a visualizer to log the experiment.
280
-
279
+
281
280
The visual log is saved in the output directory.
282
-
281
+
283
282
Notes
284
283
------
285
- Only the main process has a visualizer with it. Use multiple
286
- visualizers in multiprocess to write to a same log file may cause
284
+ Only the main process has a visualizer with it. Use multiple
285
+ visualizers in multiprocess to write to a same log file may cause
287
286
unexpected behaviors.
288
287
"""
289
288
# visualizer
@@ -292,9 +291,9 @@ def setup_visualizer(self):
292
291
293
292
@mp_tools .rank_zero_only
294
293
def dump_config (self ):
295
- """Save the configuration used for this experiment.
296
-
297
- It is saved in to ``config.yaml`` in the output directory at the
294
+ """Save the configuration used for this experiment.
295
+
296
+ It is saved in to ``config.yaml`` in the output directory at the
298
297
beginning of the experiment.
299
298
"""
300
299
with open (self .output_dir / "config.yaml" , 'wt' ) as f :
@@ -312,13 +311,13 @@ def valid(self):
312
311
raise NotImplementedError ("valid should be implemented." )
313
312
314
313
def setup_model (self ):
315
- """Setup model, criterion and optimizer, etc. A subclass should
314
+ """Setup model, criterion and optimizer, etc. A subclass should
316
315
implement this method.
317
316
"""
318
317
raise NotImplementedError ("setup_model should be implemented." )
319
318
320
319
def setup_dataloader (self ):
321
- """Setup training dataloader and validation dataloader. A subclass
320
+ """Setup training dataloader and validation dataloader. A subclass
322
321
should implement this method.
323
322
"""
324
323
raise NotImplementedError ("setup_dataloader should be implemented." )
0 commit comments