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)
Related
I was looking for the scenario which can be executed based on the files in a folder.
Folder -
test.xml
test1.xml
test2.xml
I've a feature file like:
Feature: Test Feature
Scenario: Test Scenario
Given create XML Files in {Folder}
Then use the {file}
Then Process the xml
It's written in python behave framework. Please suggest.
I tried using the Runner file
def main(Repetitions, Tag_Expression):
parser = argparse.ArgumentParser(
description="Test Runner allowing to run n test scenarios",
formatter_class=RawTextHelpFormatter)
parser.add_argument(
"repetitions", type=int, nargs="?", default=1,
help="Number of repetitions to run some given test/s\n"
+ "(default=1)")
parser.add_argument(
"-n", "--scenario-names", type=str, nargs="+", default=None,
help="Name of the scenario/s to be run 'x' times\n"
+ "(default=None)")
parser.add_argument(
"-f", "--feature-files", type=str, nargs="+", default=None,
help="Name of the behave feature-file/s to be run 'x' times\n"
+ "(default=None)")
parser.add_argument(
"-o", "--output", type=str, default=None,
help="Write output on specified file instead of stdout\n"
+ "(default=None)")
args = parser.parse_args()
runner_scenario_x_times(Repetitions,
Tag_Expression,
args.scenario_names,
args.feature_files,
args.output)
It's executing for only one tag/scenario/feature.
I am coding an argument parser for my script:
import argparse
parser = argparse.ArgumentParser(description='My parser.')
parser.add_argument('path',
type=str)
parser.add_argument('-a',
'--all',
action='store_true')
parser.add_argument('-t',
'--type',
type=str)
parser.add_argument('-d',
'--date',
type=str)
This is the logic I want to implement:
path: must be provided always.
--all: if it is provided, the --type and --date should not appear.
--type and --date: must be provided only if the --all flag is not introduced.
The command would look something like this:
python myscript.py mypath [-a] OR [-t mytype -d mydate]
How can I implement this logic?
You can do something like this:
from argparse import ArgumentParser
parser = ArgumentParser(description='My parser.')
parser.add_argument('path',
type=str)
parser.add_argument('-a',
'--all',
action='store_true')
parser.add_argument('-t',
'--type',
type=str)
parser.add_argument('-d',
'--date',
type=str)
args = parser.parse_args()
if args.all:
print('all argument flow')
else:
if not args.type or not args.date:
print('you need to put either all or specify both type and date')
else:
print(args.type, args.date)
print('and',args.path)
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
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')
I need to view the complete string in the Argparse object args.networkModel
Original code is from https://github.com/cmusatyalab/openface/blob/master/demos/classifier.py
I only have access to the pdb in the terminal.
When I try the print(args.networkModel) I get
/home/aanilil/ml/openface/demos/../models/openargs.networkModelface/nn4.small2.v1.t7
Is there a way to Print the complete string?
I have also tried the pprint(args.networkModel)
Where I get the output
*** TypeError: 'module' object is not callable
The original parser is constructed like so
parser = argparse.ArgumentParser()
parser.add_argument(
'--dlibFacePredictor',
type=str,
help="Path to dlib's face predictor.",
default=os.path.join(
dlibModelDir,
"shape_predictor_68_face_landmarks.dat"))
parser.add_argument(
'--networkModel',
type=str,
help="Path to Torch network model.",
default=os.path.join(
openfaceModelDir,
'nn4.small2.v1.t7'))
parser.add_argument('--imgDim', type=int,
help="Default image dimension.", default=96)
parser.add_argument('--cuda', action='store_true')
parser.add_argument('--verbose', action='store_true')
subparsers = parser.add_subparsers(dest='mode', help="Mode")
trainParser = subparsers.add_parser('train',
help="Train a new classifier.")
trainParser.add_argument('--ldaDim', type=int, default=-1)
trainParser.add_argument(
'--classifier',
type=str,
choices=[
'LinearSvm',
'GridSearchSvm',
'GMM',
'RadialSvm',
'DecisionTree',
'GaussianNB',
'DBN'],
help='The type of classifier to use.',
default='LinearSvm')
trainParser.add_argument(
'workDir',
type=str,
help="The input work directory containing 'reps.csv' and 'labels.csv'. Obtained from aligning a directory with 'align-dlib' and getting the representations with 'batch-represent'.")
inferParser = subparsers.add_parser(
'infer', help='Predict who an image contains from a trained classifier.')
inferParser.add_argument(
'classifierModel',
type=str,
help='The Python pickle representing the classifier. This is NOT the Torch network model, which can be set with --networkModel.')
inferParser.add_argument('imgs', type=str, nargs='+',
help="Input image.")
inferParser.add_argument('--multi', help="Infer multiple faces in image",
action="store_true")
args = parser.parse_args()