-
Notifications
You must be signed in to change notification settings - Fork 18
/
createDenoisingAutoEncoderSuperResolutionModel.R
135 lines (117 loc) · 4.95 KB
/
createDenoisingAutoEncoderSuperResolutionModel.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
#' 2-D implementation of the denoising autoencoder image super resolution architecture.
#'
#' @param inputImageSize Used for specifying the input tensor shape. The
#' shape (or dimension) of that tensor is the image dimensions followed by
#' the number of channels (e.g., red, green, and blue). The batch size
#' (i.e., number of training images) is not specified a priori.
#' @param convolutionKernelSizes a 2-element list of 2-D vectors specifying the
#' kernel size at each convolution layer. The first element is the kernel size
#' of the encoding layers and the 2nd element is the kernel size of the final
#' convolution layer.
#' @param numberOfEncodingLayers the number of encoding layers.
#' @param numberOfFilters the number of filters for each encoding layer.
#'
#' @return a keras model for image super resolution
#' @author Tustison NJ
#' @examples
#' createDenoisingAutoEncoderSuperResolutionModel2D(c( 28, 28, 1 ))
#' @import keras
#' @export
createDenoisingAutoEncoderSuperResolutionModel2D <- function( inputImageSize,
convolutionKernelSizes = list( c( 3, 3 ), c( 5, 5 ) ),
numberOfEncodingLayers = 2,
numberOfFilters = 64 )
{
inputs <- layer_input( shape = inputImageSize )
outputs <- inputs
encodingConvolutionLayers <- list()
for( i in 1:numberOfEncodingLayers )
{
if( i == 1 )
{
encodingConvolutionLayers[[i]] <- inputs %>%
layer_conv_2d( filters = numberOfFilters, activation = 'relu',
kernel_size = convolutionKernelSizes[[1]], padding = 'same' )
} else {
encodingConvolutionLayers[[i]] <- encodingConvolutionLayers[[i-1]] %>%
layer_conv_2d( filters = numberOfFilters, activation = 'relu',
kernel_size = convolutionKernelSizes[[1]], padding = 'same' )
}
}
outputs <- encodingConvolutionLayers[[length( encodingConvolutionLayers )]]
for( i in 1:numberOfEncodingLayers )
{
index <- length( encodingConvolutionLayers ) - i + 1
deconvolution <- outputs %>%
layer_conv_2d_transpose( filters = numberOfFilters,
kernel_size = convolutionKernelSizes[[1]],
padding = 'same' )
outputs <- layer_add(
list( encodingConvolutionLayers[[index]], deconvolution ) )
}
numberOfChannels <- tail( inputImageSize, 1 )
outputs <- outputs %>% layer_conv_2d( filters = numberOfChannels,
kernel_size = convolutionKernelSizes[[2]], activation = "linear",
padding = 'same' )
srModel <- keras_model( inputs = inputs, outputs = outputs )
return( srModel )
}
#' 3-D implementation of the denoising autoencoder image super resolution architecture.
#'
#' @param inputImageSize Used for specifying the input tensor shape. The
#' shape (or dimension) of that tensor is the image dimensions followed by
#' the number of channels (e.g., red, green, and blue). The batch size
#' (i.e., number of training images) is not specified a priori.
#' @param convolutionKernelSizes a 2-element list of 3-D vectors specifying the
#' kernel size at each convolution layer. The first element is the kernel size
#' of the encoding layers and the 2nd element is the kernel size of the final
#' convolution layer.
#' @param numberOfEncodingLayers the number of encoding layers.
#' @param numberOfFilters the number of filters for each encoding layer.
#'
#' @return a keras model for image super resolution
#' @author Tustison NJ
#' @examples
#' createDenoisingAutoEncoderSuperResolutionModel3D(c( 28, 28, 28, 1 ))
#' gc()
#' @import keras
#' @export
createDenoisingAutoEncoderSuperResolutionModel3D <- function( inputImageSize,
convolutionKernelSizes = list( c( 3, 3, 3 ), c( 5, 5, 5 ) ),
numberOfEncodingLayers = 2,
numberOfFilters = 64 )
{
inputs <- layer_input( shape = inputImageSize )
outputs <- inputs
encodingConvolutionLayers <- list()
for( i in 1:numberOfEncodingLayers )
{
if( i == 1 )
{
encodingConvolutionLayers[[i]] <- inputs %>%
layer_conv_3d( filters = numberOfFilters, activation = 'relu',
kernel_size = convolutionKernelSizes[[1]], padding = 'same' )
} else {
encodingConvolutionLayers[[i]] <- encodingConvolutionLayers[[i-1]] %>%
layer_conv_3d( filters = numberOfFilters, activation = 'relu',
kernel_size = convolutionKernelSizes[[1]], padding = 'same' )
}
}
outputs <- encodingConvolutionLayers[[length( encodingConvolutionLayers )]]
for( i in 1:numberOfEncodingLayers )
{
index <- length( encodingConvolutionLayers ) - i + 1
deconvolution <- outputs %>%
layer_conv_3d_transpose( filters = numberOfFilters,
kernel_size = convolutionKernelSizes[[1]],
padding = 'same' )
outputs <- layer_add(
list( encodingConvolutionLayers[[index]], deconvolution ) )
}
numberOfChannels <- tail( inputImageSize, 1 )
outputs <- outputs %>% layer_conv_3d( filters = numberOfChannels,
kernel_size = convolutionKernelSizes[[2]], activation = "linear",
padding = 'same' )
srModel <- keras_model( inputs = inputs, outputs = outputs )
return( srModel )
}