forked from ke-22/deeplabv3-Tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_utils.py
70 lines (55 loc) · 1.81 KB
/
data_utils.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
import cv2, os
import numpy as np
from sklearn.model_selection import train_test_split
import tensorflow as tf
class DataSet():
'''
通过读入文件生成数据集
'''
def __init__(self, image_path, label_path):
self.image_path = np.array(image_path)
self.label_path = np.array(label_path)
self.batch_count = 0
self.epoch_count = 0
def num_examples(self):
'''
得到样本的数量
:return:
'''
return self.image_path.shape[0]
def next_batch(self, batch_size):
'''
next_batch函数
:param batch_size:
:return:
'''
start = self.batch_count * batch_size
end = start + batch_size
self.batch_count += 1
if end > self.image_path.shape[0]:
self.batch_count = 0
random_index = np.random.permutation(self.image_path.shape[0])
self.image_path = self.image_path[random_index]
self.label_path = self.label_path[random_index]
self.epoch_count += 1
start = self.batch_count * batch_size
end = start + batch_size
self.batch_count += 1
image_batch, label_batch = self.read_path(self.image_path[start:end],
self.label_path[start:end])
return image_batch, label_batch
def read_path(self, x_path, y_path):
'''
将路径读为图片
:param x_path:
:param y_path:
:return:
'''
x = []
y = []
for i in range(x_path.shape[0]):
x.append(self.transform(cv2.imread(x_path[i], cv2.CAP_MODE_RGB)))
y.append(cv2.imread(y_path[i], cv2.CAP_MODE_GRAY))
return np.array(x), np.array(y)
def transform(self, img):
return img