Handling Inputs Using Argparse — Command Line Data Science

Uniqtech
Uniqtech
Feb 13 · 3 min read

How to use argparse in data science, machine learning and deep learning tasks? Here’s your tutorial on command line data science. Specifically we will talk about how to handle terminal arguments in a python script. The argparse module is for “argparse — Parser for command-line options, arguments and sub-commands” — official documentation of Python 3. Argparse allows our python script to take in arguments using flags such as -- double dashes.

python my_python_script.py --input_flag input_value

One reason to use argparse is that it plays nicely with deep learning training functions and prediction functions that are already written in Python. We just have to write the code to accept arguments through the command line. The rest of the Python code is similar to the code in our Jupyter Notebook. In addition to argparse parsers, it is also important to write handlers that can accept these arguments and customizer our model, training and prediction based on input parameters.

Import and Install Argparse

Argparse should be easily imported without further installation. Be sure to import and initiate the ArgumentParser object.

import argparse
parser = argparse.ArgumentParser()

Add an Argument or Input

The official documentation of Python 3 Argparse gives this sample code for accepting named input. In this case the named argument is sum.

parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')

The help option gives a hint when user uses the help flag to preview what the script is expecting in terms of user inputs. Here’s a relevant example we came up with for machine learning, deep learning and data science using command line.

parser.add_argument("--gpu", help="use gpu? 1 yes 0 no",type=int) 
# boolean would have been better

In this case you can specify whether to use GPU by using an integer flag 1,0 . Of course, you can use True or False for this. We just find 1,0 easier to type and spell.

Here’s another example we came up with, you can specify the CNN architecture you’d like to load using a string.

parser.add_argument("--model", help="choose model architecture from: vgg19 vgg16 alexnet", type=str)

Write code to handle the inputs

First we want to be able to access all the argparse arguments using python. Let’s store all the parsed arguments in a a variable called args.

args = parser.parse_args()

Now we can access the named inputs using python dot notation args.gpu.

You can set a flag here that turns 1 into True, and 0 into False. It is essentially a flag that checks if GPU should be used.

flag = args.gpu==1if flag is True:
# do the gpu training
else:
# do the cpu training

Above you can see our args.gpu flag handler code.

Below let’s try some code to handle the args.model input.

print("model choice is set to {}".format(args.model))

Here’s a sample code / logic how to handle string input that refers to different Pytorch torchvision library CNN architectures.

if model_name is "vgg16":
model = torchvision.models.vgg16(pretrained=True)
elif model_name is "vgg19":
model = torchvision.models.vgg19(pretrained=True)
else:
model = torchvision.models.alexnet(pretrained=True)

Load vgg16 or vgg19 from the torchvision models library as needed. If no architecture is specified or if AlexNet is specified, use AlexNet.

Run python training script and pass in named arguments

$ python my_training_script.py --model "vgg16" --gpu 0

Use this command line command python my_training_script.py and substitute the correct script name. Pass in the argument name using double dash, followed by the name of the input without space, then follow up with the value of the input with space.

Get help hints for the scripts

$ python my_training_script.py -h

The helpful message we added to the declaration of argparse arguments will be displayed. Here’s the declaration again.

parser.add_argument("--gpu", help="use gpu? 1 yes 0 no",type=int)

Testing and mocking up inputs

As seen on the official documentation, you can also literally pass in a list of arguments to mockup and test your script.

parser.parse_args(['--sum', '7', '-1', '42'])

This article has been curated by Medium to be featured on the Programming thread. Thank you for your support. Please report / submit any suggestion, feedback, errors asap to hi@uniqtech.co or by replying to this article.

Data Science Bootcamp

Introductory Data Science, Machine Learning and Artificial Intelligence for Bootcamp and Nanodegree Graduates. By a bootcamp grad for bootcamp grads.

Uniqtech

Written by

Uniqtech

We are Uniqtech Co. Learn coding, data and software package skills with Uniqtech tutorials and articles. Contact us hi@uniqtech.co We’d like to hear from you!

Data Science Bootcamp

Introductory Data Science, Machine Learning and Artificial Intelligence for Bootcamp and Nanodegree Graduates. By a bootcamp grad for bootcamp grads.

More From Medium

More from Data Science Bootcamp

More on Deep Learning from Data Science Bootcamp

More on Deep Learning from Data Science Bootcamp

Cool plots with Seaborn barplot with hue and proportions

More on Machine Learning from Data Science Bootcamp

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade