-
Notifications
You must be signed in to change notification settings - Fork 8
/
README.Rmd
372 lines (302 loc) · 9.82 KB
/
README.Rmd
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
---
output:
md_document:
variant: markdown_github
params:
repo_name: platypus
repo_url: https://github.com/maju116/platypus
chagelog_url: https://github.com/maju116/platypus/blob/master/CHANGELOG.md
code_of_coduct: https://github.com/maju116/platypus/blob/develop/CODE_OF_CONDUCT.md
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
<img src="man/figures/hexsticker_platypus.png" align="right" alt="" width="130" />
# `r params$repo_name`
<!-- badges: start -->
[![codecov](https://codecov.io/gh/maju116/platypus/branch/master/graph/badge.svg)](https://codecov.io/gh/maju116/platypus)
<!-- badges: end -->
**R package for object detection and image segmentation**
With `platypus` it is easy create advanced computer vision models like YOLOv3 and U-Net in a few lines of code.
How to install?
---------------
You can install the latest version of `platypus` with `remotes`:
```{r, eval=FALSE}
remotes::install_github("maju116/platypus")
```
(`master` branch contains the stable version. Use `develop` branch for latest features)
To install [previous versions](`r params$changelog_url`) you can run:
```{r, eval=FALSE}
remotes::install_github("maju116/platypus", ref = "0.1.0")
```
In order to install `platypus` you need to install `keras` and `tensorflow` packages and `Tensorflow` version `>= 2.0.0` (`Tensorflow 1.x` will not be supported!)
YOLOv3 bounding box prediction with pre-trained COCO weights:
---------------
To create `YOLOv3` architecture use:
```{r, message = FALSE}
library(tidyverse)
library(platypus)
library(abind)
test_yolo <- yolo3(
net_h = 416, # Input image height. Must be divisible by 32
net_w = 416, # Input image width. Must be divisible by 32
grayscale = FALSE, # Should images be loaded as grayscale or RGB
n_class = 80, # Number of object classes (80 for COCO dataset)
anchors = coco_anchors # Anchor boxes
)
test_yolo
```
You can now load [YOLOv3 Darknet](https://pjreddie.com/darknet/yolo/) weights trained on [COCO dataset](https://cocodataset.org/#home). Download pre-trained weights from [here](https://pjreddie.com/media/files/yolov3.weights) and run:
```{r}
test_yolo %>% load_darknet_weights("development/yolov3.weights")
```
Calculate predictions for new images:
```{r}
test_img_paths <- list.files(system.file("extdata", "images", package = "platypus"), full.names = TRUE, pattern = "coco")
test_imgs <- test_img_paths %>%
map(~ {
image_load(., target_size = c(416, 416), grayscale = FALSE) %>%
image_to_array() %>%
`/`(255)
}) %>%
abind(along = 4) %>%
aperm(c(4, 1:3))
test_preds <- test_yolo %>% predict(test_imgs)
str(test_preds)
```
Transform raw predictions into bounding boxes:
```{r}
test_boxes <- get_boxes(
preds = test_preds, # Raw predictions form YOLOv3 model
anchors = coco_anchors, # Anchor boxes
labels = coco_labels, # Class labels
obj_threshold = 0.6, # Object threshold
nms = TRUE, # Should non-max suppression be applied
nms_threshold = 0.6, # Non-max suppression threshold
correct_hw = FALSE # Should height and width of bounding boxes be corrected to image height and width
)
test_boxes
```
Plot / save images:
```{r}
plot_boxes(
images_paths = test_img_paths, # Images paths
boxes = test_boxes, # Bounding boxes
correct_hw = TRUE, # Should height and width of bounding boxes be corrected to image height and width
labels = coco_labels # Class labels
)
```
YOLOv3 Object detection with custom dataset:
---------------
Download images and annotations: [BCCD dataset](https://www.kaggle.com/surajiiitm/bccd-dataset?)
Generate custom anchor boxes:
```{r}
library(tidyverse)
library(platypus)
library(abind)
BCCD_path <- "development/BCCD/"
annot_path <- file.path(BCCD_path, "Annotations/")
blood_labels <- c("Platelets", "RBC", "WBC")
n_class <- length(blood_labels)
net_h <- 416 # Must be divisible by 32
net_w <- 416 # Must be divisible by 32
anchors_per_grid <- 3
blood_anchors <- generate_anchors(
anchors_per_grid = anchors_per_grid, # Number of anchors (per one grid) to generate
annot_path = annot_path, # Annotations directory
labels = blood_labels, # Class labels
n_iter = 10, # Number of k-means++ iterations
annot_format = "pascal_voc", # Annotations format
seed = 55, # Random seed
centroid_fun = mean # Centroid function
)
blood_anchors
```
Build `YOLOv3` model and compile it with correct loss and metric:
```{r}
blood_yolo <- yolo3(
net_h = net_h, # Input image height
net_w = net_w, # Input image width
grayscale = FALSE, # Should images be loaded as grayscale or RGB
n_class = n_class, # Number of object classes (80 for COCO dataset)
anchors = blood_anchors # Anchor boxes
)
blood_yolo %>% load_darknet_weights("development/yolov3.weights") # Optional
blood_yolo %>% compile(
optimizer = optimizer_adam(lr = 1e-5),
loss = yolo3_loss(blood_anchors, n_class = n_class),
metrics = yolo3_metrics(blood_anchors, n_class = n_class)
)
```
Create data generators:
```{r}
train_blood_yolo_generator <- yolo3_generator(
annot_path = file.path(BCCD_path, "train", "Annotations/"),
images_path = file.path(BCCD_path, "train", "JPEGImages/"),
net_h = net_h,
net_w = net_w,
batch_size = 16,
shuffle = FALSE,
labels = blood_labels
)
valid_blood_yolo_generator <- yolo3_generator(
annot_path = file.path(BCCD_path, "valid", "Annotations/"),
images_path = file.path(BCCD_path, "valid", "JPEGImages/"),
net_h = net_h,
net_w = net_w,
batch_size = 16,
shuffle = FALSE,
labels = blood_labels
)
```
Fit the model:
```{r, eval = FALSE}
blood_yolo %>%
fit_generator(
generator = blood_yolo_generator,
epochs = 1000,
steps_per_epoch = 19,
validation_data = valid_blood_yolo_generator,
validation_steps = 5,
callbacks = list(callback_model_checkpoint("development/BCCD/blood_w.hdf5",
save_best_only = TRUE,
save_weights_only = TRUE)
)
)
```
Predict on new images:
```{r, message = FALSE}
blood_yolo <- yolo3(
net_h = net_h,
net_w = net_w,
grayscale = FALSE,
n_class = n_class,
anchors = blood_anchors
)
blood_yolo %>% load_model_weights_hdf5("development/BCCD/blood_w.hdf5")
test_blood_yolo_generator <- yolo3_generator(
annot_path = file.path(BCCD_path, "test", "Annotations/"),
images_path = file.path(BCCD_path, "test", "JPEGImages/"),
net_h = net_h,
net_w = net_w,
batch_size = 4,
shuffle = FALSE,
labels = blood_labels
)
test_preds <- predict_generator(blood_yolo, test_blood_yolo_generator, 1)
test_boxes <- get_boxes(test_preds, blood_anchors, blood_labels,
obj_threshold = 0.6)
plot_boxes(
images_paths = list.files(file.path(BCCD_path, "test", "JPEGImages/"), full.names = TRUE),
boxes = test_boxes,
labels = blood_labels)
```
See full example [here](https://github.com/maju116/platypus/blob/master/examples/Blood%20Cell%20Detection/Blood-Cell-Detection.md)
U-Net image segmentation with custom dataset:
---------------
Build `U-Net` model and compile it with correct loss and metric:
```{r, message = FALSE, warning = FALSE}
library(tidyverse)
library(platypus)
library(abind)
train_DCB2018_path <- "development/data-science-bowl-2018/stage1_train"
test_DCB2018_path <- "development/data-science-bowl-2018/stage1_test"
blocks <- 4 # Number of U-Net convolutional blocks
n_class <- 2 # Number of classes
net_h <- 256 # Must be in a form of 2^N
net_w <- 256 # Must be in a form of 2^N
DCB2018_u_net <- u_net(
net_h = net_h,
net_w = net_w,
grayscale = FALSE,
blocks = blocks,
n_class = n_class,
filters = 16,
dropout = 0.1,
batch_normalization = TRUE,
kernel_initializer = "he_normal"
)
DCB2018_u_net %>%
compile(
optimizer = optimizer_adam(lr = 1e-3),
loss = loss_dice(),
metrics = metric_dice_coeff()
)
```
Create data generator:
```{r}
train_DCB2018_generator <- segmentation_generator(
path = train_DCB2018_path, # directory with images and masks
mode = "nested_dirs", # Each image with masks in separate folder
colormap = binary_colormap,
only_images = FALSE,
net_h = net_h,
net_w = net_w,
grayscale = FALSE,
scale = 1 / 255,
batch_size = 32,
shuffle = TRUE,
subdirs = c("images", "masks") # Names of subdirs with images and masks
)
```
Fit the model:
```{r, eval = FALSE}
history <- DCB2018_u_net %>%
fit_generator(
train_DCB2018_generator,
epochs = 20,
steps_per_epoch = 21,
callbacks = list(callback_model_checkpoint(
"development/data-science-bowl-2018/DSB2018_w.hdf5",
save_best_only = TRUE,
save_weights_only = TRUE,
monitor = "dice_coeff",
mode = "max",
verbose = 1)
)
)
```
Predict on new images:
```{r, message = FALSE}
DCB2018_u_net <- u_net(
net_h = net_h,
net_w = net_w,
grayscale = FALSE,
blocks = blocks,
filters = 16,
dropout = 0.1,
batch_normalization = TRUE,
kernel_initializer = "he_normal"
)
DCB2018_u_net %>% load_model_weights_hdf5("development/data-science-bowl-2018/DSB2018_w.hdf5")
test_DCB2018_generator <- segmentation_generator(
path = test_DCB2018_path,
mode = "nested_dirs",
colormap = binary_colormap,
only_images = TRUE,
net_h = net_h,
net_w = net_w,
grayscale = FALSE,
scale = 1 / 255,
batch_size = 32,
shuffle = FALSE,
subdirs = c("images", "masks")
)
test_preds <- predict_generator(DCB2018_u_net, test_DCB2018_generator, 3)
test_masks <- get_masks(test_preds, binary_colormap)
```
Plot / save images with masks:
```{r, warning = FALSE}
test_imgs_paths <- create_images_masks_paths(test_DCB2018_path, "nested_dirs", FALSE, c("images", "masks"), ";")$images_paths
plot_masks(
images_paths = test_imgs_paths[1:4],
masks = test_masks[1:4],
labels = c("background", "nuclei"),
colormap = binary_colormap
)
```
See full example [here](https://github.com/maju116/platypus/blob/master/examples/2018%20Data%20Science%20Bowl/2018-Data-Science-Bowl.md)