-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_predict_label.py
77 lines (61 loc) · 2.52 KB
/
run_predict_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
""" functions to regress y (labels) based on z (latent space) """
import os
import pickle
import h5py
import pandas as pd
import time
import feature_axis as feature_axis
def gen_time_str():
""" tool function to generate time str like 20180927_205959 """
return time.strftime("%Y%m%d_%H%M%S", time.gmtime())
##
""" get y and z from pre-generated files """
path_gan_sample_img = './asset_results/pggan_celeba_sample_jpg/'
path_celeba_att = './data/raw/celebA_annotation/list_attr_celeba.txt'
path_feature_direction = './asset_results/pg_gan_celeba_feature_direction_40'
filename_sample_y = 'sample_y.h5'
filename_sample_z = 'sample_z.h5'
pathfile_y = os.path.join(path_gan_sample_img, filename_sample_y)
pathfile_z = os.path.join(path_gan_sample_img, filename_sample_z)
with h5py.File(pathfile_y, 'r') as f:
y = f['y'][:]
with h5py.File(pathfile_z, 'r') as f:
z = f['z'][:]
# read feature name
df_attr = pd.read_csv(path_celeba_att, sep='\s+', header=1, index_col=0)
y_name = df_attr.columns.values.tolist()
##
""" regression: use latent space z to predict features y """
feature_slope = feature_axis.find_feature_axis(z, y, method='tanh')
##
""" normalize the feature vectors """
yn_normalize_feature_direction = True
if yn_normalize_feature_direction:
feature_direction = feature_axis.normalize_feature_axis(feature_slope)
else:
feature_direction = feature_slope
""" save_regression result to hard disk """
if not os.path.exists(path_feature_direction):
os.mkdir(path_feature_direction)
pathfile_feature_direction = os.path.join(path_feature_direction,
'feature_direction_{}.pkl'.format(gen_time_str()))
dict_to_save = {'direction': feature_direction, 'name': y_name}
with open(pathfile_feature_direction, 'wb') as f:
pickle.dump(dict_to_save, f)
##
""" disentangle correlated feature axis """
# pathfile_feature_direction = glob.glob(os.path.join(path_feature_direction, 'feature_direction_*.pkl'))[-1]
#
# with open(pathfile_feature_direction, 'rb') as f:
# feature_direction_name = pickle.load(f)
#
# feature_direction = feature_direction_name['direction']
# feature_name = np.array(feature_direction_name['name'])
#
# len_z, len_y = feature_direction.shape
#
# feature_direction_disentangled = feature_axis.disentangle_feature_axis_by_idx(
# feature_direction, idx_base=range(len_y // 4), idx_target=None)
#
# feature_axis.plot_feature_cos_sim(feature_direction_disentangled, feature_name=feature_name)
##