-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLIDARK_script.py
1344 lines (1087 loc) · 59.6 KB
/
LIDARK_script.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import glob
import rasterio
import gc
import random
import numpy as np
import pandas as pd
import geopandas as gp
import tensorflow as tf
from tqdm import tqdm
from affine import Affine
from shapely.geometry import box
from rasterio.mask import geometry_mask
from datetime import datetime
from rasterstats import zonal_stats
import warnings
warnings.filterwarnings("ignore")
"""
To begin with,
place the .las or .laz files in :
{project_folder} / Lidar / {project_area} / 00_Original /
place the false and true label files in :
{project_folder} / GIS / {project_area} / labels /
"""
class CreateMap:
"""
This class creates data-maps used for traning and prediction.
Parameters
----------
project_folder : path to the project folder ex. 'C:/project/'
project_area : name of the project area ex. 'Naljanka'
map_type : choose between 'DTM','TPI','MDHS'.
'DTM' requiers lastools path 'C:/lastools/bin/' in the laz2dtm def
cores : only relevant for lastools. default is 8.
buffer : the overlap between mapsheets when generating data-maps.
Overlaps is needed to get a fully covered area. default is 30.
DTM shoud have a slightly wider buffer then the other layers.
when using 30 for TPI, 50 is recommended for DTM.
pxl_size : the resolution used in generating data-maps. should be the same for all products.
gpu : If torch is installed, GPU-processing of the TPIs can be done. Else they will be processed by CPU.
"""
def __init__(self, project_folder, project_area, map_type, cores=8, buffer=30, pxl_size=0.25, gpu=True):
self.project_folder = project_folder
self.project_area = project_area
self.map_type = map_type
self.cores = cores
self.buffer = buffer
self.pxl_size = pxl_size
self.gpu = gpu
# these folders will be generated automatically, except the self.laz_folder.
self.laz_folder = self.project_folder + '/Lidar/' + self.project_area + '/00_Original'
self.DTM_folder = self.project_folder + '/Lidar/' + self.project_area + '/01_DTM/'
self.TPI_folder = self.project_folder + '/Lidar/' + self.project_area + '/02_TPI/'
self.MDHS_folder = self.project_folder + '/Lidar/' + self.project_area + '/03_MDHS/'
# execution
if map_type == 'DTM':
print('generating DTM from laz')
print('num cores: ' + str(self.cores))
self.laz2dtm()
if map_type == 'TPI':
print('generating TPI from DTM')
print('radius: ' + str(self.buffer))
self.tpi_index()
if map_type == 'MDHS':
print('generating multi-directional hillshade')
self.mdhs()
else:
print('map-type not recognized')
def laz2dtm(self):
"""
las2DTM generates DTM rasters of all laser data in the self.laz_folder
:return: DTM rasters in the self.DTM_folder
"""
# define the path to the lastools bin-folder.
lastools_path = 'C:/lastools/bin/'
# creating folder if not already exists
if not os.path.exists(self.DTM_folder):
os.makedirs(self.DTM_folder)
def lasindex(input, cores):
"""
lasindex creates a lastools call readable for the os.
lasindex documentation:
Creates a *.lax file for a given *.las or *.laz file that
contains spatial indexing information. When this LAX file is
present it will be used to speed up access to the relevant
areas of the LAS/LAZ file
:param input: the folder where las-data is located, defined in __init__ as self.laz_folder
:param cores: number of cores that should be used for processing
:return: a os call for lasindex with these parameters
"""
return lastools_path + \
'lasindex ' + \
'-i ' + input + '/*.laz ' + \
'-cores ' + str(cores)
def las2dem(input, odir, odix, kill, keep_class, step, nodata, buffer, cores):
"""
las2dem creates a lastools call readable for the os.
las2dem documentation:
This tool reads LIDAR points from the LAS/LAZ format (or some
ASCII format), triangulates them temporarily into a TIN, and
then rasters the TIN onto a DEM.
:param input: the folder where las-data is located, defined in __init__ as self.laz_folder
:param odir: the folder where DTM-data should be generated, defined in __init__ as self.DTM_folder
:param odix: output filename suffix
:param kill: maximum triangle length.
:param keep_class: laser class filter.
:param step: resolution of output raster
:param nodata: nodata value
:param buffer: buffer size for mapsheets. should be wider than the other products.
:param cores: number of cores that should be used for processing
:return: a os call for las2dem with these parameters
"""
return lastools_path + \
'las2dem ' + \
'-i ' + input + '/*.laz ' + \
'-odir ' + odir + ' ' + \
'-otif ' + \
'-odix ' + odix + ' ' + \
'-elevation ' + \
'-kill ' + str(kill) + ' ' + \
'-keep_class ' + keep_class + ' ' + \
'-step ' + str(step) + ' ' + \
'-nodata ' + str(nodata) + ' ' + \
'-buffered ' + str(buffer) + ' ' + \
'-cores ' + str(cores)
# runs the call generated from def
os.system(lasindex(input=self.laz_folder, cores=self.cores))
# runs the call generated from def
os.system(las2dem(input=self.laz_folder,
odir=self.DTM_folder,
odix='_DTM',
kill=50,
keep_class='2',
step=self.pxl_size,
nodata=0,
buffer=self.buffer,
cores=self.cores))
def tpi_index(self):
"""
tpi_index generates TPI rasters with a given radius equal the defined self.buffer,
of all laser data in the self.DTM_folder.
This algorithm has been optimized to run on GPU.
If no GPU is availible, CPU will be used.
TPI documentation:
https://landscapearchaeology.org/2019/tpi/
:return: TPI files in the self.TPI_folder
"""
# creating output-folder if not already exists
tpi_output = self.TPI_folder + '/radius_' + str(self.buffer) + '/'
if not os.path.exists(tpi_output):
os.makedirs(tpi_output)
def tpi(mx_z, mx_temp, mx_count, range_list):
"""
:param mx_z: DTM as array
:param mx_temp: temporary array
:param mx_count: tracking number of neighbours
:param range_list: positions to be processed
:return: TPI output file
"""
def view(offset_y, offset_x, shape, step=1):
"""
Function returning two matching numpy views for moving window routines.
- 'offset_y' and 'offset_x' refer to the shift in relation to the analysed (central) cell
- 'shape' are 2 dimensions of the data matrix (not of the window!)
- 'view_in' is the shifted view and 'view_out' is the position of central cells
(see on LandscapeArchaeology.org/2018/numpy-loops/)
"""
size_y, size_x = shape
x, y = abs(offset_x), abs(offset_y)
x_in = slice(x, size_x, step)
x_out = slice(0, size_x - x, step)
y_in = slice(y, size_y, step)
y_out = slice(0, size_y - y, step)
# the swapping trick
if offset_x < 0: x_in, x_out = x_out, x_in
if offset_y < 0: y_in, y_out = y_out, y_in
# return window view (in) and main view (out)
return (y_in, x_in), (y_out, x_out)
# loop through window and accumulate values
for (y, x), weight in range_list:
if weight == 0: continue # skip zero values !
# determine views to extract data
view_in, view_out = view(y - r_y, x - r_x, mx_z.shape)
# using window weights (eg. for a Gaussian function)
mx_temp[view_out] += mx_z[view_in] * weight
# track the number of neighbours
# (this is used for weighted mean : Σ weights*val / Σ weights)
mx_count[view_out] += weight
out = mx_z - mx_temp / mx_count
return out
r = self.buffer # radius for the TPI calculation
win = np.ones((2 * r + 1, 2 * r + 1)) # filter used as moving window
r_y, r_x = win.shape[0] // 2, win.shape[1] // 2 # radius of the filter in x and y directions.
win[r_y, r_x] = 0 # removing the middle pixel
range_list = [] # fetching positions in the moving window
for (y, x), weight in np.ndenumerate(win):
range_list.append([(y, x), weight])
if self.gpu:
import torch
print('GPU processing enabled...')
proc_files = glob.glob(self.DTM_folder + '/*.tif') # gathering paths for files to be processed
for i in tqdm(range(len(proc_files))):
proc_file = proc_files[i]
if not os.path.exists(tpi_output + str(os.path.basename(proc_file)[:-7]) + 'TPI.tif'):
# if not file already exists, DTM raster is opened and read as numpy array.
dtm_img = rasterio.open(proc_file)
profile = dtm_img.profile
dtm_arr = dtm_img.read(1)
dtm_arr[dtm_arr <= 0] = np.nan # values lower than 0 in the DTM is beeing ignored.
# if gpu-processing is enabled, torch converts numpy arrays to tensors that can be stored in GPU memory
if self.gpu:
mx_z = torch.from_numpy(dtm_arr).cuda()
mx_temp = torch.zeros(mx_z.shape).cuda()
mx_count = torch.zeros(mx_z.shape).cuda()
else:
mx_z = dtm_arr
mx_temp = np.zeros(mx_z.shape)
mx_count = np.zeros(mx_z.shape)
out = tpi(mx_z, mx_temp, mx_count, range_list)
if self.gpu:
out = out.cpu()
out = np.array(out)
# converting the float array output to integer
out = np.nan_to_num(out, nan=0)
out = out * 100
out[out > 100] = 100
out[out < -100] = -100
out = out.astype(dtype='int16')
profile['dtype'] = 'int16'
profile['nodata'] = None
# cutting away the buffer area as the TPI will generate false values here.
old_trans = profile['transform']
buffer_size = int(self.buffer / old_trans[0])
out = out[buffer_size:-buffer_size, buffer_size:-buffer_size]
# updating metadata for the new image size and positioning
profile['height'] = out.shape[0]
profile['width'] = out.shape[1]
profile['transform'] = Affine(old_trans[0], old_trans[1], old_trans[2] + self.buffer, old_trans[3],
old_trans[4], old_trans[5] - self.buffer)
# the CPU memory can sometimes be overloaded with waste when switching between CPU and GPU
# therefore this will clean up memory waste.
if self.gpu:
gc.collect()
torch.cuda.empty_cache()
# tries to export as geotiff
try:
with rasterio.open(tpi_output + str(os.path.basename(proc_file)[:-7]) + 'TPI.tif', 'w',
**profile) as dst:
dst.write(out, 1)
except:
print("cannot export file")
def mdhs(self):
"""
MDHS (Multi-directional hillshade) is a hillshade product that combines several hillshade directions
for optimal insight in the terrain. The lowest value at each pixle for each direction is kept.
:return: MDHS files in the self.MDHS_folder
"""
# creating folder if not already exists
if not os.path.exists(self.MDHS_folder):
os.makedirs(self.MDHS_folder)
# predefined hillshade directions and altitude. Can be edited.
directions = [330, 270, 210, 150, 90, 30]
angle_altitude = 45
imgs = glob.glob(self.DTM_folder + '/*.tif') # gathering paths for files to be processed
for i in range(len(imgs)):
# DTM raster is opened and read as numpy array.
dtm_img = rasterio.open(imgs[i])
dtm_arr = dtm_img.read(1)
dtm_arr[dtm_arr <= 0] = np.nan
# creating empty hillshade array with one band per direction
mdhs_arr = np.zeros(shape=(len(directions), dtm_arr.shape[0], dtm_arr.shape[1]))
for j in tqdm(range(len(directions))):
# for each direction, a hillshade is created
azimuth = directions[j]
x, y = np.gradient(dtm_arr)
slope = np.pi / 2. - np.arctan(np.sqrt(x * x + y * y))
aspect = np.arctan2(-x, y)
azimuthrad = azimuth * np.pi / 180.
altituderad = angle_altitude * np.pi / 180.
shaded = np.sin(altituderad) * np.sin(slope) + np.cos(altituderad) * np.cos(slope) * np.cos(
(azimuthrad - np.pi / 2.) - aspect)
# each hillshade is added to a band
mdhs_arr[j, :, :] = shaded
# converting the float array output to integer
mdhs_arr = mdhs_arr.min(axis=0)
mdhs_arr = np.nan_to_num(mdhs_arr, nan=0)
mdhs_arr = 255 * mdhs_arr
mdhs_arr = mdhs_arr.astype(dtype='uint8')
# updating metadata for the new image
profile = dtm_img.profile
profile['count'] = 1
profile['dtype'] = 'uint8'
profile['nodata'] = 0
# tries to export as geotiff
try:
with rasterio.open(self.MDHS_folder + str(os.path.basename(imgs[i])[:-11]) + '_MDHS.tif', 'w',
**profile) as dst:
dst.write(mdhs_arr, 1)
except:
print("cannot export file")
class Training:
"""
This class both builds a training dataset, and trains a model. It can do both in combination, or seperated if
a training dataset exists. After the update, this is Class is no longer connnected to a project area. True and
false objects are instead stored under /DeepLearning/train_annotations/'.
If the gpu-version of tensorflow is installed, and it is able to use the GPU in the machine, training will be
done using GPU memory.
Parameters
----------
project_folder : path to the project folder ex. 'C:/project/'
true_objects : name of shapefile (.shp) with true objects.
false_objects : name of shapefile (.shp) with false objects.
feature_list : list of data maps that will be used for creating a training dataset. ex. ['02_TPI/radius_30/'] or
['02_TPI/radius_20/','02_TPI/radius_30/']
test_mapsheets : define a list with mapsheet names for that should not be included in training.
If any names in the list is contained in a mapsheet, it will be excluded. ex. ['S5133G1','S5133E3']
fetch_data : choose if a training dataset should be created or not.
data_timestamp : when creating a training dataset, it is saved with a timestamp. define the timestamp to a already
existing training dataset if you don't want to generate a new one. (if fetch_data = False)
augment : if you want to augment the trianing data, to gain a bigger training dataset.
train_on_data : If you want to use your traning dataset for training.
"""
def __init__(self, project_folder, true_objects, false_objects, feature_list,
test_mapsheets, fetch_data=True, data_timestamp=None, augment=True, train_on_data=True):
self.project_folder = project_folder
self.true_objects = true_objects
self.false_objects = false_objects
self.feature_list = feature_list
self.test_mapsheets = test_mapsheets
self.fetch_data = fetch_data
self.data_timestamp = data_timestamp
self.augment = augment
self.train_on_data = train_on_data
# these folders will be generated automatically
self.training_folder = self.project_folder + '/DeepLearning/'
self.label_folder = self.training_folder + '/train_annotations/'
self.log_folder = self.training_folder + '/logs/'
# the EPSG code can be changed, but should be the same as the laser-data.
self.EPSG_code = 'EPSG:3067'
# length of mapsheet names
self.mapsheet_digits = 9
# training hyper-parameters. Can be edited.
self.img_size = (512, 512)
self.learning_rate = 0.001
self.batch_size = 32
self.val_split = 0.2
self.max_epochs = 200
self.unet_size = 16
self.dropout = 0.0
# if not a training dataset is created, use fetch data to create one. Else, use the timestamp to locate it.
if self.fetch_data:
self.samples, self.labels = self.create_samples()
if self.augment:
self.augment_data()
self.save_training_data()
else:
self.samples, self.labels = self.load_training_data()
# data being shuffled for a optimal training situation
self.train_samples, self.train_labels, self.val_samples, self.val_labels = self.shuffle_data()
# start training
if self.train_on_data:
self.train_model()
self.create_log()
def create_samples(self):
"""
Creates the training dataset based on the data maps, and labels defined
:return: a numpy array with samples, and one with labels.
"""
# reading the labels as vector data
true_objects = gp.read_file(self.label_folder + '/' + self.true_objects)
false_objects = gp.read_file(self.label_folder + '/' + self.false_objects)
# gathering information about the label positions in the data maps
imgs = glob.glob(self.project_folder + '/Lidar/*/' + self.feature_list[0] + '/*.tif')
training_meta = pd.DataFrame(columns=['mapsheet', 'y_min', 'y_max', 'x_min', 'x_max'])
for i in tqdm(range(len(imgs))):
# selecting a image to gather information about its positioning
img = rasterio.open(imgs[i])
mapsheet_name = os.path.basename(imgs[i])[:self.mapsheet_digits]
# checking to see if the selected image is in the test-list
train = True
for test in self.test_mapsheets:
if test in mapsheet_name:
train = False
if train:
# define the boundaries of the image
img_bounds = gp.GeoDataFrame(
geometry=[box(img.bounds[0], img.bounds[1], img.bounds[2], img.bounds[3])], crs=self.EPSG_code)
# clipping the vector labels to the image bounds
true_temp = gp.clip(true_objects, img_bounds)
false_temp = gp.clip(false_objects, img_bounds)
# if the image contains any true labels, 2 random snapshot positions are stored for each label
if len(true_temp) > 0:
for j in range(len(true_temp)):
object_bounds = true_temp.iloc[j].geometry.bounds
min_minx = object_bounds[2] - (self.img_size[1] * img.transform[0])
max_minx = object_bounds[0]
min_maxy = object_bounds[3]
max_maxy = object_bounds[1] + (self.img_size[0] * img.transform[0])
for k in range(2):
minx = random.randint(int(min_minx), int(max_minx))
maxy = random.randint(int(min_maxy), int(max_maxy))
x_arr_min = int((minx - img.transform[2]) / img.transform[0])
y_arr_min = int((img.transform[5] - maxy) / img.transform[0])
x_arr_max = int(x_arr_min + self.img_size[1])
y_arr_max = int(y_arr_min + self.img_size[0])
data = [mapsheet_name, y_arr_min, y_arr_max, x_arr_min, x_arr_max]
training_meta = training_meta.append(
pd.DataFrame([data], columns=['mapsheet', 'y_min', 'y_max', 'x_min', 'x_max']))
# if the image contains any false labels, 2 random snapshot positions are stored for each label
if len(false_temp) > 0:
for j in range(len(false_temp)):
object_bounds = false_temp.iloc[j].geometry.bounds
min_minx = object_bounds[2] - (self.img_size[1] * img.transform[0])
max_minx = object_bounds[0]
min_maxy = object_bounds[3]
max_maxy = object_bounds[1] + (self.img_size[0] * img.transform[0])
for k in range(2):
minx = random.randint(int(min_minx), int(max_minx))
maxy = random.randint(int(min_maxy), int(max_maxy))
x_arr_min = int((minx - img.transform[2]) / img.transform[0])
y_arr_min = int((img.transform[5] - maxy) / img.transform[0])
x_arr_max = int(x_arr_min + self.img_size[1])
y_arr_max = int(y_arr_min + self.img_size[0])
data = [mapsheet_name, y_arr_min, y_arr_max, x_arr_min, x_arr_max]
training_meta = training_meta.append(
pd.DataFrame([data], columns=['mapsheet', 'y_min', 'y_max', 'x_min', 'x_max']),
ignore_index=True)
else:
print('skipping test mapsheet')
# two arrays are being created to contain data from the data maps, and the co-responding label.
labels = np.empty(shape=(len(training_meta), self.img_size[0], self.img_size[1]), dtype='uint8')
samples = np.empty(shape=(len(training_meta), self.img_size[0], self.img_size[1], len(self.feature_list)),
dtype='int8')
# finding all mapsheets that will be included in generating training data
train_mapsheets = training_meta['mapsheet'].unique()
# iterating over the label positions, creating arrays for each
label_index = 0
for i in tqdm(range(len(train_mapsheets))):
mapsheet = train_mapsheets[i]
local_samples = training_meta.loc[training_meta['mapsheet'] == mapsheet]
mask_img = \
glob.glob(self.project_folder + '/Lidar/*/' + self.feature_list[
0] + '/' + mapsheet + '*.tif')[0]
mask_img = rasterio.open(mask_img)
x_clip_max = mask_img.shape[1]
y_clip_max = mask_img.shape[0]
label_mask = geometry_mask(true_objects.geometry, mask_img.shape, mask_img.transform, all_touched=True,
invert=True)
for j in range(len(local_samples)):
xmin = np.clip(local_samples['x_min'].iloc[j], 0, x_clip_max)
ymin = np.clip(local_samples['y_min'].iloc[j], 0, y_clip_max)
xmax = np.clip(local_samples['x_max'].iloc[j], 0, x_clip_max)
ymax = np.clip(local_samples['y_max'].iloc[j], 0, y_clip_max)
label = label_mask[ymin:ymin + self.img_size[0], xmin:xmin + self.img_size[1]]
if not label.shape == self.img_size:
label = label_mask[ymax - self.img_size[0]:ymax, xmax - self.img_size[1]:xmax]
if not label.shape == self.img_size:
label = label_mask[ymin:ymin + self.img_size[0], xmax - self.img_size[1]:xmax]
if not label.shape == self.img_size:
label = label_mask[ymax - self.img_size[0]:ymax, xmin:xmin + self.img_size[1]]
labels[label_index, :, :] = label
label_index += 1
# iterating over all data maps. creating arrays for each position and stacking data maps if more than one.
for k in range(len(self.feature_list)):
feature_index = 0
for i in tqdm(range(len(train_mapsheets))):
mapsheet = train_mapsheets[i]
local_samples = training_meta.loc[training_meta['mapsheet'] == mapsheet]
feature_img = glob.glob(
self.project_folder + '/Lidar/*/' + self.feature_list[k] + '/' +
mapsheet + '*.tif')[0]
feature_img = rasterio.open(feature_img)
x_clip_max = feature_img.shape[1]
y_clip_max = feature_img.shape[0]
feature_arr = feature_img.read(1)
feature_arr = feature_arr.astype(dtype='int8')
for j in range(len(local_samples)):
xmin = np.clip(local_samples['x_min'].iloc[j], 0, x_clip_max)
ymin = np.clip(local_samples['y_min'].iloc[j], 0, y_clip_max)
xmax = np.clip(local_samples['x_max'].iloc[j], 0, x_clip_max)
ymax = np.clip(local_samples['y_max'].iloc[j], 0, y_clip_max)
feature = feature_arr[ymin:ymin + self.img_size[0], xmin:xmin + self.img_size[1]]
if not feature.shape == self.img_size:
feature = feature_arr[ymax - self.img_size[0]:ymax, xmax - self.img_size[1]:xmax]
if not feature.shape == self.img_size:
feature = feature_arr[ymin:ymin + self.img_size[0], xmax - self.img_size[1]:xmax]
if not feature.shape == self.img_size:
feature = feature_arr[ymax - self.img_size[0]:ymax, xmin:xmin + self.img_size[1]]
samples[feature_index, :, :, k] = feature
feature_index += 1
return samples, labels
def augment_data(self):
"""
Augmenting the dataset generated in create_samples. Flipping the images aand labels horizontally and vertically
:return: an extended version of the training dataset
"""
# iterating thorugh the training dataset
aug_samples = np.empty(
shape=(len(self.samples), self.img_size[0], self.img_size[1], len(self.feature_list)), dtype='int8')
aug_labels = np.empty(
shape=(len(self.labels), self.img_size[0], self.img_size[1]), dtype='uint8')
for i in tqdm(range(len(self.samples))):
# horizontal flip
X_lr = np.fliplr(np.copy(self.samples[i]))
y_lr = np.fliplr(np.copy(self.labels[i]))
aug_samples[i, :, :, :] = X_lr
aug_labels[i, :, :] = y_lr
self.samples = np.append(self.samples, aug_samples, axis=0)
self.labels = np.append(self.labels, aug_labels, axis=0)
aug_samples = np.empty(
shape=(len(self.samples), self.img_size[0], self.img_size[1], len(self.feature_list)), dtype='int8')
aug_labels = np.empty(
shape=(len(self.labels), self.img_size[0], self.img_size[1]), dtype='uint8')
for i in tqdm(range(len(self.samples))):
# vertical flip
X_ud = np.flipud(np.copy(self.samples[i]))
y_ud = np.flipud(np.copy(self.labels[i]))
aug_samples[i, :, :, :] = X_ud
aug_labels[i, :, :] = y_ud
self.samples = np.append(self.samples, aug_samples, axis=0)
self.labels = np.append(self.labels, aug_labels, axis=0)
def save_training_data(self):
"""
Saves the training dataset to.npy files with a timestamp generated at the actual time of saving.
:return: two .npy files. One for samples and one for labels.
"""
self.data_timestamp = datetime.now().strftime('%m%d%H%M%S')
samples_folder = self.training_folder + '/samples/'
labels_folder = self.training_folder + '/labels/'
if not os.path.exists(samples_folder):
os.makedirs(samples_folder)
if not os.path.exists(labels_folder):
os.makedirs(labels_folder)
np.save(samples_folder + 'samples_' + self.data_timestamp + '.npy', self.samples)
np.save(labels_folder + 'labels_' + self.data_timestamp + '.npy', self.labels)
def load_training_data(self):
"""
Loads the samples and labels from .npy files at the selected timestamp.
:return: samples and lables as numpy arrays
"""
samples = np.load(self.training_folder + '/samples/samples_' + self.data_timestamp + '.npy')
labels = np.load(self.training_folder + '/labels/labels_' + self.data_timestamp + '.npy')
return samples, labels
def shuffle_data(self):
"""
Shuffels the training dataset for an optimal training situation.
Also splits the data into training and validation datasets. Based on the selected ratio at self.val_split
:return: samples and labels for training and validation data.
"""
idx = list(range(len(self.samples)))
random.shuffle(idx)
split = int(len(self.samples) * self.val_split)
train_idx = idx[:-split]
val_idx = idx[-split:]
train_samples = self.samples[train_idx]
train_labels = self.labels[train_idx]
val_samples = self.samples[val_idx]
val_labels = self.labels[val_idx]
return train_samples, train_labels, val_samples, val_labels
def train_model(self):
"""
Trains a model based on the training and validation data.
Saves the best model to a .h5 file at a new timestamp folder.
This folder will also be where predictions with the given model is stored.
Several hyper-parameters can be manually tuned in the __init__ def
:return: a trained model ready for predictions
"""
# creating a directory for the model output
self.model_timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
model_dir = self.training_folder + 'model/' + self.model_timestamp
if not os.path.exists(model_dir):
os.makedirs(model_dir)
# defining the model
model = self.get_unet_model((self.img_size[0], self.img_size[1], len(self.feature_list)),
1, 'relu', self.dropout)
# choosing an optimizer for the model
opt = tf.keras.optimizers.Adam(learning_rate=self.learning_rate)
# compiling the model
model.compile(optimizer=opt, loss='binary_crossentropy', metrics=[self.iou])
# A checkpointer that saves only the best model based on validation results for each epoch
model_checkpointer_callback = tf.keras.callbacks.ModelCheckpoint(model_dir + '/model.h5',
monitor='val_iou',
save_best_only=True,
verbose=1, mode='max')
# Stops the training if the model reaches a performance roof
early_stopping_callback = tf.keras.callbacks.EarlyStopping(monitor='val_iou', patience=10, mode='max')
# training the model
history = model.fit(self.train_samples,
self.train_labels,
batch_size=self.batch_size,
epochs=self.max_epochs,
callbacks=[model_checkpointer_callback, early_stopping_callback],
validation_data=(self.val_samples, self.val_labels))
self.best_val = max(history.history['val_iou'])
def pool_encoder_block(self, x, conv_block, features, block_name, activation):
"""
Encoder blocks using max pooling
"""
x = conv_block(x, features, block_name, activation)
p = tf.keras.layers.MaxPool2D((2, 2), name=f"{block_name}_down_pool")(x)
return x, p
def simple_conv_block(self, x, features, block_name, activation):
"""
Create conv2D - BN - act - conv2D - BN - act block.
A simple convolution block with two convolution layers and batch normalization.
"""
# Two 3x3 convolutions
x = self.conv_bn_act(x, features, f"{block_name}_c1", 3, 1, activation)
x = self.conv_bn_act(x, features, f"{block_name}_c2", 3, 1, activation)
return x
def conv_bn_act(self, x, features, name, kernel=1, stride=1, activation=None):
"""
Basic building block of Convolution - Batch normalization - Activation
"""
# 3x3 convolution layer without bias, as we have learned gamma - beta parameters in the BN
x = tf.keras.layers.Conv2D(features, kernel, stride,
padding="same",
name=name + "_conv",
use_bias=False,
data_format="channels_last")(x)
# Batch normalization, with learned bias
x = tf.keras.layers.BatchNormalization(name=name + "_batchnorm")(x)
# Activation
if activation:
x = tf.keras.layers.Activation(activation, name=name + "_activation")(x)
return x
def decoder_block_addskip(self, input, skip_features, conv_block, features, block_name, activation, dropout=0.0):
# Upscale convolution
x = tf.keras.layers.Conv2DTranspose(
features, (3, 3), strides=2, name=block_name + "_conv_up", padding="same")(input)
# Add in skip connection
x = tf.keras.layers.Add(name=block_name + "_add_skip")([x, skip_features])
# Dropout
if dropout > 0:
x = tf.keras.layers.Dropout(dropout, name=block_name + "_drop")(x)
# Convolution block
x = conv_block(x, features, block_name, activation)
return x
def get_unet_model(self, img_size, num_classes, activation_function, dropout=0.0):
inputs = tf.keras.layers.Input(img_size) #TODO fix input potblem
features = self.unet_size
# Downsample
s1, p1 = self.pool_encoder_block(inputs, self.simple_conv_block, features, 'encoder1',
activation_function)
s2, p2 = self.pool_encoder_block(p1, self.simple_conv_block, features * 2, 'encoder2',
activation_function)
s3, p3 = self.pool_encoder_block(p2, self.simple_conv_block, features * 4, 'encoder3',
activation_function)
s4, p4 = self.pool_encoder_block(p3, self.simple_conv_block, features * 8, 'encoder4',
activation_function)
# Final downsampled block
b1 = self.simple_conv_block(p4, features * 16, 'b1', activation_function)
# Upsample
d1 = self.decoder_block_addskip(b1, s4, self.simple_conv_block, features * 8, 'decoder1',
activation_function, dropout)
d2 = self.decoder_block_addskip(d1, s3, self.simple_conv_block, features * 4, 'decoder2',
activation_function, dropout)
d3 = self.decoder_block_addskip(d2, s2, self.simple_conv_block, features * 2, 'decoder3',
activation_function, dropout)
d4 = self.decoder_block_addskip(d3, s1, self.simple_conv_block, features, 'decoder4',
activation_function, dropout)
# Add a per-pixel classification layer
outputs = tf.keras.layers.Conv2D(num_classes, 1, padding="same", activation="sigmoid")(d4)
model = tf.keras.Model(inputs, outputs, name="U-Net")
return model
def iou(self, y_true, y_pred):
"""
Intersect over union (IoU) used for validation metric in the training process
"""
labels = y_true
predictions = y_pred
# Perform threshold (used to convert fuzzy results to mask, at a given threshold)
predictions_thresholded = tf.cast(predictions > 0.5, tf.int32)
# defining the prediction and the labels for overlap
labels_c = tf.keras.backend.cast(tf.keras.backend.equal(labels, 1), tf.keras.backend.floatx())
pred_c = tf.keras.backend.cast(tf.keras.backend.equal(predictions_thresholded, 1), tf.keras.backend.floatx())
# setting the maxim values
labels_c_sum = tf.keras.backend.sum(labels_c)
pred_c_sum = tf.keras.backend.sum(pred_c)
# calculating IoU
intersect = tf.keras.backend.sum(labels_c * pred_c)
union = labels_c_sum + pred_c_sum - intersect
iou = intersect / union
return iou
def create_log(self):
if not os.path.exists(self.log_folder):
os.makedirs(self.log_folder)
log_df = pd.DataFrame(index=['log_id', 'data_id', 'epsg', 'num samples', 'augmented',
'features', 'img size', 'learning rate', 'batch size',
'validation split', 'max epochs', 'unet size', 'dropout', 'best iou'])
#TODO fill
log_df.loc['log_id', 'values'] = self.model_timestamp
log_df.loc['data_id','values'] = self.data_timestamp
log_df.loc['epsg', 'values'] = self.EPSG_code
log_df.loc['num samples', 'values'] = len(self.samples)
log_df.loc['augmented', 'values'] = self.augment
log_df.loc['features', 'values'] = self.feature_list
log_df.loc['img size', 'values'] = self.img_size
log_df.loc['learning rate', 'values'] = self.learning_rate
log_df.loc['batch size', 'values'] = self.batch_size
log_df.loc['validation split', 'values'] = self.val_split
log_df.loc['max epochs', 'values'] = self.max_epochs
log_df.loc['unet size', 'values'] = self.unet_size
log_df.loc['dropout', 'values'] = self.dropout
log_df.loc['best iou', 'values'] = self.best_val
log_df.to_csv(self.log_folder + '/' + self.model_timestamp + '.csv')
class Predict:
"""
This class predicts an project area, or a selection of test mapsheets.
It also combines the result into a merged shapefile.
The output vector result will have both probability and size as attributes.
An already trained model is needed. And selecting a model is done by defining a timestamp.
Parameters
----------
project_folder : path to the project folder ex. 'C:/project/'
project_areas : name of the project areas in a list ex. ['Naljanka'] or ['Naljanka','Kuivaniemi']
feature_list : list of data maps that will be used for prediction. Must be the same as the model is trained on
ex. ['02_TPI/radius_30/']
model_timestamp : the timestamp for an erlier trained model.
test_mapsheets : a selection of mapsheets for the model to predict on. ex. ['S5133G1','S5133E3']
pred_type : if the whole area should be predicted, choose 'all'. If only test mapsheets should be predicted,
choose 'test'
"""
def __init__(self, project_folder, project_areas, feature_list, model_timestamp, test_mapsheets, pred_type='all'):
self.project_folder = project_folder
self.project_areas = project_areas
self.feature_list = feature_list
self.model_timestamp = model_timestamp
self.test_mapsheets = test_mapsheets
self.pred_type = pred_type
# these folders will be generated automatically
self.model_folder = self.project_folder + '/DeepLearning/model/'
# the EPSG code can be changed, but should be the same as the laser-data.
self.EPSG_code = 'EPSG:3067'
# training hyper-parameters. Can be edited. But needs to be the same as in the training class.
self.img_size = (512, 512)
# loading model
self.model = self.load_model()
# predicting on mapsheets
if pred_type == 'all':
# looping through the selected project areas
for i in range(len(project_areas)):
# a merge-list is defined to append every predicted mapsheet path. This to merge all in the end
merge_list = []
# locating all mapsheets in the selected area ready for prediction
pred_list = glob.glob(self.project_folder + '/Lidar/' + project_areas[i] + '/' +
self.feature_list[0] + '/*.tif')
if len(pred_list) == 0:
print('No mapsheets found. Check if feature maps have been generated for ' + self.feature_list[0])
# predicting mapsheets
for j in range(len(pred_list)):
ms_name = os.path.basename(pred_list[j])[:9]
print(ms_name)
try:
merge_list.append(self.pred_mapsheet(ms_name, project_areas[i]))
except:
print('could not predict mapsheet')
# merging results to a merged vector output
self.merge_pred_shapes(project_areas[i], pred_type, merge_list)
elif pred_type == 'test':
# looping through the selected project areas
for i in range(len(project_areas)):
# a merge-list is defined to append every predicted mapsheet path. This to merge all in the end
merge_list = []
# looping through the selected test mapsheets
for j in range(len(self.test_mapsheets)):
test_mapsheet = self.test_mapsheets[j]
pred_list = glob.glob(self.project_folder + '/Lidar/' + project_areas[i] + '/' +
self.feature_list[0] + '/' + test_mapsheet + '*.tif')
if len(pred_list) == 0:
print(
'No mapsheets found. Check if feature maps have been generated for ' + self.feature_list[0])
for k in range(len(pred_list)):
ms_name = os.path.basename(pred_list[k])[:9]
print(ms_name)
try:
merge_list.append(self.pred_mapsheet(ms_name, project_areas[i]))
except:
print('could not predict mapsheet')
# merging results to a merged vector output
self.merge_pred_shapes(project_areas[i], pred_type, merge_list)
else:
print('type "all" or "test" in "pred_type" to predict')
def pred_mapsheet(self, ms_name, project_area):
"""
predicts a given mapsheet with a trained model.
:param ms_name: the name of the mapsheet being predicted
:return: both a raster and a vector prediction of the given mapsheet.
"""
# gathering metadata for the actual mapsheet
height_list = []
width_list = []
for i in range(len(self.feature_list)):
pred_meta = rasterio.open(glob.glob(self.project_folder + '/Lidar/' + project_area + '/' +
self.feature_list[i] + '/' + ms_name + '*.tif')[0])
height_list.append(pred_meta.height)
width_list.append(pred_meta.width)
pred_height = min(height_list)
pred_width = min(width_list)
# defining the array where data maps are stored for prediction
pred_arr = np.empty(shape=(pred_height, pred_width, len(self.feature_list)), dtype='int8')
# defining output paths
pred_output_path = self.model_folder + self.model_timestamp + '/predictions/'
raster_output_path = pred_output_path + project_area + '/raster/'
shape_output_path = pred_output_path + project_area + '/shapes/'
if not os.path.exists(raster_output_path + ms_name + '.tif'):
# fetching data from each data map into the array for prediction
for i in range(len(self.feature_list)):
feature_name = self.feature_list[i]
feature_path = glob.glob(self.project_folder + '/Lidar/' + project_area + '/' +
feature_name + '/' + ms_name + '*.tif')[0]
feature_img = rasterio.open(feature_path)
feature_arr = feature_img.read(1)
feature_arr = feature_arr.astype(dtype='int8')
height_cut = int((feature_arr.shape[0] - pred_height) / 2)
width_cut = int((feature_arr.shape[1] - pred_width) / 2)
if height_cut > 0:
feature_arr = feature_arr[height_cut:-height_cut,:]