-
Notifications
You must be signed in to change notification settings - Fork 18
/
createSimpleClassificationWithSpatialTransformerNetworkModel.R
216 lines (189 loc) · 8.07 KB
/
createSimpleClassificationWithSpatialTransformerNetworkModel.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
#' 2-D implementation of the spatial transformer network.
#'
#' Creates a keras model of the spatial transformer network:
#'
#' \url{https://arxiv.org/abs/1506.02025}
#'
#' based on the following python Keras model:
#'
#' \url{https://github.com/oarriaga/STN.keras/blob/master/src/models/STN.py}
#'
#' @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 resampledSize resampled size of the transformed input images.
#' @param numberOfClassificationLabels Number of classes.
#'
#' @return a keras model
#' @author Tustison NJ
#' @examples
#'
#' library( ANTsRNet )
#' library( keras )
#'
#' mnistData <- dataset_mnist()
#' numberOfLabels <- 10
#'
#' # Extract a small subset for something that can run quickly
#'
#' X_trainSmall <- mnistData$train$x[1:100,,]
#' X_trainSmall <- array( data = X_trainSmall, dim = c( dim( X_trainSmall ), 1 ) )
#' Y_trainSmall <- to_categorical( mnistData$train$y[1:100], numberOfLabels )
#'
#' X_testSmall <- mnistData$test$x[1:10,,]
#' X_testSmall <- array( data = X_testSmall, dim = c( dim( X_testSmall ), 1 ) )
#' Y_testSmall <- to_categorical( mnistData$test$y[1:10], numberOfLabels )
#'
#' # We add a dimension of 1 to specify the channel size
#'
#' inputImageSize <- c( dim( X_trainSmall )[2:3], 1 )
#'
#' \dontrun{
#' model <- createSimpleClassificationWithSpatialTransformerNetworkModel2D(
#' inputImageSize = inputImageSize,
#' resampledSize = c( 30, 30 ), numberOfClassificationLabels = numberOfLabels )
#' }
#' @import keras
#' @export
createSimpleClassificationWithSpatialTransformerNetworkModel2D <- function( inputImageSize,
resampledSize = c( 30, 30 ), numberOfClassificationLabels = 10 )
{
getInitialWeights2D <- function( outputSize )
{
np <- reticulate::import( "numpy" )
b <- np$zeros( c( 2L, 3L ), dtype = "float32" )
b[1, 1] <- 1
b[2, 2] <- 1
W <- np$zeros( c( as.integer( outputSize ), 6L ), dtype = 'float32' )
# Layer weights in R keras are stored as lists of length 2 (W & b)
weights <- list()
weights[[1]] <- W
weights[[2]] <- as.array( as.vector( t( b ) ) )
return( weights )
}
inputs <- layer_input( shape = inputImageSize )
localization <- inputs
localization <- localization %>% layer_max_pooling_2d( pool_size = c( 2, 2 ) )
localization <- localization %>% layer_conv_2d( filters = 20, kernel_size = c( 5, 5 ) )
localization <- localization %>% layer_max_pooling_2d( pool_size = c( 2, 2 ) )
localization <- localization %>% layer_conv_2d( filters = 20, kernel_size = c( 5, 5 ) )
localization <- localization %>% layer_flatten()
localization <- localization %>% layer_dense( units = 50L )
localization <- localization %>% layer_activation( 'relu' )
weights <- getInitialWeights2D( outputSize = 50L )
localization <- localization %>% layer_dense( units = 6L, weights = weights )
outputs <- layer_spatial_transformer_2d( list( inputs, localization ),
resampledSize, transformType = 'affine', interpolatorType = 'linear',
name = "layer_spatial_transformer" )
outputs <- outputs %>%
layer_conv_2d( filters = 32L, kernel_size = c( 3, 3 ), padding = 'same' )
outputs <- outputs %>% layer_activation( activation = "relu" )
outputs <- outputs %>% layer_max_pooling_2d( pool_size = c( 2, 2 ) )
outputs <- outputs %>% layer_conv_2d( filters = 32L, kernel_size = c( 3, 3 ) )
outputs <- outputs %>% layer_activation( activation = "relu" )
outputs <- outputs %>% layer_max_pooling_2d( pool_size = c( 2, 2 ) )
outputs <- outputs %>% layer_flatten()
outputs <- outputs %>% layer_dense( units = 256L )
outputs <- outputs %>% layer_activation( activation = "relu" )
outputs <- outputs %>% layer_dense( units = numberOfClassificationLabels )
outputs <- outputs %>% layer_activation('softmax')
stnModel <- keras_model( inputs = inputs, outputs = outputs )
return( stnModel )
}
#' 3-D implementation of the spatial transformer network.
#'
#' Creates a keras model of the spatial transformer network:
#'
#' \url{https://arxiv.org/abs/1506.02025}
#'
#' based on the following python Keras model:
#'
#' \url{https://github.com/oarriaga/STN.keras/blob/master/src/models/STN.py}
#'
#' @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 resampledSize resampled size of the transformed input images.
#' @param numberOfClassificationLabels Number of classes.
#'
#' @return a keras model
#' @author Tustison NJ
#' @examples
#'
#' \dontrun{
#'
#' library( ANTsRNet )
#' library( keras )
#'
#' mnistData <- dataset_mnist()
#' numberOfLabels <- 10
#'
#' # Extract a small subset for something that can run quickly
#'
#' X_trainSmall <- mnistData$train$x[1:100,,]
#' X_trainSmall <- array( data = X_trainSmall, dim = c( dim( X_trainSmall ), 1 ) )
#' Y_trainSmall <- to_categorical( mnistData$train$y[1:100], numberOfLabels )
#'
#' X_testSmall <- mnistData$test$x[1:10,,]
#' X_testSmall <- array( data = X_testSmall, dim = c( dim( X_testSmall ), 1 ) )
#' Y_testSmall <- to_categorical( mnistData$test$y[1:10], numberOfLabels )
#'
#' # We add a dimension of 1 to specify the channel size
#'
#' inputImageSize <- c( dim( X_trainSmall )[2:3], 1 )
#'
#' model <- createSimpleClassificationWithSpatialTransformerNetworkModel2D(
#' inputImageSize = inputImageSize,
#' resampledSize = c( 30, 30 ), numberOfClassificationLabels = numberOfLabels )
#'
#'}
#' @import keras
#' @export
createSimpleClassificationWithSpatialTransformerNetworkModel3D <- function( inputImageSize,
resampledSize = c( 30, 30, 30 ), numberOfClassificationLabels = 10 )
{
getInitialWeights3D <- function( outputSize )
{
np <- reticulate::import( "numpy" )
b <- np$zeros( c( 3L, 4L ), dtype = "float32" )
b[1, 1] <- 1
b[2, 2] <- 1
b[3, 3] <- 1
W <- np$zeros( c( as.integer( outputSize ), 12L ), dtype = 'float32' )
# Layer weights in R keras are stored as lists of length 2 (W & b)
weights <- list()
weights[[1]] <- W
weights[[2]] <- as.array( as.vector( t( b ) ) )
return( weights )
}
inputs <- layer_input( shape = inputImageSize )
localization <- inputs
localization <- localization %>% layer_max_pooling_3d( pool_size = c( 2, 2, 2 ) )
localization <- localization %>% layer_conv_3d( filters = 20, kernel_size = c( 5, 5, 5 ) )
localization <- localization %>% layer_max_pooling_3d( pool_size = c( 2, 2, 2 ) )
localization <- localization %>% layer_conv_3d( filters = 20, kernel_size = c( 5, 5, 5 ) )
localization <- localization %>% layer_flatten()
localization <- localization %>% layer_dense( units = 50L )
localization <- localization %>% layer_activation( 'relu' )
weights <- getInitialWeights3D( outputSize = 50L )
localization <- localization %>% layer_dense( units = 12L, weights = weights )
outputs <- layer_spatial_transformer_3d( list( inputs, localization ),
resampledSize, transformType = 'affine', interpolatorType = 'linear',
name = "layer_spatial_transformer" )
outputs <- outputs %>%
layer_conv_3d( filters = 32L, kernel_size = c( 3, 3, 3 ), padding = 'same' )
outputs <- outputs %>% layer_activation( activation = "relu" )
outputs <- outputs %>% layer_max_pooling_3d( pool_size = c( 2, 2, 2 ) )
outputs <- outputs %>% layer_conv_3d( filters = 32L, kernel_size = c( 3, 3, 3 ) )
outputs <- outputs %>% layer_activation( activation = "relu" )
outputs <- outputs %>% layer_max_pooling_3d( pool_size = c( 2, 2, 2 ) )
outputs <- outputs %>% layer_flatten()
outputs <- outputs %>% layer_dense( units = 256L )
outputs <- outputs %>% layer_activation( activation = "relu" )
outputs <- outputs %>% layer_dense( units = numberOfClassificationLabels )
outputs <- outputs %>% layer_activation_softmax()
stnModel <- keras_model( inputs = inputs, outputs = outputs )
return( stnModel )
}