-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathfull_image_crop_generator.py
164 lines (131 loc) · 6.24 KB
/
full_image_crop_generator.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
import os
import numpy as np
import pandas as pd
import skimage.io
import tensorflow as tf
import multiprocessing
import deepprofiler.imaging.cropping
import deepprofiler.dataset.pixels
import deepprofiler.dataset.utils
tf.compat.v1.disable_v2_behavior()
tf.config.run_functions_eagerly(False)
class GeneratorClass(deepprofiler.imaging.cropping.CropGenerator):
def __init__(self, config, dset, mode="training"):
super(GeneratorClass, self).__init__(config, dset)
self.num_channels = len(self.config["dataset"]["images"]["channels"])
self.box_size = self.config["dataset"]["locations"]["box_size"]
self.view_size = self.config["dataset"]["locations"]["view_size"]
self.batch_size = self.config["train"]["model"]["params"]["batch_size"]
self.mode = mode
# Load metadata
self.all_images = pd.read_csv(self.config["paths"]["index"])
if self.config['prepare']['compression']['implement']:
self.directory = self.config["paths"]["compressed_images"]
self.all_images.replace({'.tiff': '.png', '.tif': '.png'}, inplace=True, regex=True)
else:
self.directory = self.config["paths"]["images"]
self.target = self.config["train"]["partition"]["targets"][0]
# Index targets for one-hot encoded labels
self.split_data = self.all_images[self.all_images[self.config["train"]["partition"]["split_field"]].isin(
self.config["train"]["partition"][self.mode])].reset_index(drop=True)
self.classes = list(self.all_images[self.target].unique())
self.num_classes = len(self.classes)
self.classes.sort()
self.classes = {self.classes[i]: i for i in range(self.num_classes)}
# Identify targets and samples
self.balanced_sample()
self.expected_steps = (self.samples.shape[0] // self.batch_size) + \
int(self.samples.shape[0] % self.batch_size > 0)
# Report number of classes globally
self.config["num_classes"] = self.num_classes
print(" >> Number of classes:", self.num_classes)
def start(self, session):
pass
def balanced_sample(self):
# Obtain distribution of images per class
counts = self.split_data.groupby(self.target).count().reset_index()[[self.target, 'Metadata_Site']]
sample_size = int(counts.Metadata_Site.median())
counts = {r[self.target]: r.Metadata_Site for k, r in counts.iterrows()}
# Sample the same number of images per class
class_samples = []
for cls in self.split_data[self.target].unique():
class_samples.append(self.split_data[self.split_data[self.target] == cls].sample(
n=sample_size, replace=counts[cls] < sample_size))
samples = pd.concat(class_samples)
# Randomize order
samples = samples.sample(frac=1.0).reset_index(drop=True)
self.samples = samples
# Report numbers
if self.mode == "training":
print(" >> Shuffling training sample with", len(self.samples), "examples")
else:
print(" >> Validation samples per class:", np.mean(self.samples[self.target].value_counts()))
def get_image_paths(self, r):
return [os.path.join(self.directory, r[ch]) for ch in self.config["dataset"]["images"]["channels"]]
def generator(self, sess, global_step=0):
pointer = 0
image_loader = deepprofiler.dataset.utils.Parallel(
[self.config["dataset"]["locations"]["view_size"], True],
self.config["train"]["sampling"]["workers"]
)
while True:
# Prepare batch metadata
y = []
batch_paths = []
for i in range(self.batch_size):
if pointer >= len(self.samples):
self.balanced_sample()
pointer = 0
batch_paths.append(self.get_image_paths(self.samples.iloc[pointer]))
y.append(self.classes[self.samples.loc[pointer, self.target]])
pointer += 1
# Load batch images
x = np.zeros([self.batch_size, self.view_size, self.view_size, self.num_channels])
images = image_loader.compute(load_and_crop, batch_paths)
for i in range(len(batch_paths)):
x[i, :, :, :] = images[i]
inputs = [x, np.asarray([[0, 0, 1, 1]]*len(y)), np.arange(0, len(y))]
yield inputs, tf.keras.utils.to_categorical(y, num_classes=self.num_classes)
image_loader.close()
def generate(self, sess):
pointer = 0
image_loader = deepprofiler.dataset.utils.Parallel(
[self.config["dataset"]["locations"]["view_size"], False],
self.config["train"]["sampling"]["workers"]
)
while True:
# Prepare metadata
y = []
batch_paths = []
for i in range(self.batch_size):
if pointer >= len(self.samples):
pointer = 0
break
batch_paths.append(self.get_image_paths(self.samples.iloc[pointer]))
y.append(self.classes[self.samples.loc[pointer, self.target]])
pointer += 1
# Load images
x = np.zeros([self.batch_size, self.view_size, self.view_size, self.num_channels])
images = image_loader.compute(load_and_crop, batch_paths)
for i in range(len(images)):
x[i, :, :, :] = images[i]
if len(y) < x.shape[0]:
x = x[0:len(y), ...]
inputs = [x, np.asarray([[0, 0, 1, 1]]*len(y)), np.arange(0, len(y))]
yield inputs, tf.keras.utils.to_categorical(y, num_classes=self.num_classes)
image_loader.close()
def stop(self, session):
pass
def load_and_crop(params):
paths, others = params
view_size, random = others
im = deepprofiler.dataset.pixels.openImage(paths, None)
im = im / 255.
H, W, C = im.shape
if random:
q = np.random.randint(0, H - view_size)
else:
q = (H - view_size)//2
return im[q:q+view_size, q:q+view_size, :]
# Reusing the Single Image Crop Generator. No changes needed
SingleImageGeneratorClass = deepprofiler.imaging.cropping.SingleImageCropGenerator