Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are Config and Trainer?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is Config and Trainer". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is Config and Trainer".

Code structure overview core section

Configs: stores yaml configuration files for various networks

Datasets: where the dataset is stored

Detectron2: the core component of running code

Tools: provides an entry point for running code and all visual code files.

Tutorial part

Demo: it's obviously demo

Docs: it's also obvious.

Tests: provides some test code

Projects: provides a real project code example, and then your own code structure can be written with reference to this structure.

Code logic analysis hyperparameter configuration

Enter the main function of tools/train_net.py, and the first line cfg = setup (args) is the configuration parameter. The parameter configuration in Detectron2 uses the yacs library, which is a good library for reusing and stitching hyperparameter file configurations.

Let's first look at the file structure of detrctron2/config/:

Compat.py: it should be compatible with the previous Detectron library, but it can be ignored. Http://www.0398hfyy.com/ of Sanmenxia Gynecology Hospital

Config.py: defines a CfgNode class that inherits from the CfgNode defined in the fvcore library (a common library written by fb that provides some shared functions to facilitate the use of different projects). The inheritance relationship goes like this:

Detrctron2.config.CfgNode- > fcvore.common.config.CfgNode- > yacs.config.CfgNode- > dict

In addition, the file also provides the get_cfg () method, which returns a CfgNode with the default configuration, which is defined in the following default.py because there are too many default values to configure, so it is written in a new file for clarity, but the author of the yacs library also recommends doing so.

Default.py: as mentioned above, this file defines the default values for various parameters.

After we understand the method of configuring the function, we will go back to tools/train_net.py, and we will understand it one by one.

Tools/train_net.py

From detectron2.config import get_cfgfrom detectron2.engine import DefaultTrainer, default_argument_parser, default_setup, hooks, launch...def setup (args): "Create configs and perform basic setups." Cfg = get_cfg () cfg.merge_from_file (args.config_file) cfg.merge_from_list (args.opts) cfg.freeze () default_setup (cfg, args) return cfg

Cfg = get_cfg (): get the cfg that has been configured with default parameters

Cfg.merge_from_file (args.config_file): config_file is the specified yaml configuration file, and the merge_from_file function overrides the default values for the hyperparameters specified in the yaml file.

Cfg.merge_from_list (args.opts): merge_from_list works like the one above, except that it is overridden on the command line.

For example

Opts = ["SYSTEM.NUM_GPUS", 8, "TRAIN.SCALES", "(1,2,3,4)"] cfg.merge_from_list (opts) print ("cfg\ n", cfg)

Then eventually there will be

Cfg... (some default hyperparameters) SYSTEM: NUM_GPUS: 8TRAIN: SCALES: (1meme2pence3pr 4)

Cfg.freeze (): the function of the freeze function is to freeze the value of the hyperparameter to avoid being accidentally modified by the program.

Default_setup (cfg, args): default_setup is a default configuration function provided in detectron2/engine/default.py. How to configure it is not detailed here. However, many other configuration functions are provided in this file, such as two classes: DefaultPredictor and DefaultTrainer.

Trainer

Now that DefaultTrainer is mentioned above, let's take a look at detectron2.engine from this class, whose code structure is as follows:

Train_loop.py: the main purpose of this function is to provide three important classes

Register_hooks: this is easy to understand, that is, to register some user-defined hooks, to speak vernacular is to put several Hook in one list. Then you just need to traverse the list and execute it in turn.

The second type is actually traversing hook list and executing hook mentioned above, but there are four kinds of traversal, which is before_train,after_train,before_step,after_step. Another is run_step, which is actually the code we usually write for the training process, such as reading data, training models, obtaining lost values, finding derivatives, reverse gradient updates, etc., but not defined in this class.

The third type is the train function, which has two parameters, the initial number of iterations and the maximum number of iterations. After that, the specified iterations of the functions in the second category are repeated.

HookBase: this is a base class of Hook that specifies what needs to be done before and after training or before and after each step, so the following four methods are defined differently according to specific needs: before_train,after_train,before_step,after_step. To before_step.

TrainerBase: the functions defined in this class can be summarized into three categories:

SimpleTrainer: actually inherits from TrainerBase, and then defines methods such as run_step. We can also inherit this class for further customization later.

Defaults.py: as described above, two classes are provided: DefaultPredictor and DefaultTrainer. This DefaultTrainer inherits from SimpleTrainer, so the inheritance relationship exists as follows:

Detectron2.engine.default.DefaultTrainer- > detectron2.engine.train_loop.SimpleTrainer- > detectron2.engine.train_loop.TrainerBase

Hooks.py: defines a lot of Hook that inherit from train_loop.HookBase.

Launch.py: as mentioned earlier, it can be understood as a code initiator, and you can decide whether to use distributed training (or stand-alone multi-card training) or stand-alone single-card training according to the command.

All right, let's go back to the main function of tools/train_net.py, as shown below.

Def main (args): cfg = setup (args) if args.eval_only:... Trainer = Trainer (cfg) trainer.resume_or_load (resume=args.resume) if cfg.TEST.AUG.ENABLED: trainer.register_hooks ([hooks.EvalHook (0, lambda: trainer.test_with_TTA (cfg, trainer.model)]) return trainer.train ()

You can see that a Trainer is defined below, which inherits from detectron2.engine.default.DefaultTrainer, and this parent class automatically parses cfg. Then you just need to call trainer.train () to start the training.

Thank you for your reading, the above is the content of "what is Config and Trainer". After the study of this article, I believe you have a deeper understanding of what is Config and Trainer, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report