forked from AllenCellModeling/pytorch_integrated_cell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_latent_walk.py
198 lines (111 loc) · 3.9 KB
/
print_latent_walk.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
#######
### This function prints off the most likely predicted
### channels for each of the cells in our dataset
#######
import argparse
import importlib
import numpy as np
import os
import pickle
import math
import torch
import torch.nn as nn
from torch.autograd import Variable
import torchvision.utils
#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 time
import model_utils
import torch.backends.cudnn as cudnn
cudnn.benchmark = True
import pdb
from tqdm import tqdm
parent_dir = './test_aaegan/aaegan3Dv5_128D'
model_dir = parent_dir + os.sep + 'struct_model'
# 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 = [0, 1]
torch.manual_seed(opt.myseed)
torch.cuda.manual_seed(opt.myseed)
np.random.seed(opt.myseed)
data_path = './data_{0}x{1}.pyt'.format(str(opt.imsize), str(opt.imsize))
dp = model_utils.load_data_provider(data_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)
dec = models['dec']
dec.train(False)
models = None
optimizers = None
print('Done loading model.')
#######
### Main Loop
#######
import pdb
from aicsimage.io import omeTifWriter
from imgToProjection import imgtoprojection
from IPython.core.display import display
import PIL.Image
import matplotlib.pyplot as plt
import scipy.misc
import pandas as pd
from corr_stats import pearsonr, corrcoef
opt.batch_size = 400
gpu_id = opt.gpu_ids[0]
save_parent = opt.save_dir + os.sep + 'latent_walk' + os.sep
if not os.path.exists(save_parent):
os.makedirs(save_parent)
nclasses = dp.get_n_classes()
nref = opt.nRef
nlatent = opt.nlatentdim
nframes = 500
stdstep = 0.1
init = Variable(torch.Tensor(1, nref).normal_().repeat(nclasses,1).cuda(gpu_id))
classes = torch.Tensor(nclasses, nclasses).fill_(0).cuda(gpu_id)
for i in range(0,nclasses): classes[i,i] = 1
classes = (classes - 1) * 25
classes = Variable(classes)
struct = Variable(torch.Tensor(nclasses, nlatent).fill_(0).cuda(gpu_id))
for i in range(0, nframes):
path = './{0}/step_{1}.png'.format(save_parent, int(i));
if os.path.exists(path): os.remove(path)
def tensor2img(img):
colormap = 'hsv'
colors = plt.get_cmap(colormap)(np.linspace(0, 1, img.shape[1]+1))
img = img.numpy()
im_out = list()
for i in range(0, img.shape[0]):
img_tmp = img[i]
for j in range(0, len(img_tmp)):
img_tmp[j] = img_tmp[j]/np.max(img_tmp[j])
img_tmp = np.swapaxes(img_tmp, 1,3)
im_proj = imgtoprojection(img_tmp, proj_all=True, colors = colors, global_adjust=True)
im_proj = np.swapaxes(im_proj, 0, 2)
im_proj = np.flip(im_proj,0)
im_proj = np.flip(im_proj,1)
im_out.append(im_proj)
img = np.concatenate(im_out, 1)
# if len(img.shape) == 3:
# img = np.expand_dims(img, 3)
# for i in range(0, len(img)):
# img[i] = img[i]/np.max(img[i])
# img = np.swapaxes(img, 2,3)
# img = imgtoprojection(np.swapaxes(img, 1, 3), proj_all=True, colors = colors, global_adjust=True)
return img
for i in tqdm(range(0, nframes)):
im_out = dec([classes, init, struct])
im_out = tensor2img(im_out.data.cpu())
scipy.misc.imsave('./{0}/step_{1}.png'.format(save_parent, int(i)), im_out)
step_pt1 = torch.Tensor(1, nref).normal_(0,stdstep).repeat(nclasses,1).cuda(gpu_id)
init = Variable(init.data + step_pt1)
init = init - init*0.01