I am training an AI but I am getting this error and cant figure out why.
usage: car.py [-h] [--dataset /path/to/car/dataset/] --weights /path/to/weights.h5 [--logs /path/to/logs/] [--image path or URL to image] [--video path or URL to video]
car.py: error: the following arguments are required: , --weights
this is my parser
if __name__ == '__main__':
import argparse
# Parse command line arguments
parser = argparse.ArgumentParser(
description='Train Mask R-CNN to detect cars.')
parser.add_argument("command",
metavar="<command>",
help="'train' or 'splash'")
parser.add_argument('--dataset', required=False,
metavar="/path/to/car/dataset/",
help='Directory of the Car dataset')
parser.add_argument('--weights', required=True,
metavar="/path/to/weights.h5",
help="Path to weights .h5 file or 'coco'")
parser.add_argument('--logs', required=False,
default=DEFAULT_LOGS_DIR,
metavar="/path/to/logs/",
help='Logs and checkpoints directory (default=logs/)')
parser.add_argument('--image', required=False,
metavar="path or URL to image",
help='Image to apply the color splash effect on')
parser.add_argument('--video', required=False,
metavar="path or URL to video",
help='Video to apply the color splash effect on')
args = parser.parse_args()
The error says that your weights argument is required, and you yourself have written so in your code. It won't run until you provide the argument --weights in your call
Related
I am trying to code a module for my deep learning model. I wish to store these arguments using argeparse. There is some problem occuring in args = parser.parse_args()! Also What are the benefits of using the argparse library?
import numpy as np
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str,
help='Dataset')
parser.add_argument('--epoch', type=int, default=40,
help='Training Epochs')
parser.add_argument('--node_dim', type=int, default=64,
help='Node dimension')
parser.add_argument('--num_channels', type=int, default=2,
help='number of channels')
parser.add_argument('--lr', type=float, default=0.005,
help='learning rate')
parser.add_argument('--weight_decay', type=float, default=0.001,
help='l2 reg')
parser.add_argument('--num_layers', type=int, default=2,
help='number of layer')
parser.add_argument('--norm', type=str, default='true',
help='normalization')
parser.add_argument('--adaptive_lr', type=str, default='false',
help='adaptive learning rate')
args = parser.parse_args()
print(args)
The above code is a part of full code, as given in the link below
Error:
usage: ipykernel_launcher.py [-h] [--dataset DATASET] [--epoch EPOCH]
[--node_dim NODE_DIM]
[--num_channels NUM_CHANNELS] [--lr LR]
[--weight_decay WEIGHT_DECAY]
[--num_layers NUM_LAYERS] [--norm NORM]
[--adaptive_lr ADAPTIVE_LR]
ipykernel_launcher.py: error: unrecognized arguments: -f /Users/anshumansinha/Library/Jupyter/runtime/kernel-1e4c0f41-a4d7-4388-be24-640dd3411f56.json
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
The above code is a part of a code, the full code can be seen on the following github link : link
You called ipykernel_launcher.py with the argument -f. But -f is not in your list of arguments.
Is -f the argument, which you want to use for the input file? Then you should add something like this to your code:
parser.add_argument('--file', '-f', type=str)
The benefit of argparse is that you don't have to write by hand the code for parsing the arguments. You can try this. It is more code than one normally thinks. Especially if you have positional arguments.
How can I run this?
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--shape-predictor", required=True,
help="path to facial landmark predictor")
ap.add_argument("-a", "--alarm", type=str, default="",
help="path alarm .WAV file")
ap.add_argument("-w", "--webcam", type=int, default=0,
help="index of webcam on system")
args = vars(ap.parse_args())
While I run this on Spyder it gives
usage: [-h] -p SHAPE_PREDICTOR [-a ALARM] [-w WEBCAM]
: error: the following arguments are required: -p/--shape-predictor
An exception has occurred, use %tb to see the full traceback.
How can I solve this issue?
From #hiroprotagonist's comment posted above:
In spyder ctrl+F6 should allow you to pass command-line args when you execute your script. see here https://stackoverflow.com/a/26766414/4954037.
I'm trying to write a program for scraping images to create datasets to use for neural networks, however I'm getting a few problems
here's the code:
from imutils import paths
import argparse
import requests
import cv2
import os
ap = argparse.ArgumentParser()
ap.add_argument("-u", "--C:/Users/artus/datasets/urls.txt",
required=False, help="path containing URLs")
ap.add_argument("-o", "--C:/Users/artus/datasets/ShoesImage",
required=False, help="folder for downloaded images")
args = vars(ap.parse_args())
# grab the list of URLs from the input file, then initialize the
# total number of images downloaded thus far
rows = open(args["urls"]).read().strip().split("\n")
total = 0
when executed it should download all the images from the urls specified in the urls.txt file, however I'm getting this error:
Traceback (most recent call last):
File "C:/Users/artus/untitled5/imagescraping.py", line 16, in <module>
rows = open(args["urls"]).read().strip().split("\n")
KeyError: 'urls'
The second parameter of add_argument is the "long name" for the argument. For the first argument, you'll be passing --urls, and then argparse will make the value the user passes available as args["urls"]:
# ...
ap.add_argument("-u", "--urls", type=str,
required=False, help="path containing URLs")
Then, at the command line, pass in the argument:
python imagescraping.py --urls C:/Users/artus/datasets/urls.txt
Also, I don't think you need to wrap it in vars.
When I copy-n-paste your argparse code to a script:
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-u", "--C:/Users/artus/datasets/urls.txt",
required=False, help="path containing URLs")
ap.add_argument("-o", "--C:/Users/artus/datasets/ShoesImage",
required=False, help="folder for downloaded images")
args = ap.parse_args()
print(args)
and call it without arguments:
0923:~/mypy$ python3 stack56745387.py
Namespace(**{'C:/Users/artus/datasets/ShoesImage': None, 'C:/Users/artus/datasets/urls.txt': None})
and asking for help:
1743:~/mypy$ python3 stack56745387.py -h
usage: stack56745387.py [-h] [-u C:/USERS/ARTUS/DATASETS/URLS.TXT]
[-o C:/USERS/ARTUS/DATASETS/SHOESIMAGE]
optional arguments:
-h, --help show this help message and exit
-u C:/USERS/ARTUS/DATASETS/URLS.TXT, --C:/Users/artus/datasets/urls.txt C:/USERS/ARTUS/DATASETS/URLS.TXT
path containing URLs
-o C:/USERS/ARTUS/DATASETS/SHOESIMAGE, --C:/Users/artus/datasets/ShoesImage C:/USERS/ARTUS/DATASETS/SHOESIMAGE
folder for downloaded images
You probably intended the "--C:/Users/artus/datasets/urls.txt" to be something like the default value, but you defined it as the long flag and dest for the argument. (Nothing in your setup specified urls as the desired dest or key.)
which you'd have to use as:
1750:~/mypy$ python3 stack56745387.py --C:/Users/artus/datasets/urls.txt foobar
Namespace(**{'C:/Users/artus/datasets/ShoesImage': None, 'C:/Users/artus/datasets/urls.txt': 'foobar'})
Changing the code to:
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-u", "--urls", default="C:/Users/artus/datasets/urls.txt",
required=False, help="path containing URLs (default: %(default)s)")
ap.add_argument("-o", "--images",default="C:/Users/artus/datasets/ShoesImage",
required=False, help="folder for downloaded images (default: %(default)s)")
args = ap.parse_args()
print(args)
1802:~/mypy$ python3 stack56745387.py -h
usage: stack56745387.py [-h] [-u URLS] [-o IMAGES]
optional arguments:
-h, --help show this help message and exit
-u URLS, --urls URLS path containing URLs (default:
C:/Users/artus/datasets/urls.txt)
-o IMAGES, --images IMAGES
folder for downloaded images (default:
C:/Users/artus/datasets/ShoesImage)
1803:~/mypy$ python3 stack56745387.py --urls foobar
Namespace(images='C:/Users/artus/datasets/ShoesImage', urls='foobar')
Now you could use args.urls or vars(args)['urls'].
i am new to work with python and i want to run this code , but get this error.
code:
import argparse
import os
import sys
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(BASE_DIR)
sys.path.append(BASE_DIR)
from model import *
import indoor3d_util
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=int, default=0, help='GPU to use [default: GPU 0]')
parser.add_argument('--batch_size', type=int, default=1, help='Batch Size during training [default: 1]')
parser.add_argument('--num_point', type=int, default=4096, help='Point number [default: 4096]')
parser.add_argument('--model_path', required=True, help='model checkpoint file path')
parser.add_argument('--dump_dir', required=True, help='dump folder path')
parser.add_argument('--output_filelist', required=True, help='TXT filename, filelist, each line is an output for a room')
parser.add_argument('--room_data_filelist', required=True, help='TXT filename, filelist, each line is a test room data label file.')
parser.add_argument('--no_clutter', action='store_true', help='If true, donot count the clutter class')
parser.add_argument('--visu', action='store_true', help='Whether to output OBJ file for prediction visualization.')
FLAGS = parser.parse_args()
what should i do?
error :
batch_test.py: error: the following arguments are required: --model_path, --dump_dir, --output_filelist, --room_data_filelist
Those arguments are defined as required in your code required=True but don't have a default specified. You can either specify them at runtime or you can add a default value or you can make them not required with required=False
If you need additional help with any of those options, let me know.
You should see something like:
usage: batch_test.py [-h] [--gpu GPU] [--batch_size BATCH_SIZE]
[--num_point NUM_POINT] --model_path MODEL_PATH
--dump_dir DUMP_DIR --output_filelist OUTPUT_FILELIST
--room_data_filelist ROOM_DATA_FILELIST [--no_clutter]
[--visu]
And just append the arguments from command line, e.g.
python batch_test.py --model_path PATH_TO_YOUR_MODEL, --dump_dir YOUR_DUMP_DIR, --output_filelist OUT_LIST, --room_data_filelist ROOM_DATA_LIST
Or turn off the required from your source code.
When running the python script you showed, you have to include the arguments that are required (which has required=True) in the following format:
python batch_test.py <argument 1> <argument 1 value> <argument 2> <argument 2 value> ...
In the code you showed, there are several arguments that are required, which are:--model-path, --dump_dir, --output_filelist, --room_data_filelist.
You can actually see the instruction on how to fill each argument by entering the command:
python batch_test.py -h
I am trying to pass a .py file to ipython notebook environment.
I have never had to deal directly with argparse before. How do I rewrite the main() function?
I tried to delete the line of def main(): and keep the rest of the code.
But args = parser.parse_args()" returned an error:
ipykernel_launcher.py: error: unrecognized arguments: -f.
And when I run . %tb:
showing this
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str, default='data/tinyshakespeare',
help='data directory containing input.txt')
parser.add_argument('--input_encoding', type=str, default=None,
help='character encoding of input.txt, from https://docs.python.org/3/library/codecs.html#standard-encodings')
parser.add_argument('--log_dir', type=str, default='logs',
help='directory containing tensorboard logs')
parser.add_argument('--save_dir', type=str, default='save',
help='directory to store checkpointed models')
parser.add_argument('--rnn_size', type=int, default=256,
help='size of RNN hidden state')
parser.add_argument('--num_layers', type=int, default=2,
help='number of layers in the RNN')
parser.add_argument('--model', type=str, default='lstm',
help='rnn, gru, or lstm')
parser.add_argument('--batch_size', type=int, default=50,
help='minibatch size')
parser.add_argument('--seq_length', type=int, default=25,
help='RNN sequence length')
parser.add_argument('--num_epochs', type=int, default=50,
help='number of epochs')
parser.add_argument('--save_every', type=int, default=1000,
help='save frequency')
parser.add_argument('--grad_clip', type=float, default=5.,
help='clip gradients at this value')
parser.add_argument('--learning_rate', type=float, default=0.002,
help='learning rate')
parser.add_argument('--decay_rate', type=float, default=0.97,
help='decay rate for rmsprop')
parser.add_argument('--gpu_mem', type=float, default=0.666,
help='%% of gpu memory to be allocated to this process. Default is 66.6%%')
parser.add_argument('--init_from', type=str, default=None,
help="""continue training from saved model at this path. Path must contain files saved by previous training process:
'config.pkl' : configuration;
'words_vocab.pkl' : vocabulary definitions;
'checkpoint' : paths to model file(s) (created by tf).
Note: this file contains absolute paths, be careful when moving files around;
'model.ckpt-*' : file(s) with model definition (created by tf)
""")
args = parser.parse_args()
train(args)
You can try args = parser.parse_args(args=[]).
It's better to use #nbro 's answer for Jupyter execution.
args = parser.parse_args(args=[])
If you want to manage parameters as class format, you can try this.
class Args:
data = './data/penn'
model = 'LSTM'
emsize = 200
nhid = 200
args=Args()
As #nbro suggested, the following command should work:
args = parser.parse_args(args=[])
In addition, if you have required arguments in your parser, set them inside the list:
args = parser.parse_args(args=['--req_1', '10', '--req_2', '10'])
Where you previously used:
import argparse
parser = argparse.ArgumentParser(description="Dummy parser")
parser.add_argument("--req_1", type=int, required=True, help="required int 1")
parser.add_argument("--req_2", type=int, required=True, help="required int 2")
You can also see from the notebook all params:
print("see all args:", args)
print("use one arg:", args.req_1)
You can find more information in the docs: Parsing arguments
An example is:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('echo')
args = parser.parse_args(['aa']) # actually you don't have to write (args=['aa'])
print(args.echo)
the output should be
>>> aa