forked from AllenCellModeling/pytorch_integrated_cell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inception_score.py
346 lines (230 loc) · 10.1 KB
/
inception_score.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#######
### This function prints off the inception score
### for both the input images and generated images
#######
import argparse
import importlib
import numpy as np
import pandas as pd
import os
import pickle
import math
import torch
import torch.nn as nn
from torch.autograd import Variable
from scipy.spatial.distance import pdist
from scipy.spatial.distance import squareform
#have to do this import to be able to use pyplot in the docker image
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from IPython import display
import model_utils
import torch.backends.cudnn as cudnn
cudnn.benchmark = True
import pdb
from tqdm import tqdm
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--parent_dir', help='save dir')
parser.add_argument('--gpu_ids', nargs='+', type=int, default=[0], help='gpu id')
parser.add_argument('--batch_size', type=int, default=200, help='gpu id')
parser.add_argument('--overwrite', type=bool, default=False, help='overwrite?')
args = parser.parse_args()
model_dir = args.parent_dir + os.sep + 'struct_model'
ref_dir = args.parent_dir + os.sep + 'ref_model'
save_dir = args.parent_dir + os.sep + 'analysis' + os.sep + 'inception_score' + os.sep
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# logger_file = '{0}/logger_tmp.pkl'.format(model_dir)
opt = pickle.load(open( '{0}/opt.pkl'.format(model_dir), "rb" ))
print(opt)
opt.gpu_ids = args.gpu_ids
gpu_id = opt.gpu_ids[0]
torch.manual_seed(opt.myseed)
torch.cuda.manual_seed(opt.myseed)
np.random.seed(opt.myseed)
dp = model_utils.load_data_provider(opt.data_save_path, opt.imdir, opt.dataProvider)
#######
### Load REFERENCE MODEL
#######
opt.channelInds = [0, 1, 2]
dp.opts['channelInds'] = opt.channelInds
opt.nch = len(opt.channelInds)
opt.nClasses = dp.get_n_classes()
opt.nRef = opt.nlatentdim
models, _, _, _, opt = model_utils.load_model(opt.model_name, opt)
opt.batch_size = args.batch_size
enc = models['enc']
dec = models['dec']
enc.train(False)
dec.train(False)
models = None
optimizers = None
print('Done loading model.')
#######
### Main Loop
#######
import pdb
from aicsimage.io import omeTifWriter
from imgToProjection import imgtoprojection
import PIL.Image
from aicsimage.io import omeTifWriter
import scipy.misc
import pandas as pd
##########
## data ##
##########
fname = os.path.join(save_dir,'im_class_log_probs_data.pickle')
if os.path.exists(fname) and not args.overwrite:
pass
else:
im_class_log_probs = {}
# For train or test
for train_or_test in ['test', 'train']:
ndat = dp.get_n_dat(train_or_test)
inds = np.arange(0, ndat)
pred_log_probs = np.zeros([ndat,opt.nClasses])
iter_struct = [inds[j:j+opt.batch_size] for j in range(0, len(inds), opt.batch_size)]
# For each cell in the data split
# for i in tqdm(range(0, 1)):
for i in tqdm(iter_struct, desc='data, ' + train_or_test):
# Load the image
img_in = dp.get_images(i, train_or_test)
img_in = Variable(img_in.cuda(gpu_id), volatile=True)
# pass forward through the model
z = enc(img_in)
p = z[0].data.cpu().numpy()
pred_log_probs[i,:] = p
im_class_log_probs[train_or_test] = pred_log_probs
# save test and train preds
with open(fname, 'wb') as handle:
pickle.dump(im_class_log_probs, handle, protocol=pickle.HIGHEST_PROTOCOL)
#############
# autoencoded #
#############
fname = os.path.join(save_dir,'im_class_log_probs_autoencode.pickle')
if os.path.exists(fname) and not args.overwrite:
pass
else:
im_class_log_probs = {}
# For train or test
for train_or_test in ['test', 'train']:
ndat = dp.get_n_dat(train_or_test)
inds = np.arange(0, ndat)
pred_log_probs = np.zeros([ndat,opt.nClasses])
iter_struct = [inds[j:j+opt.batch_size] for j in range(0, len(inds), opt.batch_size)]
for i in tqdm(iter_struct, desc='autoencode, ' + train_or_test):
# Load the image
img_in = dp.get_images(i, train_or_test)
img_in = Variable(img_in.cuda(gpu_id), volatile=True)
# pass forward through the model
z = enc(dec(enc(img_in)))
p = z[0].data.cpu().numpy()
pred_log_probs[i,:] = p
im_class_log_probs[train_or_test] = pred_log_probs
# save test and train preds
with open(fname, 'wb') as handle:
pickle.dump(im_class_log_probs, handle, protocol=pickle.HIGHEST_PROTOCOL)
#############
# generated #
#############
fname = os.path.join(save_dir,'im_class_log_probs_gen.pickle')
if os.path.exists(fname) and not args.overwrite:
pass
else:
im_class_log_probs = {}
# For train or test
for train_or_test in ['test', 'train']:
ndat = dp.get_n_dat(train_or_test)
inds = np.arange(0, ndat)
pred_log_probs = np.zeros([ndat,opt.nClasses])
iter_struct = [inds[j:j+opt.batch_size] for j in range(0, len(inds), opt.batch_size)]
for i in tqdm(iter_struct, desc='gen, ' + train_or_test):
npts = len(i)
# Load the image
class_ids = dp.get_classes(i, train_or_test)
classes = Variable(torch.Tensor(npts, opt.nClasses).fill_(-25).cuda(gpu_id), volatile=True)
for j, class_id in zip(range(0, npts), class_ids):
classes[j, class_id] = 0
# sample random latent space vectors
ref = Variable(torch.Tensor(npts, opt.nRef).normal_().cuda(gpu_id), volatile=True)
struct = Variable(torch.Tensor(npts, opt.nRef).normal_().cuda(gpu_id), volatile=True)
# generate a fake cell of corresponding class
img_in = dec([classes, ref, struct])
# pass forward through the model
z = enc(img_in)
p = z[0].data.cpu().numpy()
pred_log_probs[i,:] = p
im_class_log_probs[train_or_test] = pred_log_probs
# save test and train preds
with open(fname, 'wb') as handle:
pickle.dump(im_class_log_probs, handle, protocol=pickle.HIGHEST_PROTOCOL)
#############
# print csv #
#############
dirname = args.parent_dir
fname = 'analysis/inception_score/im_class_log_probs_data.pickle'
with open(os.path.join(dirname, fname), 'rb') as handle:
data_logprobs = pickle.load(handle)
fname = 'analysis/inception_score/im_class_log_probs_autoencode.pickle'
with open(os.path.join(dirname, fname), 'rb') as handle:
autoencode_logprobs = pickle.load(handle)
fname = 'analysis/inception_score/im_class_log_probs_gen.pickle'
with open(os.path.join(dirname, fname), 'rb') as handle:
gen_logprobs = pickle.load(handle)
def D_KL(P,Q):
return -np.sum(P*np.log(Q/P))
def inception_score_all_ims(P_yGx):
p_y = np.mean(P_yGx, axis=0)
KL_divs = np.array([D_KL(p_yGx,p_y) for p_yGx in P_yGx])
return np.exp(np.mean(KL_divs))
def KL_divs_per_im(P_yGx):
p_y = np.mean(P_yGx, axis=0)
KL_divs = np.array([D_KL(p_yGx,p_y) for p_yGx in P_yGx])
return KL_divs
df = pd.DataFrame(columns=['phase', 'inds_phase', 'inds_master',
'structureProteinName',
'KLdiv_data', 'KLdiv_autoencode', 'KLdiv_gen'])
# Get all of the inception scores into a big list
data_list = list()
for phase in dp.data.keys():
KL_divs_data = KL_divs_per_im(np.exp(data_logprobs[phase]))
KL_divs_autoencode = KL_divs_per_im(np.exp(autoencode_logprobs[phase]))
KL_divs_gen = KL_divs_per_im(np.exp(gen_logprobs[phase]))
for ind_phase, ind_master in enumerate(tqdm(dp.data[phase]['inds'])):
data = [phase, ind_phase, ind_master,
dp.label_names[dp.get_classes([ind_phase], phase)[0]],
KL_divs_data[ind_phase], KL_divs_autoencode[ind_phase], KL_divs_gen[ind_phase]]
data_list.append(data)
df = pd.DataFrame(data_list, columns=['phase', 'inds_phase', 'inds_master',
'structureProteinName',
'KLdiv_data', 'KLdiv_autoencode', 'KLdiv_gen'])
# compute the inception scores for each class, and all classes for training, test, and generated data
inception_scores = list()
train_inds = df['phase'] == 'train';
test_inds = df['phase'] == 'test';
for label in dp.label_names:
struct_inds = df['structureProteinName'] == label;
all_train_inds = train_inds & struct_inds
all_test_inds = test_inds & struct_inds
incept_gen_train = np.exp(np.mean(df['KLdiv_gen'][all_train_inds]))
incept_gen_test = np.exp(np.mean(df['KLdiv_gen'][all_test_inds]))
incept_data_train = np.exp(np.mean(df['KLdiv_data'][all_train_inds]))
incept_data_test = np.exp(np.mean(df['KLdiv_data'][all_test_inds]))
incept_autoencode_train = np.exp(np.mean(df['KLdiv_autoencode'][all_train_inds]))
incept_autoencode_test = np.exp(np.mean(df['KLdiv_autoencode'][all_test_inds]))
inception_scores.append([incept_data_train, incept_data_test,
incept_autoencode_train, incept_autoencode_test,
incept_gen_train, incept_gen_test])
incept_gen_train = np.exp(np.mean(df['KLdiv_gen'][train_inds]))
incept_gen_test = np.exp(np.mean(df['KLdiv_gen'][test_inds]))
incept_data_train = np.exp(np.mean(df['KLdiv_data'][train_inds]))
incept_data_test = np.exp(np.mean(df['KLdiv_data'][test_inds]))
incept_autoencode_train = np.exp(np.mean(df['KLdiv_autoencode'][train_inds]))
incept_autoencode_test = np.exp(np.mean(df['KLdiv_autoencode'][test_inds]))
inception_scores.append([incept_data_train, incept_data_test, incept_autoencode_train, incept_autoencode_test, incept_gen_train, incept_gen_test])
df_inception_scores = pd.DataFrame(inception_scores, index=list(dp.label_names) + ['all classes'], columns=['data train', 'data test', 'autoencoded data train', 'autoencoded data test', 'generated data train', 'generated data test'])
df_inception_scores.to_csv(save_dir + os.sep + 'inception_scores.csv')
df_inception_scores_sigfigs = df_inception_scores.round(decimals=3)
df_inception_scores_sigfigs.to_csv(save_dir + os.sep + 'inception_scores_sigfigs.csv')