Skip to content
Advertisement

Argument parser object does not contain attribute defined in parser

I am working on a program that works on hyperspectral image super-resolution by using Neural Networks, Now in here the Mains directory of the program contains multiple parsers. The parsers and subparsers seem to have been defined correctly

def main():
# parsers
main_parser = argparse.ArgumentParser(description="parser for SR network")
subparsers = main_parser.add_subparsers(title="subcommands", dest="subcommand")
train_parser = subparsers.add_parser("train", help="parser for training arguments")
train_parser.add_argument("--cuda", type=int, required=False,default=1,
                          help="set it to 1 for running on GPU, 0 for CPU")
train_parser.add_argument("--batch_size", type=int, default=32, help="batch size, default set to 64")
train_parser.add_argument("--epochs", type=int, default=40, help="epochs, default set to 20")
train_parser.add_argument("--n_feats", type=int, default=256, help="n_feats, default set to 256")
train_parser.add_argument("--n_blocks", type=int, default=3, help="n_blocks, default set to 6")
train_parser.add_argument("--n_subs", type=int, default=8, help="n_subs, default set to 8")
train_parser.add_argument("--n_ovls", type=int, default=2, help="n_ovls, default set to 1")
train_parser.add_argument("--n_scale", type=int, default=4, help="n_scale, default set to 2")
train_parser.add_argument("--use_share", type=bool, default=True, help="f_share, default set to 1")
train_parser.add_argument("--dataset_name", type=str, default="Chikusei", help="dataset_name, default set to dataset_name")
train_parser.add_argument("--model_title", type=str, default="SSPSR", help="model_title, default set to model_title")
train_parser.add_argument("--seed", type=int, default=3000, help="start seed for model")
train_parser.add_argument("--learning_rate", type=float, default=1e-4,
                          help="learning rate, default set to 1e-4")
train_parser.add_argument("--weight_decay", type=float, default=0, help="weight decay, default set to 0")
train_parser.add_argument("--save_dir", type=str, default="./trained_model/",
                          help="directory for saving trained models, default is trained_model folder")
train_parser.add_argument("--gpus", type=str, default="1", help="gpu ids (default: 7)")

test_parser = subparsers.add_parser("test", help="parser for testing arguments")
test_parser.add_argument("--cuda", type=int, required=False,default=1,
                         help="set it to 1 for running on GPU, 0 for CPU")
test_parser.add_argument("--gpus", type=str, default="0,1", help="gpu ids (default: 7)")


args = main_parser.parse_args()

os.environ['CUDA_VISIBLE_DEVICES'] = args.gpus
print(args.gpus)
if args.subcommand is None:
    print("ERROR: specify either train or test")
    sys.exit(1)
if args.cuda and not torch.cuda.is_available():
    print("ERROR: cuda is not available, try running on CPU")
    sys.exit(1)
if args.subcommand == "train":
    train(args)
else:
    test(args)
pass

however, upon using the args object, the compiler throws an error saying that the object has no attribute gpus. Though, the test parser does contain the attribute ‘gpus’

"G:Python projectsvenvScriptspython.exe" "G:/Hyperspectral ISRO/SSPSR-master/mains.py"
Traceback (most recent call last):
  File "G:Hyperspectral ISROSSPSR-mastermains.py", line 309, in <module>
    main()
  File "G:Hyperspectral ISROSSPSR-mastermains.py", line 70, in main
    os.environ['CUDA_VISIBLE_DEVICES'] = args.gpus
AttributeError: 'Namespace' object has no attribute 'gpus'

I cannot figure out as to why this is happening, as I believe I am parsing the arguments correctly before using args, I tried to find similar issues on forums, but failed to do so.

Advertisement

Answer

All you need to do is:

args = main_parser.parse_args()
print(args) # for debugging help

if args.subcommand is None:
    print("ERROR: specify either train or test")
    sys.exit(1)

# now it's safe to reference `gpus` and `cuda` which are defined by both subparsers

os.environ['CUDA_VISIBLE_DEVICES'] = args.gpus

if args.cuda and not torch.cuda.is_available():
    print("ERROR: cuda is not available, try running on CPU")
    sys.exit(1)
if args.subcommand == "train":
    train(args)
else:
    test(args)

Just beware that train and test will get different args. train won’t get any of the test values.

Try different command line values and note the differences in the args.

if you use

subparsers = main_parser.add_subparsers(title="subcommands", dest="subcommand",
     required=True)

you don’t need to do your own test for args.subcommand is None. The parser will do that for you.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement