forked from beasteers/auditory-slow-fast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
174 lines (139 loc) · 4.91 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import argparse
import json
import os
from typing import Any, Dict, List
from time import sleep
import librosa
import pandas as pd
import torch
import yaml
from loguru import logger
from audio_slowfast.models.build import build_model
import src.utils
from audio_slowfast import test, train
from audio_slowfast.config.defaults import get_cfg
from audio_slowfast.utils.discretize import discretize
from audio_slowfast.utils.misc import launch_job
from src.dataset import prepare_dataset
from src.pddl import Predicate
def load_config(config_path: str) -> Dict[str, Any]:
"""
Load the configuration file.
Parameters
----------
`config_path` : `str`
The path to the configuration file.
Returns
-------
`Dict[str, Any]`
The configuration file.
"""
with open(config_path, "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
return config
def parse_args() -> Dict[str, Any]:
"""
Parse the arguments passed to the script.
Returns
-------
`Dict[str, Any]`
The arguments passed to the script.
"""
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, default="config.yaml")
parser.add_argument("--example", type=str, default=None)
parser.add_argument("--train", action="store_true")
parser.add_argument("--test", action="store_true")
args = parser.parse_args()
return vars(args)
def validate_args(args: Dict[str, Any]) -> None:
"""
Validate the arguments passed to the script.
Parameters
----------
`args` : `Dict[str, Any]`
The arguments passed to the script.
"""
logger.debug(f"Arguments:\n{json.dumps(args, indent=4)}")
if not os.path.exists(args["config"]):
logger.error(f"Config file {args['config']} does not exist")
exit(1)
if args["example"] and not os.path.exists(args["example"]):
logger.error(f"Example file {args['example']} does not exist")
exit(1)
def main(args: Dict[str, Any]) -> None:
"""
Main function of the script.
Parameters
----------
`args` : `Dict[str, Any]`
The arguments passed to the script.
Raises
------
`ValueError`
In case the povided model is not supported.
"""
cfg = get_cfg()
cfg.merge_from_file(args["config"])
os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
if args["train"]:
if not torch.cuda.is_available():
logger.warning("No GPU found. Running on CPU. Also deactivating W&B reports.")
# Modify config for debug training
cfg.NUM_GPUS = 0
cfg.WANDB.ENABLE = False
cfg.TENSORBOARD.ENABLE = False
cfg.DATA_LOADER.NUM_WORKERS = 4
cfg.TRAIN.BATCH_SIZE = 2
# Prepare the dataset
if not cfg.EPICKITCHENS.SKIP_PREPARATION:
prepare_dataset(cfg=cfg)
else:
if not os.path.exists(cfg.EPICKITCHENS.PROCESSED_TRAIN_LIST):
logger.error(f"Train list {cfg.EPICKITCHENS.PROCESSED_TRAIN_LIST} does not exist")
exit(1)
if not os.path.exists(cfg.EPICKITCHENS.PROCESSED_VAL_LIST):
logger.error(f"Val list {cfg.EPICKITCHENS.PROCESSED_VAL_LIST} does not exist")
exit(1)
sleep(1)
launch_job(cfg=cfg, init_method=None, func=train)
cfg = get_cfg()
cfg.merge_from_file(args["config"])
launch_job(cfg=cfg, init_method=None, func=test)
elif args["test"]:
launch_job(cfg=cfg, init_method=None, func=test)
def example(
model: torch.nn.Module,
attributes: List[str],
file_path: str,
device: str = "cuda",
) -> None:
logger.warning(model)
model.eval()
vocab_verb, vocab_noun = model.vocab
logger.info(f"Loading input audio from {args['example']}")
y, sr = librosa.load(file_path, sr=24_000)
spec = model.prepare_audio(y, sr)
logger.debug(f"Spec shapes: {[x.shape for x in spec]}")
verb, noun, prec, postc = model([x.to(device) for x in spec])
i_vs, i_ns = (
torch.argmax(verb, dim=-1),
torch.argmax(noun, dim=-1),
)
i_pres, i_poss = prec[0], postc[0]
logger.info(f"{i_pres=}")
logger.info(f"{i_poss=}")
logger.debug(f"Discrete pre: {discretize(i_pres)}")
logger.debug(f"Discrete posts: {discretize(i_poss)}")
for v, n, _, _, i_v, i_n, i_pre, i_pos in zip(verb, noun, prec, postc, i_vs, i_ns, prec, postc):
logger.debug(
f"\nVerb: {vocab_verb[i_v]} ({v[i_v]:.2%})\n"
f"Noun: {vocab_noun[i_n]} ({n[i_n]:.2%})\n"
f"Preconditions: {Predicate.predicates_from_vector(vector=discretize(i_pre), attributes=attributes, to_str=True,)}\n"
f"Postconditions: {Predicate.predicates_from_vector(vector=discretize(i_pos), attributes=attributes, to_str=True,)}\n"
)
if __name__ == "__main__":
src.utils.setup_run()
args = parse_args()
validate_args(args=args)
main(args=args)