knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The goal of torchvisionlib is to provide access to C++ opeartions implemented in torchvision. It provides plain R acesss to some of those C++ operations but, most importantly it provides full support for JIT operators defined in torchvision, allowing us to load 'scripted' object detection and image segmentation models.
torchvisionlib can be installed from CRAN with:
install.packages("torchvisionlib")
You can also install the development version of torchvisionlib from GitHub with:
# install.packages("devtools") devtools::install_github("mlverse/torchvisionlib")
Suppose that we want to load an image detection model implemented in torchvision. First, in Python, we can save JIT script and then save this model:
import torch import torchvision model = torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn(pretrained=True) model.eval() jit_model = torch.jit.script(model) torch.jit.save(jit_model, "fasterrcnn_mobilenet_v3_large_320_fpn.pt")
We can then load this model in R. Simply loading torchvisionlib will register all
JIT operators, and we can use torch::jit_load()
.
url <- "https://storage.googleapis.com/torch-lantern-builds/testing-models/fasterrcnn_mobilenet_v3_large_320_fpn.pt" download.file(url, destfile = "fasterrcnn_mobilenet_v3_large_320_fpn.pt", mode = "wb")
library(torchvisionlib) model <- torch::jit_load("fasterrcnn_mobilenet_v3_large_320_fpn.pt") model
You can then use this model to make preditions or even fine tuning.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.