简体   繁体   English

如何用 python 中的输入替换命令行参数?

[英]how to replace the command line argument by inputs in python?

I downloaded the code and I run it through the command-line argument, but I want to modify them with raw input, the code I used as follows (the part of the code to modify)我下载了代码并通过命令行参数运行它,但我想用原始输入修改它们,我使用的代码如下(要修改的部分代码)

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--image_folder', required=True, help='path to image_folder which contains text images')
parser.add_argument('--workers', type=int, help='number of data loading workers', default=4)
parser.add_argument('--batch_size', type=int, default=192, help='input batch size')
parser.add_argument('--saved_model', required=True, help="path to saved_model to evaluation")
""" Data processing """
parser.add_argument('--batch_max_length', type=int, default=25, help='maximum-label-length')
parser.add_argument('--imgH', type=int, default=32, help='the height of the input image')
parser.add_argument('--imgW', type=int, default=100, help='the width of the input image')
parser.add_argument('--rgb', action='store_true', help='use rgb input')
parser.add_argument('--character', type=str, default='0123456789\"abcdefghijklmnopqrstuvwxyz\'ABCDEFGHIJKLMNOPQRS°TUV()WXY.Z', help='character label') #must be same as the characters in train.py
#'0123456789\"abcdefghijklmnopqrstuvwxyz\'ABCDEFGHIJKLMNOPQRS°TUV()WXY.Z'
parser.add_argument('--sensitive', action='store_true', help='for sensitive character mode')
parser.add_argument('--PAD', action='store_true', help='whether to keep ratio then pad for image resize')
""" Model Architecture """
parser.add_argument('--Transformation', type=str, required=True, help='Transformation stage. None|TPS')
parser.add_argument('--FeatureExtraction', type=str, required=True, help='FeatureExtraction stage. VGG|RCNN|ResNet')
parser.add_argument('--SequenceModeling', type=str, required=True, help='SequenceModeling stage. None|BiLSTM')
parser.add_argument('--Prediction', type=str, required=True, help='Prediction stage. CTC|Attn')
parser.add_argument('--num_fiducial', type=int, default=20, help='number of fiducial points of TPS-STN')
parser.add_argument('--input_channel', type=int, default=1, help='the number of input channel of Feature extractor')
parser.add_argument('--output_channel', type=int, default=512,
                    help='the number of output channel of Feature extractor')
parser.add_argument('--hidden_size', type=int, default=256, help='the size of the LSTM hidden state')

opt = parser.parse_args()

""" vocab / character number configuration """
if opt.sensitive:
    opt.character = string.printable[:-6]  # same with ASTER setting (use 94 char).

cudnn.benchmark = True
cudnn.deterministic = True
opt.num_gpu = torch.cuda.device_count()

demo(opt)

the argument I used is python demo.py --image_folder eval --Transformation TPS --FeatureExtraction ResNet --SequenceModeling BiLSTM --Prediction Attn --saved_model saved_models/TPS-ResNet-BiLSTM-Attn-Seed1111/best_accuracy.pth我使用的参数是python demo.py --image_folder eval --Transformation TPS --FeatureExtraction ResNet --SequenceModeling BiLSTM --Prediction Attn --saved_model saved_models/TPS-ResNet-BiLSTM-Attn-Seed1111/best_accuracy.pth

now I want to hardcode this with direct input, I don't know how to do it, please anyone help me.现在我想用直接输入对其进行硬编码,我不知道该怎么做,请任何人帮助我。

Thanks in advance.提前致谢。

For each argument in argparse, it is possible to set default values so that if no flag is passed, it fallsback onto the values provided.对于 argparse 中的每个参数,可以设置默认值,以便如果没有传递标志,它会回退到提供的值。

In your code you have this: parser.add_argument('--output_channel', type=int, default=512)在您的代码中,您有: parser.add_argument('--output_channel', type=int, default=512)

Note the default argument.注意default参数。 For each of you values that you want to hardcoded, you can set them as defaults.对于您想要硬编码的每个值,您可以将它们设置为默认值。

you can specify default value:您可以指定默认值:

Eg例如

parser.add_argument('--workers', type=int, help='number of data loading workers', default=4)

The default=4 is the hardcode value if you don't modify --workers in command line.如果您不在命令行中修改--workers ,则default=4是硬编码值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM