-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode_posterior.py
77 lines (64 loc) · 1.92 KB
/
decode_posterior.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
import os
import argparse
import yaml
import pickle
from pathlib import Path
import numpy as np
import torch
import utils
from PracticalCoding.decode import decode_irec
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--seed", type=int, default=22,
help="manual seed"
)
parser.add_argument(
"--dataset", default="kodak", choices=("cifar", "kodak", ),
help="dataset selection"
)
parser.add_argument(
"--image_id", type=int, required=True,
help="kodak image id"
)
parser.add_argument(
"-c", "--config_path", type=Path, required=True,
help="configuration file"
)
args = parser.parse_args()
return args
def make_args(args, cfgs):
for key, value in cfgs.items():
setattr(args, key, value)
args.total_gpus = torch.cuda.device_count()
args.log_dir = "log_{}_num{}_emd{}_lat{}_beta{}".format(
args.dataset,
args.num_layers,
args.dim_emb,
args.dim_hid,
args.weight_kl
)
args.save_dir = "save_{}_num{}_emd{}_lat{}_beta{}".format(
args.dataset,
args.num_layers,
args.dim_emb,
args.dim_hid,
args.weight_kl
)
return args
def main():
args = parse_args()
cfgs = yaml.load(open(args.config_path, "r"), Loader=yaml.FullLoader)
args = make_args(args, cfgs)
print(args)
torch.manual_seed(args.seed)
np.random.seed(args.seed)
model_prior = utils.load_model_prior(args)
# Load parameter groups. How to partition the parameters is synced for the encoder and decoder.
groups_filename = os.path.join(args.save_dir, "groups.pkl")
assert os.path.exists(groups_filename), "Parameters should be partitioned into groups first"
with open(groups_filename, "rb") as f:
groups = pickle.load(f)
decode_irec(args, model_prior, groups)
if __name__ == "__main__":
main()