-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
278 lines (232 loc) · 10.3 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os
import json
import glob
import torch
import math
import random
from tqdm import tqdm
from PIL import Image, ImageEnhance
from torch.utils.data import Dataset
from torchvision.transforms.functional import pil_to_tensor
from kmeans_pytorch import kmeans
from matplotlib import pyplot as plt
from sklearn.cluster import KMeans
from pathlib import Path
import config as cfg
from logger import get_logger
logger = get_logger("Dataset logger")
import pdb
class SatelliteDataset(Dataset):
def __init__(self, data_paths, metadata=None, transform=None, device='cuda', shuffle=True, brightness=1.0) -> None:
super().__init__()
self.data_paths = data_paths
self.device = device
# Image enhancement
self.brightness = brightness
self.transform = transform
if metadata:
self.metadata = metadata
else:
self.metadata = self.extract_metadata(data_paths, shuffle=shuffle)
logger.info(f"Loaded a data set with {self.__len__()} images.")
def extract_metadata(self, data_paths, shuffle=True):
positive_data_paths = [os.path.join(data_path, "positive") for data_path in data_paths]
negative_data_paths = [os.path.join(data_path, "negative") for data_path in data_paths]
formats_list = ['.jpg', '.png']
# Initialize the metadata list
metadata = []
for img_format in formats_list:
for positive_data_path in positive_data_paths:
# Record positive labels [0 stands for the positive labels]
for img_path in glob.glob(positive_data_path + "/*" + img_format):
img_label = 0
metadata_ = {
"image_path": img_path,
"category_id": img_label,
"brightness": self.brightness
}
metadata.append(metadata_)
for negative_data_path in negative_data_paths:
# Record negative labels [1 stands for the negative labels]
for img_path in glob.glob(negative_data_path + "/*" + img_format):
img_label = 1
metadata_ = {
"image_path": img_path,
"category_id": img_label,
"brightness": self.brightness
}
metadata.append(metadata_)
if shuffle:
random.shuffle(metadata)
return metadata
def __getitem__(self, idx):
data = self.metadata[idx]
# Extract the label
label = data['category_id']
# Extract the image
image = Image.open(data['image_path']).convert('RGB') # force RGB instead of RGBA
# Extract image brightness
brightness = data['brightness']
# Modify image brightness
if brightness != 1.0:
enhancer = ImageEnhance.Brightness(image)
image = enhancer.enhance(brightness)
if self.transform:
image = self.transform(image).to(self.device)
else:
image = pil_to_tensor(image).to(self.device)
# Remove the 4th channel
image = image[:3, ...]
return (image, label)
def __len__(self):
return len(self.metadata)
def get_metadata(self):
return self.metadata
def set_brightness(self, brightness):
self.brightness = brightness
for idx in range(len(self.metadata)):
self.metadata[idx]['brightness'] = brightness
def leave_fraction_of_negatives(self, fraction):
"""
This function randomly selects a subset of the dataset which will be retained, given the fraction of the dataset.
"""
assert fraction >= 0.0 and fraction <= 1.0
total_pos, total_neg = self.get_posneg_count()
keep_count = int(total_neg * fraction)
positives, negatives = self.get_posneg()
negatives = random.sample(negatives, keep_count)
self.build_metadata_from_posneg(positives, negatives)
def leave_number_of_negatives(self, number):
"""
This function randomly selects a subset of the dataset which will be retained, given the number of
images to be retained.
"""
total_pos, total_neg = self.get_posneg_count()
assert number >= 0 and number <= total_neg
keep_count = number
positives, negatives = self.get_posneg()
negatives = random.sample(negatives, keep_count)
self.build_metadata_from_posneg(positives, negatives)
def remove_positives(self):
_, negatives = self.get_posneg()
self.build_metadata_from_posneg([], negatives)
def remove_negatives(self):
positives, _ = self.get_posneg()
self.build_metadata_from_posneg(positives, [])
def build_metadata_from_posneg(self, positives, negatives):
metadata = positives + negatives
random.shuffle(metadata)
self.metadata = metadata
def get_posneg(self):
positives = []
negatives = []
for data in self.metadata:
if data['category_id'] == 0:
positives.append(data)
elif data['category_id'] == 1:
negatives.append(data)
else:
raise NotImplementedError
return (positives.copy(), negatives.copy())
def get_posneg_count(self):
total_pos = 0
total_neg = 0
for data in self.metadata:
if data['category_id'] == 0:
total_pos += 1
elif data['category_id'] == 1:
total_neg += 1
else:
raise NotImplementedError
return (total_pos, total_neg)
def shuffle(self):
random.shuffle(self.metadata)
def details(self):
total_pos, total_neg = self.get_posneg_count()
text = f"Positive: {total_pos}. Negative: {total_neg}."
return text
def augment_brightness(self, brightness_levels):
brightness_levels = [x for x in brightness_levels if x != 0.0] # exclude 0.0 to avoid confusion during training
logger.info("Augmenting the data set.")
new_metadata = []
for data in tqdm(self.metadata):
for brightness in brightness_levels:
data_ = data.copy()
data_['brightness'] = brightness
new_metadata.append(data_)
self.metadata = new_metadata
def KMeansAnalysis(self, K_max=5):
def AverageKMeansError(cluster_centers, cluster_idxs, pixels, num_classes, device='cuda'):
mean_error = torch.tensor(0, device=device, dtype=torch.float32)
n_pixels = len(pixels)
for k_idx in range(num_classes):
active_pixels = (cluster_idxs == k_idx).float().to(device)
mean_error += torch.sum(active_pixels * pixels.T)
mean_error /= n_pixels
return mean_error
def SaveCenterColorsPlot(cluster_centers, save_dir):
# Get the number of clusters
n_centers = len(cluster_centers)
# Get the number of rows and columns required
n_rows = round(math.sqrt(n_centers))
n_cols = math.ceil(n_centers / n_rows)
# Plot the colors
fig, axs = plt.subplots(nrows=n_rows, ncols=n_cols, squeeze=False)
for i in range(n_rows):
for j in range(n_cols):
idx = i * n_cols + j
if idx >= n_centers:
break
axs[i][j].imshow([[cluster_centers[idx]]])
fig.tight_layout()
fig.savefig(save_dir)
def SaveCenterColorsTensor(cluster_centers, save_dir):
torch.save(cluster_centers, save_dir)
def SaveErrorsPlot(k_errors, K_max, save_dir):
plt.close()
fig = plt.figure()
plt.plot(range(2, K_max + 1), k_errors, 'b')
plt.xlabel("# of clusters")
plt.ylabel("Error")
plt.title("Error vs # of clusters")
plt.savefig(save_dir)
assert K_max >= 2, "Number of clusters must be greater than 2."
logger.info("Running K-means analysis.")
# Extract pixel values
logger.info("Extracting pixel values...")
pixels = torch.empty(size=(0, 3), device=self.device)
for idx in tqdm(range(len(self.metadata))):
image, _ = self.__getitem__(idx)
pixels_ = image.permute(1, 2, 0).view(-1, 3)
pixels = torch.cat((pixels, pixels_))
# Perform K-means clustering
k_errors = []
cluster_idxs_list = []
cluster_centers_list = []
for k in range(2, K_max + 1):
logger.info(f"K-means with {k} clusters.")
km = KMeans(init="random", n_clusters=k, n_init=10, max_iter=300, tol=1e-04, random_state=0, verbose=0)
cluster_idxs = km.fit_predict(pixels.cpu())
cluster_centers = km.cluster_centers_
mean_error = km.inertia_
# Record the data
k_errors.append(mean_error)
cluster_idxs_list.append(cluster_idxs)
cluster_centers_list.append(cluster_centers)
# Extract the error gradients
error_gradients = []
for i in range(0, len(k_errors) - 1):
change = k_errors[i + 1] - k_errors[i]
error_gradients.append(-change)
# Save cluster colors
Path(cfg.RESULTS_DIR).mkdir(parents=True, exist_ok=True)
for cluster_centers in cluster_centers_list:
save_dir = os.path.join(cfg.RESULTS_DIR, f"cluster_centers_{len(cluster_centers)}.png")
SaveCenterColorsPlot(cluster_centers, save_dir)
save_dir = os.path.join(cfg.RESULTS_DIR, f"cluster_centers_{len(cluster_centers)}.pth")
SaveCenterColorsTensor(cluster_centers, save_dir)
# Save errors plot
save_dir = os.path.join(cfg.RESULTS_DIR, "k_errors.png")
SaveErrorsPlot(k_errors, K_max, save_dir)
save_dir = os.path.join(cfg.RESULTS_DIR, "error_gradients.png")
SaveErrorsPlot(error_gradients, K_max - 1, save_dir)