/
vision_data.R
229 lines (195 loc) · 4.89 KB
/
vision_data.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
#' @title Get_grid
#'
#' @description Return a grid of `n` axes, `rows` by `cols`
#'
#'
#' @param n n
#' @param nrows number of rows
#' @param ncols number of columns
#' @param add_vert add vertical
#' @param figsize figure size
#' @param double double
#' @param title title
#' @param return_fig return figure or not
#' @param imsize image size
#' @return None
#' @export
get_grid <- function(n, nrows = NULL, ncols = NULL, add_vert = 0,
figsize = NULL, double = FALSE, title = NULL,
return_fig = FALSE, imsize = 3) {
args = list(
n = as.integer(n),
nrows = nrows,
ncols = ncols,
add_vert = as.integer(add_vert),
figsize = figsize,
double = double,
title = title,
return_fig = return_fig,
imsize = as.integer(imsize)
)
if(!is.null(nrows)) {
args$nrows <- as.integer(args$nrows)
}
if(!is.null(ncols)) {
args$ncols <- as.integer(args$ncols)
}
do.call(vision()$all$get_grid, args)
}
#' @title Clip_remove_empty
#'
#' @description Clip bounding boxes with image border and label background the empty ones
#'
#'
#' @param bbox bbox
#' @param label label
#' @return None
#' @export
clip_remove_empty <- function(bbox, label) {
vision()$all$clip_remove_empty(
bbox = bbox,
label = label
)
}
#' @title Bb_pad
#'
#' @description Function that collect `samples` of labelled bboxes and adds padding with `pad_idx`.
#'
#'
#' @param samples samples
#' @param pad_idx pad index
#' @return None
#' @export
bb_pad <- function(samples, pad_idx = 0) {
vision()$all$bb_pad(
samples = samples,
pad_idx = as.integer(pad_idx)
)
}
#' @title PointBlock
#'
#' @description A `TransformBlock` for points in an image
#'
#'
#' @return None
#' @export
PointBlock <- function() {
invisible(vision()$all$PointBlock)
}
#' @title BBoxBlock
#'
#' @description A `TransformBlock` for bounding boxes in an image
#'
#'
#' @return None
#' @export
BBoxBlock <- function() {
invisible(vision()$all$BBoxBlock)
}
#' @title BBoxLblBlock
#'
#' @description A `TransformBlock` for labeled bounding boxes, potentially with `vocab`
#'
#' @return None'
#' @param vocab vocabulary
#' @param add_na add NA
#'
#' @examples
#'
#' \dontrun{
#'
#' URLs_COCO_TINY()
#'
#' c(images, lbl_bbox) %<-% get_annotations('coco_tiny/train.json')
#' timg = Transform(ImageBW_create)
#' idx = 49
#' c(coco_fn,bbox) %<-% list(paste('coco_tiny/train',images[[idx]],sep = '/'),
#' lbl_bbox[[idx]])
#' coco_img = timg(coco_fn)
#'
#' tbbox = LabeledBBox(TensorBBox(bbox[[1]]), bbox[[2]])
#'
#' coco_bb = function(x) {
#' TensorBBox_create(bbox[[1]])
#' }
#'
#' coco_lbl = function(x) {
#' bbox[[2]]
#' }
#'
#' coco_dsrc = Datasets(c(rep(coco_fn,10)),
#' list(Image_create(), list(coco_bb),
#' list( coco_lbl, MultiCategorize(add_na = TRUE) )
#' ), n_inp = 1)
#'
#' coco_tdl = TfmdDL(coco_dsrc, bs = 9,
#' after_item = list(BBoxLabeler(), PointScaler(),
#' ToTensor()),
#' after_batch = list(IntToFloatTensor(), aug_transforms())
#' )
#'
#' coco_tdl %>% show_batch(dpi = 200)
#'
#' }
#'
#' @export
BBoxLblBlock <- function(vocab = NULL, add_na = TRUE) {
vision()$all$BBoxLblBlock(
vocab = vocab,
add_na = add_na
)
}
#' @title SegmentationDataLoaders_from_label_func
#'
#' @description Create from list of `fnames` in `path`s with `label_func`.
#'
#' @param path path
#' @param fnames file names
#' @param label_func label function
#' @param valid_pct validation percentage
#' @param seed seed
#' @param codes codes
#' @param item_tfms item transformations
#' @param batch_tfms batch transformations
#' @param bs batch size
#' @param val_bs validation batch size
#' @param shuffle_train shuffle train
#' @param device device name
#' @return None
#' @export
SegmentationDataLoaders_from_label_func <- function(path, fnames, label_func, valid_pct = 0.2,
seed = NULL, codes = NULL, item_tfms = NULL,
batch_tfms = NULL, bs = 64, val_bs = NULL,
shuffle_train = TRUE, device = NULL) {
args = list(
path = path,
fnames = fnames,
label_func = label_func,
valid_pct = valid_pct,
seed = seed,
codes = codes,
item_tfms = item_tfms,
batch_tfms = batch_tfms,
bs = as.integer(bs),
val_bs = val_bs,
shuffle_train = shuffle_train,
device = device
)
strings = c('codes', 'item_tfms',
'batch_tfms', 'device')
for(i in 1:length(strings)) {
if(is.null(args[[strings[i]]]))
args[[strings[i]]] <- NULL
}
if(is.null(args$seed)) {
args$seed <- NULL
} else {
args$seed <- as.integer(args$seed)
}
if(is.null(args$val_bs)) {
args$val_bs <- NULL
} else {
args$val_bs <- as.integer(args$val_bs)
}
do.call(vision()$all$SegmentationDataLoaders$from_label_func, args)
}