forked from lne-lab/actor-retina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_fig_9_xfold.py
142 lines (113 loc) · 4.3 KB
/
gen_fig_9_xfold.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
import numpy as np
import glob
import os
import matplotlib.pyplot as plt
import pandas as pd
import time
import gc
import itertools
from collections import defaultdict
import math
import datetime
import logging
from collections import defaultdict
import seaborn as sns
import tensorflow as tf
import utils,config
import custom_metrics
import custom_model
from tensorflow.keras import layers,optimizers
from cnn_models import create_ecker_cnn_model
__basename__ = os.path.basename(__file__)
__name__, _ = os.path.splitext(__basename__)
__time__ = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0' # i.e. keep all message
experiment_directory = r'F:\Retina_project\Dataset_public\models\forward_model'
dataset_folder = r'F:\Retina_project\Dataset_public\models\forward_model'
gpus = tf.config.list_physical_devices(device_type='GPU')
if gpus:
is_gpu_available = True
else:
is_gpu_available = False
train_x,val_x,test_x,train_y,val_y,test_y = utils.load_train_val_test(dataset_folder)
forward_inp = layers.Input(shape = train_x.shape[1:])
model_kwargs = {
"core": {
"nbs_kernels": (4,),
"kernel_sizes": (7,),
"strides": (1,),
"paddings": ('valid',),
"dilation_rates": (1,),
# "activations": ('relu',),
"activations": ('softplus',),
"smooth_factors": (0.001,),
"sparse_factors": (None,),
"name": 'core',
},
"readout": {
'nb_cells':train_y.shape[1],
"spatial_sparsity_factor": 0.0001,
"feature_sparsity_factor": 0.1,
"name": 'readout',
},
}
train_data = (train_x, train_y)
val_data = (val_x, val_y)
test_data = (test_x,test_y)
fwd_model = create_ecker_cnn_model(model_kwargs=model_kwargs, train_data=train_data, name="model")
model_handler = fwd_model.create_handler(
directory=experiment_directory,
train_data=(train_x, train_y),
val_data=(val_x, val_y),
test_data=(test_x, test_y),
)
run_name = "best_run"
model_handler.load(run_name=run_name)
#randomly initialize hyperparams and change later
model_args = {}
model_args['n_channel'] = 2
model_args['kernal_size']=2
model_args['l2_reg'] = 1e-3
model_args['n_out'] = 64
experiment_directory = r'F:\Retina_project\Dataset_public\models\xfold_models'
final_fig_dir = r'F:\Retina_project\Dataset_public\figures\figure_9'
os.makedirs(final_fig_dir,exist_ok=True)
n_outs = [64,32,16,8,4]
perc_increase_lst=[]
diff_df = []
n_out_order = []
act_avg_order = []
perc_increase_df = pd.DataFrame()
boxplot_data = {}
for n_out in n_outs:
run_name = "best_%d"%(n_out)
load_dir =os.path.join(experiment_directory,run_name)
model_args = utils.load_params_actor(load_dir, model_args)
actor_network = custom_model.img_transformation_network(fwd_model,forward_inp,train_y.shape[1],model_args)
actor_network.load_weights(os.path.join(load_dir,'my_model_checkpoint')).expect_partial()
avg_network = custom_model.avg_downsample_network(fwd_model,forward_inp,red_dim = model_args['n_out'])
avg_network.compile(loss='poisson', optimizer=optimizers.Adam(0.002), metrics=[custom_metrics.cc_met,custom_metrics.rmse_met, custom_metrics.fev_met])
perc_increase,diff_results = utils.plot_paired_test(fwd_model,avg_network,actor_network,test_x,test_y, save_dir = None,nout=n_out)
perc_increase_lst.append(np.percentile(perc_increase,50))
perc_increase_df[str(n_out)] = perc_increase
diff_df.append(diff_results['fwd - act'])
diff_df.append(diff_results['fwd - avg'])
n_out_order.append(str(n_out))
n_out_order.append(str(n_out))
act_avg_order.append('act')
act_avg_order.append('avg')
boxplot_data['act_%d'%(n_out)] = diff_results['fwd - act']
boxplot_data['avg_%d'%(n_out)] = diff_results['fwd - avg']
plt.close('all')
diff_df = pd.DataFrame(np.array(diff_df))
diff_df['n_out_order'] = n_out_order
diff_df['act_avg_order'] = act_avg_order
boxplot_data = pd.DataFrame(boxplot_data)
boxplot_data.to_csv(os.path.join(final_fig_dir,'all_boxplot.csv'))
diff_df = pd.melt(diff_df, id_vars=['n_out_order','act_avg_order'])
sns.boxplot(x="n_out_order", y="value",
hue="act_avg_order",
data=diff_df,
flierprops={"marker": "o"})
plt.savefig(os.path.join(final_fig_dir,'xfold_results.jpeg'))
# diff_df.to_csv(os.path.join(final_fig_dir,'all_boxplot.csv'))