-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.py
162 lines (123 loc) · 5.03 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
"""Main script for ADDA."""
import sys
sys.path.append("../_EXTRAS")
import matplotlib
matplotlib.use('Agg')
import torch
import argparse
import pandas as pd
import numpy as np
import misc as ms
import experiments
from addons import vis
from addons import pretty_plot
import train
if __name__ == '__main__':
# SEE IF CUDA IS AVAILABLE
assert torch.cuda.is_available()
print("CUDA: %s" % torch.version.cuda)
print("Pytroch: %s" % torch.__version__)
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--expList', nargs="+", default=None)
parser.add_argument('-b', '--borgy', default=0, type=int)
parser.add_argument('-br', '--borgy_running', default=0, type=int)
parser.add_argument('-m', '--mode', default="max")
parser.add_argument('-rs', '--reset_src', default=0, type=int)
parser.add_argument('-rt', '--reset_tgt', default=0, type=int)
parser.add_argument('-g', '--gpu', type=int)
parser.add_argument('-s', '--summary', type=int, default=0)
parser.add_argument('-c', '--configList', nargs="+", default=None)
parser.add_argument('-l', '--lossList', nargs="+", default=None)
parser.add_argument('-d', '--datasetList', nargs="+", default=None)
parser.add_argument('-metric', '--metricList', nargs="+", default=None)
parser.add_argument('-model', '--modelList', nargs="+", default=None)
args = parser.parse_args()
ms.set_gpu(args.gpu)
# init random seed
# init_random_seed(10)
results = {}
pp_main = pretty_plot.PrettyPlot(
ratio=0.5,
figsize=(5, 4),
legend_type="line",
yscale="linear",
subplots=(1, 1),
shareRowLabel=True)
for exp_name in args.expList:
exp_dict = experiments.get_experiment_dict(args, exp_name)
exp_dict["reset_src"] = args.reset_src
exp_dict["reset_tgt"] = args.reset_tgt
# SET SEED
np.random.seed(10)
torch.manual_seed(10)
torch.cuda.manual_seed_all(10)
history = ms.load_history(exp_dict)
# Main options
if args.mode == "test_model":
results[exp_name] = ms.test_latest_model(exp_dict, verbose=0)
elif args.mode == "train":
train.train(exp_dict)
if args.mode == "copy_models":
results[exp_name] = ms.copy_models(
exp_dict, path_dst="{}/".format(exp_name))
# MISC
if args.mode == "plot_src":
src_losses = np.array(pd.DataFrame(history["src_train"])["loss"])
src_epochs = np.array(pd.DataFrame(history["src_train"])["epoch"])
pp_main.add_yxList(
y_vals=src_losses[1:101],
x_vals=src_epochs[1:101],
label=exp_name.split("2")[0].upper().replace("BIG", ""),
converged=None)
if args.mode == "plot_tgt":
tgt_acc = np.array(pd.DataFrame(history["tgt_train"])["acc_tgt"])
src_epochs = np.array(pd.DataFrame(history["tgt_train"])["epoch"])
pp_main.add_yxList(
y_vals=tgt_acc[1:101],
x_vals=src_epochs[1:101],
label=exp_name.split("2")[1].upper().replace("BIG", ""),
converged=None)
# vis.visEmbed(exp_dict)
if args.mode == "vis":
vis.visEmbed(exp_dict)
elif args.mode == "summary":
summary = pd.DataFrame(history["tgt_train"][1:])["acc_tgt"]
print(summary.describe())
elif args.mode == "acc_tgt":
summary = pd.DataFrame(history["tgt_train"][1:200])["acc_tgt"]
print(summary)
elif args.mode == "max":
try:
summary = pd.DataFrame(history["tgt_train"][1:])["acc_tgt"]
results[exp_name] = summary.max()
except:
print("{} skipped...".format(exp_name))
print(pd.Series(results))
# Train Source
if args.mode == "plot_src":
pp_main.plot(ylabel="Triplet Loss", xlabel="Epochs", yscale="log")
path = exp_dict["summary_path"]
pp_main.fig.tight_layout(rect=[0, 0.03, 1, 0.95])
figName = "%s/png_plots/SRC_%s.png" % (path, exp_name)
ms.create_dirs(figName)
pp_main.fig.savefig(figName)
pp_main.fig.tight_layout()
pp_main.fig.suptitle("")
figName = "%s/pdf_plots/SRC_%s.pdf" % (path, exp_name)
ms.create_dirs(figName)
pp_main.fig.savefig(figName, dpi=600)
print("saved {}".format(figName))
if args.mode == "plot_tgt":
pp_main.plot(
ylabel="Classifcation Accuracy", xlabel="Epochs", yscale="log")
path = exp_dict["summary_path"]
pp_main.fig.tight_layout(rect=[0, 0.03, 1, 0.95])
figName = "%s/png_plots/TGT_%s.png" % (path, exp_name)
ms.create_dirs(figName)
pp_main.fig.savefig(figName)
pp_main.fig.tight_layout()
pp_main.fig.suptitle("")
figName = "%s/pdf_plots/TGT_%s.pdf" % (path, exp_name)
ms.create_dirs(figName)
pp_main.fig.savefig(figName, dpi=600)
print("saved {}".format(figName))