-
Notifications
You must be signed in to change notification settings - Fork 1
/
land-cover.jl
377 lines (326 loc) · 13.7 KB
/
land-cover.jl
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
using Random
using ImageFiltering
using DataStructures
using MAT
using StatsBase
using SoleData
function LandCoverDataset(
dataset_name::String
;
window_size::Union{Integer,NTuple{2,Integer}} = 1,
pad_window_size::Union{Integer,NTuple{2,Integer}} = window_size,
ninstances_per_class::Union{Nothing,Integer} = nothing,
ninstances_per_class_strategy::Symbol = :updownsampling,
flattened::Union{Bool,Symbol} = false,
apply_filter::Union{Bool,Tuple} = false,
seed = 1 :: Integer,
return_dicts = false :: Bool,
return_imgsize = false :: Bool,
return_label_dict = false :: Bool,
)
if window_size isa Integer
window_size = (window_size, window_size)
end
if pad_window_size isa Integer
pad_window_size = (pad_window_size, pad_window_size)
end
@assert pad_window_size[1] >= window_size[1] && pad_window_size[2] >= window_size[2]
@assert isodd(window_size[1]) && isodd(window_size[2])
@assert ninstances_per_class_strategy in [:updownsampling, :discard_classes] "Unknown ninstances_per_class_strategy: $(ninstances_per_class_strategy)."
########################################################################################
function IndianPinesDataset(;modIndianPines8 = false)
X = matread(data_dir * "indian-pines/Indian_pines_corrected.mat")["indian_pines_corrected"]
Y = matread(data_dir * "indian-pines/Indian_pines_gt.mat")["indian_pines_gt"]
(X, Y) = map(((x)->round.(Int,x)), (X, Y))
(X,Y), (modIndianPines8 == false ? [
"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",
] : OrderedDict(
2 => "Corn-notill",
3 => "Corn-mintill",
5 => "Grass-pasture",
8 => "Hay-windrowed", # "Grass-trees",
10 => "Soybean-notill",
11 => "Soybean-mintill",
12 => "Soybean-clean",
14 => "Woods",
)
)
end
function SalinasDataset()
X = matread(data_dir * "salinas/Salinas_corrected.mat")["salinas_corrected"]
Y = matread(data_dir * "salinas/Salinas_gt.mat")["salinas_gt"]
(X, Y) = map(((x)->round.(Int,x)), (X, Y))
(X, Y), [
"Brocoli_green_weeds_1",
"Brocoli_green_weeds_2",
"Fallow",
"Fallow_rough_plow",
"Fallow_smooth",
"Stubble",
"Celery",
"Grapes_untrained",
"Soil_vinyard_develop",
"Corn_senesced_green_weeds",
"Lettuce_romaine_4wk",
"Lettuce_romaine_5wk",
"Lettuce_romaine_6wk",
"Lettuce_romaine_7wk",
"Vinyard_untrained",
"Vinyard_vertical_trellis",
]
end
function SalinasADataset()
X = matread(data_dir * "salinas-A/SalinasA_corrected.mat")["salinasA_corrected"]
Y = matread(data_dir * "salinas-A/SalinasA_gt.mat")["salinasA_gt"]
(X, Y) = map(((x)->round.(Int,x)), (X, Y))
(X, Y), OrderedDict(
1 => "Brocoli_green_weeds_1",
10 => "Corn_senesced_green_weeds",
11 => "Lettuce_romaine_4wk",
12 => "Lettuce_romaine_5wk",
13 => "Lettuce_romaine_6wk",
14 => "Lettuce_romaine_7wk",
)
end
function PaviaCentreDataset()
X = matread(data_dir * "paviaC/Pavia.mat")["pavia"]
Y = matread(data_dir * "paviaC/Pavia_gt.mat")["pavia_gt"]
(X, Y) = map(((x)->round.(Int,x)), (X, Y))
(X,Y), [
"Water",
"Trees",
"Asphalt",
"Self-Blocking Bricks",
"Bitumen",
"Tiles",
"Shadows",
"Meadows",
"Bare Soil",
]
end
function PaviaUniversityDataset()
X = matread(data_dir * "paviaU/PaviaU.mat")["paviaU"]
Y = matread(data_dir * "paviaU/PaviaU_gt.mat")["paviaU_gt"]
(X, Y) = map(((x)->round.(Int,x)), (X, Y))
(X,Y), [
"Asphalt",
"Meadows",
"Gravel",
"Trees",
"Painted metal sheets",
"Bare Soil",
"Bitumen",
"Self-Blocking Bricks",
"Shadows",
]
end
########################################################################################
rng = Random.MersenneTwister(seed)
println("Load LandCoverDataset: $(dataset_name)...")
println("window_size = $(window_size)")
println("pad_window_size = $(pad_window_size)")
println("ninstances_per_class = $(ninstances_per_class)")
println("ninstances_per_class_strategy = $(ninstances_per_class_strategy)")
println("flattened = $(flattened)")
println("apply_filter = $(apply_filter)")
println("seed = $(seed)")
(Xmap, Ymap), class_names_map =
if dataset_name == "IndianPines"
IndianPinesDataset()
elseif dataset_name == "IndianPines8"
IndianPinesDataset(; modIndianPines8 = true)
elseif dataset_name == "Salinas"
SalinasDataset()
elseif dataset_name == "Salinas-A"
SalinasADataset()
elseif dataset_name == "Pavia Centre"
PaviaCentreDataset()
elseif dataset_name == "Pavia University"
PaviaUniversityDataset()
else
throw_n_log("Unknown land cover dataset_name: $(dataset_name)")
end
println()
println("Image size: $(size(Xmap))")
X, Y, tot_variables = size(Xmap, 1), size(Xmap, 2), size(Xmap, 3)
# Note: important that these are sorted
# existingLabels = sort(filter!(l->l≠0, unique(Ymap)))
existingLabels = sort(collect(keys(class_names_map)))
n_classes = length(existingLabels)
x_pad, y_pad = floor(Int,window_size[1]/2), floor(Int,window_size[2]/2)
x_dummypad, y_dummypad = floor(Int,pad_window_size[1]/2), floor(Int,pad_window_size[2]/2)
# println(1+x_dummypad, ":", (X-x_dummypad))
# println(1+y_dummypad, ":", (Y-y_dummypad))
pixel_coords, ninstances, _X, labels = begin
pixel_coords =
if isnothing(ninstances_per_class) # obtain all
pixel_coords = []
for x in 1+x_dummypad:(X-x_dummypad)
for y in 1+y_dummypad:(Y-y_dummypad)
exLabel = Ymap[x,y];
if exLabel == 0 || ! (exLabel in existingLabels)
continue
end
push!(pixel_coords, (x,y))
end
end
pixel_coords
else # obtain_with_random_sampling
# Derive the total number of samples per class
class_counts_d = OrderedDict(y => 0 for y in existingLabels)
no_class_counts = 0
for exLabel in Ymap
if exLabel == 0 || ! (exLabel in existingLabels)
no_class_counts += 1
else
class_counts_d[exLabel] += 1
end
end
println("class_counts_d = $(zip(class_names_map,class_counts_d) |> collect)")
println("no_class_counts = $(no_class_counts)")
class_is_to_ignore = OrderedDict(y => (ninstances_per_class_strategy == :discard_classes && class_counts_d[y] < ninstances_per_class) for y in existingLabels)
n_classes = begin
if sum(values(class_is_to_ignore)) != 0
@warn "Warning! The following classes will be ignored in order to balance the dataset:"
ignored_existingLabels = filter(y->(class_is_to_ignore[y]), existingLabels)
non_ignored_existingLabels = map(y->!(class_is_to_ignore[y]), existingLabels)
print("ignored classes: $([(class_names_map[y],class_counts_d[y]) for y in ignored_existingLabels])")
filter(y->(class_is_to_ignore[y]), existingLabels)
sum(non_ignored_existingLabels)
else
n_classes
end
end
println("n_classes = $(n_classes)")
ninstances = ninstances_per_class * n_classes
println("ninstances = $(ninstances_per_class) * $(n_classes) = $(ninstances)")
allow_upsampling = (ninstances_per_class_strategy in [:updownsampling])
pixel_coords = []
sampled_class_counts_d = OrderedDict(y=>0 for y in existingLabels)
for i_instance in 1:ninstances
# print(i_instance)
while (
x = rand(rng, 1+x_dummypad:(X-x_dummypad));
y = rand(rng, 1+y_dummypad:(Y-y_dummypad));
exLabel = Ymap[x,y];
exLabel == 0 || (! (exLabel in existingLabels)) || # Dummy class
class_is_to_ignore[exLabel] || # Must ignore class
((x,y) in pixel_coords && !allow_upsampling) || # Pixel already picked
sampled_class_counts_d[exLabel] == ninstances_per_class # Already picked enough pixels for this class
)
end
push!(pixel_coords, (x,y))
sampled_class_counts_d[exLabel] += 1
# readline()
end
if (length(pixel_coords) != ninstances)
throw_n_log("ERROR! Sampling failed! $(ninstances) $(length(pixel_coords))")
end
pixel_coords
end
ninstances = length(pixel_coords)
_X = Array{eltype(Xmap), 4}(undef, window_size[1], window_size[2], ninstances, tot_variables)
labels = Vector{eltype(Ymap)}(undef, ninstances)
for (i,(x,y)) in enumerate(pixel_coords)
_X[:,:,i,:] .= Xmap[x-x_pad:x+x_pad, y-y_pad:y+y_pad, :]
labels[i] = Ymap[x,y]
end
pixel_coords, ninstances, _X, labels
end
# Apply a convolutional filter
if apply_filter != false
if apply_filter[1] == "avg"
k = apply_filter[2]
_X = parent(imfilter(_X, ones(k,k,1,1)/9, Inner()))
@assert size(_X)[1:2] == (window_size[1]-k+1, window_size[2]-k+1)
else
throw_n_log("Unexpected value for apply_filter: $(apply_filter)")
end
end
_X =
if flattened != false
_X =
if flattened == :flattened
reshape(_X, (ninstances,(size(_X, 1)*size(_X, 2)*size(_X, 4))))
elseif flattened == :averaged
_X = sum(_X, dims=(1,2))./(size(_X, 1)*size(_X, 2))
dropdims(_X; dims=(1,2))
elseif flattened == :minmax
_X_min = dropdims(minimum(_X, dims=(1,2)); dims=(1,2))
_X_max = dropdims(maximum(_X, dims=(1,2)); dims=(1,2))
vcat(_X_min, _X_max)
else
throw_n_log("Unexpected value for flattened: $(flattened)")
end
permutedims(_X, [2,1])
elseif (size(_X, 1), size(_X, 2)) == (1, 1)
_X = dropdims(_X; dims=(1,2))
permutedims(_X, [2,1])
else
permutedims(_X, [1,2,4,3])
end
effective_class_counts_d = OrderedDict(y => 0 for y in existingLabels)
for i_instance in 1:ninstances
effective_class_counts_d[labels[i_instance]] += 1
end
println("effective_class_counts_d = $(zip(class_names_map,effective_class_counts_d) |> collect)")
println("countmap(labels) = $(countmap(labels))")
# # Sort pixel_coords by label
# sp = sortperm(labels)
# labels = labels[sp]
# _X = _X[:,:,sp,:]
# class_counts = Tuple(effective_class_counts_d[y] for y in existingLabels) # Note: dangerous: assumes existingLabels is sorted!
# println("class_counts = $(class_counts)")
# @assert length(labels) == sum(class_counts) "length(labels) = $(length(labels)) != sum(class_counts) = $(sum(class_counts))"
# println([class_names_map[y] for y in existingLabels])
# println(labels)
_Y = [class_names_map[y] for y in labels]
_X = OrderedDict([(i_pixel_coord => instance) for (i_pixel_coord, instance) in zip(enumerate(pixel_coords), eachslice(_X; dims=length(size(_X))))])
_Y = begin
if return_label_dict
OrderedDict([(i_pixel_coord => y) for (i_pixel_coord, y) in zip(enumerate(pixel_coords), _Y)])
else
_Y
end
end
dataset = begin
if return_dicts
(_X, _Y)
else
SoleData.cube2dataframe(dict2cube(_X)), _Y
end
end
if return_imgsize
dataset, size(Xmap)
else
dataset
end
end
dict2cube((X,Y)::Tuple{OrderedDict,Union{OrderedDict,AbstractVector}}) = (dict2cube(X), collect(values(Y)))
function dict2cube(X::OrderedDict)
# cat(values(X)...; dims=(length(size(first(values(X))))+1)) # Simple but causes StackOverflowError because of the splatting
_X = collect(values(X))
s = unique(size.(_X))
@assert length(s) == 1 "$(s)"
s = s[1]
__X = similar(first(_X), s..., length(_X))
for (i,x) in enumerate(_X)
__X[[(:) for j in 1:length(s)]...,i] .= x
end
__X
end