-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcreateDeepDenoiseSuperResolutionModel.R
215 lines (182 loc) · 6.97 KB
/
createDeepDenoiseSuperResolutionModel.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
#' 2-D implementation of the deep denoise image super resolution architecture.
#'
#' Creates a keras model of the expanded image super resolution deep learning
#' framework based on the following python implementation:
#'
#' \url{https://github.com/titu1994/Image-Super-Resolution}
#'
#' @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 layers number of architecture layers.
#' @param lowestResolution number of filters at the beginning and end of
#' the architecture.
#' @param convolutionKernelSize 2-D vector defining the kernel size
#' during the encoding path
#' @param poolSize 2-D vector defining the region for each pooling layer.
#' @param strides 2-D vector describing the stride length in each direction.
#'
#' @return a keras model for image super resolution
#' @author Tustison NJ
#' @examples
#' \dontrun{
#' createDeepDenoiseSuperResolutionModel2D(c(256L, 256L, 3L))
#' }
#' @import keras
#' @export
createDeepDenoiseSuperResolutionModel2D <- function( inputImageSize,
layers = 2,
lowestResolution = 64,
convolutionKernelSize = c( 3, 3 ),
poolSize = c( 2, 2 ),
strides = c( 2, 2 ) )
{
inputs <- layer_input( shape = inputImageSize )
# encoding layers
encodingConvolutionLayers <- list()
for( i in 1:length( 1:layers ) )
{
numberOfFilters <- lowestResolution * 2 ^ ( i - 1 )
if( i == 1 )
{
conv <- inputs %>%
layer_conv_2d( filters = numberOfFilters,
kernel_size = convolutionKernelSize, activation = 'relu',
padding = 'same' )
} else {
conv <- pool %>%
layer_conv_2d( filters = numberOfFilters,
kernel_size = convolutionKernelSize, activation = 'relu',
padding = 'same' )
}
encodingConvolutionLayers[[i]] <- conv %>%
layer_conv_2d( filters = numberOfFilters,
kernel_size = convolutionKernelSize, activation = 'relu',
padding = 'same' )
pool <- encodingConvolutionLayers[[i]] %>%
layer_max_pooling_2d( pool_size = poolSize, strides = strides,
padding = 'same' )
}
numberOfFilters <- lowestResolution * 2 ^ ( layers )
outputs <- pool %>%
layer_conv_2d( filters = numberOfFilters,
kernel_size = convolutionKernelSize,
activation = 'relu', padding = 'same' )
# upsampling layers
for( i in 1:length( layers ) )
{
numberOfFilters <- lowestResolution * 2 ^ ( layers - i )
outputs <- outputs %>% layer_upsampling_2d()
conv <- outputs %>%
layer_conv_2d( filters = numberOfFilters,
kernel_size = convolutionKernelSize, activation = 'relu',
padding = 'same' )
conv <- conv %>%
layer_conv_2d( filters = numberOfFilters,
kernel_size = convolutionKernelSize, activation = 'relu',
padding = 'same' )
stop("Need to fix layer add")
outputs <- outputs %>%
layer_add( list( encodingConvolutionLayers[[layers - i + 1]], conv ))
outputs <- outputs %>% layer_upsampling_2d()
}
numberOfChannels <- tail( inputImageSize, 1 )
finalConvolutionKernelSize <- convolutionKernelSize + 2
outputs <- outputs %>% layer_conv_2d( filters = numberOfChannels,
kernel_size = finalConvolutionKernelSize, activation = "linear",
padding = 'same' )
srModel <- keras_model( inputs = inputs, outputs = outputs )
return( srModel )
}
#' 3-D implementation of the deep denoise image super resolution architecture.
#'
#' Creates a keras model of the expanded image super resolution deep learning
#' framework based on the following python implementation:
#'
#' \url{https://github.com/titu1994/Image-Super-Resolution}
#'
#' @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 layers number of architecture layers.
#' @param lowestResolution number of filters at the beginning and end of
#' the architecture.
#' @param convolutionKernelSize 3-D vector defining the kernel size
#' during the encoding path
#' @param poolSize 3-D vector defining the region for each pooling layer.
#' @param strides 3-D vector describing the stride length in each direction.
#'
#' @return a keras model for image super resolution
#' @author Tustison NJ
#' @examples
#' \dontrun{
#' createDeepDenoiseSuperResolutionModel3D(c(256L, 256L, 45L, 1L))
#' }
#' @import keras
#' @export
createDeepDenoiseSuperResolutionModel3D <- function( inputImageSize,
layers = 2,
lowestResolution = 64,
convolutionKernelSize = c( 3, 3, 3 ),
poolSize = c( 2, 2, 2 ),
strides = c( 2, 2, 2 ) )
{
inputs <- layer_input( shape = inputImageSize )
# encoding layers
encodingConvolutionLayers <- list()
for( i in 1:length( 1:layers ) )
{
numberOfFilters <- lowestResolution * 2 ^ ( i - 1 )
if( i == 1 )
{
conv <- inputs %>%
layer_conv_3d( filters = numberOfFilters,
kernel_size = convolutionKernelSize, activation = 'relu',
padding = 'same' )
} else {
conv <- pool %>%
layer_conv_3d( filters = numberOfFilters,
kernel_size = convolutionKernelSize, activation = 'relu',
padding = 'same' )
}
encodingConvolutionLayers[[i]] <- conv %>%
layer_conv_3d( filters = numberOfFilters,
kernel_size = convolutionKernelSize, activation = 'relu',
padding = 'same' )
pool <- encodingConvolutionLayers[[i]] %>%
layer_max_pooling_3d( pool_size = poolSize, strides = strides,
padding = 'same' )
}
numberOfFilters <- lowestResolution * 2 ^ ( layers )
outputs <- pool %>%
layer_conv_3d( filters = numberOfFilters,
kernel_size = convolutionKernelSize,
activation = 'relu', padding = 'same' )
# upsampling layers
for( i in 1:length( layers ) )
{
numberOfFilters <- lowestResolution * 2 ^ ( layers - i )
outputs <- outputs %>% layer_upsampling_3d()
conv <- outputs %>%
layer_conv_3d( filters = numberOfFilters,
kernel_size = convolutionKernelSize, activation = 'relu',
padding = 'same' )
conv <- conv %>%
layer_conv_3d( filters = numberOfFilters,
kernel_size = convolutionKernelSize, activation = 'relu',
padding = 'same' )
stop("Need to fix layer add")
outputs <- outputs %>%
layer_add( list( encodingConvolutionLayers[[layers - i + 1]], conv ))
outputs <- outputs %>% layer_upsampling_3d()
}
numberOfChannels <- tail( inputImageSize, 1 )
finalConvolutionKernelSize <- convolutionKernelSize + 2
outputs <- outputs %>% layer_conv_3d( filters = numberOfChannels,
kernel_size = finalConvolutionKernelSize, activation = "linear",
padding = 'same' )
srModel <- keras_model( inputs = inputs, outputs = outputs )
return( srModel )
}