Skip to content

cli

Module that contains the command line application.

Functions:

  • get_parser

    Return the CLI argument parser.

  • main

    Run the main program.

get_parser

get_parser() -> ArgumentParser

Return the CLI argument parser.

Returns:

Source code in src/autoden/cli.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def get_parser() -> argparse.ArgumentParser:
    """
    Return the CLI argument parser.

    Returns
    -------
    argparse.ArgumentParser
        An argparse parser.
    """
    parser = argparse.ArgumentParser(
        prog="autoden",
        description="Denoise the given images, using deep-learning-based unsupervised or self-supervised algorithms.",
    )
    parser.add_argument("algorithm", choices=["N2N", "N2V", "DIP"], help="Denoising algorithm to use")
    parser.add_argument(
        "--epochs",
        "-e",
        type=int,
        help=f"Number of epochs to use, by default {DEFAULT_EPOCHS}.",
        metavar="E",
        default=DEFAULT_EPOCHS,
    )
    parser.add_argument(
        "--unet-levels",
        "-l",
        type=int,
        help=f"Number of UNet levels to use, by default: {NetworkParamsUNet.DEFAULT_LEVELS}.",
        default=NetworkParamsUNet.DEFAULT_LEVELS,
        metavar="L",
    )
    parser.add_argument(
        "--unet-features",
        "-f",
        type=int,
        help=f"Number of UNet features to use, by default: {NetworkParamsUNet.DEFAULT_FEATURES}.",
        default=NetworkParamsUNet.DEFAULT_FEATURES,
        metavar="F",
    )
    parser.add_argument(
        "--regularization",
        "-r",
        type=float,
        help=f"Total Variation regularization value, by default: {DEFAULT_TV_VAL}.",
        default=DEFAULT_TV_VAL,
        metavar="R",
    )
    parser.add_argument("src_file", nargs="+", help="Path of each input image.", type=argparse.FileType("rb"))
    parser.add_argument("dst_file", help="Path of the output image.", type=argparse.FileType("wb"))
    parser.add_argument("-V", "--version", action="version", version=f"%(prog)s {debug.get_version()}")
    parser.add_argument("--debug-info", action=_DebugInfo, help="Print debug information.")
    return parser

main

main(args: list[str] | None = None) -> int

Run the main program.

This function is executed when you type autoden or python -m autoden.

Parameters:

  • args (list[str] | None, default: None ) –

    Arguments passed from the command line, by default None.

Returns:

  • int

    An exit code.

Source code in src/autoden/cli.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def main(args: list[str] | None = None) -> int:
    """
    Run the main program.

    This function is executed when you type `autoden` or `python -m autoden`.

    Parameters
    ----------
    args : list[str] | None
        Arguments passed from the command line, by default None.

    Returns
    -------
    int
        An exit code.
    """
    parser = get_parser()
    opts = parser.parse_args(args=args)
    # print(opts)  # noqa: WPS421 (side-effect in main is fine)

    inp_imgs = [iio.imread(f) for f in opts.src_file]
    if any(x.ndim > 2 for x in inp_imgs):
        print("Color images not supported, yet.")
        return 1
    inp_imgs_stack = np.stack(inp_imgs, axis=0)

    net_pars = NetworkParamsUNet(n_levels=opts.unet_levels, n_features=opts.unet_features)

    if opts.algorithm.upper() == "DIP":
        algo = DIP(model=net_pars, reg_val=opts.regularization)
        inp_img = algo.train_unsupervised(inp_imgs_stack, epochs=opts.epochs)
        out_img = algo.infer(inp_img)
    elif opts.algorithm.upper() == "N2N":
        if len(inp_imgs) < 2:
            print(f"Not enough input images, only {len(inp_imgs)} were passed.")
            return 1

        algo = N2N(model=net_pars, reg_val=opts.regularization)
        algo.train_selfsupervised(inp_imgs_stack, epochs=opts.epochs)
        out_img = algo.infer(inp_imgs_stack)
    else:
        print(f"Not implemented support for algorithm {opts.algorithm} in command-line, yet.")
        return 1

    iio.imwrite(opts.dst_file, out_img)
    return 0