Example #1
0
def add_flag(name, *args, **kwargs):
    """Add a flag.

    Added flags can be accessed by `FLAGS` module variable.
    (e.g. `FLAGS.my_flag_name`)

    - Args
        - `name`: Flag name. Real flag name will be `"--{}".format(name)`.
        - `*args`, `**kwargs`: The rest arguments are the same as
            `argparse.ArgumentParser.add_argument()`.
    """
    global _FLAG_NAMES

    if name not in _FLAG_NAMES:
        _FLAG_NAMES.add(name)
        gargparse.add_argument("--" + name, *args, **kwargs)
Example #2
0
import gargparse

gargparse.add_argument("foo", type=str, help="This is an example.")
print(gargparse.ARGS.foo)
gargparse.parse_args(["bar"])
print(gargparse.ARGS.foo)
Example #3
0
import gargparse


gargparse.add_argument("-f", "--foo",
                       type=str, default="bar",
                       help="This is an example.")
print(gargparse.ARGS.foo)
Example #4
0
#!/usr/bin/python3

import json
import multiprocessing
import os
import os.path

import gargparse
from gargparse import ARGS
import langdetect
import nltokeniz

gargparse.add_argument('--train_data_size', type=int, required=True)
gargparse.add_argument('--develop_data_size', type=int, required=True)
gargparse.add_argument('--test_data_size', type=int, required=True)
gargparse.add_argument('example_filename')
gargparse.add_argument('output_dirname', default='.', nargs='?')


def write_json_file(line, data_dir):
    records = line.split('\t')
    assert len(records) == 9

    try:
        document = nltokeniz.tokenize(records[-1])
    except langdetect.lang_detect_exception.LangDetectException as e:
        document = [[records[-1]]]

    example = {
        'id': int(records[0]),
        'document': document,
#!/usr/bin/env python

import argparse
import json
import os
import os.path

import gargparse
import matplotlib.image as image
import numpy as np

gargparse.add_argument('char_file', type=argparse.FileType())
gargparse.add_argument('font_file', type=argparse.FileType())
gargparse.add_argument('attention_file', type=argparse.FileType())
gargparse.add_argument('dest_dir')


def main():
    os.makedirs(gargparse.ARGS.dest_dir, exist_ok=True)

    fonts = np.array(json.load(gargparse.ARGS.font_file), dtype=np.uint8)
    assert fonts.min() == 0 and fonts.max() == 255

    for char, font, attention in zip(
        [char.strip() for char in gargparse.ARGS.char_file.readlines()],
            np.stack([255 - fonts] * 3, axis=-1),
            np.array(json.load(gargparse.ARGS.attention_file))):
        assert np.isclose(attention.sum(), 1)

        font[:, :, 1] = font[:, :, 2] = np.minimum(
            font[:, :, 0], 255 - (255 * np.sqrt(attention)).astype(np.uint8))