Python argparse: unrecognized arguments while passing args in command - python

I have a code in python as follows:
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Experiments for optimizer')
parser.add_argument('list_experiments', type=str, nargs='+',
help='List of experiment names. E.g. CDSGD EASGD FASGD SGD Adam --> will run a training session with each optimizer')
parser.add_argument('--model_name', default='CNN', type=str,
help='Model name: CNN, Big_CNN or FCN')
parser.add_argument('--batch_size', default=128, type=int,
help='Batch size')
parser.add_argument('--nb_epoch', default=30, type=int,
help='Number of epochs')
parser.add_argument('--dataset', type=str, default="cifar10",
help='Dataset, cifar10, cifar100 or mnist')
parser.add_argument('--n_agents', default=5, type=int,
help='Number of agents')
parser.add_argument('--communication_period', default=1, type=int,
help='Gap between the communication of the agents')
parser.add_argument('--sparsity', default=False, type=bool,
help='The connection between agents if sparse or not, default: False i.e. fully connected')
args = parser.parse_args()
I wanna run it with command
python main.py CDSGD -m CNN -b 512 -ep 200 -d cifar10 -n 5 -cp 1 -s 3
but i get the following error:
main.py: error: unrecognized arguments: -m CNN -b 512 -ep 200 -d cifar10 -n 5 -cp 1 -s 3
how can i fix this problem?

A long option can be abbreviated with a unique prefix (e.g., --model will be recognized as --model_name, but short options have to be defined explicitly.
For example,
parser.add_argument('--model_name', '-m', default='CNN', type=str,
help='Model name: CNN, Big_CNN or FCN')

Related

Arg parser error :args = vars(ap.parse_args()) exception occured : python

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-d", "--dataset", required=True,
help="path to dataset")
ap.add_argument("-m", "--model", required=True,
help="path to Caffe pre-trained model")
ap.add_argument("-l", "--labels", required=True,
help="path to ImageNet labels (i.e., syn-sets)")
args = vars(ap.parse_args())
and I'm getting the output as
usage: train.py [-h] -i IMAGE -d DATASET -m MODEL -l LABELS
ipykernel_launcher.py: error: the following arguments are required: -i/--image, -p/--prototxt, -m/--model, -l/--labels.
An exception has occurred, use %tb to see the full traceback.
System Exit: 2
You have this error because you are running your script without passing the required parameters you defined.
You will not have the error if you run the script like this:
script_name.py -i image_path -d data_path -m model_path -l label_path

While Executing code on google colab error regarding the argparse.ArgumentParser()

I am not able to solve this error is it that we cannot use argparse on google colab or there is some alternative to it
The code is:-
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--data", required=True, help="CSV file with quotes to run the model")
parser.add_argument("-m", "--model", required=True, help="Model file to load")
parser.add_argument("-b", "--bars", type=int, default=50, help="Count of bars to feed into the model")
parser.add_argument("-n", "--name", required=True, help="Name to use in output images")
parser.add_argument("--commission", type=float, default=0.1, help="Commission size in percent, default=0.1")
parser.add_argument("--conv", default=False, action="store_true", help="Use convolution model instead of FF")
args = parser.parse_args()
prices = data.load_relative(args.data)
env = environ.StocksEnv({"TEST": prices}, bars_count=args.bars, reset_on_close=False, commission=args.commission,
state_1d=args.conv, random_ofs_on_reset=False, reward_on_close=False, volumes=False)
The Error is :
usage: ipykernel_launcher.py [-h] -d DATA -m MODEL [-b BARS] -n NAME
[--commission COMMISSION] [--conv]
ipykernel_launcher.py: error: the following arguments are required: -d/--data, -m/--model, -n/--name
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2

the following arguments are required

