-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_manager.py
More file actions
74 lines (58 loc) · 1.94 KB
/
batch_manager.py
File metadata and controls
74 lines (58 loc) · 1.94 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
"""
Author: Sigve Rokenes
Date: February, 2019
Batch manager
"""
import os
import random
import skimage as sk
from skimage import io
from skimage import transform
from util import gray2rgb
import numpy as np
class BatchManager:
def __init__(self, path, gray=False, resize=None, limit=None, skip=0):
self.path = path
self.resize = resize
self.gray = gray
self.image_paths = os.listdir(path)
self.images = []
for p in self.image_paths:
if skip > 0:
skip -= 1
continue
self.images.append(self.by_name(p))
if limit and len(self.images) == limit:
break
random.shuffle(self.images)
self.batch_index = 0
def by_name(self, name):
img = sk.io.imread(os.path.join(self.path, name), as_gray=self.gray)
img = sk.img_as_float(img)
if self.resize:
img = sk.transform.resize(img, self.resize)
if self.gray:
w, h = img.shape[0], img.shape[1]
img = np.reshape(img, [w, h, 1])
elif len(img.shape) == 2:
img = gray2rgb(img)
return img
def next(self):
return self.next_batch(1)
def next_batch(self, batch_size=32):
assert len(self.images) > 0, "Batch manager contains no images."
assert batch_size <= len(self.images), "Batch size larger than data set."
batch = []
while len(batch) < batch_size:
end = min(self.batch_index + batch_size, len(self.images))
sub = self.images[self.batch_index:end-len(batch)]
batch.extend(sub)
self.batch_index += len(sub)
if self.batch_index >= len(self.images):
self.batch_index = 0
random.shuffle(self.images)
return batch
def num_examples(self):
return len(self.images)
def sample(self, size=1):
return random.sample(self.images, size)