1
+ import numpy as np
2
+ import scipy .io as scio
3
+ from sklearn .preprocessing import MinMaxScaler
4
+ from sklearn .decomposition import FastICA
5
+ from sklearn .decomposition import PCA
6
+ import cv2
7
+
8
+ class load ():
9
+ # load dataset(indian_pines & pavia_univ.)
10
+ def load_data (self ,flag = 'indian' ):
11
+ if flag == 'indian' :
12
+ Ind_pines_dict = scio .loadmat ('/data/di.wang/ordinary/23DCNN/Indian_pines.mat' )
13
+ Ind_pines_gt_dict = scio .loadmat ('/data/di.wang/ordinary/23DCNN/Indian_pines_gt.mat' )
14
+
15
+ print (Ind_pines_dict ['indian_pines' ].shape )
16
+ print (Ind_pines_gt_dict ['indian_pines_gt' ].shape )
17
+
18
+ # remove the water absorption bands
19
+
20
+ no_absorption = list (set (np .arange (0 , 103 )) | set (np .arange (108 , 149 )) | set (np .arange (163 , 219 )))
21
+
22
+ original = Ind_pines_dict ['indian_pines' ][:, :, no_absorption ].reshape (145 * 145 , 200 )
23
+
24
+ print (original .shape )
25
+ print ('Remove wate absorption bands successfully!' )
26
+
27
+ gt = Ind_pines_gt_dict ['indian_pines_gt' ].reshape (145 * 145 , 1 )
28
+
29
+ r = Ind_pines_dict ['indian_pines' ].shape [0 ]
30
+ c = Ind_pines_dict ['indian_pines' ].shape [1 ]
31
+ categories = 17
32
+ if flag == 'pavia' :
33
+ pav_univ_dict = scio .loadmat ('/data/di.wang/ordinary/23DCNN/PaviaU.mat' )
34
+ pav_univ_gt_dict = scio .loadmat ('/data/di.wang/ordinary/23DCNN/PaviaU_gt.mat' )
35
+
36
+ print (pav_univ_dict ['paviaU' ].shape )
37
+ print (pav_univ_gt_dict ['paviaU_gt' ].shape )
38
+
39
+ original = pav_univ_dict ['paviaU' ].reshape (610 * 340 , 103 )
40
+ gt = pav_univ_gt_dict ['paviaU_gt' ].reshape (610 * 340 , 1 )
41
+
42
+ r = pav_univ_dict ['paviaU' ].shape [0 ]
43
+ c = pav_univ_dict ['paviaU' ].shape [1 ]
44
+ categories = 10
45
+ if flag == 'ksc' :
46
+ ksc_dict = scio .loadmat ('/data/di.wang/ordinary/23DCNN/KSC.mat' )
47
+ ksc_gt_dict = scio .loadmat ('/data/di.wang/ordinary/23DCNN/KSC_gt.mat' )
48
+
49
+ print (ksc_dict ['KSC' ].shape )
50
+ print (ksc_gt_dict ['KSC_gt' ].shape )
51
+
52
+ original = ksc_dict ['KSC' ].reshape (512 * 614 , 176 )
53
+ original [original > 400 ]= 0
54
+ gt = ksc_gt_dict ['KSC_gt' ].reshape (512 * 614 , 1 )
55
+
56
+ r = ksc_dict ['KSC' ].shape [0 ]
57
+ c = ksc_dict ['KSC' ].shape [1 ]
58
+ categories = 14
59
+
60
+ rows = np .arange (gt .shape [0 ]) # start from 0
61
+ # ID(row number), data, class number
62
+ All_data = np .c_ [rows , original , gt ]
63
+
64
+ # Removing background and obtain all labeled data
65
+ labeled_data = All_data [All_data [:, - 1 ] != 0 , :]
66
+ rows_num = labeled_data [:, 0 ] # All ID of labeled data
67
+
68
+ return All_data , labeled_data , rows_num , categories , r , c , flag
69
+
70
+
71
+ class product ():
72
+ def __init__ (self ,c ,flag ):
73
+ self .c = c
74
+ self .flag = flag
75
+ # product the training and testing pixel ID
76
+ def generation_num (self ,labeled_data , rows_num , All_data ):
77
+
78
+ train_num = []
79
+
80
+ for i in np .unique (labeled_data [:, - 1 ]):
81
+ temp = labeled_data [labeled_data [:, - 1 ] == i , :]
82
+ temp_num = temp [:, 0 ] # all ID of a special class
83
+ #print(i, temp_num.shape[0])
84
+ np .random .shuffle (temp_num ) # random sequence
85
+ if self .flag == 'indian' :
86
+ if i == 1 :
87
+ train_num .append (temp_num [0 :33 ])
88
+ elif i == 7 :
89
+ train_num .append (temp_num [0 :20 ])
90
+ elif i == 9 :
91
+ train_num .append (temp_num [0 :14 ])
92
+ elif i == 16 :
93
+ train_num .append (temp_num [0 :75 ])
94
+ else :
95
+ train_num .append (temp_num [0 :100 ])
96
+ if self .flag == 'pavia' :
97
+ train_num .append (temp_num [0 :100 ])
98
+ if self .flag == 'ksc' :
99
+ if i == 1 :
100
+ train_num .append (temp_num [0 :33 ])
101
+ elif i == 2 :
102
+ train_num .append (temp_num [0 :23 ])
103
+ elif i == 3 :
104
+ train_num .append (temp_num [0 :24 ])
105
+ elif i == 4 :
106
+ train_num .append (temp_num [0 :24 ])
107
+ elif i == 5 :
108
+ train_num .append (temp_num [0 :15 ])
109
+ elif i == 6 :
110
+ train_num .append (temp_num [0 :22 ])
111
+ elif i == 7 :
112
+ train_num .append (temp_num [0 :9 ])
113
+ elif i == 8 :
114
+ train_num .append (temp_num [0 :38 ])
115
+ elif i == 9 :
116
+ train_num .append (temp_num [0 :51 ])
117
+ elif i == 10 :
118
+ train_num .append (temp_num [0 :39 ])
119
+ elif i == 11 :
120
+ train_num .append (temp_num [0 :41 ])
121
+ elif i == 12 :
122
+ train_num .append (temp_num [0 :49 ])
123
+ elif i == 13 :
124
+ train_num .append (temp_num [0 :91 ])
125
+ # else:
126
+ # train_num.append(temp_num[0:int(temp.shape[0]*0.1)])
127
+
128
+ trn_num = [x for j in train_num for x in j ] # merge
129
+ tes_num = list (set (rows_num ) - set (trn_num ))
130
+ pre_num = list (set (range (0 , All_data .shape [0 ])) - set (trn_num ))
131
+ print ('number of training sample' , len (trn_num ))
132
+ return rows_num , trn_num , tes_num , pre_num
133
+
134
+
135
+ def production_data_trn (self , rows_num , trn_num , half_s , image_3d_mat ):
136
+
137
+ trn_num = np .array (trn_num )
138
+ ##Training set(spatial)
139
+ idx_2d_trn = np .zeros ([trn_num .shape [0 ], 2 ]).astype (int )
140
+ idx_2d_trn [:, 0 ] = np .floor (trn_num / self .c )
141
+ idx_2d_trn [:, 1 ] = trn_num + 1 - self .c * idx_2d_trn [:, 0 ] - 1
142
+ # neibour area(2*half_s+1)
143
+ patch_size = 2 * half_s + 1
144
+ trn_spat = np .zeros ([trn_num .shape [0 ], patch_size , patch_size , image_3d_mat .shape [2 ]])
145
+ neibour_num = []
146
+ for i in range (idx_2d_trn .shape [0 ]):
147
+ # image expandision
148
+ row = idx_2d_trn [i , 0 ] + half_s
149
+ col = idx_2d_trn [i , 1 ] + half_s
150
+ trn_spat [i , :, :, :] = image_3d_mat [(row - half_s ):row + half_s + 1 ,
151
+ (col - half_s ):col + half_s + 1 , :]
152
+ # mapping expandision neibor pixel ID to origianal image
153
+ neibour_num = neibour_num + [(row + j - half_s ) * self .c + col + k - half_s for j in range (- half_s , half_s + 1 ) for k
154
+ in range (- half_s , half_s + 1 )]
155
+ val_num = list (set (rows_num ) - set (neibour_num )) # prevent data snooping
156
+
157
+ print ('trn_spat:' , trn_spat .shape )
158
+ print ('Training Spatial dataset preparation Finished!' )
159
+ return trn_spat , trn_num , val_num
160
+
161
+ def production_data_valtespre (self , tes_num , half_s , image_3d_mat , flag = 'Tes' ):
162
+
163
+ ##Testing set(spatial)
164
+ tes_num = np .array (tes_num )
165
+ idx_2d_tes = np .zeros ([tes_num .shape [0 ], 2 ]).astype (int )
166
+ idx_2d_tes [:, 0 ] = np .floor (tes_num / self .c )
167
+ idx_2d_tes [:, 1 ] = tes_num + 1 - self .c * idx_2d_tes [:, 0 ] - 1
168
+ # neibour area(2*half_s+1)
169
+ patch_size = 2 * half_s + 1
170
+ tes_spat = np .zeros ([tes_num .shape [0 ], patch_size , patch_size , image_3d_mat .shape [2 ]])
171
+ for i in range (idx_2d_tes .shape [0 ]):
172
+ # image expandision
173
+ row = idx_2d_tes [i , 0 ] + half_s
174
+ col = idx_2d_tes [i , 1 ] + half_s
175
+ tes_spat [i , :, :, :] = image_3d_mat [(row - half_s ):row + half_s + 1 ,
176
+ (col - half_s ):col + half_s + 1 , :]
177
+
178
+ print ('tes_spat:' , tes_spat .shape )
179
+ print ('{} Spatial dataset preparation Finished!' .format (flag ))
180
+ return tes_spat ,tes_num
181
+
182
+ def normlization (self , data_spat , mi , ma ,flag = 'trn' ):
183
+
184
+ scaler = MinMaxScaler (feature_range = (mi , ma ))
185
+
186
+ spat_data = data_spat .reshape (- 1 , data_spat .shape [- 1 ])
187
+ data_spat_new = scaler .fit_transform (spat_data ).reshape (data_spat .shape )
188
+
189
+ print ('{}_spat:{}' .format (flag ,data_spat_new .shape ))
190
+ print ('{} Spatial dataset normalization Finished!' .format (flag ))
191
+ return data_spat_new
192
+
193
+ def resample (self ,data ,rsz ):
194
+
195
+ if data .shape [1 ]== rsz :
196
+ return data
197
+ else :
198
+ # data:BHWC
199
+ data = data .transpose (0 ,3 ,1 ,2 )#BCHW
200
+ B ,C ,H ,W = data .shape
201
+ re_data = np .zeros ([B ,C ,rsz ,rsz ])
202
+ for i in range (B ):
203
+ for j in range (C ):
204
+ temp = data [i ,j ,:,:]
205
+ re_data [i ,j ,:,:] = cv2 .resize (temp , (rsz , rsz ), interpolation = cv2 .INTER_LINEAR )
206
+ re_data = re_data .transpose (0 ,2 ,3 ,1 )#BHWC
207
+ return re_data
208
+
209
+
210
+
211
+ class preprocess ():
212
+ def __init__ (self ,t ,dr_num ):
213
+ self .transform = t
214
+ self .dr_num = dr_num
215
+ def Dim_reduction (self , All_data ):
216
+
217
+ Alldata_DR = All_data
218
+
219
+ # if self.transform =='ica':
220
+ # ica_data_pre = All_data[:, 1:-1]
221
+ # print(ica_data_pre.shape)
222
+ # transformer = FastICA(n_components=50, whiten=True, random_state=None)
223
+ # fastica_data = transformer.fit_transform(ica_data_pre)
224
+ # print(fastica_data.shape)
225
+ #
226
+ # Alldata_DR = fastica_data
227
+ #
228
+ # print('ICA Finished!')
229
+
230
+ if self .transform == 'pca' :
231
+ pca_data_pre = All_data [:, 1 :- 1 ]
232
+ print (pca_data_pre .shape )
233
+ pca_transformer = PCA (n_components = self .dr_num )
234
+ pca_data = pca_transformer .fit_transform (All_data [:, 1 :- 1 ])
235
+ print (pca_data .shape )
236
+
237
+ Alldata_DR = pca_data
238
+
239
+ print ('PCA Finished!' )
240
+
241
+ return Alldata_DR
0 commit comments