-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09-predict_image_directories.py
176 lines (104 loc) · 4 KB
/
09-predict_image_directories.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
# coding: utf-8
# # Last step
# # Predict segmentations
# In[1]:
# In[2]:
import os
import os.path
import glob
import matplotlib.pyplot as plt
import numpy as np
import skimage.io
import skimage.morphology
import tqdm
import tensorflow as tf
import keras
import utils.metrics
import utils.model_builder
# Configurable parameter:
# * config (experiment)
# * GPU
# * input folder
# * output folder
# # Configuration
# In[3]:
GPU_NO = "1"
input_directory = "/storage/data/2018_tim_tracking/2018_06_29_lineage_tracking_blainey/images/movies/12_2018_MCF10_Drug_full/images"
# "/storage/data/2018_tim_tracking/2018_08_06_migration_perturbations_asthma/images/2018_asthma_migration_data_kira"
output_directory = "/storage/data/2018_tim_tracking/2018_06_29_lineage_tracking_blainey/images/movies/12_2018_MCF10_Drug_full/predictions"
#"/storage/data/2018_tim_tracking/2018_08_06_migration_perturbations_asthma/images/2018_asthma_migration_data_kira_predictions"
experiment_name = "09"
# # Initialize keras
# In[4]:
from config import config_vars
# load configuration file
config_vars = utils.dirtools.setup_experiment(config_vars, experiment_name)
# output directory
config_vars["probmap_out_dir"] = output_directory
# initialize GPU
configuration = tf.ConfigProto()
configuration.gpu_options.allow_growth = True
configuration.gpu_options.visible_device_list = GPU_NO
session = tf.Session(config = configuration)
# apply session
keras.backend.set_session(session)
# # Load and preprocessing images
# In[5]:
def load_and_prepare_images_from_directory(input_directory):
image_names = glob.glob(input_directory + "/*png")
images = []
imagebuffer = skimage.io.imread_collection( image_names )
for image in imagebuffer:
#image = skimage.io.imread(filename)
images.append(skimage.color.rgb2gray(image))
#print("found {} images".format(len(imagebuffer)))
images = np.array(images)
dim1 = np.floor(images.shape[1]/16) * 16
dim1 = dim1.astype(np.int)
dim2 = np.floor(images.shape[2]/16) * 16
dim2 = dim2.astype(np.int)
images = images[:,0:dim1,0:dim2]
dim1 = images.shape[1]
dim2 = images.shape[2]
images = images.reshape((-1, dim1, dim2, 1))
#print(dim1,dim2)
# preprocess images
percentile = 99.9
for image_no in range(images.shape[0]):
orig_img = images[image_no,:,:,:]
high = np.percentile(orig_img, percentile)
low = np.percentile(orig_img, 100-percentile)
img = np.minimum(high, orig_img)
img = np.maximum(low, img)
img = (img - low) / (high - low)
img = skimage.img_as_ubyte(img)
images[image_no,:,:,:] = img # gives float64, thus cast to 8 bit later
images = images.astype(float)
images = images / 256
return(images,imagebuffer)
# # Predict images
# In[6]:
for directory in tqdm.tqdm(os.listdir(input_directory)):
if os.path.exists(os.path.join(output_directory,directory)):
print("Folder {} processed!".format(directory))
else:
print("Predicting images in folder {}".format(directory))
[images,imagebuffer] = load_and_prepare_images_from_directory(os.path.join(input_directory,directory))
# build model and load weights
dim1 = images.shape[1]
dim2 = images.shape[2]
model = utils.model_builder.get_model_3_class(dim1, dim2)
model.load_weights(config_vars["model_file"])
# prediction
#for i in
predictions = model.predict(images, batch_size=1)
os.makedirs(os.path.join(output_directory,directory))
print("Saving image data into folder {}".format(output_directory))
for i in range(len(images)):
image_savename = os.path.join(
output_directory,
directory,
os.path.basename(imagebuffer.files[i])
)
probmap = predictions[i].squeeze()
skimage.io.imsave(os.path.splitext(image_savename)[0] + ".png", probmap)