forked from ryanhe312/ABAW2-FPNMAA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_label.py
59 lines (48 loc) · 1.74 KB
/
gen_label.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
from argparse import ArgumentParser
import pytorch_lightning as pl
import pretty_errors
import yaml
import numpy as np
from os.path import *
from datasets import *
from model import *
def cli_main():
pl.seed_everything(1234, workers=True)
# ------------
# args
# ------------
parser = ArgumentParser()
parser = pl.Trainer.add_argparse_args(parser)
parser.add_argument('--config', type=str, default=None)
parser.add_argument('--checkpoint', type=str, default=None)
args = parser.parse_args()
# ------------
# trainer
# ------------
trainer = pl.Trainer.from_argparse_args(args)
# ------------
# model
# ------------
model_args = yaml.safe_load(open(args.config, 'r')) if args.config else {}
dm = AffWild2DataModule(model_args)
model = MonoPyramidClassifier.load_from_checkpoint(args.checkpoint, strict=False) if args.checkpoint else MonoPyramidClassifier(model_args)
# ------------
# predicting
# ------------
pred = trainer.predict(model, datamodule=dm)
# ------------
# writing
# ------------
soft_label_dict = dict(zip(dm.predict_dataset.image, np.concatenate(pred, axis=0)))
hard_label_dict = dict(zip(dm.train_dataset.image, dm.train_dataset.label))
label_dict = soft_label_dict
label_dict.update(hard_label_dict)
file = open(join(dm.dataset_dir, 'file.txt'), 'w+')
file.writelines([join(basename(dirname(path)), basename(path)) + '\n' for path in label_dict.keys()])
file.close()
label = open(join(dm.dataset_dir, dm.data_type+'.txt'), 'w+')
label.write(dm.data_type+'\n')
label.writelines([','.join(label.astype(str))+'\n' for label in label_dict.values()])
label.close()
if __name__ == '__main__':
cli_main()