This repository was archived by the owner on Dec 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_random_patches.py
More file actions
302 lines (228 loc) · 9.72 KB
/
extract_random_patches.py
File metadata and controls
302 lines (228 loc) · 9.72 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
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
from collections import namedtuple
from threading import Thread, Event
from queue import Queue, Empty
from typing import Optional
from os.path import exists, dirname
from os import makedirs
from io import BytesIO
import csv
import numpy as np
from skimage.transform import resize
from skimage.color import gray2rgb, rgb2gray
from skimage.filters import scharr
from sklearn.feature_extraction.image import extract_patches_2d
from PIL import Image
from cropping.edge_stats import get_edge_statistics
from cropping.shrink import crop
from cropping.normalize import normalize
import tensorflow as tf
from apiclient import discovery
from oauth2client.client import GoogleCredentials
OutputItem = namedtuple('OutputItem', ['image_id', 'patch_id', 'patch'])
ImageItem = namedtuple('ImageItem', ['image_id', 'bytes'])
FileItem = namedtuple('FileItem', ['image_id', 'bucket', 'object'])
class QueueWorker(Thread):
def __init__(self, queue: Queue, stop: Event):
self.__queue = queue # type: Queue
self.__stop = stop # type: Event
Thread.__init__(self)
def run(self):
while not (self.__stop.is_set() and self.__queue.empty()):
try:
item = self.__queue.get(block=True, timeout=1)
self.process(item)
self.__queue.task_done()
except Empty:
pass
self.done()
def process(self, item):
pass
def done(self):
pass
class TFRecordWriteWorker(QueueWorker):
def __init__(self, queue: Queue, stop: Event):
self.__writer = None # type: Optional[tf.python_io.TFRecordWriter]
self.__writer_options = tf.python_io.TFRecordOptions(tf.python_io.TFRecordCompressionType.GZIP)
self.__written_patches = 0 # type: int
self.__max_written_patches = 100000 # 1000 patches are approximately 0.7 .. 1.1 MB
self.__file_counter = -1
QueueWorker.__init__(self, queue, stop)
def __del__(self):
self._close_writer()
def process(self, item: OutputItem):
feature = {
'iid': _int64_feature(item.patch_id),
'pid': _int64_feature(item.image_id),
'raw': _bytes_feature(item.patch.tostring())
}
self._write_feature(feature)
def done(self):
self._close_writer()
def _get_filename(self):
filename = 'data/%05i.tfrecord.gz' % self.__file_counter
d = dirname(filename)
if not exists(d):
makedirs(d)
return filename
def _write_feature(self, feature: tf.train.Feature):
if (self.__written_patches >= self.__max_written_patches) or (self.__writer is None):
self.__file_counter += 1
self.__written_patches = 0
self._close_writer()
filename = self._get_filename()
self.__writer = tf.python_io.TFRecordWriter(filename, options=self.__writer_options)
example = tf.train.Example(features=tf.train.Features(feature=feature))
self.__writer.write(example.SerializeToString())
self.__written_patches += 1
def _close_writer(self):
if self.__writer is not None:
self.__writer.close()
self.__writer = None
class PatchExtractionWorker(QueueWorker):
def __init__(self, input_queue: Queue, stop_input: Event, output_queue: Queue):
self.__output_queue = output_queue # type: Queue
self.__patch_id = -1
# patch sets
PatchSet = namedtuple('PatchSet', ['size', 'count'])
self.__target_size = 32
self.__sets = [PatchSet(size=32, count=64),
PatchSet(size=64, count=64),
PatchSet(size=128, count=32)]
QueueWorker.__init__(self, input_queue, stop_input)
def process(self, item: ImageItem):
with BytesIO(item.bytes) as buffer, Image.open(buffer) as pi:
image = np.array(pi)
# fetch one image
image = gray2rgb(image)
# apply energy cropping
energy = scharr(rgb2gray(image))
energy = normalize(energy)
stats = get_edge_statistics(energy, edge_width=50)
# crop the image
cropped = crop(image, energy, threshold=None, stats=stats)
height, width = image.shape[0], image.shape[1]
# extract patches
unique_patches = []
extracted_patches = 0
for patch_set in self.__sets:
patch_size = patch_set.size
# sanity check the patch size
if (patch_size >= height) or (patch_size >= width):
continue
# extract the patches
patch_count = patch_set.count
try:
patches = extract_patches_2d(cropped, (patch_size, patch_size), patch_count)
except ValueError:
continue
extracted_patches += len(patches)
# sort patches by variance
patches = np.split(patches, patch_count, axis=0)
patches = np.squeeze(patches)
patches = sorted(patches, key=get_variance)
# deduplication of exactly identical images might be something sane;
# we might end up with half of the patches being identical due to border effects
for patch in patches:
if patch.shape[0] != self.__target_size:
patch = resize(patch, (self.__target_size, self.__target_size), preserve_range=True)
patch = np.floor(patch).astype(np.uint8)
did_match = False
for up in unique_patches:
if np.array_equal(patch, up):
did_match = True
break
if not did_match:
unique_patches.append(patch)
print('File %i: Added %i patches; skipped %i non-distinct patches.' % (item.image_id, len(unique_patches),
extracted_patches - len(unique_patches)))
# stash patches for serialization
for patch in unique_patches:
self.__patch_id += 1
item = OutputItem(image_id=item.image_id, patch_id=self.__patch_id, patch=patch)
self.__output_queue.put(item)
def done(self):
pass
class DownloadWorker(QueueWorker):
def __init__(self, input_queue: Queue, stop_input: Event, output_queue: Queue, client):
self.__output_queue = output_queue # type: Queue
self.__client = client
QueueWorker.__init__(self, input_queue, stop_input)
def process(self, item: FileItem):
assert item is not None
assert item.bucket is not None and item.object is not None
assert item.image_id >= 0
request = self.__client.objects().get_media(bucket=item.bucket, object=item.object)
result = request.execute()
item = ImageItem(image_id=item.image_id, bytes=result)
self.__output_queue.put(item)
def done(self):
pass
def get_variance(patch: np.ndarray) -> float:
v = np.var(patch) # type: float
return -v
def _int64_feature(value) -> tf.train.Feature:
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value) -> tf.train.Feature:
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def main():
# prepare the clients
credentials = GoogleCredentials.from_stream('google-credentials.json')
def create_client():
return discovery.build('storage', 'v1', credentials=credentials)
# prepare the worker
image_id = -1
store_queue = Queue(maxsize=2000)
stop_output = Event()
record_writer = TFRecordWriteWorker(store_queue, stop_output)
image_queue = Queue(maxsize=4000)
stop_images = Event()
n_extractors = 32
patch_extractors = [PatchExtractionWorker(input_queue=image_queue, stop_input=stop_images,
output_queue=store_queue)
for _ in range(0, n_extractors)]
file_queue = Queue(maxsize=400000)
stop_files = Event()
n_downloaders = 8
download_workers = [DownloadWorker(input_queue=file_queue, stop_input=stop_files,
output_queue=image_queue, client=create_client())
for _ in range(0, n_downloaders)]
record_writer.start()
for thread in patch_extractors:
thread.start()
for thread in download_workers:
thread.start()
# fetch all images
bucket = 'product-pictures'
client = create_client()
request = client.objects().list(
bucket=bucket,
prefix='',
fields='nextPageToken,items(name)')
with open('data/index.tsv', 'w', newline='') as csv_file:
writer = csv.writer(csv_file, delimiter='\t', quotechar='\"', quoting=csv.QUOTE_MINIMAL)
object_page = -1
while request is not None:
object_page += 1
print('Requesting page %i of objects (seen objects: %i)...' % (object_page,
image_id if image_id >= 0 else 0))
response = request.execute()
request = client.objects().list_next(request, response)
if 'items' not in response:
continue
for item in response['items']:
image_id += 1
image_file = item['name']
writer.writerow([image_id, image_file])
item = FileItem(bucket=bucket, object=image_file, image_id=image_id)
file_queue.put(item)
csv_file.flush()
stop_files.set()
for thread in download_workers:
thread.join()
stop_images.set()
for thread in patch_extractors:
thread.join()
stop_output.set()
record_writer.join()
if __name__ == '__main__':
main()