The fastai library simplifies training fast and accurate neural nets using modern best practices. See the fastai website to get started. The library is based on research into deep learning best practices undertaken at fast.ai
, and includes “out of the box” support for vision
, text
, tabular
, and collab
(collaborative filtering) models.
Via rvest
package we can search and scrape any category of image. Int this example we will classify 4 cat categories:
# cat categories: https://www.purina.com/cats/cat-breeds
= 'cats'
f_n
if(!dir.exists(f_n)) {
dir.create(f_n)
}
A function to download cat images:
library(rvest)
= function(name, dest) {
download_pet = name
query = gsub('\\s', '%20', query)
query
<- read_html(paste("https://www.google.com/search?site=&tbm=isch&q", query, sep = "="))
search
<- search %>% html_nodes("img") %>% html_attr("src") %>% .[-1]
urls
= gsub('\\s|[[:punct:]]', '_', name)
fixed_name
for (i in 1:length(urls)) {
download.file(urls[i], destfile =
file.path(dest,
paste(
paste(fixed_name,
round(runif(1)*10000),
sep = '_'),
'.jpg', sep = ''
)mode = 'wb'
),
)
} }
Lets define cat groups:
= c('Balinese-Javanese Cat Breed', 'Chartreux Cat Breed',
cat_names 'Norwegian Forest Cat Breed', 'Turkish Angora Cat Breed')
And iterate throught vector:
for (i in 1:length(cat_names)) {
download_pet(cat_names[i], f_n)
print(paste('Done',cat_names[i]))
}
Call libraries and import dataset:
library(fastai)
library(magrittr)
= 'cats'
path = get_image_files(path)
fnames
1]
fnames[# cats/Turkish_Angora_Cat_Breed_8583.jpg
See batch:
= ImageDataLoaders_from_name_re(
dls pat='(.+)_\\d+.jpg$',
path, fnames, item_tfms = Resize(size = 200), bs = 15,
batch_tfms = list(aug_transforms(size = 224, min_scale = 0.75),
Normalize_from_stats( imagenet_stats() )
)
)
%>% show_batch(dpi = 200) dls
Define cnn_learner
and fit. But before that it is better to see the train metrics as well.
= cnn_learner(dls, resnet50(), metrics = list(accuracy, error_rate))
learn
$recorder$train_metrics = TRUE learn
%>% fit_one_cycle(5, 1e-3) learn
epoch train_loss train_accuracy train_error_rate valid_loss valid_accuracy valid_er_r
0 2.145128 0.333333 0.666667 1.205951 0.500000 0.500000
1 1.480864 0.650000 0.350000 0.605784 0.812500 0.187500
2 1.114904 0.850000 0.150000 0.353506 0.812500 0.187500
3 0.858903 0.916667 0.083333 0.219998 0.875000 0.125000
4 0.706619 0.933333 0.066667 0.231960 0.937500 0.062500
Predict random image:
1]
fnames[
# cats/Turkish_Angora_Cat_Breed_8583.jpg
%>% predict(as.character(fnames[1])) learn
$probabilities
Balinese_Javanese_Cat_Breed Chartreux_Cat_Breed Norwegian_Forest_Cat_Breed Turkish_Angora_Cat_Breed
1 0.001805532 0.004121034 0.09873049 0.8953429
$labels
[1] "Turkish_Angora_Cat_Breed"