-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
145 lines (116 loc) · 4.41 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
import argparse
from copy import deepcopy
import os
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
from src.load import MeshMeasurements, load, load_from_shapes
from src.metrics import evaluate
from src.model import Model
from src.logs import log, save_results
from src.visualize import visualize, visualize_measurement_distribution
RESULTS_DIR = './results/'
def init_argparse():
parser = argparse.ArgumentParser()
parser.add_argument(
'--data_root',
type=str,
help='root data folder'
)
parser.add_argument(
'--dataset_name',
type=str,
help='dataset name'
)
parser.add_argument(
'--features',
type=str,
choices=['baseline', 'measurements'],
help='input features (for fitting from measurements to PCs)'
)
parser.add_argument(
'--target',
type=str,
choices=['shape', 'measurements'],
help='target variable'
)
parser.add_argument(
'--model_type',
type=str,
choices=['linear', 'poly', 'tree', 'mlp'],
default='linear',
help='target variable'
)
parser.add_argument(
'--gender',
type=str,
choices=['male', 'female', 'neutral', 'both'],
help='If both, then evaluate gender-specific model on all data. If neutral, use neutral model'
)
parser.add_argument(
'--height_noise',
type=float,
help='std added to height GT'
)
parser.add_argument(
'--weight_noise',
type=float,
help='std added to weight GT'
)
parser.add_argument(
'--weight_noise2',
type=float,
help='std added to weight GT'
)
parser.add_argument(
'--num_interaction',
type=int,
help='# interaction terms added to linear model'
)
return parser
if __name__ == '__main__':
np.random.seed(2021)
parser = init_argparse()
args = parser.parse_args()
print(f'Preparing {args.dataset_name} dataset...')
#if args.dataset_name != 'star':
# X, y, measurements, genders = load_from_shapes(args)
#else:
#X, y, measurements, genders = load_from_shapes(args)
X, y, measurements, genders = load(args)
all_inputs = np.concatenate([X, measurements], axis=1)
#X, y, measurements, genders = load_from_shapes(args)
print('Train/test splitting...')
np.save(f'{args.gender}_measurements_{args.weight_noise}_{args.weight_noise2}_{args.height_noise}.npy', np.concatenate([X, measurements], axis=1))
np.save('measurement_names.npy', np.array(['height', 'weight'] + MeshMeasurements.labels()))
indices = np.arange(X.shape[0])
assert(args.features == 'baseline' or (args.features == 'measurements' and args.target == 'shape'))
X = deepcopy(X) if args.features == 'baseline' else all_inputs
X_train, X_test, y_train, y_test, meas_train, meas_test, _, gender_test, train_indices, test_indices = train_test_split(
X, y, measurements, genders, indices, test_size=0.33, random_state=2021)
print(f'Creating model...')
model = Model(args.model_type)
print(f'Target variable: {args.target}...')
reg = model.fit(X_train, y_train if args.target == 'shape' else meas_train)
print('Predicting...')
y_predict = reg.predict(X_test)
importances = model.feature_importances()
intercepts = model.intercepts()
if args.target == 'measurements':
importances = importances
intercepts = intercepts
all_coefs = np.concatenate([importances, intercepts.reshape((-1, 1))], axis=1)
np.save(os.path.join(RESULTS_DIR, f'{args.gender}_meas_coefs.npy'), all_coefs)
print('Evaluating...')
y_gt = y_test if args.target == 'shape' else meas_test
params_errors, maes, s2s_dists, mres, allowable_ratios = evaluate(y_predict, y_gt, gender_test, args.target)
score = r2_score(y_gt, y_predict)
print(f'R2-score: {score}')
# NOTE: Need to predict shape parameters here.
print('Saving results...')
save_results(genders[0], y_predict, maes, s2s_dists, test_indices, args)
print('Logging to stdout...')
log(model, args, params_errors, maes, s2s_dists, mres, allowable_ratios)
print('Visualizing...')
visualize_measurement_distribution(args.gender, X, measurements)
#visualize(model, args, params_errors, measurement_errors, s2s_dists)