-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_frame.py
75 lines (61 loc) · 2.03 KB
/
data_frame.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
import cv2
import numpy as np
import tensorflow as tf
IMAGE_SIZE = 256
seq_size = 0
def read_image(images_path):
images = []
for path in images_path:
path = path.decode()
x = cv2.imread(path, cv2.IMREAD_COLOR)
x = cv2.resize(x, (IMAGE_SIZE, IMAGE_SIZE))
x = x / 255.0
x = x.astype(np.float32)
images.append(x)
return np.array(images)
def read_mask(masks_path):
masks = []
for path in masks_path:
path = path.decode()
x = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
x = cv2.resize(x, (IMAGE_SIZE, IMAGE_SIZE))
x = x / 255.0
x = x.astype(np.float32)
x = np.expand_dims(x, axis=-1)
masks.append(x)
return np.array(masks)
def preprocess(image_path, mask_path):
def f(img_path, msk_path):
x = read_image(img_path)
y = read_mask(msk_path)
return x, y
image, mask = tf.numpy_function(f, [image_path, mask_path], [tf.float32, tf.float32])
image.set_shape([seq_size, IMAGE_SIZE, IMAGE_SIZE, 3])
mask.set_shape([seq_size, IMAGE_SIZE, IMAGE_SIZE, 1])
return image, mask
def tf_dataset(images, masks, batch=2):
dataset = tf.data.Dataset.from_tensor_slices((images, masks))
dataset = dataset.map(preprocess)
dataset = dataset.batch(batch, drop_remainder=True)
dataset = dataset.prefetch(1)
return dataset
def image_seq(images):
img_list = []
sub_list = []
count = 0
for image in images:
count = count + 1
if count != seq_size:
sub_list.append(image)
else:
sub_list.append(image)
img_list.append(sub_list)
sub_list = []
count = 0
return img_list
def get_data(train_df, test_df, frame_size):
global seq_size
seq_size = frame_size
train_dataset = tf_dataset(image_seq(train_df['image'].tolist()), image_seq(train_df['mask'].tolist()))
test_dataset = tf_dataset(image_seq(test_df['image'].tolist()), image_seq(test_df['mask'].tolist()))
return train_dataset, test_dataset