-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdatasets.py
More file actions
343 lines (292 loc) · 14.3 KB
/
datasets.py
File metadata and controls
343 lines (292 loc) · 14.3 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# -*- coding: utf-8 -*-
"""
This file contains the PyTorch dataset for hyperspectral images and
related helpers.
# 此文件包含用于高光谱图像和相关助手的PyTorch数据集。
"""
import spectral
import numpy as np
import torch
import torch.utils
import torch.utils.data
import os
from tqdm import tqdm
try:
# Python 3
from urllib.request import urlretrieve
except ImportError:
# Python 2
from urllib import urlretrieve
from utils import open_file
# DATASETS_CONFIG是字典类型,键值对的键是数据集的名字,值是数据集的urls、img和gt
DATASETS_CONFIG = {
'PaviaC': {
'urls': ['http://www.ehu.eus/ccwintco/uploads/e/e3/Pavia.mat', # urls是链接
'http://www.ehu.eus/ccwintco/uploads/5/53/Pavia_gt.mat'],
'img': 'Pavia.mat',
'gt': 'Pavia_gt.mat'
},
'PaviaU': {
'urls': ['http://www.ehu.eus/ccwintco/uploads/e/ee/PaviaU.mat',
'http://www.ehu.eus/ccwintco/uploads/5/50/PaviaU_gt.mat'],
'img': 'PaviaU.mat',
'gt': 'PaviaU_gt.mat'
},
'KSC': {
'urls': ['http://www.ehu.es/ccwintco/uploads/2/26/KSC.mat',
'http://www.ehu.es/ccwintco/uploads/a/a6/KSC_gt.mat'],
'img': 'KSC.mat',
'gt': 'KSC_gt.mat'
},
'IndianPines': {
'urls': ['http://www.ehu.eus/ccwintco/uploads/6/67/Indian_pines_corrected.mat',
'http://www.ehu.eus/ccwintco/uploads/c/c4/Indian_pines_gt.mat'],
'img': 'Indian_pines_corrected.mat',
'gt': 'Indian_pines_gt.mat'
},
'Botswana': {
'urls': ['http://www.ehu.es/ccwintco/uploads/7/72/Botswana.mat',
'http://www.ehu.es/ccwintco/uploads/5/58/Botswana_gt.mat'],
'img': 'Botswana.mat',
'gt': 'Botswana_gt.mat',
}
}
# 尝试将 自定义数据集配置 加入到 数据集配置
try:
from custom_datasets import CUSTOM_DATASETS_CONFIG
DATASETS_CONFIG.update(CUSTOM_DATASETS_CONFIG)
except ImportError:
pass
# 一个class 进度条功能
class TqdmUpTo(tqdm):
"""Provides `update_to(n)` which uses `tqdm.update(delta_n)`."""
def update_to(self, b=1, bsize=1, tsize=None): # 更新参数
"""
b : int, optional
Number of blocks transferred so far [default: 1]. # 到目前为止传输的块数
bsize : int, optional
Sizeof each block (in tqdm units) [default: 1]. # 每个块的大小(以tqdm为单位)
tsize : int, optional
Total size (in tqdm units). If [default: None] remains unchanged. # 总大小(以tqdm为单位)。
"""
if tsize is not None:
self.total = tsize
self.update(b * bsize - self.n) # will also set self.n = b * bsize
# 下载并读取数据集
def get_dataset(dataset_name, target_folder="./", datasets=DATASETS_CONFIG):
# def get_dataset(dataset_name, target_folder="C:\\Users\\73416\\PycharmProjects\\HSIproject\\Datasets\\", datasets=DATASETS_CONFIG):
""" Gets the dataset specified by name and return the related components.
Args:
dataset_name: string with the name of the dataset
target_folder (optional): folder to store the datasets, defaults to ./
datasets (optional): dataset configuration dictionary, defaults to prebuilt one
Returns:
img: 3D hyperspectral image (WxHxB)
gt: 2D int array of labels # 标签array
label_values: list of class names # 类的名单
ignored_labels: list of int classes to ignore
rgb_bands: int tuple that correspond to red, green and blue bands # int元组,对应红色、绿色和蓝色波段
"""
# target_folder = "C:\\Datasets\\" # 自加,修改数据集的路径
# print(target_folder) # 自加
palette = None
# 当输入的数据集的名字没有在数据集字典datasets=DATASETS_CONFIG中,则报错dataset is unknown
if dataset_name not in datasets.keys():
raise ValueError("{} dataset is unknown.".format(dataset_name))
# 字典操作,取得数据集字典datasets中,键(key)为dataset_name的值(urls、img和gt)
dataset = datasets[dataset_name]
folder = target_folder + datasets[dataset_name].get('folder', dataset_name + '/')
# folder为:C:\Datasets\PaviaU/
# Download the dataset if is not present
if dataset.get('download', True):
# 如果没有folder(C:\Datasets\PaviaU/)文件夹,则创建该文件夹
if not os.path.isdir(folder):
os.mkdir(folder)
# 下载数据集(暂pass)
for url in datasets[dataset_name]['urls']:
# download the files
filename = url.split('/')[-1]
if not os.path.exists(folder + filename):
with TqdmUpTo(unit='B', unit_scale=True, miniters=1,
desc="Downloading {}".format(filename)) as t:
urlretrieve(url, filename=folder + filename,
reporthook=t.update_to)
elif not os.path.isdir(folder):
print("WARNING: {} is not downloadable.".format(dataset_name))
# 读取数据集
if dataset_name == 'PaviaC':
# Load the image
# 通过自己写的open_file()函数打开C:\Datasets\PaviaU/Pavia.mat文件,返回值为字典类型,通过['pavia']来提取键值对的中的值
img = open_file(folder + 'Pavia.mat')['pavia']
# 取RGB波段,为什么这么取不知道
rgb_bands = (55, 41, 12)
# 通过自己写的open_file()函数打开C:\Datasets\PaviaU/Pavia_gt.mat文件,返回值为字典类型,通过['pavia_gt']来提取键值对的中的值
gt = open_file(folder + 'Pavia_gt.mat')['pavia_gt']
# ???label_values有什么用,如何和gt链接
label_values = ["Undefined", "Water", "Trees", "Asphalt",
"Self-Blocking Bricks", "Bitumen", "Tiles", "Shadows",
"Meadows", "Bare Soil"]
ignored_labels = [0]
elif dataset_name == 'PaviaU':
# Load the image
img = open_file(folder + 'PaviaU.mat')['paviaU']
rgb_bands = (55, 41, 12)
gt = open_file(folder + 'PaviaU_gt.mat')['paviaU_gt']
label_values = ['Undefined', 'Asphalt', 'Meadows', 'Gravel', 'Trees',
'Painted metal sheets', 'Bare Soil', 'Bitumen',
'Self-Blocking Bricks', 'Shadows']
ignored_labels = [0]
elif dataset_name == 'IndianPines':
# Load the image
img = open_file(folder + 'Indian_pines_corrected.mat')
img = img['indian_pines_corrected']
rgb_bands = (43, 21, 11) # AVIRIS sensor
gt = open_file(folder + 'Indian_pines_gt.mat')['indian_pines_gt']
label_values = ["Undefined", "Alfalfa", "Corn-notill", "Corn-mintill",
"Corn", "Grass-pasture", "Grass-trees",
"Grass-pasture-mowed", "Hay-windrowed", "Oats",
"Soybean-notill", "Soybean-mintill", "Soybean-clean",
"Wheat", "Woods", "Buildings-Grass-Trees-Drives",
"Stone-Steel-Towers"]
ignored_labels = [0]
elif dataset_name == 'Botswana':
# Load the image
img = open_file(folder + 'Botswana.mat')['Botswana']
rgb_bands = (75, 33, 15)
gt = open_file(folder + 'Botswana_gt.mat')['Botswana_gt']
label_values = ["Undefined", "Water", "Hippo grass",
"Floodplain grasses 1", "Floodplain grasses 2",
"Reeds", "Riparian", "Firescar", "Island interior",
"Acacia woodlands", "Acacia shrublands",
"Acacia grasslands", "Short mopane", "Mixed mopane",
"Exposed soils"]
ignored_labels = [0]
elif dataset_name == 'KSC':
# Load the image
img = open_file(folder + 'KSC.mat')['KSC']
rgb_bands = (43, 21, 11) # AVIRIS sensor
gt = open_file(folder + 'KSC_gt.mat')['KSC_gt']
label_values = ["Undefined", "Scrub", "Willow swamp",
"Cabbage palm hammock", "Cabbage palm/oak hammock",
"Slash pine", "Oak/broadleaf hammock",
"Hardwood swamp", "Graminoid marsh", "Spartina marsh",
"Cattail marsh", "Salt marsh", "Mud flats", "Wate"]
ignored_labels = [0]
else:
# 详细见自定义数据集模块
# Custom dataset
img, gt, rgb_bands, ignored_labels, label_values, palette = CUSTOM_DATASETS_CONFIG[dataset_name]['loader'](folder)
# 处理NaN的情况
# Filter NaN out
nan_mask = np.isnan(img.sum(axis=-1))
if np.count_nonzero(nan_mask) > 0:
print("Warning: NaN have been found in the data. It is preferable to remove them beforehand. Learning on NaN data is disabled.")
img[nan_mask] = 0
gt[nan_mask] = 0
ignored_labels.append(0)
ignored_labels = list(set(ignored_labels))
# Normalization 归一化
img = np.asarray(img, dtype='float32')
img = (img - np.min(img)) / (np.max(img) - np.min(img))
return img, gt, label_values, ignored_labels, rgb_bands, palette
# 高光谱场景的通用类(暂pass)
class HyperX(torch.utils.data.Dataset):
""" Generic class for a hyperspectral scene """
def __init__(self, data, gt, **hyperparams):
"""
Args:
data: 3D hyperspectral image 图形
gt: 2D array of labels 标签
patch_size: int, size of the spatial neighbourhood (int,空间邻域的大小)
center_pixel: bool, set to True to consider only the label of the
center pixel (bool类型,设置为True仅考虑中心像素的标签)
data_augmentation: bool, set to True to perform random flips (数据增强)
supervision: 'full' or 'semi' supervised algorithms (监督方式:监督或半监督)
"""
super(HyperX, self).__init__()
# 读取img
self.data = data
# 读取gt
self.label = gt
# 读取超参数
self.name = hyperparams['dataset']
self.patch_size = hyperparams['patch_size']
self.ignored_labels = set(hyperparams['ignored_labels'])
self.flip_augmentation = hyperparams['flip_augmentation']
self.radiation_augmentation = hyperparams['radiation_augmentation']
self.mixture_augmentation = hyperparams['mixture_augmentation']
self.center_pixel = hyperparams['center_pixel']
supervision = hyperparams['supervision']
# 监督方式
# Fully supervised : use all pixels with label not ignored. 完全监督:使用标签未被忽略的所有像素
if supervision == 'full':
mask = np.ones_like(gt)
for l in self.ignored_labels:
mask[gt == l] = 0
# Semi-supervised : use all pixels, except padding. 半监督:使用除填充之外的所有像素
elif supervision == 'semi':
mask = np.ones_like(gt)
x_pos, y_pos = np.nonzero(mask)
p = self.patch_size // 2
self.indices = np.array([(x,y) for x,y in zip(x_pos, y_pos) if x > p and x < data.shape[0] - p and y > p and y < data.shape[1] - p])
self.labels = [self.label[x,y] for x,y in self.indices]
np.random.shuffle(self.indices)
@staticmethod
def flip(*arrays):
horizontal = np.random.random() > 0.5
vertical = np.random.random() > 0.5
if horizontal:
arrays = [np.fliplr(arr) for arr in arrays]
if vertical:
arrays = [np.flipud(arr) for arr in arrays]
return arrays
@staticmethod
def radiation_noise(data, alpha_range=(0.9, 1.1), beta=1/25):
alpha = np.random.uniform(*alpha_range)
noise = np.random.normal(loc=0., scale=1.0, size=data.shape)
return alpha * data + beta * noise
def mixture_noise(self, data, label, beta=1/25):
alpha1, alpha2 = np.random.uniform(0.01, 1., size=2)
noise = np.random.normal(loc=0., scale=1.0, size=data.shape)
data2 = np.zeros_like(data)
for idx, value in np.ndenumerate(label):
if value not in self.ignored_labels:
l_indices = np.nonzero(self.labels == value)[0]
l_indice = np.random.choice(l_indices)
assert(self.labels[l_indice] == value)
x, y = self.indices[l_indice]
data2[idx] = self.data[x,y]
return (alpha1 * data + alpha2 * data2) / (alpha1 + alpha2) + beta * noise
def __len__(self):
return len(self.indices)
def __getitem__(self, i):
x, y = self.indices[i]
x1, y1 = x - self.patch_size // 2, y - self.patch_size // 2
x2, y2 = x1 + self.patch_size, y1 + self.patch_size
data = self.data[x1:x2, y1:y2]
label = self.label[x1:x2, y1:y2]
if self.flip_augmentation and self.patch_size > 1:
# Perform data augmentation (only on 2D patches)
data, label = self.flip(data, label)
if self.radiation_augmentation and np.random.random() < 0.1:
data = self.radiation_noise(data)
if self.mixture_augmentation and np.random.random() < 0.2:
data = self.mixture_noise(data, label)
# Copy the data into numpy arrays (PyTorch doesn't like numpy views)
data = np.asarray(np.copy(data).transpose((2, 0, 1)), dtype='float32')
label = np.asarray(np.copy(label), dtype='int64')
# Load the data into PyTorch tensors
data = torch.from_numpy(data)
label = torch.from_numpy(label)
# Extract the center label if needed
if self.center_pixel and self.patch_size > 1:
label = label[self.patch_size // 2, self.patch_size // 2]
# Remove unused dimensions when we work with invidual spectrums
elif self.patch_size == 1:
data = data[:, 0, 0]
label = label[0, 0]
# Add a fourth dimension for 3D CNN
if self.patch_size > 1:
# Make 4D data ((Batch x) Planes x Channels x Width x Height)
data = data.unsqueeze(0)
return data, label