Adaptation Example of the Model Evaluation Startup Script (argparse)

import argparse
import logging
import json
import stat
import os

DEFAULT_FLAGS = os.O_RDWR | os.O_CREAT
DEFAULT_MODES = stat.S_IWUSR | stat.S_IRUSR

# Simulate the logic of the model source code.
def model_eval(args):	
    logging.info('%s', args.data_path)	# Receive corresponding parameters from the model code.
    logging.info('%s', args.output_path)
    logging.info('%s', args.model_config)
    logging.info('%s', args.learning_rate)
    eval_result = json.dumps({'Evaluation result': 'evaluation result to be output'})
    eval_result_path = os.path.join(args.output_path, 'eval_result.json')	# The evaluation result must be stored in the specified output path as eval_result.json. Otherwise, the evaluation job fails.
    with os.fdopen(os.open(eval_result_path, DEFAULT_FLAGS, DEFAULT_MODES), 'w') as file:
        json.dump(eval_result, file)

# Convert the argparse parameter type.
def str2bool(param):
    return param.lower() == 'true'

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    # The following is mandatory adaptation parameters.
    parser.add_argument('-dp', '--data_path', type=str, required=True)	# Dataset path
    parser.add_argument('-op', '--output_path', type=str, required=True)	# Output path
    parser.add_argument('-cp', '--ckpt_path', type=str, required=True)	# Model result path

    # Configuration items contained in the model configuration file
    parser.add_argument('-ep', '--eval_param', type=float, required=False)
    parser.add_argument('-bp', '--bool_param', type=str2bool, required=False)  # argparse does not support parameters of the Boolean type, which needs to be converted by type.

    # Parameter sets received by the model script
    args = parser.parse_args()
    model_eval(args)

The following is content of the params configuration items:

params:
 eval_param: 'your settings'
 bool_param: false