-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdataset.py
170 lines (142 loc) · 4.86 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import glob
import numpy as np
# Local dependencies
import constants
class Dataset:
"""
This class manages the information for the dataset.
"""
def __init__(self, path):
"""
Initialize the Dataset object.
Args:
path: The path on where the dataset of images is stored.
Returns:
void
"""
self.path = path
self.train_set = []
self.test_set = []
self.classes = []
self.classes_counts = []
def generate_sets(self):
"""
Reads the information of the training and testings sets and stores it into attributes of the object.
Returns:
void
"""
dataset_classes = glob.glob(self.path + "/*")
for folder in dataset_classes:
path = folder.replace("\\", "/")
if "/" in folder:
class_name = folder.split("/")[-1]
else:
class_name = folder.split("\\")[-1]
self.classes.append(class_name)
train = glob.glob(path + "/train/*")
test = glob.glob(path + "/test/*")
self.train_set.append(train)
self.test_set.append(test)
self.classes_counts.append(0)
def get_train_set(self):
"""
Get the paths of the objects in the training set.
Returns:
list of strings: Paths for objects in the training set.
"""
if len(self.train_set) == 0:
self.generate_sets()
return self.train_set
def get_test_set(self):
"""
Get the paths of the objects in the testing set.
Returns:
list of strings: Paths for objects in the testing set.
"""
if len(self.test_set) == 0:
self.generate_sets()
return self.test_set
def get_classes(self):
"""
Get the names of the classes that are in the dataset.
Returns:
list of strings: List with the names of the classes.
"""
if len(self.classes) == 0:
self.generate_sets()
return self.classes
def get_classes_counts(self):
"""
Get a list with the count of total local descriptors for each class.
Returns:
list of integers: List with the count of all the local descriptors in each class.
"""
return self.classes_counts
def get_y(self, my_set):
"""
Get the labels for the a given set.
Args:
my_set (matrix of strings): Each row has the paths for the objects in that class.
Returns:
NumPy float array: The labels for a given set.
"""
y = []
if len(my_set) == 0:
self.generate_sets()
for class_ID in range(len(my_set)):
y += [class_ID] * len(my_set[class_ID])
# Transform the list in to a vector
y = np.float32(y)[:, np.newaxis]
return y
def get_train_y(self):
"""
Get the labels for the training set.
Returns:
NumPy float array: The labels for the training set.
"""
return self.get_y(self.train_set)
def get_test_y(self):
"""
Get the labels for the testing set.
Returns:
NumPy float array: The labels for the testing set.
"""
return self.get_y(self.test_set)
def store_listfile(self):
"""
Used for creating files in the format filelist used in Caffe for
converting an image set. (caffe/tools/convert_imageset.cpp)
Returns:
void
"""
train_file = open(constants.TRAIN_TXT_FILE, "w")
test_file = open(constants.TEST_TXT_FILE, "w")
self.get_train_set()
self.get_test_set()
for class_id in range(len(self.classes)):
current_train = self.train_set[class_id]
for filename in current_train:
# Changing path in Windows
path = filename.replace("\\", "/")
idx = path.index("/")
path = path[(idx + 1):]
train_file.write("{0} {1}\n".format(path, class_id))
current_test = self.test_set[class_id]
for filename in current_test:
# Changing path in Windows
path = filename.replace("\\", "/")
idx = path.index("/")
path = path[(idx + 1):]
test_file.write("{0} {1}\n".format(path, class_id))
train_file.close()
test_file.close()
def set_class_count(self, class_number, class_count):
"""
Set the count of local descriptors in one class.
Args:
class_number: ID for the class.
class_count: Number of local descriptors that were found in the class.
Returns:
void
"""
self.classes_counts[class_number] = class_count