-
Notifications
You must be signed in to change notification settings - Fork 9
/
data.py
64 lines (53 loc) · 1.67 KB
/
data.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
import os
import numpy as np
import cv2
from glob import glob
import tensorflow as tf
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import CustomObjectScope
from metrics import iou
H = 256
W = 256
def load_names(path, file_path):
f = open(file_path, "r")
data = f.read().split("\n")[:-1]
images = [os.path.join(path,"images", name) + ".jpg" for name in data]
masks = [os.path.join(path,"masks", name) + ".jpg" for name in data]
return images, masks
def load_data(path):
train_names_path = f"{path}/train.txt"
valid_names_path = f"{path}/val.txt"
train_x, train_y = load_names(path, train_names_path)
valid_x, valid_y = load_names(path, valid_names_path)
return (train_x, train_y), (valid_x, valid_y)
def read_image(path):
path = path.decode()
x = cv2.imread(path, cv2.IMREAD_COLOR)
x = cv2.resize(x, (W, H))
x = x/255.0
x = x.astype(np.float32)
return x
def read_mask(path):
path = path.decode()
x = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
x = cv2.resize(x, (W, H))
x = x/255.0
x = np.expand_dims(x, axis=-1)
x = x.astype(np.float32)
return x
def tf_parse(x, y):
def _parse(x, y):
x = read_image(x)
y = read_mask(y)
return x, y
x, y = tf.numpy_function(_parse, [x, y], [tf.float32, tf.float32])
x.set_shape([H, W, 3])
y.set_shape([H, W, 1])
return x, y
def tf_dataset(x, y, batch=8):
dataset = tf.data.Dataset.from_tensor_slices((x, y))
dataset = dataset.map(tf_parse)
dataset = dataset.batch(batch)
dataset = dataset.repeat()
dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE)
return dataset