-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild_image_generator.py
More file actions
57 lines (42 loc) · 1.34 KB
/
build_image_generator.py
File metadata and controls
57 lines (42 loc) · 1.34 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
# coding: utf-8
# In[1]:
import numpy as np
from PIL import Image
import os
from random import choice
import matplotlib.pyplot as plt
from generate_masks import pad_mask
# In[42]:
base_path = './tiles/keras_folders/'
def image_data_generator(path=base_path, train=True, batch_size=10, shuffle=False):
"""Custom image data generator for this problem."""
if train:
path += 'train/'
else:
path += 'test/'
filenames = [x[:-4] for x in os.listdir(path)]
ind = 0
while True:
x, y = [], []
while len(x) < batch_size:
if ind == len(filenames):
ind = 0
if shuffle:
id_no = choice(filenames)
else:
id_no = filenames[ind]
x_array = np.array(Image.open(path+'{}.png'.format(id_no)))/255
x.append(np.expand_dims(x_array, axis=-1))
y_array = np.array(Image.open(path[:-1]+'_mask/{}_mask.png'.format(id_no)))/255
y.append(np.expand_dims(y_array, axis=-1))
ind += 1
x = np.array(x)
y = np.array(y)
yield x, y
# In[63]:
if __name__ == '__main__':
gen = image_data_generator(shuffle=False, train=False)
x1, y1 = next(gen)
fig, ax = plt.subplots()
ax.imshow(x1[7,:,:,0], cmap='Greys');
ax.imshow(pad_mask(y1[7,:,:,0]), alpha=.2);