-
Notifications
You must be signed in to change notification settings - Fork 6
/
dataset.py
133 lines (106 loc) · 4.33 KB
/
dataset.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import numpy as np
import os
import glob
from sklearn.utils import shuffle
from skimage import io
from matplotlib import pyplot as plt
import cv2
def load_train(train_path, img_size, classes):
images = []
labels = []
img_names = []
cls = []
print("读取训练图片...")
for fields in classes:
index = classes.index(fields)
print("Now going to read {} files (Index:{})".format(fields, index))
path = os.path.join(train_path, fields, '*/*g')
files = glob.glob(path)
for fl in files:
# image = cv2.imread(fl,flags=cv2.IMREAD_GRAYSCALE)
# image = cv2.resize(image, (img_size, img_size), 0, 0, cv2.INTER_LINEAR)
# image=np.reshape(image,[32,32,1])
# print(fl)
image = cv2.imread(fl)
image = cv2.resize(image, (img_size, img_size), 0, 0, cv2.INTER_LINEAR)
# io.imshow(image[:, :, 2])
# c1,c2,c3=image.split()
#0917
image = image[:,:,1:3]
# image = image[:, :, 1]
# image = np.reshape(image, [48, 48, 1])
image = image.astype(np.float32)
image = np.multiply(image, 1.0 / 255.0)
# print(np.shape(image))
images.append(image)
label = np.zeros(len(classes))
label[index] = 1.0
labels.append(label)
flbase = os.path.basename(fl)
img_names.append(flbase)
cls.append(fields)
images = np.array(images)
labels = np.array(labels)
# np.save("/media/data2/mhy/data/1022Ne/Images.npy", images)
# np.save("/media/data2/mhy/data/1022Ne/Labels.npy", labels)
img_names = np.array(img_names)
cls = np.array(cls)
# np.save("/media/data2/mhy/data/1022Ne/ImageNames.npy", img_names)
# np.save("/media/data2/mhy/data/1022Ne/CLS.npy", cls)
return images, labels, img_names, cls
class DataSet(object):
def __init__(self, images, labels, img_names, cls):
self._num_examples = images.shape[0]
self._images = images
self._labels = labels
self._img_names = img_names
self._cls = cls
self._epochs_done = 0
self._index_in_epoch = 0
def images(self):
return self._images
def labels(self):
return self._labels
def img_names(self):
return self._img_names
def cls(self):
return self._cls
def num_examples(self):
return self._num_examples
def epochs_done(self):
return self._epochs_done
def next_batch(self, batch_size):
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
self._epochs_done += 1
start = 0
self._index_in_epoch = batch_size
assert batch_size <= self._num_examples
end = self._index_in_epoch
return self._images[start:end], self._labels[start:end], self._img_names[start:end], self._cls[start:end]
def read_train_sets(train_path, image_size, classes, validation_size):
class DataSets(object):
pass
data_sets = DataSets()
images, labels, img_names, cls = load_train(train_path, image_size, classes)
# images = np.load("/media/data2/mhy/data/1012N1/Images.npy")
# img_names= np.load("/media/data2/mhy/data/1012N1/ImageNames.npy")
# labels = np.load("/media/data2/mhy/data/1012N1/Labels.npy")
# cls = np.load("/media/data2/mhy/data/1012N1/CLS.npy")
print("Load Data Finished")
images, labels, img_names, cls = shuffle(images, labels, img_names, cls)
if isinstance(validation_size, float):
validation_size = int(validation_size * images.shape[0]) # 1000,64,64,3;验证集200个
print(images.shape)
validation_images = images[:validation_size]
validation_labels = labels[:validation_size]
validation_img_names = img_names[:validation_size]
validation_cls = cls[:validation_size]
train_images = images[validation_size:]
train_labels = labels[validation_size:]
train_img_names = img_names[validation_size:]
train_cls = cls[validation_size:]
data_sets.train = DataSet(train_images, train_labels, train_img_names, train_cls)
data_sets.valid = DataSet(validation_images, validation_labels, validation_img_names, validation_cls)
return data_sets