-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsrif.py
More file actions
377 lines (280 loc) · 13.2 KB
/
Copy pathsrif.py
File metadata and controls
377 lines (280 loc) · 13.2 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 13 10:36:05 2019
@author: Nikolina Mileva
"""
import time
import scipy
import rasterio
import numpy as np
import skimage as sk
from sklearn.decomposition import PCA
from sklearn.feature_extraction import image
from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import DictionaryLearning, MiniBatchDictionaryLearning
import matplotlib.pyplot as plt
def extract_patch(array, step):
#array_w = sk.util.view_as_windows(array, (9,9), step=2) # same functionality
# step = 1
windowSize = 9
result = image.extract_patches(array, windowSize, step)
d1,d2,d3,d4 = result.shape
result = np.reshape(result, (d1*d2, d3, d4))
result = result.reshape(result.shape[0], -1)
return result
###############################################################
############# PREPROCESSING AND FEATURE EXTRACTION ############
###############################################################
# Load low resolution image
#product = rasterio.open('C:/Users/Nikolina Mileva/Documents/Sparseland/Images/NIR/subset_S2A_S3A_20180901_20180924_NIR.tif')
#profile = product.profile
#y_h = product.read(1)
#product = rasterio.open('C:/Users/Nikolina Mileva/Documents/Sparseland/Images/NIR/S3A_20180924_B17.tif')
#profile = product.profile
#y_h = rasterio.open('C:/Users/Nikolina Mileva/Documents/Sparseland/Images/NIR/S3A_20180924_B17.tif').read(1)[3:-3,3:-3]
y_h = rasterio.open('C:/Users/Nikolina Mileva/Desktop/Single_Image_SR/CVPR08-SR/Data/Training/tt2.bmp').read(1)[0:288, 0:288]
# Blur
blur_filter_hor = np.array(([0.25, 0.5, 0.25]))
blur_filter_ver = np.array(([0.25], [0.5], [0.25]))
y_h_hor_blur = scipy.ndimage.convolve1d(y_h, blur_filter_hor, mode='reflect')
y_h_blur = scipy.ndimage.convolve(y_h_hor_blur, blur_filter_ver, mode='reflect')
# Downscale by a factor of 2
z_l = sk.transform.rescale(y_h_blur, scale=0.5, anti_aliasing=False, multichannel=False)
y_l = sk.transform.rescale(z_l, scale=2, order=3, anti_aliasing=False, multichannel=False)
#y_l = rasterio.open('C:/Users/Nikolina Mileva/Documents/Sparseland/Images/NIR/S3A_20180924_B17_600m_to_300m.tif').read(1)[3:-3,3:-3]
# Downscale again by a factor of 2
z_ll = sk.transform.rescale(z_l, scale=0.5, anti_aliasing=False, multichannel=False)
## Filtering
filter_1 = np.array(([1, -1]))
filter_2 = np.array(([1], [-1]))
filter_3 = np.array(([1, -2, 1]))
filter_4 = np.array(([1], [-2], [1]))
im_fil_1 = scipy.ndimage.convolve1d(z_ll, filter_1, mode='reflect')
im_fil_2 = scipy.ndimage.convolve(z_ll, filter_2, mode='reflect')
im_fil_3 = scipy.ndimage.convolve1d(z_ll, filter_3, mode='reflect')
im_fil_4 = scipy.ndimage.convolve(z_ll, filter_4, mode='reflect')
# Up-sampling by a factor of 2 with bi-cubic interpolation
im_fil_1_l = sk.transform.rescale(im_fil_1, scale = 2, order=3,
anti_aliasing=False,
preserve_range=True,
multichannel=False)
im_fil_2_l = sk.transform.rescale(im_fil_2, scale = 2, order=3,
anti_aliasing=False,
preserve_range=True,
multichannel=False)
im_fil_3_l = sk.transform.rescale(im_fil_3, scale = 2, order=3,
anti_aliasing=False,
preserve_range=True,
multichannel=False)
im_fil_4_l = sk.transform.rescale(im_fil_4, scale = 2, order=3,
anti_aliasing=False,
preserve_range=True,
multichannel=False)
## Load z_l
#z_l = rasterio.open('C:/Users/Nikolina Mileva/Documents/Sparseland/Images/NIR/S3A_20180924_B17_600m.tif').read(1)[3:-3,3:-3]
#
## Load z_ll
#z_ll = rasterio.open('C:/Users/Nikolina Mileva/Documents/Sparseland/Images/NIR/S3A_20180924_B17_1200m.tif').read(1)[3:-3,3:-3]
# Load low resolution images (filtered)
#product = rasterio.open('C:/Users/Nikolina Mileva/Documents/Sparseland/Images/NIR/S3A_20180924_B17_h1der_600m.tif')
#profile = product.profile
#im_fil_1_l = rasterio.open('C:/Users/Nikolina Mileva/Documents/Sparseland/Images/NIR/S3A_20180924_B17_h1der_600m.tif').read(1)[3:-3,3:-3]
#im_fil_2_l = rasterio.open('C:/Users/Nikolina Mileva/Documents/Sparseland/Images/NIR/S3A_20180924_B17_v1der_600m.tif').read(1)[3:-3,3:-3]
#im_fil_3_l = rasterio.open('C:/Users/Nikolina Mileva/Documents/Sparseland/Images/NIR/S3A_20180924_B17_h2der_600m.tif').read(1)[3:-3,3:-3]
#im_fil_4_l = rasterio.open('C:/Users/Nikolina Mileva/Documents/Sparseland/Images/NIR/S3A_20180924_B17_v2der_600m.tif').read(1)[3:-3,3:-3]
# Extract low resolution patches
print('Extracting patches...')
t0 = time.time()
patches_1_l = extract_patch(im_fil_1_l, 2)
patches_2_l = extract_patch(im_fil_2_l, 2)
patches_3_l = extract_patch(im_fil_3_l, 2)
patches_4_l = extract_patch(im_fil_4_l, 2)
print('Done "extracting patches" in %.2fs.' % (time.time() - t0))
# Concatenate patches from the different filters into one vector
patches_l = np.hstack((patches_1_l, patches_2_l, patches_3_l, patches_4_l))
## Normalize the patches
#patches_l -= np.mean(patches_l, axis=0)
#patches_l /= np.std(patches_l, axis=0)
# Feature extraction from high resolution image
#y_ll = rasterio.open('C:/Users/Nikolina Mileva/Documents/Sparseland/Images/NIR/S3A_20180924_B17_1200m_to_600m.tif').read(1)[3:-3,3:-3]
y_ll = sk.transform.rescale(z_ll, scale = 2, order=3,
anti_aliasing=False,
preserve_range=True,
multichannel=False)
e_h = z_l - y_ll
#e_h = y_h - y_l
# Extract high resolution patches
patches_h = extract_patch(e_h, 2)
## Normalize the patches
#patches_h -= np.mean(patches_h, axis=0)
#patches_h /= np.std(patches_h, axis=0)
###########################################################
############### DIMENSIONALITY REDUCTION ##################
###########################################################
## Fine tuning of PCA number of components
#scaler = MinMaxScaler(feature_range=[0, 1])
#data_rescaled = scaler.fit_transform(patches_l)
##Fitting the PCA algorithm with our Data
#pca = PCA().fit(data_rescaled)
##Plotting the Cumulative Summation of the Explained Variance
#plt.figure()
#plt.plot(np.cumsum(pca.explained_variance_ratio_))
#plt.xlabel('Number of Components')
#plt.ylabel('Variance (%)') #for each component
#plt.title('Explained Variance')
#plt.show()
# Dimensionality reduction with PCA
pca = PCA(n_components=49) # 47 changed to 49 for display purposes
pca.fit(patches_l)
patches_l = pca.transform(patches_l)
########################################################
################## DICTIONARY LEARNING #################
########################################################
# Dictionary learning on low resolution patches
print('Learning the low resolution dictionary...')
t0 = time.time()
#dico = DictionaryLearning(n_components=1000, alpha=3, max_iter=40)
dico = MiniBatchDictionaryLearning(n_components=1000, alpha=3, n_iter=40) # try with less iterations
np.save('C:/Users/Nikolina Mileva/Documents/Sparseland/dico.npy', dico)
#V = dico.fit(patches_l).components_
#np.save('C:/Users/Nikolina Mileva/Documents/Sparseland/dictionary_l.npy', V)
# Load and visualize the low resolution dictionary
#V = np.load('C:/Users/Nikolina Mileva/Documents/Sparseland/dictionary_l.npy')
#patch_size = (7,7) # patch size after PCA
#plt.figure(figsize=(4.2, 4))
#for i, comp in enumerate(V[:100]):
# plt.subplot(10, 10, i + 1)
# plt.imshow(comp.reshape(patch_size), cmap=plt.cm.gray_r,
# interpolation='nearest')
# plt.xticks(())
# plt.yticks(())
#
#plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)
#
#plt.imshow(V)
#plt.gray()
#plt.show()
transform_algorithms = [('omp',{'transform_n_nonzero_coefs': 3})]
for transform_algorithm, kwargs in transform_algorithms:
dico.set_params(transform_algorithm=transform_algorithm, **kwargs)
Q = dico.fit_transform(patches_l) # sparse representation
dt = time.time() - t0
print('done in %.2fs.' % dt)
# Dictionary learning on high resolution patches
print('Learning the high resolution dictionary...')
t0 = time.time()
Q_t = np.transpose(Q)
P_t = np.transpose(patches_h)
dictionary_h = P_t @ Q @ (np.linalg.pinv(Q_t @ Q))
dt = time.time() - t0
print('done in %.2fs.' % dt)
#np.save('C:/Users/Nikolina Mileva/Documents/Sparseland/dictionary_h.npy', dictionary_h)
#np.save('C:/Users/Nikolina Mileva/Documents/Sparseland/sparse_representation.npy', Q)
## Visualize high resolution dictionary
#plt.figure(figsize=(4.2, 4))
#for i, comp in enumerate(dictionary_h[:324]):
# plt.subplot(18, 18, i + 1)
# plt.imshow(comp.reshape(patch_size), cmap=plt.cm.gray_r,
# interpolation='nearest')
# plt.xticks(())
# plt.yticks(())
#
#plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)
#
#plt.imshow(dictionary_h)
#plt.gray()
#plt.show()
#####################################################################
###################### RECONSTRUCTION PHASE #########################
#####################################################################
print('Reconstruction...')
# Filtering
im_fil_1 = scipy.ndimage.convolve1d(z_l, filter_1, mode='reflect')
im_fil_2 = scipy.ndimage.convolve(z_l, filter_2, mode='reflect')
im_fil_3 = scipy.ndimage.convolve1d(z_l, filter_3, mode='reflect')
im_fil_4 = scipy.ndimage.convolve(z_l, filter_4, mode='reflect')
## Up-sampling by a factor of 2 with bi-cubic interpolation
#im_fil_1_h = sk.transform.rescale(im_fil_1, scale = 2, order=3,
# anti_aliasing=False,
# preserve_range=True,
# multichannel=False)
#
#im_fil_2_h = sk.transform.rescale(im_fil_2, scale = 2, order=3,
# anti_aliasing=False,
# preserve_range=True,
# multichannel=False)
#
#im_fil_3_h = sk.transform.rescale(im_fil_3, scale = 2, order=3,
# anti_aliasing=False,
# preserve_range=True,
# multichannel=False)
#
#im_fil_4_h = sk.transform.rescale(im_fil_4, scale = 2, order=3,
# anti_aliasing=False,
# preserve_range=True,
# multichannel=False)
# Patch extraction
patches_1_h = extract_patch(im_fil_1, 1)
patches_2_h = extract_patch(im_fil_2, 1)
patches_3_h = extract_patch(im_fil_3, 1)
patches_4_h = extract_patch(im_fil_4, 1)
# Concatenate patches from the different filters into one vector
patches = np.hstack((patches_1_h, patches_2_h, patches_3_h, patches_4_h))
# Dimensionality reduction with PCA
pca = PCA(n_components=49) # 47 changed to 49 for display purposes
pca.fit(patches)
patches = pca.transform(patches)
# Deriving the sparse representation
#dico = np.load('C:/Users/Nikolina Mileva/Documents/Sparseland/dico.npy', allow_pickle=True)
#dictionary_h = np.load('C:/Users/Nikolina Mileva/Documents/Sparseland/dictionary_h.npy')
transform_algorithms = [('omp',{'transform_n_nonzero_coefs': 3})]
for transform_algorithm, kwargs in transform_algorithms:
dico.set_params(transform_algorithm=transform_algorithm, **kwargs)
Q = dico.transform(patches) # sparse representation
# Reconstruct high resolution patches
dictionary_h = np.transpose(dictionary_h)
patches = np.dot(Q, dictionary_h)
#intercept = np.mean(patches, axis=0)
#patches += intercept
patch_size = (9,9)
patches = patches.reshape(len(patches), *patch_size)
# Reconstruct the final image from the patches
y_r_diff = image.reconstruct_from_patches_2d(patches, image_size=z_l.shape)
y_r_diff = sk.transform.rescale(y_r_diff, scale=2, order=3, anti_aliasing=False, multichannel=False)
y_r = y_l + y_r_diff
dt = time.time() - t0
print('done in %.2fs.' % dt)
plt.imshow(y_h)
plt.gray()
plt.show()
plt.imshow(y_r_diff)
plt.gray()
plt.show()
plt.imshow(y_r)
plt.gray()
plt.show()
## Write the results to a .tif file
#print ('Writing product...')
#profile = product.profile
#profile.update(dtype='float64', count=1) # number of bands
#file_name = 'output.tif'
#
#result = rasterio.open(file_name, 'w', **profile)#rasterio.open(file_name, 'w', **defaults(count=1))#
#result.write(y_r, 1)
#result.close()
#array_up = sk.transform.rescale(array, scale = 2, order=3, anti_aliasing=False, multichannel=False)
#print(array_up)
#patch_size = (5,5)
#array = np.arange(100).reshape(10,10)
#array_p = image.extract_patches(array, (5,5), 1)
#d1,d2,d3,d4 = array_p.shape
#array_p = np.reshape(array_p, (d1*d2, d3, d4))
#array_p = array_p.reshape(array_p.shape[0], -1)
#
#patches = array_p.reshape(len(array_p), *patch_size)
#
#print(array)
#print(array_p.shape)
#array_r = image.reconstruct_from_patches_2d(array_p, image_size=array.shape)
#print(array_r.shape)
#print(array_r)