I am not familiar with Python, trying to build some DNN. So when I tried to parse some arguments I got this error in main.
usage: main.py [-h] [-j N] [--resume PATH] [--epochs N] [--start-epoch N] [-b N] [--lr LR]
[--weight-decay W] [-e] [--print-freq N]
DIR
main.py: error: the following arguments are required: DIR
Here is some part of the code:
# Parse arguments and prepare program
parser = argparse.ArgumentParser(description='Training and Using ColorNet')
parser.add_argument('data', metavar='DIR', help='path to dataset')
parser.add_argument('-j', '--workers', default=0, type=int, metavar='N', help='number of data loading workers (default: 0)')
parser.add_argument('--resume', default='', type=str, metavar='PATH', help='path to .pth file checkpoint (default: none)')
parser.add_argument('--epochs', default=50, type=int, metavar='N', help='number of total epochs to run')
parser.add_argument('--start-epoch', default=0, type=int, metavar='N', help='manual epoch number (overridden if loading from checkpoint)')
parser.add_argument('-b', '--batch-size', default=16, type=int, metavar='N', help='size of mini-batch (default: 16)')
parser.add_argument('--lr', '--learning-rate', default=0.1, type=float, metavar='LR', help='learning rate at start of training')
parser.add_argument('--weight-decay', '--wd', default=1e-10, type=float, metavar='W', help='weight decay (default: 1e-4)')
parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true', help='use this flag to validate without training')
parser.add_argument('--print-freq', '-p', default=10, type=int, metavar='N', help='print frequency (default: 10)')
# Current best losses
best_losses = 1000.0
use_gpu = torch.cuda.is_available()
def main():
global args, best_losses, use_gpu
args = parser.parse_args()
print('Arguments: {}'.format(args))
I read some comments to change
parser.parse_args()
to
parser.parse_args(args)
but it didn't work :)
As DIR doesn't have a default value, you need to supply one when running the program. The easiest way to do this is via a command line interface. Consult the documentation of the library you are using for further hints on that.
Related
I want to get the arguments from the parse_args() in the train function. It is giving error as this
ipykernel_launcher.py: error: the following arguments are required: --dataset
import argparse
import torch
from torchsummary import summary
def parse_args():
# Training settings
parser = argparse.ArgumentParser(description='Training Settings')
parser.add_argument("--num_class", type=int, default=1)
parser.add_argument("--k_shot", type=int, default=5)
parser.add_argument("--batch_size", type=int, default=16)
parser.add_argument("--num_episode", type=int, default=500000)
parser.add_argument("--test_episode", type=int, default=2000)
parser.add_argument("--lr", type=float, default=0.001)
parser.add_argument("--gpu", type=int, default=0)
parser.add_argument("--dataset", type=int, default=None, required=True)
parser.add_argument("--finetune", type=bool, default=True)
opt = parser.parse_args()
return opt
def train():
args = parse_args()
k_shot = args.k_shot
model = SegmentationModel("efficientnet-b7")
model.cuda()
train()
You can always pass a list of arguments to parse_args:
def train(args):
args = parse_args(args)
k_shot = args.k_shot
model = SegmentationModel("efficientnet-b7")
model.cuda()
Then do:
>>> train(['--dataset', 'mnist'])
when i am trying to install that particular module utils.logger ,it is showing
ERROR: Could not find a version that satisfies the requirement utils.logger (from versions: none)
ERROR: No matching distribution found for utils.logger.Sorry if my way of interpretation of question is not in an appropriate manner as i am a novice.
This is the code
import logging
import functools
import argparse
import os
from datetime import datetime
from utils.logger import setlogger
from utils.train_utils import train_utils
args = None
def parse_args():
parser = argparse.ArgumentParser(description='Train')
# basic parameters
parser.add_argument('--model_name', type=str, default='cnn_2d', help='the name of the model')
parser.add_argument('--data_name', type=str, default='CWRUSlice', help='the name of the data')
parser.add_argument('--data_dir', type=str, default= "E:\Data\西储大学轴承数据中心网站", help='the directory of the data')
parser.add_argument('--normlizetype', type=str, choices=['0-1', '1-1', 'mean-std'], default='0-1', help='data normalization methods')
parser.add_argument('--processing_type', type=str, choices=['R_A', 'R_NA', 'O_A'], default='R_A',
help='R_A: random split with data augmentation, R_NA: random split without data augmentation, O_A: order split with data augmentation')
parser.add_argument('--cuda_device', type=str, default='0', help='assign device')
parser.add_argument('--checkpoint_dir', type=str, default='./checkpoint', help='the directory to save the model')
parser.add_argument("--pretrained", type=bool, default=True, help='whether to load the pretrained model')
parser.add_argument('--batch_size', type=int, default=64, help='batchsize of the training process')
parser.add_argument('--num_workers', type=int, default=0, help='the number of training process')
# optimization information
parser.add_argument('--opt', type=str, choices=['sgd', 'adam'], default='adam', help='the optimizer')
parser.add_argument('--lr', type=float, default=0.001, help='the initial learning rate')
parser.add_argument('--momentum', type=float, default=0.9, help='the momentum for sgd')
parser.add_argument('--weight_decay', type=float, default=1e-5, help='the weight decay')
parser.add_argument('--lr_scheduler', type=str, choices=['step', 'exp', 'stepLR', 'fix'], default='fix', help='the learning rate schedule')
parser.add_argument('--gamma', type=float, default=0.1, help='learning rate scheduler parameter for step and exp')
parser.add_argument('--steps', type=str, default='9', help='the learning rate decay for step and stepLR')
# save, load and display information
parser.add_argument('--max_epoch', type=int, default=100, help='max number of epoch')
parser.add_argument('--print_step', type=int, default=100, help='the interval of log training information')
args = parser.parse_args()
return args
if name == 'main':
args = parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.cuda_device.strip()
# Prepare the saving path for the model
sub_dir = args.model_name+'_'+args.data_name + '_' + datetime.strftime(datetime.now(), '%m%d-%H%M%S')
save_dir = os.path.join(args.checkpoint_dir, sub_dir)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# set the logger
setlogger(os.path.join(save_dir, 'training.log'))
# save the args
for k, v in args.__dict__.items():
logging.info("{}: {}".format(k, v))
trainer = train_utils(args, save_dir)
trainer.setup()
trainer.train()
my code is a below:
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--episode', type=int, default=2000,
help='number of episode to run')
parser.add_argument('-b', '--batch_size', type=int, default=32,
help='batch size for experience replay')
parser.add_argument('-i', '--initial_invest', type=int, default=20000,
help='initial investment amount')
parser.add_argument('-m', '--mode', type=str, required=True,
help='either "train" or "test"')
parser.add_argument('-w', '--weights', type=str, help='a trained model weights')
args = parser.parse_args()
maybe_make_dir('weights')
maybe_make_dir('portfolio_val')
Error:-
usage: [-h] [-e EPISODE] [-b BATCH_SIZE] [-i INITIAL_INVEST] -m MODE
[-w WEIGHTS]
: error: the following arguments are required: -m/--mode
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
So in the above error i am not able to understand what is the mistake i have also imported all the libraries correctly.
try changing this:
parser.add_argument('-m', '--mode', type=str, required=True,
help='either "train" or "test"')
to this: (notice nargs and const)
parser.add_argument('-m', '--mode',nargs='?', const='' , type=str, required=True,
help='either "train" or "test"')
then you can add this to your code:
if len(args.mode) == 0:
print('-m either "train" or "test"')
import sys
sys.exit() #closes python script
as explained here: https://stackoverflow.com/a/58396626/13093413
I need to integrate two files, one in python the other in julia. More precisely, I need to call a julia function, snapshot_sphere(b,h,data,m,r,d), from a python file. Moreover,I need to provide the parameters via the command line. I had difficulties in using PyJulia. For the moment an error message is persisting:
ModuleNotFoundError: No module named '_ctypes'
I found some dependencies related to libffi-dev. Installing it did not change anything. What should I do to call the julia function from the python file ?
Here is the code of the python file:
import argparse
from julia import Main
from PIL import Image, ImageShow
parser = argparse.ArgumentParser(description="Projection of the picture")
parser.add_argument(
"-f",
"--filename",
type=str,
action="store",
dest="fname",
help="Name of the picture"
)
parser.add_argument(
"-d",
"--density",
type=int,
action="store",
dest="density",
help="Number samples per Pixel"
)
parser.add_argument(
"-r",
"--radius",
type=float,
action="store",
dest="radius",
help="Radius of the sphere"
)
parser.add_argument(
"-x",
type=float,
action="store",
dest="x",
help="X coordinate of the center"
)
parser.add_argument(
"-y",
type=float,
action="store",
dest="y",
help="Y coordinate of the center"
)
parser.add_argument(
"-z",
type=float,
action="store",
dest="z",
help="Z coordinate of the center"
)
args = parser.parse_args()
print(args)
m = (args.x, args.y, args.z)
print(m)
im = Image.open(args.fname)
b, h = im.size
data = list(im.getdata())
Main.b = b
Main.h = h
Main.m = m
Main.r = args.radius
Main.d = args.density
Main.data = data
Main.include("projekt1.jl")
result = Main.eval("snapshot_sphere(b,h,data,m,r,d)")
I have the below mutual exclusive argument group (group), where I want the default to be args.thread=10 and args.process=None, if neither --thread or --process is specified. Furhermore, if --thread or --process is specified, the other should be set to None.
With my current code, the result is always args={ ..., 'thread': 10, 'process': 10, ... }.
How can I fix my code to get the anticipated behavior?
fmt = lambda prog: argparse.HelpFormatter(prog, width=140)
p = argparse.ArgumentParser(description='Atlassian Watchdog: Search through all resources on Confluence, Bitbucket, or Jira looking for secrets.', formatter_class=fmt)
group = p.add_argument_group('Concurrency')
group = group.add_mutually_exclusive_group()
group.add_argument('-t', '--thread', type=int, default=10, metavar='N', dest='thread', help='number of threads (default: %(default)s)')
group.add_argument('-p', '--process', type=int, default=10, metavar='N', dest='process', help='number of subprocesses (default: %(default)s)')