I have the Python script . What I'm trying to do is to test this code in colab
The problem is that the initial script requires arguments. They are defined as follows:
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Pipeline to train a NN model specified by a YML config")
parser.add_argument("-t", "--tag", nargs="?", type=str, help="Model tag of the experiment", required=True)
parser.add_argument("-c", "--config", nargs="?", type=str, default="syndoc.yml", help="Config file name")
parser.add_argument("-s", "--seed", nargs="?", type=int, default=4321, help="Seed number")
parser.add_argument('-wt', '--with_test', action='store_true', help='Whether to run corresponding Tester')
args = parser.parse_args()
config = coerce_to_path_and_check_exist(CONFIGS_PATH / args.config)
run_dir = MODELS_PATH / args.tag
trainer = Trainer(config, run_dir, seed=args.seed)
trainer.run(seed=args.seed)
the error
usage: trainer.py [-h] -t [TAG] [-c [CONFIG]] [-s [SEED]] [-wt]
trainer.py: error: the following arguments are required: -t/--tag
Arguments are received when you run a program from the command line (shell, bash, cmd) and they enable effecting the program without changing it e.g. my-program -varX 1 vs my-program -varX 2, you are not doing so, so instead you can remove that code and replace args.config, args.tag etc. with variables e.g. config and tag that you define and set to the values you want.
parser = argparse.ArgumentParser(description="Pipeline to train a NN model specified by a YML config")
parser.add_argument("-t", "--tag", nargs="?", type=str, help="Model tag of the experiment", required=True)
parser.add_argument("-c", "--config", nargs="?", type=str, default="syndoc.yml", help="Config file name")
parser.add_argument("-s", "--seed", nargs="?", type=int, default=4321, help="Seed number")
parser.add_argument('-wt', '--with_test', action='store_true', help='Whether to run corresponding Tester')
args = parser.parse_args()
config = coerce_to_path_and_check_exist(CONFIGS_PATH / args.config)
run_dir = MODELS_PATH / args.tag
trainer = Trainer(config, run_dir, seed=args.seed)
trainer.run(seed=args.seed)

Parse command line argument in python

have to do command line parsing and I am getting this error. I'm very new to Python and I don't know what the error means.
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-m", "--mask-rcnn", required=True,
help="base path to mask-rcnn directory")
ap.add_argument("-v", "--visualize", type=int, default=0,
help="whether or not we are going to visualize each instance")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
help="minimum probability to filter weak detections")
ap.add_argument("-t", "--threshold", type=float, default=0.3,
help="minimum threshold for pixel-wise mask segmentation")
args = vars(ap.parse_args())
I am getting this error:
usage: main.py [-h] -i IMAGE -m MASK_RCNN [-v VISUALIZE] [-c CONFIDENCE]
[-t THRESHOLD]
main.py: error: the following arguments are required: -i/--image, -m/--mask-rcnn
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2889: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
​
argparse processes the strings in the sys.argv list. Normally this comes from the command line values provided when calling the script containing the argparse code:
$ python main.py -i foo -m bar
But it looks like you are using ipython. It would be good to see how you call this script. But you might need to use:
$ ipython -i main.py -- -i foo -m bar
The '--' separates the input that ipython uses from the input that it should make available to main.py.

How to parse arguments in python (spyder)?

I am following this tutorial and trying to run the below part of the script. I am using python 3.7 and spyder 3.3.4.
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True,
help="path to input dataset (i.e., directory of images)")
ap.add_argument("-m", "--model", required=True,
help="path to output model")
ap.add_argument("-l", "--labelbin", required=True,
help="path to output label binarizer")
ap.add_argument("-p", "--plot", type=str, default="plot.png",
help="path to output accuracy/loss plot")
args = vars(ap.parse_args())
I have tried going to Run > Configuration per file and entering the arguments as advised by this post and and this post.
command line options: path1, path2, path3, path4
I filled out the appropriate paths for the arguments above and then ran the script, but go the error below.
usage: train.py [-h] -d DATASET -m MODEL -l LABELBIN [-p PLOT]
train.py: error: the following arguments are required: -d/--dataset,
-m/--model, -l/--labelbin An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
How can I fix this error to run my script appropriately and pass the arguments in spyder?
You can parse arguments by doing a special run from the settings and putting in the order that the arguments are expected.

Categories