forked from AllenCellModeling/pytorch_integrated_cell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_images_proj_single_channel.py
More file actions
204 lines (139 loc) · 5.54 KB
/
print_images_proj_single_channel.py
File metadata and controls
204 lines (139 loc) · 5.54 KB
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
#######
### This function prints off the most likely predicted
### channels for each of the cells in our dataset
#######
#######
### Load the Model Parts
#######
import SimpleLogger as SimpleLogger
import importlib
import numpy as np
import os
import pickle
import math
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import torchvision.utils
import model_utils
from tqdm import tqdm
import torch.backends.cudnn as cudnn
cudnn.benchmark = True
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=400, help='batch_size')
parser.add_argument('--overwrite', type=bool, default=False, help='overwrite existing results')
parser.add_argument('--model_dir', default='struct_model', help='Model component direcoty')
args = parser.parse_args()
model_dir = args.parent_dir + os.sep + args.model_dir
opt = pickle.load(open( '{0}/opt.pkl'.format(model_dir), "rb" ))
print(opt)
opt.gpu_ids = args.gpu_ids
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, optimizers, _, _, opt = model_utils.load_model(opt.model_name, opt)
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
from IPython.core.display import display
import PIL.Image
import matplotlib.pyplot as plt
import scipy.misc
import pandas as pd
gpu_id = opt.gpu_ids[0]
colormap = 'hsv'
colors = plt.get_cmap(colormap)(np.linspace(0, 1, 4))
# [magenta, yellow, cyan]
# colors = [[1, 0, 1], [1, 1, 0], [0, 1, 1]]
px_size = [1,1,1]
train_or_test_split = ['test', 'train']
img_paths_all = list()
save_parent = args.parent_dir + os.sep + 'analysis' + os.sep + 'proj_single_channel'
save_out_table = save_parent + os.sep + 'list_of_images.csv'
column_names = ['orig', 'recon'] + ['pred_' + name for name in dp.label_names] + ['train_or_test', 'orig_struct', 'img_index']
if not os.path.exists(save_parent):
os.makedirs(save_parent)
def convert_image(img):
img = img.data[0].cpu().numpy()
img = np.transpose(img, (3, 0, 1, 2))
return img
def img2projection(img):
img = convert_image(img)
img = np.transpose(img, (1,0,2,3))
img = imgtoprojection(img, proj_all=True, colors = colors, global_adjust=True)
img = np.transpose(img, (1,2,0))
return img
# For train or test
for train_or_test in train_or_test_split:
ndat = dp.get_n_dat(train_or_test)
# For each cell in the data split
for i in tqdm(range(0, ndat)):
img_index = dp.data[train_or_test]['inds'][i]
img_class = dp.image_classes[img_index]
img_name = os.path.basename(dp.get_image_paths([i], train_or_test)[0])[0:-3]
save_dir = save_parent + os.sep + train_or_test + os.sep + img_name
if not os.path.exists(save_dir):
os.makedirs(save_dir)
pred_all_path = save_dir + os.sep + 'img' + str(img_index) + '_' + img_class + '-pred_all.png'
if os.path.exists(pred_all_path) and not args.overwrite:
continue
#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)
img_recon = dec(z)
pred_imgs = list()
img_paths = list()
### Original
img = img2projection(img_in)
img_path_base = save_dir + os.sep + 'img' + str(img_index) + '_orig'
img_name = img_path_base + '_memb.png'
scipy.misc.imsave(img_name, img[:,:,0])
img_name = img_path_base + '_' + img_class + '.png'
scipy.misc.imsave(img_name, img[:,:,1])
img_name = img_path_base + '_nuc.png'
scipy.misc.imsave(img_name, img[:,:,2])
### Reconstruction
img = img2projection(img_recon)
img_path_base = save_dir + os.sep + 'img' + str(img_index) + '_recon'
img_name = img_path_base + '_memb.png'
scipy.misc.imsave(img_name, img[:,:,0])
img_name = img_path_base + '_' + img_class + '.png'
scipy.misc.imsave(img_name, img[:,:,1])
img_name = img_path_base + '_nuc.png'
scipy.misc.imsave(img_name, img[:,:,2])
var_classes = (np.identity(dp.get_n_classes()) - 1) * 25
var_struct = np.zeros([dp.get_n_classes(), z[-1].size()[1]])
z[0] = Variable(torch.Tensor(var_classes).cuda(gpu_id), volatile=True)
z[1] = z[1].repeat(dp.get_n_classes(), 1)
z[2] = Variable(torch.Tensor(var_struct).cuda(gpu_id), volatile=True)
img_pred = dec(z)
img_path_base = save_dir + os.sep + 'img' + str(img_index) + '_pred_'
for j, class_name in zip(range(0, dp.get_n_classes()), dp.label_names):
img_name = img_path_base + class_name + '.png'
img = img2projection(img_pred[[j]])
scipy.misc.imsave(img_name, img[:,:,1])