-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.py
136 lines (112 loc) · 3.63 KB
/
entry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
93
94
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
import logging
import sys
import click
import pytorch_lightning as pl
import torch
import torch.backends.mps
from hginput import command
pl.seed_everything(42)
@click.group()
@click.option("--verbose", "-v", is_flag=True, help="Show debug log")
def cli(verbose: bool):
logger = logging.getLogger("hginput")
log_level = logging.DEBUG if verbose else logging.INFO
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setLevel(log_level)
stdout_handler.setFormatter(
logging.Formatter("[%(levelname)s] %(asctime)s - %(message)s")
)
logger.setLevel(logging.DEBUG)
logger.addHandler(stdout_handler)
@cli.group(help="Dataset utilities")
def dataset():
pass
@dataset.command(
help="Gather samples of dataset."
"Press R to switch recording status, Press Q to quit."
)
@click.option("-l", "--label")
@click.option("--device", type=int, default=0)
@click.option("--width", type=int, default=480)
@click.option("--height", type=int, default=270)
@click.option("--fps", type=int, default=30)
def gather(label: str, device: int, width: int, height: int, fps: int):
command.gather(label, device, width, height, fps)
@dataset.command(help="Create a dataset from raw datasets.")
@click.option("-t", "--tag", required=True, help="Tag of the dataset.")
@click.option(
"--exclude", multiple=True, help="These labels will be excluded.", default=[]
)
def create(tag: str, exclude: list[str]):
command.create(tag, tuple(exclude))
@cli.group(help="Model utilities")
def model():
pass
@model.command(help="Train a model.")
@click.option("-t", "--tag", required=True, help="Tag of the dataset.")
@click.option("-p", "--project", required=True, help="Project name of wandb.")
@click.option("--layers", type=int, default=0, help="Number of middle layers.")
@click.option("--units", type=int, default=32, help="Number of middle layer units.")
@click.option("--lr", type=float, default=1e-3, help="Learning rate.")
@click.option("--dropout", type=float, default=0.1, help="Dropout rate.")
@click.option("--batch-size", type=int, default=32, help="Batch size.")
@click.option("--validation-rate", type=float, default=0.2, help="Validation rate.")
@click.option(
"--cpu", is_flag=True, help="Use CPU instead of any accelerators(CUDA, MPS)."
)
def train(
tag: str,
project: str,
layers: int,
units: int,
lr: float,
dropout: float,
batch_size: int,
validation_rate: float,
cpu: bool,
):
accelerator = (
"cpu"
if cpu
else "mps"
if torch.backends.mps.is_available()
else "gpu"
if torch.cuda.is_available()
else "cpu"
)
command.train(
tag,
project,
accelerator=accelerator,
n_layers=layers,
n_units=units,
lr=lr,
dropout=dropout,
batch_size=batch_size,
validation_rate=validation_rate,
)
@model.command(help="Show the structure of a model.")
def summary():
command.summary()
@model.command(help="Optimize")
@click.option("-t", "--tag", required=True, help="Tag of the dataset.")
@click.option(
"--cpu", is_flag=True, help="Use CPU instead of any accelerators(CUDA, MPS)."
)
@click.option("--n-trials", type=int, default=100, help="Number of trials.")
def optimize(tag: str, cpu: bool, n_trials: int):
accelerator = (
"cpu"
if cpu
else "mps"
if torch.backends.mps.is_available()
else "gpu"
if torch.cuda.is_available()
else "cpu"
)
command.optimize(tag, accelerator, n_trials)
@cli.command(help="Run app")
def launch():
command.launch("config.json")
if __name__ == "__main__":
cli()