/
callbacks.R
393 lines (323 loc) · 8.37 KB
/
callbacks.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
#' @title WandbCallback
#'
#' @description Saves model topology, losses & metrics
#'
#'
#' @param log "gradients" (default), "parameters", "all" or None. Losses & metrics are always logged.
#' @param log_preds whether we want to log prediction samples (default to True).
#' @param log_model whether we want to log our model (default to True). This also requires SaveModelCallback.
#' @param log_dataset Options:
#' - False (default)
#' - True will log folder referenced by learn.dls.path.
#' - a path can be defined explicitly to reference which folder to log.
#' Note: subfolder "models" is always ignored.
#' @param dataset_name name of logged dataset (default to folder name).
#' @param valid_dl DataLoaders containing items used for prediction samples (default to random items from learn.dls.valid.
#' @param n_preds number of logged predictions (default to 36).
#' @param seed used for defining random samples.
#' @param reorder reorder or not
#' @return None
#' @export
WandbCallback <- function(log = "gradients", log_preds = TRUE, log_model = TRUE,
log_dataset = FALSE, dataset_name = NULL, valid_dl = NULL,
n_preds = 36, seed = 12345, reorder = TRUE) {
args <- list(
log = log,
log_preds = log_preds,
log_model = log_model,
log_dataset = log_dataset,
dataset_name = dataset_name,
valid_dl = valid_dl,
n_preds = as.integer(n_preds),
seed = as.integer(seed),
reorder = reorder
)
if(is.null(args$dataset_name))
args$dataset_name <- NULL
if(is.null(args$valid_dl))
args$valid_dl <- NULL
do.call(fastai2$callback$wandb()$WandbCallback, args)
}
#' @title Wandb login
#'
#' @description Log in to W&B.
#'
#'
#' @param anonymous must,never,allow,false,true
#' @param key API key (secret)
#' @param relogin relogin or not
#' @param host host address
#' @param force whether to force a user to be logged into wandb when running a script
#'
#' @return None
#'
#'
#' @export
login <- function(anonymous = NULL, key = NULL, relogin = NULL, host = NULL, force = NULL) {
args = list(
anonymous = anonymous,
key = key,
relogin = relogin,
host = host,
force = force
)
if(is.null(args$anonymous))
args$anonymous <- NULL
if(is.null(args$key))
args$key <- NULL
if(is.null(args$relogin))
args$relogin <- NULL
if(is.null(args$host))
args$host <- NULL
if(is.null(args$force))
args$force <- NULL
do.call(wandb()$login, args)
}
#' @title Wandb init
#'
#' @description Initialize a wandb Run.
#'
#'
#' @param ... parameters to pass
#'
#' @return wandb Run object
#' @section see https://docs.wandb.com/library/init
#' @return None
#' @export
init <- function(...) {
wandb()$init(
...
)
}
#' @title CSVLogger
#'
#' @description Basic class handling tweaks of the training loop by changing a `Learner` in various events
#'
#' @param fname file name
#' @param append append or not
#' @return None
#'
#' @examples
#'
#' \dontrun{
#'
#' URLs_MNIST_SAMPLE()
#' # transformations
#' tfms = aug_transforms(do_flip = FALSE)
#' path = 'mnist_sample'
#' bs = 20
#'
#' #load into memory
#' data = ImageDataLoaders_from_folder(path, batch_tfms = tfms, size = 26, bs = bs)
#'
#'
#' learn = cnn_learner(data, resnet18(), metrics = accuracy, path = getwd())
#'
#' learn %>% fit_one_cycle(2, cbs = CSVLogger())
#'
#' }
#'
#' @export
CSVLogger <- function(fname = "history.csv", append = FALSE) {
fastai2$callback$all$CSVLogger(
fname = fname,
append = append
)
}
#' @title CudaCallback
#'
#' @description Move data to CUDA device
#'
#'
#' @param device device name
#' @return None
#' @export
CudaCallback <- function(device = NULL) {
if(is.null(device)) {
fastai2$callback$all$CudaCallback
} else {
fastai2$callback$all$CudaCallback(
device = device
)
}
}
#' @title HookCallback
#'
#' @description `Callback` that can be used to register hooks on `modules`
#'
#'
#' @param modules the modules
#' @param every int, every epoch
#' @param remove_end logical, remove_end
#' @param is_forward logical, is_forward
#' @param detach detach
#' @param cpu to cpu or not
#' @return None
#' @export
HookCallback <- function(modules = NULL, every = NULL, remove_end = TRUE,
is_forward = TRUE, detach = TRUE, cpu = TRUE) {
args = list(
modules = modules,
every = every,
remove_end = remove_end,
is_forward = is_forward,
detach = detach,
cpu = cpu
)
if(is.null(args$modules))
args$modules <- NULL
if(is.null(args$every))
args$every <- NULL
do.call(fastai2$callback$all$HookCallback, args)
}
#' @title TrackerCallback
#'
#' @description A `Callback` that keeps track of the best value in `monitor`.
#'
#'
#' @param monitor monitor the loss
#' @param comp comp
#' @param min_delta minimum delta
#' @return None
#' @export
TrackerCallback <- function(monitor = "valid_loss", comp = NULL, min_delta = 0.0) {
args = list(
monitor = monitor,
comp = comp,
min_delta = min_delta
)
if(is.null(args$comp))
args$comp <- NULL
do.call(fastai2$callback$all$TrackerCallback, args)
}
#' @title CollectDataCallback
#'
#' @param ... parameters to pass
#' @return None
#' @export
CollectDataCallback <- function(...) {
fastai2$callback$all$CollectDataCallback(...)
}
#' @title ReduceLROnPlateau
#'
#' @param ... parameters to pass
#' @return None
#'
#' @examples
#'
#' \dontrun{
#'
#' URLs_MNIST_SAMPLE()
#' # transformations
#' tfms = aug_transforms(do_flip = FALSE)
#' path = 'mnist_sample'
#' bs = 20
#'
#' #load into memory
#' data = ImageDataLoaders_from_folder(path, batch_tfms = tfms, size = 26, bs = bs)
#'
#'
#' learn = cnn_learner(data, resnet18(), metrics = accuracy, path = getwd())
#'
#' learn %>% fit_one_cycle(10, 1e-2, cbs = ReduceLROnPlateau(monitor='valid_loss', patience = 1))
#'
#' }
#'
#' @export
ReduceLROnPlateau <- function(...) {
fastai2$callback$all$ReduceLROnPlateau(...)
}
#' @title FetchPredsCallback
#'
#' @description A callback to fetch predictions during the training loop
#'
#' @param ds_idx dataset index
#' @param dl DL application
#' @param with_input with input or not
#' @param with_decoded with decoded or not
#' @param cbs callbacks
#' @param reorder reorder or not
#' @return None
#' @export
FetchPredsCallback <- function(ds_idx = 1, dl = NULL, with_input = FALSE,
with_decoded = FALSE, cbs = NULL, reorder = TRUE) {
args = list(
ds_idx = as.integer(ds_idx),
dl = dl,
with_input = with_input,
with_decoded = with_decoded,
cbs = cbs,
reorder = reorder
)
if(is.null(args$dl))
args$dl <- NULL
if(is.null(args$cbs))
args$cbs <- NULL
do.call(fastai2$callback$all$FetchPredsCallback, args)
}
#' @title SaveModelCallback
#' @param ... parameters to pass
#' @return None
#' @export
SaveModelCallback <- function(...) {
fastai2$callback$all$SaveModelCallback(...)
}
#' @title ShowGraphCallback
#' @param ... parameters to pass
#' @return None
#' @export
ShowGraphCallback <- function(...) {
fastai2$callback$all$ShowGraphCallback(...)
}
#' @title TrainEvalCallback
#' @param ... parameters to pass
#' @return None
#'
#'
#' @export
TrainEvalCallback <- function(...) {
fastai2$callback$all$TrainEvalCallback(...)
}
#' @title GatherPredsCallback
#'
#' @description `Callback` that saves the predictions and targets, optionally `with_loss`
#'
#' @param with_input include inputs or not
#' @param with_loss include loss or not
#' @param save_preds save predictions
#' @param save_targs save targets/actuals
#' @param concat_dim concatenate dimensions
#' @return None
#' @export
GatherPredsCallback <- function(with_input = FALSE, with_loss = FALSE,
save_preds = NULL, save_targs = NULL, concat_dim = 0) {
args = list(
with_input = with_input,
with_loss = with_loss,
save_preds = save_preds,
save_targs = save_targs,
concat_dim = as.integer(concat_dim)
)
if(is.null(args$save_preds))
args$save_preds <- NULL
if(is.null(args$save_targs))
args$save_targs <- NULL
do.call(fastai2$callback$all$GatherPredsCallback, args)
}
#' @title EarlyStoppingCallback
#'
#' @param ... parameters to pass
#' @return None
#' @export
EarlyStoppingCallback <- function(...) {
fastai2$callback$all$EarlyStoppingCallback(...)
}
#' @title TerminateOnNaNCallback
#'
#' @param ... parameters to pass
#' @return None
#'
#' @export
TerminateOnNaNCallback <- function(...) {
fastai2$callback$all$TerminateOnNaNCallback(...)
}