Tensorflow - 命令行参数定义 - tf.app.flags

Python 命令行参数定义和 Tensorflow 命令行参数定义

1. Python 命令行参数定义

argparse 库, 如:

import argparse

def parse_args():
    """Parse input arguments."""
    parser = argparse.ArgumentParser()
    parser.add_argument('-b', dest='batchsize',default=1)
    parser.add_argument('-g', dest='gpuid', default=0)  
    args = parser.parse_args()
    return args

if __name__ == "__main__":
    args = parse_args()
    print(args.batchsize)
    print(args.gpuid)

2. Tensorflow 命令行参数定义

Tensorflow 采用tf.app.flags 来进行命令行参数传递.
如 - flags_test.py

import tensorflow as tf 

flags = tf.app.flags
FLAGS = flags.FLAGS

# Settings for some training parameters.
flags.DEFINE_enum('learning_policy', 'poly', ['poly', 'step'],  
                  'Learning rate policy for training.')
flags.DEFINE_float('base_learning_rate', .0001,  
                   'The base learning rate for model training.')
flags.DEFINE_integer('learning_rate_decay_step', 2000, 
                     'Decay the base learning rate at a fixed step.')
flags.DEFINE_integer('train_batch_size', 12,
                     'The number of images in each batch during training.')
flags.DEFINE_multi_integer('train_crop_size', [513, 513],
                           'Image crop size [height, width] during training.')
flags.DEFINE_boolean('upsample_logits', True,
                     'Upsample logits during training.')
flags.DEFINE_string('dataset', 'dataset_name',
                    'Name of the test dataset.')

def main(_):  
    print(FLAGS.learning_policy)
    print(FLAGS.base_learning_rate)
    print(FLAGS.learning_rate_decay_step)
    print(FLAGS.train_batch_size)
    print(FLAGS.train_crop_size)
    print(FLAGS.upsample_logits)
    print(FLAGS.dataset)

if __name__ == '__main__':
    tf.app.run()

运行

python flags_test.py

输出:

poly
0.0001
2000
12
[513, 513]
True
dataset_name
Last modification:October 9th, 2018 at 09:31 am

2 comments

  1. Kelvin

    博主好,请问你用的tensorflow版本是什么?我使用DEFINE_enum时会报错不存在此属性。
    1.4.0 GPU 和 1.8.0 GPU似乎都不行? :cry:

    1. AIHGF
      @Kelvin

      tensorflow-gpu 1.8.0 本机测过是可以,现在是 1.9.0 版本,也是可以的,没有出现错误. 1.4.0 版本的未测试过.

Leave a Comment