-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.R
1213 lines (1048 loc) · 35.4 KB
/
function.R
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
#' Get common filenames
#'
#' @description
#' Get common filenames from four folders.
#'
#' @param annotationsDir Directory of annotation files.
#' @param rgbDir Directory of RGB files.
#' @param hyperspectralDir Directory of hyperspectral files.
#' @param chmDir Directory of CHM files.
#' @returns A vector of common filenames without directory and filename extension.
getCommonFiles <- function(annotationsDir, rgbDir, hyperspectralDir, chmDir) {
# list file names
annotations <- list.files(annotationsDir)
RGB <- list.files(rgbDir)
Hyperspectral <- list.files(hyperspectralDir)
CHM <- list.files(chmDir)
# remove file extensions
annotations <- tools::file_path_sans_ext(annotations)
RGB <- tools::file_path_sans_ext(RGB)
Hyperspectral <- tools::file_path_sans_ext(Hyperspectral)
CHM <- tools::file_path_sans_ext(CHM)
# remove _hypersectral and _CHM
Hyperspectral <- gsub("_hyperspectral", "", Hyperspectral)
CHM <- gsub("_CHM", "", CHM)
# find common files
common <- intersect(annotations, RGB)
common <- intersect(common, Hyperspectral)
intersect(common, CHM)
}
#' Get metadata
#'
#' @description
#' Get metadata of raster files.
#'
#' @param inpPath Path to the raster file.
#' @returns A data.frame with filename, dimension, resolution, and crs.
getMetadata <- function(inpPath) {
# read file
r <- terra::rast(inpPath)
# get filename
filename <- tools::file_path_sans_ext(basename(inpPath))
# get dimension
rdim <- dim(r)
# get resolution
rres <- terra::res(r)
# put into data.frame
out <- data.frame(
filename = filename,
x = rdim[1],
y = rdim[2],
band = rdim[3],
xres = rres[1],
yres = rres[2],
crs = terra::crs(r, proj = TRUE)
)
}
#' Tile calculator
#'
#' @description
#' Calculate how many tiles will be generated if cropped and not mixed only?
#'
#' @param inpPath Path to input raster file.
#' @param targetShape A vector with two elements representing output width and height in pixels.
#' @returns Number of tiles.
tileCalculator <- function(inpPath, targetShape) {
r <- terra::rast(inpPath)
inputSize <- dim(r)[1:2]
tileSize <- as.integer(inputSize / targetShape)
tileSize[1] * tileSize[2]
}
#' Generate mask
#'
#' @description
#' Generate ground truth files.
#'
#' @param xmlPath Path to input annotation file.
#' @param dopPath Path to input RGB file.
#' @param targetShape A vector with two elements representing output width and height in pixels.
#' @param outDir Directory to output mask tiles.
#' @param gpkgDir Directory to output gpkg file.
#' @param croppedDir Directory to output mask file, cropped evenly on all sides.
#' @param mixedOnly Keep only tiles with both foreground and background?
#' @param overlapStride A vector with two elements representing overlapping width and height in pixels.
#' @returns NULL, as the function does not return a value.
generateMask <- function(
xmlPath, dopPath, targetShape, outDir,
gpkgDir = NULL, croppedDir = NULL, mixedOnly = TRUE, overlapStride = NULL) {
# create output folder if not exist
toCheck <- c(outDir, gpkgDir, croppedDir)
for (i in toCheck) {
if (!dir.exists(i)) {
dir.create(i)
}
}
# convert xml to sf
im <- raster::stack(dopPath)
dat <- dplyr::bind_rows(parallel::mclapply(xmlPath, NeonTreeEvaluation::xml_parse))
mask <- NeonTreeEvaluation::boxes_to_spatial_polygons(dat, im)
# plotRGB(im)
# plot(mask, add = TRUE, col = NA)
# save gpkg file?
if (!is.null(gpkgDir)) {
f <- file.path(gpkgDir, paste0(tools::file_path_sans_ext(basename(dopPath)), ".gpkg"))
sf::st_write(mask[, "crown_id"], f, append = FALSE, delete_dsn = TRUE, quiet = TRUE)
}
# convert sf to raster mask
dop <- terra::rast(dopPath)
mask <- terra::rasterize(mask, dop)
# reclassify to 0 and 1s
mask[is.na(mask[])] <- 0
mask[mask > 1] <- 1
if (!is.null(overlapStride)) {
# fill mask to integer multiple of targetShape
cropped <- fillAndOverlap(mask, targetShape, overlapStride, fillOnly = TRUE)
} else {
# crop evenly to fit target shape
cropped <- cropEvenly(mask, targetShape)
}
# save cropped file?
if (!is.null(croppedDir)) {
f <- file.path(croppedDir, basename(dopPath))
terra::writeRaster(cropped, f, overwrite = TRUE)
}
# grid for splitting
if (!is.null(overlapStride)) {
maskGrid <- terra::aggregate(cropped, targetShape - overlapStride * 2)
} else {
maskGrid <- terra::aggregate(cropped, targetShape)
}
values(maskGrid) <- 1:ncell(maskGrid)
maskGrid <- terra::as.polygons(maskGrid)
names(maskGrid) <- "cell"
if (!is.null(overlapStride)) {
# fill and overlap
cropped <- fillAndOverlap(mask, targetShape, overlapStride, fillOnly = FALSE)
}
# split
for (i in seq_along(maskGrid)) {
e1 <- terra::ext(maskGrid[maskGrid$cell == i, ])
if (!is.null(overlapStride)) {
toAdd <- c(overlapStride[1], overlapStride[1], overlapStride[2], overlapStride[2]) * terra::res(mask)
e1 <- e1 + toAdd
}
splitted <- terra::crop(cropped, e1)
# save
f <- paste0(tools::file_path_sans_ext(basename(dopPath)), "_", i, ".tif")
f <- file.path(outDir, f)
if (mixedOnly) {
if (isMixed(splitted)) {
terra::writeRaster(splitted, f, overwrite = TRUE)
}
} else {
terra::writeRaster(splitted, f, overwrite = TRUE)
}
}
}
#' Is the tile mixed?
#'
#' @description
#' Does the tile have both foreground and background pixels?
#'
#' @param r Input raster.
#' @returns Logical.
isMixed <- function(r) {
if (1 %in% values(r)) {
if (0 %in% values(r)) {
return(TRUE)
}
}
FALSE
}
#' Crop a raster evenly
#'
#' @description
#' Crop a raster evenly on all sides.
#'
#' @param r Input raster.
#' @param targetShape A vector with two elements representing output width and height in pixels.
#' @returns An evenly cropped raster.
cropEvenly <- function(r, targetShape) {
# define target and input size on x and y direction
targetSizeX <- targetShape[1]
targetSizeY <- targetShape[2]
inputX <- terra::ncol(r)
inputY <- terra::nrow(r)
# difference of input and target size
diffX <- inputX %% targetSizeX
diffY <- inputY %% targetSizeY
# determine new dimensions of raster and crop,
# cutting evenly on all sides if possible
newXmin <- terra::ext(r)[1] + ceiling(diffX / 2) * terra::res(r)[1]
newXmax <- terra::ext(r)[2] - floor(diffX / 2) * terra::res(r)[1]
newYmin <- terra::ext(r)[3] + ceiling(diffY / 2) * terra::res(r)[2]
newYmax <- terra::ext(r)[4] - floor(diffY / 2) * terra::res(r)[2]
terra::crop(r, terra::ext(newXmin, newXmax, newYmin, newYmax))
}
#' Generate weight
#'
#' @description
#' Generate weight based on Ronneberger2015.
#'
#' @param maskPath Path to the ground truth file.
#' @param gpkgPath Path to the annotation gpkg file.
#' @param outDir Output directory.
#' @returns Weight raster.
#' @reference https://doi.org/10.1007/978-3-319-24574-4_28
generateWeight <- function(maskPath, gpkgPath, outDir) {
# create output folder if not exist
if (!dir.exists(outDir)) {
dir.create(outDir)
}
r <- terra::rast(maskPath)
# class weight
rtable <- table(values(r == 0))
weights <- (1 / rtable) / max(1 / rtable) # is between 0 and 1
class_weight <- r
class_weight[class_weight == 1] <- weights["FALSE"]
class_weight[class_weight == 0] <- weights["TRUE"]
# take middle point of only background cell
r[r == 1] <- NA
p <- terra::as.points(r)
# crop gpkg (rasterize, then convert back to vector)
gpkg <- terra::vect(gpkgPath)
gpkg <- terra::rasterize(gpkg, r, field = "crown_id")
gpkg <- terra::as.polygons(gpkg)
# calculate distance (between each polygon and each point)
d <- terra::distance(gpkg, p)
# saveRDS(d, file.path("tem", "d.RDS"))
# distance to the nearest cell
d1 <- distanceToNNearestCell(d, 1)
a <- values(r)
a <- replace(a, !is.na(a), d1)
d1 <- terra::rast(r, vals = a)
# terra::writeRaster(d1, file.path("tem", "d1.tif"), overwrite = TRUE)
# distance to the second nearest cell
d2 <- distanceToNNearestCell(d, 2)
a <- values(r)
a <- replace(a, !is.na(a), d2)
d2 <- terra::rast(r, vals = a)
# terra::writeRaster(d2, file.path("tem", "d2.tif"), overwrite = TRUE)
# calculate weight map
w <- weightMap(d1, d2, class_weight)
w <- terra::cover(w, class_weight) # give foreground the class weight
# plot(w)
# save
if (!is.null(outDir)) {
f <- file.path(outDir, basename(maskPath))
terra::writeRaster(w, f, overwrite = TRUE)
}
w
}
#' Distance to Nth nearest cell
#'
#' @description
#' Distance to Nth nearest cell in pixels.
#'
#' @param distanceMatrix A distance matrix.
#' @param n N as in Nth nearest.
#' @returns NULL, as the function does not return a value.
distanceToNNearestCell <- function(distanceMatrix, n) {
out <- parallel::mclapply(seq_len(ncol(distanceMatrix)), function(i) {
sort(distanceMatrix[, i])[n]
})
as.vector(do.call(cbind, out))
}
#' Weight formula
#'
#' @description
#' According to Ronneberger2015.
#'
#' @param d1 Distance to nearest cell in pixel.
#' @param d2 Distance to second nearest cell in pixel.
#' @param wc Class weight.
#' @param w0 A constant.
#' @param sigma A constant.
#' @returns Weight raster.
#' @reference https://doi.org/10.1007/978-3-319-24574-4_28
weightMap <- function(d1, d2, wc, w0 = 10, sigma = 5) {
wc + w0 * exp(-(d1 + d2)^2 / (2 * sigma^2))
}
#' Generate image tiles
#'
#' @description
#' Generate 15-band image tile. The bands are R, G, B, CHM,
#' hyperspectral bands 11, 55, 113, and
#' vegetation indices NIR, NDVI, EVI, ARVI, PRI, NDLI, SAVI, GNDVI.
#'
#' @param maskPath Path to mask.
#' @param dopPath Path to RGB.
#' @param hyperspectralPath Path to hyperspectral.
#' @param chmPath Path to CHM.
#' @param outDir Output directory.
#' @param overlapStride Overlap size in pixel.
#' @returns A 15-band image tile, normalized to 0 and 1.
generateImage <- function(
maskPath, dopPath, hyperspectralPath, chmPath, outDir,
overlapStride = NULL, useBands = NULL) {
# create output folder if not exist
if (!dir.exists(outDir)) {
dir.create(outDir)
}
# read files
mask <- terra::rast(maskPath)
dop <- terra::rast(dopPath)
hyp <- terra::rast(hyperspectralPath)
chm <- terra::rast(chmPath)
# select only some hyperspectral bands
selectedBands <- c(11, 55, 113, 96, 54, 18, 31, 38, 260, 275, 84, 58)
hyp <- hyp[[selectedBands]]
names(hyp) <- c("11", "55", "113", "NIR", "RED", "BLUE", "531", "570", "1680", "1754", "800", "670")
# extent in some files don't match exactly
# assume they should
if (!is.null(overlapStride)) {
terra::ext(hyp) <- terra::ext(dop)
terra::ext(chm) <- terra::ext(dop)
}
if (!is.null(overlapStride)) {
targetShape <- dim(mask)[1:2]
# fill and overlap
dop <- fillAndOverlap(dop, targetShape, overlapStride, fillOnly = FALSE)
hyp <- fillAndOverlap(hyp, targetShape / 10, overlapStride / 10, fillOnly = FALSE)
chm <- fillAndOverlap(chm, targetShape / 10, overlapStride / 10, fillOnly = FALSE)
}
# crop files
# crop bigger for hyp and chm to prevent NA after resampling
dop <- terra::crop(dop, mask)
hyp <- terra::crop(hyp, mask, snap = "out")
chm <- terra::crop(chm, mask, snap = "out")
# replace na with mean until no na anymore
dop <- replaceNA(dop)
hyp <- replaceNA(hyp)
chm <- replaceNA(chm)
# resample due to different resolution
hyp <- terra::resample(hyp, dop, method = "bilinear", threads = TRUE, overwrite = TRUE)
chm <- terra::resample(chm, dop, method = "bilinear", threads = TRUE, overwrite = TRUE)
# calculate indices from hyp, + 1e-7 to avoid divided by zero
NDVI <- (hyp[["NIR"]] - hyp[["RED"]]) / ((hyp[["NIR"]] + hyp[["RED"]]) + 1e-7)
EVI <- 2.5 * (hyp[["NIR"]] - hyp[["RED"]]) / ((hyp[["NIR"]] + 6 * hyp[["RED"]] - 7.5 * hyp[["BLUE"]] + 1) + 1e-7)
ARVI <- (hyp[["NIR"]] - 2 * hyp[["RED"]] + hyp[["BLUE"]]) / (hyp[["NIR"]] + 2 * hyp[["RED"]] - hyp[["BLUE"]] + 1e-7)
PRI <- (hyp[["531"]] - hyp[["570"]]) / (hyp[["531"]] + hyp[["570"]] + 1e-7)
NDLI <- (log10(hyp[["1754"]]) - log10(hyp[["1680"]])) / (log10(hyp[["1754"]]) + log10(hyp[["1680"]]) + 1e-7)
SAVI <- (log10(hyp[["800"]]) - log10(hyp[["670"]])) / ((log10(hyp[["800"]]) + log10(hyp[["670"]]) + 0.5) * 1.5 + 1e-7)
GNDVI <- (hyp[["NIR"]] - hyp[["531"]]) / ((hyp[["NIR"]] + hyp[["531"]]) + 1e-7)
hyp <- c(hyp[[c("11", "55", "113", "NIR")]], NDVI, EVI, ARVI, PRI, NDLI, SAVI, GNDVI)
# combine
out <- c(dop, chm, hyp)
names(out) <- c("R", "G", "B", "CHM", "11", "55", "113", "NIR", "NDVI", "EVI", "ARVI", "PRI", "NDLI", "SAVI", "GNDVI")
# output only selected ones
if (!is.null(useBands)) {
out <- out[[useBands]]
}
# normalize each layer to zero to one
out <- zeroToOne(out)
# save
if (!is.null(outDir)) {
f <- file.path(outDir, basename(maskPath))
terra::writeRaster(out, f, overwrite = TRUE)
}
out
}
#' Replace NA values in a raster
#'
#' @description
#' Replace NA values in a raster repeatedly with mean value of nearby cells.
#'
#' @param r A raster.
#' @returns A raster without NA. If all pixels are NA, return a raster of 0.
replaceNA <- function(r) {
# return raster of 0 if all na
if (all(is.na(values(r)))) {
values(r) <- 0
return(r)
}
# replace na
while (TRUE) {
if (any(is.na(values(r)))) {
r <- terra::focal(r, w = 3, fun = mean, na.policy = "only", na.rm = TRUE)
} else {
break
}
}
r
}
#' Normalize a raster
#'
#' @description
#' Normalize each layer in a terra raster to 0-1.
#' + 1e-7 to avoid divided by zero, which could happen in CHM for example
#'
#' @param ras A raster.
#' @returns A normalized raster.
zeroToOne <- function(ras) {
out <- lapply(names(ras), function(i) {
layer <- ras[[i]]
imax <- max(values(layer), na.rm = TRUE)
imin <- min(values(layer), na.rm = TRUE)
normalized <- (layer - imin) / ((imax - imin) + 1e-7)
})
out <- terra::rast(out)
}
#' My loss function
#'
#' @description
#' My loss function that calculates mean of output. For weighted model.
#'
#' @returns A loss function.
myLoss_mean <- function() {
function(target, output) {
tf$math$reduce_mean(output)
}
}
#' Training function
#'
#' @description
#' Training function.
#'
#' @param imageDir Image directory.
#' @param maskDir Mask directory.
#' @param weightDir Weight directory.
#' @param outDir Output directory.
#' @param model Model object for use in the training.
#' @param optimizer Optimizer.
#' @param loss Loss function.
#' @param metrics List of metrics.
#' @param batch_size Number of samples per gradient update, default 32.
#' @param epochs Number of epochs to train the model.
#' @param callbacks List of callbacks to be called during training.
#' @param class_weight List of callbacks to be called during training.
#' @param sampling A vector of indices to choose sample from all available input files.
#' @param augment Augment training data?
#' @param weighted Is the model weighted?
#' @param useBands A vector of band names. Only these bands from the image will be used.
#' @returns Training history.
myTrain <- function(
imageDir, maskDir, weightDir, outDir,
model, optimizer, loss, metrics,
batch_size, epochs, callbacks,
class_weight = NULL, sampling = NULL, augment = TRUE, weighted = FALSE,
useBands = NULL) {
# create output folder
if (!dir.exists(outDir)) {
dir.create(outDir, recursive = TRUE)
}
# get a list of files
files <- list(
img = list.files(imageDir, full.names = TRUE),
mask = list.files(maskDir, full.names = TRUE)
)
if (weighted) {
files$weight <- list.files(weightDir, full.names = TRUE)
}
# sampling
if (!is.null(sampling)) {
files$mask <- sort(files$mask[sampling])
files$img <- sort(files$img[basename(files$img) %in% basename(files$mask)])
if (weighted) {
files$weight <- sort(files$weight[basename(files$weight) %in% basename(files$mask)])
}
}
# get number of bands in the image
if (!is.null(useBands)) {
imgBands <- length(useBands)
} else {
imgBands <- terra::nlyr(terra::rast(files$img[1]))
}
# split files randomly into training and validation
files <- data.frame(files)
data <- rsample::initial_split(files, prop = 0.8)
# get arrays from files
## training image
files <- training(data)$img
outShape <- c(targetShape, imgBands)
x <- filesToArray(files, outShape, useBands)
## training mask
files <- training(data)$mask
outShape <- c(targetShape, 1)
y <- filesToArray(files, outShape)
## validation image
files <- testing(data)$img
outShape <- c(targetShape, imgBands)
vx <- filesToArray(files, outShape, useBands)
## validation mask
files <- testing(data)$mask
outShape <- c(targetShape, 1)
vy <- filesToArray(files, outShape)
if (weighted) {
## training weight
files <- training(data)$weight
shape <- c(targetShape, 1)
w <- filesToArray(files, shape)
## validation weight
files <- testing(data)$weight
shape <- c(targetShape, 1)
vw <- filesToArray(files, shape)
}
# showImage(x, y)
# augment training data
if (augment) {
x <- augmentation(x)
y <- augmentation(y)
if (weighted) {
w <- augmentation(w)
}
}
# showImage(as.array(x), as.array(y), length(training(data)$img)+1)
# compile model
model %>% compile(
optimizer = optimizer,
loss = loss,
metrics = metrics
)
# train model
if (weighted) {
trainingHistory <- model %>% fit(
x = list(x, y, w),
y = y,
batch_size = batch_size,
epochs = epochs,
callbacks = callbacks,
validation_data = list(list(vx, vy, vw), vy),
class_weight = class_weight
)
} else {
trainingHistory <- model %>% fit(
x = x,
y = y,
batch_size = batch_size,
epochs = epochs,
callbacks = callbacks,
validation_data = list(vx, vy),
class_weight = class_weight
)
}
# save model
keras::save_model_tf(model, outDir)
# save training history
path <- file.path(outDir, "trainingHistory.RDS")
saveRDS(trainingHistory, file = path)
# save training history plot
path <- file.path(outDir, "trainingHistory.png")
png(path)
print(plot(trainingHistory))
dev.off()
# plot training history
# plot(trainingHistory)
trainingHistory
}
#' U-Net
#'
#' @description
#' Create a U-Net model.
#'
#' Compared with original model: use_batch_norm was taken as a parameter;
#' Option for weighted model was added; Output datatype was set to float32
#' for calculation stability.
#'
#' @reference https://github.com/r-tensorflow/unet/blob/c47cf31f13050722b587a5c394d4511d8f5e50b9/R/model.R
#' @reference https://github.com/keras-team/keras/blob/r2.15/keras/backend.py#L5802
#' @reference https://www.tensorflow.org/guide/mixed_precision
#'
#' @param input_shape Input shape.
#' @param num_classes Number of classes.
#' @param dropout Dropout rate.
#' @param filters Number of filters in the first layer.
#' @param num_layers Number of layers.
#' @param output_activation Output activation function.
#' @param weighted Create a weighted model?
#' @param use_batch_norm Use batch normalization?
#' @returns A model.
unet <- function(input_shape, num_classes = 1, dropout = 0.5, filters = 64,
num_layers = 4, output_activation = "sigmoid", weighted = FALSE, use_batch_norm = FALSE) {
input <- keras::layer_input(shape = input_shape)
x <- input
down_layers <- list()
for (i in seq_len(num_layers)) {
x <- conv2d_block(
inputs = x,
filters = filters,
use_batch_norm = use_batch_norm,
dropout = 0,
padding = "same"
)
down_layers[[i]] <- x
x <- keras::layer_max_pooling_2d(x, pool_size = c(2, 2), strides = c(2, 2))
filters <- filters * 2
}
if (dropout > 0) {
x <- keras::layer_dropout(x, rate = dropout)
}
x <- conv2d_block(
inputs = x,
filters = filters,
use_batch_norm = use_batch_norm,
dropout = 0.0,
padding = "same"
)
for (conv in rev(down_layers)) {
filters <- filters / 2L
x <- keras::layer_conv_2d_transpose(
x,
filters = filters,
kernel_size = c(2, 2),
padding = "same",
strides = c(2, 2)
)
x <- keras::layer_concatenate(list(conv, x))
x <- conv2d_block(
inputs = x,
filters = filters,
use_batch_norm = use_batch_norm,
dropout = 0.0,
padding = "same"
)
}
x <- keras::layer_conv_2d(
x,
filters = num_classes,
kernel_size = c(1, 1)
)
# https://www.tensorflow.org/guide/mixed_precision
output <- keras::layer_activation(
x,
activation = output_activation,
dtype = "float32"
)
if (weighted) {
target <- keras::layer_input(shape = c(input_shape[1:2], num_classes))
weight <- keras::layer_input(shape = c(input_shape[1:2], num_classes))
# https://github.com/keras-team/keras/blob/r2.15/keras/backend.py#L5802
# epsilon_ <- tf$convert_to_tensor(keras::k_epsilon(), output$dtype$base_dtype)
# output <- tf$clip_by_value(output, epsilon_, 1.0 - epsilon_)
# Compute cross entropy from probabilities.
bce <- target * tf$math$log(output + keras::k_epsilon())
bce <- bce + (1 - target) * tf$math$log(1 - output + keras::k_epsilon())
bce <- -bce
wbce <- weight * bce
model <- keras::keras_model(list(input, target, weight), wbce)
} else {
model <- keras::keras_model(input, output)
}
model
}
#' 2D convolution layer block
#'
#' @description
#' Create a 2D convolution layer block with two convolution layers.
#'
#' Same as reference
#'
#' @reference https://github.com/r-tensorflow/unet/blob/c47cf31f13050722b587a5c394d4511d8f5e50b9/R/model.R
#'
#' @param inputs Input model for this block to be added on.
#' @param use_batch_norm Use batch normalization?
#' @param dropout Dropout rate.
#' @param filters Number of filters in the convolution layers.
#' @param kernel_size Kernel size.
#' @param activation Activation function to use.
#' @param kernel_initializer Initializer for the kernel weights matrix.
#' @param padding Padding option. One of "valid" or "same".
#' @returns A 2D convolution layer block.
conv2d_block <- function(inputs, use_batch_norm = TRUE, dropout = 0.3,
filters = 16, kernel_size = c(3, 3), activation = "relu",
kernel_initializer = "he_normal", padding = "same") {
x <- keras::layer_conv_2d(
inputs,
filters = filters,
kernel_size = kernel_size,
activation = activation,
kernel_initializer = kernel_initializer,
padding = padding
)
if (use_batch_norm) {
x <- keras::layer_batch_normalization(x)
}
if (dropout > 0) {
x <- keras::layer_dropout(x, rate = dropout)
}
x <- keras::layer_conv_2d(
x,
filters = filters,
kernel_size = kernel_size,
activation = activation,
kernel_initializer = kernel_initializer,
padding = padding
)
if (use_batch_norm) {
x <- keras::layer_batch_normalization(x)
}
x
}
#' Files to array
#'
#' @description
#' Transform image files to array.
#'
#' @param files A vector of image file paths.
#' @param outShape Output shape of the array.
#' @param useBands A vector of band names. Only these bands from the image will be extracted.
#' @returns An array representing extracted pixel values.
filesToArray <- function(files, outShape, useBands = NULL) {
x <- array(, dim = c(length(files), outShape))
for (i in 1:length(files)) {
f <- files[i]
r <- terra::rast(f)
if (!is.null(useBands)) {
r <- r[[useBands]]
}
a <- terra::as.array(r)
x[i, , , ] <- a
}
x
}
#' Show image
#'
#' @description
#' Show two images from two arrays, RGB on the left, mask on the right.
#'
#' @param x Array for RGB.
#' @param y Array for mask.
#' @param i Which image in the array to show.
#' @param r Band index in the array for R.
#' @param g Band index in the array for G.
#' @param b Band index in the array for B.
#' @param fx Factor to be multiplied with values in x.
#' @param fy Factor to be multiplied with values in y.
#' @returns A plot with RGB on the left and mask on the right.
showImage <- function(x, y, i = 1, r = 1, g = 2, b = 3, fx = 255, fy = 1) {
par(mfrow = c(1, 2))
plotRGB(c(terra::rast(x[i, , , r] * fx), terra::rast(x[i, , , g] * fx), terra::rast(x[i, , , b] * fx)))
plot(terra::rast(y[i, , , 1]) * fy)
}
#' Augmentation
#'
#' @description
#' Augmentation procedure. First randomly flip input horizontally and vertically,
#' and then randomly rotate it between plus/minus 45 degrees.
#'
#' @param x Input array.
#' @returns Concatenation of the original array and augmented array.
#' The output size is two times that of the original array.
augmentation <- function(x) {
xaug <- x %>%
keras::layer_random_flip(mode = "horizontal_and_vertical", seed = 42) %>%
keras::layer_random_rotation(factor = 0.125, seed = 42)
x <- tensorflow::tf$concat(list(x, xaug), axis = 0L)
}
#' Fill and overlap
#'
#' @description
#' Fill (and overlap) a raster to integer multiple of targetShape by mirroring.
#'
#' @param r Input raster.
#' @param targetShape A vector with two elements representing output width and height in pixels.
#' @param overlapStride A vector with two elements representing overlapping width and height in pixels.
#' @param fillOnly Fill only without overlapping?.
#' @returns Filled (and overlapped) raster.
fillAndOverlap <- function(r, targetShape, overlapStride, fillOnly = FALSE) {
targetShape <- targetShape - overlapStride * 2
# distance needed to move on x, y to fill
rSize <- dim(r)[1:2]
rNeed <- ceiling(rSize / targetShape) * targetShape - rSize
rResolution <- terra::res(r)
rDistance <- ceiling(rNeed / 2) * rResolution
if (fillOnly) {
if (sum(rDistance) == 0) {
return(r)
} else {
rFilled <- fourMirrors(r, rDistance)
return(rFilled)
}
} else {
# distance needed to move on x, y to fill and to overlay
rDistance <- rDistance + overlapStride * rResolution
rDistance <- ceiling(rDistance / rResolution) * rResolution
rFilledAndOverlapped <- fourMirrors(r, rDistance)
}
}
#' Mirroring
#'
#' @description
#' Mirroring a raster on all sides.
#'
#' @param r5 Input raster to be mirrored.
#' @param d Mirroring distance in pixels.
#' @returns A raster mirrored on all sides.
fourMirrors <- function(r5, d) {
# imagine the output is a merge of 9 tiles arranged in:
# 123
# 456
# 789
r5e <- terra::ext(r5)
e <- terra::ext(r5e[1], r5e[2], r5e[4] - d[2], r5e[4])
r2 <- terra::crop(r5, e)
r2 <- terra::flip(r2, direction = "vertical")
r2 <- terra::shift(r2, dy = d[2])
e <- terra::ext(r5e[1], r5e[2], r5e[3], r5e[3] + d[2])
r8 <- terra::crop(r5, e)
r8 <- terra::flip(r8, direction = "vertical")
r8 <- terra::shift(r8, dy = -d[2])
e <- terra::ext(r5e[1], r5e[1] + d[1], r5e[3], r5e[4])
r4 <- terra::crop(r5, e)
r4 <- terra::flip(r4, direction = "horizontal")
r4 <- terra::shift(r4, dx = -d[1])
e <- terra::ext(r5e[2] - d[1], r5e[2], r5e[3], r5e[4])
r6 <- terra::crop(r5, e)
r6 <- terra::flip(r6, direction = "horizontal")
r6 <- terra::shift(r6, dx = d[1])
r2e <- terra::ext(r2)
e <- terra::ext(r2e[1], r2e[1] + d[1], r2e[3], r2e[4])
r1 <- terra::crop(r2, e)
r1 <- terra::flip(r1, direction = "horizontal")
r1 <- terra::shift(r1, dx = -d[1])
e <- terra::ext(r2e[2] - d[1], r2e[2], r2e[3], r2e[4])
r3 <- terra::crop(r2, e)
r3 <- terra::flip(r3, direction = "horizontal")
r3 <- terra::shift(r3, dx = d[1])
r8e <- terra::ext(r8)
e <- terra::ext(r8e[1], r8e[1] + d[2], r8e[3], r8e[4])
r7 <- terra::crop(r8, e)
r7 <- terra::flip(r7, direction = "horizontal")
r7 <- terra::shift(r7, dx = -d[2])
e <- terra::ext(r8e[2] - d[2], r8e[2], r8e[3], r8e[4])
r9 <- terra::crop(r8, e)
r9 <- terra::flip(r9, direction = "horizontal")
r9 <- terra::shift(r9, dx = d[2])
terra::merge(r1, r2, r3, r4, r5, r6, r7, r8, r9)
# plot(merged)
}
#' Predict
#'
#' @description
#' Predict procedure.
#'
#' @param imagePaths Paths to images.
#' @param maskPaths Paths to masks.
#' @param modelDir Model directory.
#' @param model Model object for use in the prediction.
#' @param optimizer Optimizer.
#' @param loss Loss function.
#' @param metrics List of metrics.
#' @param batch_size Number of samples per gradient update, default 32.
#' @param outDir Output directory.
#' @param overlapStride Overlap size in pixel.
#' @param doEvaluation Evaulate test data?.
#' @param useBands A vector of band names. Only these bands from the image will be used.
#' @returns NULL, as the function does not return a value.
myPredict <- function(
imagePaths, maskPaths, modelDir,
model, optimizer, loss, metrics,
batch_size, outDir, overlapStride = NULL, doEvaluation = FALSE,
useBands = NULL) {
# create output folder
if (!dir.exists(outDir)) {
dir.create(path = outDir, recursive = TRUE)
}
# number of bands
if (!is.null(useBands)) {
imgBands <- length(useBands)
} else {
imgBands <- terra::nlyr(terra::rast(imagePaths[1]))
}
# convert files to array
files <- imagePaths
shape <- c(targetShape, imgBands)
x <- filesToArray(files, shape, useBands)
files <- maskPaths