0

I want to execute a train.py script inside a colab or jupyter notebook.

Before running I have to set some variables, e.g. dataset-type.

I did as I would type into a terminal command but I get a SyntaxError.

--dataset-type=voc

        --dataset-type='voc'
                            ^
    SyntaxError: can't assign to operator

Executing from a terminal works fine but how do I declare the variables correctly?

Here is some code of train.py:

parser = argparse.ArgumentParser(description='Single Shot MultiBox Detector Training With PyTorch')

# Params for datasets
parser.add_argument('--dataset-type', default="voc", type=str,
                    help='Specify dataset type. Currently supports voc and open_images.')
parser.add_argument('--datasets', '--data', nargs='+', default=["data"], help='Dataset directory path')
parser.add_argument('--balance-data', action='store_true',
                    help="Balance training data by down-sampling more frequent labels.")

I tried:

from sys import argv
argv.append('--dataset-type=voc')

This solved the SyntaxError.

But I get following error in the end. There are more variables to set, but --dataset-type is still listed.

usage: ipykernel_launcher.py [-h] [--dataset-type DATASET_TYPE]
                             [--datasets DATASETS [DATASETS ...]]
                             [--balance-data] [--net NET] [--freeze-base-net]
                             [--freeze-net] [--mb2-width-mult MB2_WIDTH_MULT]
                             [--base-net BASE_NET]
                             [--pretrained-ssd PRETRAINED_SSD]
                             [--resume RESUME] [--lr LR] [--momentum MOMENTUM]
                             [--weight-decay WEIGHT_DECAY] [--gamma GAMMA]
                             [--base-net-lr BASE_NET_LR]
                             [--extra-layers-lr EXTRA_LAYERS_LR]
                             [--scheduler SCHEDULER] [--milestones MILESTONES]
                             [--t-max T_MAX] [--batch-size BATCH_SIZE]
                             [--num-epochs NUM_EPOCHS]
                             [--num-workers NUM_WORKERS]
                             [--validation-epochs VALIDATION_EPOCHS]
                             [--debug-steps DEBUG_STEPS] [--use-cuda USE_CUDA]
                             [--checkpoint-folder CHECKPOINT_FOLDER]
ipykernel_launcher.py: error: unrecognized arguments: -f /root/.local/share/jupyter/runtime/kernel-f89b4ea1-0c84-4617-af88-0191a91639c0.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2
/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py:2890: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
Tillmann2018
  • 327
  • 5
  • 10
  • You're typing `--dataset-type='voc'` into Python? data-set-type isn't a global variable and that isn't how you would set it. Presumably your parser parses the command line arguments and then incorporates them in some kind of function call. Look there for how the arguments are actually used. – khelwood Jul 28 '21 at 08:15
  • Can you provide some code? – Ivan Jul 28 '21 at 08:21

1 Answers1

1

It seems the model uses sys.argv, to assign them in a notebook:

from sys import argv
argv.append('--dataset-type=voc')

That should work the same as adding --dataset-type=voc in a terminal.

ronkov
  • 1,263
  • 9
  • 14