The idea of feeding the config file with a python file instead of yaml

n3215e416cf319a3a.jpg

Input config in yml file

I think that a yaml file is used to input config information when learning and inferring a machine learning model.

However, personally, the fact that variables cannot be used in yaml is a pain. There are variables like anchors and aliases, but since they hold values for each line, they cannot hold partial commonalities, for example, when the parent directories are common.

For example

image: "/path/to/dataset_name/data_version/Images"
label: "/path/to/dataset_name/data_version/Csv"

To

shared_dir = "/path/to/dataset_name/data_version/"

image: shared_dir + "Images"
label: shared_dir + "Csv"

Because I can't write like this, I find it difficult to edit and reuse.

At that time, some people may create only the default key and val placeholders, and then dynamically create the required yml file with the template generator and load it. It's [^ 1]. For me, it would be a hassle to add one processing process.

[^ 1]: I don't know the yaml example, but I've seen it in caffe's prototxt. The background is that the solver (corresponding to optimizer) class of caffe's python API doesn't have a setter method, so I think it was because the parameters had to be given via a file.

Input config in python file

So I came up with the idea that I should take the method of directly eating the python file. Eating here is not from config import cfg, but

Config file written in python

config.py


# config.py

path_root = '/path/to/'

cfg = {
    'key1': path_root + 'val1',
    'key2':path_root + 'val2',
    'some_number': 1+ 3,
}

,

import importlib

cfg_filepath = './config.py'
hoge = importlib.machinery.SourceFileLoader('hoge_module_name', cfg_filepath).load_module()
cfg = hoge.cfg

print(cfg)  # {'key1': '/path/to/val1', 'key2': '/path/to/val2', 'some_number': 4}

It refers to reading the contents of the config by passing it to the loader with the file path. In this case, you can switch the config file you want to read with the argument without changing the load destination of config with the import statement each time. Also, you can handle expression evaluation and objects handled by python. Of course, there is a drawback that it can not be used except for python, but variables make the config file look neat.

Can you really use it?

I wrote it down, but I felt that people wouldn't normally edit the parameters in the config file and experiment, whether it's a yaml file or a python file. If you are going through the command line, you can change only the parameters you want to change with the arguments, or you can change it through the GUI of the AI platform, or you are doing the experiment with jupyter and collecting the parameters in a cell (spitting yaml at runtime as a log). It seems that most of them are, and I have the impression that there is almost no opportunity to directly modify the config file.

As for whether the python file method can be used as in this article, the honest point is, "Although it can be used, it is better to think of another way of giving." If there is a useful case with the method in this article, I will add it later.

Thank you for reading.

References

-Python: Dynamically load the module --CUBE SUGAR CONTAINER

Recommended Posts

The idea of feeding the config file with a python file instead of yaml
Check the existence of the file with python
Convert the character code of the file with Python3
[Note] Import of a file in the parent directory in Python
Python> Read from a multi-line string instead of a file> io.StringIO ()
Extract the xz file with python
Process the contents of the file in order with a shell script
Save the result of the life game as a gif with python
The story of making a standard driver for db with python.
Read a file in Python with a relative path from the program
The story of making a module that skips mail with python
Trial of writing the configuration file in Python instead of .ini etc.
Create a compatibility judgment program with the random module of python.
Creating a simple PowerPoint file with Python
Search the maze with the python A * algorithm
Let's read the RINEX file with Python ①
Create a VM with a YAML file (KVM)
[python] [meta] Is the type of python a type?
[Python] Get the character code of the file
The story of blackjack A processing (python)
I made a configuration file with Python
[Python3] Understand the basics of file operations
The story of making a university 100 yen breakfast LINE bot with Python
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
What is the XX file at the root of a popular Python project?
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
A memo organized by renaming the file names in the folder with python
Get the number of searches with a regular expression. SeleniumBasic VBA Python
[AtCoder explanation] Control the A, B, C problems of ABC186 with Python!
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[AtCoder explanation] Control the A, B, C problems of ABC185 with Python!
Calculate the probability of being a squid coin with Bayes' theorem [python]
[AtCoder explanation] Control the A, B, C problems of ABC187 with Python!
I made a program to check the size of a file in Python
[AtCoder explanation] Control the A, B, C problems of ABC184 with Python!
nginxparser: Try parsing nginx config file with Python
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
How to read a CSV file with Python 2/3
[Python] Get the files in a folder with Python
Prepare the execution environment of Python3 with Docker
2016 The University of Tokyo Mathematics Solved with Python
[Note] Export the html of the site with python.
Get the caller of a function in Python
Check the date of the flag duty with Python
Solve A ~ D of yuki coder 247 with python
A note about the python version of python virtualenv
[Python] Summary of S3 file operations with boto3
Save the object to a file with pickle
Output in the form of a python array
Create a Photoshop format file (.psd) with python
Read line by line from a file with Python
A discussion of the strengths and weaknesses of Python
I want to write to a file with Python
Open a file dialog with a python GUI (tkinter.filedialog)
[Python] Determine the type of iris with SVM
Get the update date of the Python memo file.
Various ways to read the last line of a csv file in Python
[AtCoder explanation] Control the A, B, (C), D problems of ABC165 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC183 with Python!
Around the authentication of PyDrive2, a package that operates Google Drive with Python
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib