-
Notifications
You must be signed in to change notification settings - Fork 18
/
randomImageTransformBatchGenerator.R
418 lines (361 loc) · 12.8 KB
/
randomImageTransformBatchGenerator.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
#'
#' Random image transformation batch generator
#'
#' This R6 class can be used to generate affine and other transformations of an
#' input image predictor and outcome population. It currently works for single
#' predictor and single outcome modalities but will be extended in the future.
#' The class calls \code{\link{randomImageTransformAugmentation}}.
#'
#' @section Usage:
#' \preformatted{
#' bgen = randomImageTransformBatchGenerator$new( ... )
#'
#' bgen$generate( batchSize = 32L )
#'
#' }
#'
#' @section Arguments:
#' \code{imageList} List of lists where the embedded list contains k images.
#'
#' \code{outcomeImageList} List of outcome images.
#'
#' \code{transformType} random transform type to generate;
#' one of the following options
#' \code{c("Translation","Rigid","ScaleShear","Affine","Deformation",
#' "AffineAndDeformation")}
#'
#' \code{imageDomain} defines the spatial domain for all images.
#' NOTE: if the input images do not match the spatial domain of the domain
#' image, we internally resample the target to the domain. This may have
#' unexpected consequences if you are not aware of this.
#' This operation will test
#' \code{antsImagePhysicalSpaceConsistency} then call
#' \code{resampleImageToTarget} upon failure.
#'
#' \code{sdAffine} roughly controls deviation from identity matrix
#'
#' \code{nControlPoints} number of control points for simulated deformation
#'
#' \code{spatialSmoothing} spatial smoothing for simulated deformation
#'
#' \code{toCategorical} boolean vector denoting whether the outcome class is categorical or not
#' \code{imageDomainY} optional domain for outcome images
#' \code{normalization} optional intensity normalization either none, standardize or 01
#'
#' @section Methods:
#' \code{$new()} Initialize the class in default empty or filled form.
#'
#' \code{$generate} generate the batch of samples with given batch size
#'
#' @name randomImageTransformBatchGenerator
#' @seealso \code{\link{randomImageTransformAugmentation}}
#' @examples
#'
#' library( ANTsR )
#' i1 = antsImageRead( getANTsRData( "r16" ) )
#' i2 = antsImageRead( getANTsRData( "r64" ) )
#' s1 = thresholdImage( i1, "Otsu", 3 )
#' s2 = thresholdImage( i2, "Otsu", 3 )
#' # see ANTsRNet randomImageTransformAugmentation
#' predictors = list( list(i1), list(i2), list(i1), list(i2) )
#' outcomes = list( s1, s2, s1, s2 )
#' trainingData <- randomImageTransformBatchGenerator$new(
#' imageList = predictors,
#' outcomeImageList = outcomes,
#' transformType = "Affine",
#' imageDomain = i1,
#' toCategorical = TRUE
#' )
#' testBatchGenFunction = trainingData$generate( 2 )
#' myout = testBatchGenFunction( )
#'
NULL
#' @importFrom R6 R6Class
#' @importFrom keras to_categorical
#' @export
randomImageTransformBatchGenerator <- R6::R6Class(
"randomImageTransformBatchGenerator",
public = list(
imageList = NULL,
outcomeImageList = NULL,
transformType = NULL,
imageDomain = NULL,
sdAffine = 1,
nControlPoints = 100,
spatialSmoothing = 3,
toCategorical = FALSE,
imageDomainY = NULL,
normalization = "none",
initialize = function( imageList = NULL, outcomeImageList = NULL,
transformType = NULL, imageDomain = NULL, sdAffine = 1,
nControlPoints = 100, spatialSmoothing = 3, toCategorical = FALSE,
imageDomainY = NULL, normalization = "none" )
{
self$sdAffine <- sdAffine
self$nControlPoints <- nControlPoints
self$spatialSmoothing <- spatialSmoothing
self$toCategorical <- toCategorical
self$normalization <- normalization
if( !usePkg( "ANTsR" ) )
{
stop( "Please install the ANTsR package." )
}
if( !is.null( imageList ) )
{
self$imageList <- imageList
} else {
stop( "Input feature images must be specified." )
}
if( !is.null( outcomeImageList ) )
{
self$outcomeImageList <- outcomeImageList
} else {
stop( "Input outcome images must be specified." )
}
if( !is.null( transformType ) )
{
self$transformType <- transformType
} else {
stop( "Input transform type must be specified." )
}
if( is.null( imageDomain ) )
{
self$imageDomain <- imageList
} else {
self$imageDomain <- imageDomain
}
if( is.null( imageDomainY ) )
{
self$imageDomainY <- self$imageDomain
} else {
self$imageDomainY <- imageDomainY
}
},
generate = function( batchSize = 32L )
{
currentPassCount <- 1L
function()
{
randITX = randomImageTransformAugmentation(
self$imageDomain,
self$imageList,
self$outcomeImageList,
n = batchSize, # generates n random samples from the inputs
typeOfTransform = self$transformType,
interpolator = c("linear", "nearestNeighbor"),
sdAffine = self$sdAffine,
nControlPoints = self$nControlPoints,
spatialSmoothing = self$spatialSmoothing,
composeToField = FALSE,
imageDomainY = self$imageDomainY,
normalization = self$normalization )
gc()
imageSize <- dim( randITX$outputPredictorList[[1]][[1]] )
imageSizeY <- dim( self$imageDomainY )
imageDim = length( imageSize )
nChannels = length( self$imageList[[1]] ) # FIXME make work for multiple input features and multiple output features
nChannelsY = 1
xdims = c( batchSize, imageSize, nChannels )
ydims = c( batchSize, imageSizeY )
if ( ! self$toCategorical[ 1 ] )
ydims = c( batchSize, imageSizeY, nChannelsY )
batchX <- array( data = 0, dim = xdims )
batchY <- array( data = 0, dim = ydims )
currentPassCount <<- currentPassCount + batchSize
for( i in seq_len( batchSize ) )
{
# FIXME - make this work for multiple feature inputs
# and multiple target outputs
for ( chan in 1:nChannels ) {
warpedArrayX <- as.array( randITX$outputPredictorList[[i]][[chan]] )
warpedArrayY <- as.array( randITX$outputOutcomeList[[i]] )
if ( imageDim == 3 ) {
batchX[i,,,, chan] <- warpedArrayX # FIXME make work for multiple channels
if ( ! self$toCategorical[ 1 ] ) batchY[i,,,,1] <- warpedArrayY else batchY[i,,,] <- warpedArrayY
}
if ( imageDim == 2 ) {
batchX[i,,,chan] <- warpedArrayX # FIXME make work for multiple channels
if ( ! self$toCategorical[ 1 ] ) batchY[i,,,1] <- warpedArrayY else batchY[i,,] <- warpedArrayY
}
}
}
if ( self$toCategorical[ 1 ] ) {
segmentationLabels <- sort( unique( as.vector( batchY ) ) )
outlist = list( batchX, encodeUnet( batchY, segmentationLabels ) )
return( outlist )
} else {
return( list( batchX, batchY ) )
}
}
}
)
)
#
#'
#' Random image transform parameters batch generator
#'
#' This R6 class can be used to generate parameters to affine and other
#' transformations applied to an input image population.
#' The class calls \code{\link{randomImageTransformParametersAugmentation}}.
#'
#' @section Usage:
#' \preformatted{
#' bgen = randomImageTransformParametersBatchGenerator$new( ... )
#'
#' bgen$generate( batchSize = 32L )
#'
#' }
#'
#' @section Arguments:
#' \code{imageDomain} defines the spatial domain for all images.
#' \code{imageList} List contains k images.
#'
#' \code{transformType} random transform type to generate;
#' one of the following options
#' \code{c("Translation","Rigid","ScaleShear","Affine","DeformationBasis" ) }
#'
#' NOTE: if the input images do not match the spatial domain of the domain
#' image, we internally resample the target to the domain. This may have
#' unexpected consequences if you are not aware of this.
#' This operation will test
#' \code{antsImagePhysicalSpaceConsistency} then call
#' \code{resampleImageToTarget} upon failure.
#'
#' \code{spatialSmoothing} spatial smoothing for simulated deformation
#'
#' \code{numberOfCompositions} number of compositions
#'
#' \code{deformationBasis} list of basis deformations
#' \code{txParamMeans} vector of basis deformations means
#' \code{txParamSDs} vector of basis deformations sds
#' \code{center} center the parameters before passing to Y
#'
#' @section Methods:
#' \code{$new()} Initialize the class in default empty or filled form.
#'
#' \code{$generate} generate the batch of samples with given batch size
#'
#' @name randomImageTransformParametersBatchGenerator
#' @seealso \code{\link{randomImageTransformParametersAugmentation}}
#' @examples
#'
#' library( ANTsR )
#' i1 = antsImageRead( getANTsRData( "r16" ) )
#' i2 = antsImageRead( getANTsRData( "r64" ) )
#' s1 = thresholdImage( i1, "Otsu", 3 )
#' s2 = thresholdImage( i2, "Otsu", 3 )
#' # see ANTsRNet randomImageTransformAugmentation
#' predictors = list( i1, i2, i2, i1 )
#' trainingData <- randomImageTransformParametersBatchGenerator$new(
#' imageList = predictors,
#' transformType = "Affine",
#' imageDomain = i1, txParamMeans=c(1,0,0,1,0,0), txParamSDs=diag(6)*0.01
#' )
#' testBatchGenFunction = trainingData$generate( 2 )
#' myout = testBatchGenFunction( )
#'
NULL
#' @importFrom R6 R6Class
#' @export
randomImageTransformParametersBatchGenerator <- R6::R6Class(
"randomImageTransformParametersBatchGenerator",
public = list(
imageDomain = NULL,
imageList = NULL,
transformType = NULL,
spatialSmoothing = 3,
numberOfCompositions = 4,
deformationBasis = NULL,
txParamMeans = NULL,
txParamSDs = NULL,
center = FALSE,
initialize = function(
imageDomain = NULL,
imageList = NULL,
transformType = NULL,
spatialSmoothing = 3,
numberOfCompositions = 4,
deformationBasis = NULL,
txParamMeans = NULL,
txParamSDs = NULL,
center = FALSE
)
{
self$imageDomain <- imageDomain
self$imageList <- imageList
self$transformType <- transformType
self$spatialSmoothing <- spatialSmoothing
self$numberOfCompositions <- numberOfCompositions
self$deformationBasis <- deformationBasis
self$txParamMeans <- txParamMeans
self$txParamSDs <- txParamSDs
self$center <- center
if( !usePkg( "ANTsR" ) )
{
stop( "Please install the ANTsR package." )
}
if( !is.null( imageList ) )
{
self$imageList <- imageList
} else {
stop( "Input feature images must be specified." )
}
if( !is.null( transformType ) )
{
self$transformType <- transformType
} else {
stop( "Input transform type must be specified." )
}
if( is.null( imageDomain ) )
{
self$imageDomain <- imageList
} else {
self$imageDomain <- imageDomain
}
},
generate = function( batchSize = 8L )
{
currentPassCount <- 1L
function()
{
randITX = randomImageTransformParametersAugmentation(
self$imageDomain,
self$imageList,
n = batchSize, # generates n random samples from the inputs
typeOfTransform = self$transformType,
interpolator = 'linear',
spatialSmoothing = self$spatialSmoothing,
numberOfCompositions = self$numberOfCompositions,
deformationBasis = self$deformationBasis,
txParamMeans = self$txParamMeans,
txParamSDs = self$txParamSDs,
center = self$center )
gc()
imageSize <- dim( randITX$outputPredictorList[[1]] )
paramsSize = length( randITX$outputParameterList[[1]] )
imageDim = length( imageSize )
nChannels = 1 # FIXME make work for multiple input features and multiple output features
xdims = c( batchSize, imageSize, nChannels )
ydims = c( batchSize, paramsSize )
batchX <- array( data = 0, dim = xdims )
batchY <- array( data = 0, dim = ydims )
currentPassCount <<- currentPassCount + batchSize
for( i in seq_len( batchSize ) )
{
warpedArrayX <- as.array( randITX$outputPredictorList[[i]] )
# warpedArrayX <- ( warpedArrayX - min( as.vector( warpedArrayX ) ) ) /
# ( max( as.vector( warpedArrayX ) ) - min( as.vector( warpedArrayX ) ) )
if ( imageDim == 3 ) {
batchX[i,,,, 1] <- warpedArrayX # FIXME make work for multiple channels
}
if ( imageDim == 2 ) {
batchX[i,,,1] <- warpedArrayX # FIXME make work for multiple channels
# batchX[i,,,1] <- warpedArrayX - as.array( self$imageDomain )# FIXME make work for multiple channels
}
batchY[i,] <- randITX$outputParameterList[[i]]
}
return( list( batchX, batchY ) )
}
}
)
)