R interface to TensorFlow Hub

on

|

views

and

comments


We’re happy to announce that the primary model of tfhub is now on CRAN. tfhub is an R interface to TensorFlow Hub – a library for the publication, discovery, and consumption of reusable elements of machine studying fashions. A module is a self-contained piece of a TensorFlow graph, together with its weights and belongings, that may be reused throughout totally different duties in a course of often known as switch studying.

The CRAN model of tfhub might be put in with:

After putting in the R package deal it is advisable set up the TensorFlow Hub python package deal. You are able to do it by operating:

Getting began

The important operate of tfhub is layer_hub which works similar to a keras layer however means that you can load an entire pre-trained deep studying mannequin.

For instance you’ll be able to:

library(tfhub)
layer_mobilenet <- layer_hub(
  deal with = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4"
)

This can obtain the MobileNet mannequin pre-trained on the ImageNet dataset. tfhub fashions are cached regionally and don’t should be downloaded the subsequent time you employ the identical mannequin.

Now you can use layer_mobilenet as a typical Keras layer. For instance you’ll be able to outline a mannequin:

library(keras)
enter <- layer_input(form = c(224, 224, 3))
output <- layer_mobilenet(enter)
mannequin <- keras_model(enter, output)
abstract(mannequin)
Mannequin: "mannequin"
____________________________________________________________________
Layer (sort)                  Output Form               Param #    
====================================================================
input_2 (InputLayer)          [(None, 224, 224, 3)]      0          
____________________________________________________________________
keras_layer_1 (KerasLayer)    (None, 1001)               3540265    
====================================================================
Complete params: 3,540,265
Trainable params: 0
Non-trainable params: 3,540,265
____________________________________________________________________

This mannequin can now be used to foretell Imagenet labels for a picture. For instance, let’s see the outcomes for the well-known Grace Hopper’s photograph:

Grace Hopper
img <- image_load("https://blogs.rstudio.com/tensorflow/posts/photos/grace-hopper.jpg", target_size = c(224,224)) %>% 
  image_to_array()
img <- img/255
dim(img) <- c(1, dim(img))
pred <- predict(mannequin, img)
imagenet_decode_predictions(pred[,-1,drop=FALSE])[[1]]
  class_name class_description    rating
1  n03763968  military_uniform 9.760404
2  n02817516          bearskin 5.922512
3  n04350905              swimsuit 5.729345
4  n03787032       mortarboard 5.400651
5  n03929855       pickelhaube 5.008665

TensorFlow Hub additionally gives many different pre-trained picture, textual content and video fashions.
All attainable fashions might be discovered on the TensorFlow hub web site.

TensorFlow Hub

You’ll find extra examples of layer_hub utilization within the following articles on the TensorFlow for R web site:

Utilization with Recipes and the Characteristic Spec API

tfhub additionally gives recipes steps to make
it simpler to make use of pre-trained deep studying fashions in your machine studying workflow.

For instance, you’ll be able to outline a recipe that makes use of a pre-trained textual content embedding mannequin with:

rec <- recipe(obscene ~ comment_text, information = practice) %>%
  step_pretrained_text_embedding(
    comment_text,
    deal with = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim-with-oov/1"
  ) %>%
  step_bin2factor(obscene)

You may see an entire operating instance right here.

It’s also possible to use tfhub with the brand new Characteristic Spec API applied in tfdatasets. You may see an entire instance right here.

We hope our readers have enjoyable experimenting with Hub fashions and/or can put them to good use. When you run into any issues, tell us by creating a difficulty within the tfhub repository

Reuse

Textual content and figures are licensed underneath Artistic Commons Attribution CC BY 4.0. The figures which were reused from different sources do not fall underneath this license and might be acknowledged by a observe of their caption: “Determine from …”.

Quotation

For attribution, please cite this work as

Falbel (2019, Dec. 18). Posit AI Weblog: tfhub: R interface to TensorFlow Hub. Retrieved from https://blogs.rstudio.com/tensorflow/posts/2019-12-18-tfhub-0.7.0/

BibTeX quotation

@misc{tfhub,
  writer = {Falbel, Daniel},
  title = {Posit AI Weblog: tfhub: R interface to TensorFlow Hub},
  url = {https://blogs.rstudio.com/tensorflow/posts/2019-12-18-tfhub-0.7.0/},
  yr = {2019}
}
Share this
Tags

Must-read

Nvidia CEO reveals new ‘reasoning’ AI tech for self-driving vehicles | Nvidia

The billionaire boss of the chipmaker Nvidia, Jensen Huang, has unveiled new AI know-how that he says will assist self-driving vehicles assume like...

Tesla publishes analyst forecasts suggesting gross sales set to fall | Tesla

Tesla has taken the weird step of publishing gross sales forecasts that recommend 2025 deliveries might be decrease than anticipated and future years’...

5 tech tendencies we’ll be watching in 2026 | Expertise

Hi there, and welcome to TechScape. I’m your host, Blake Montgomery, wishing you a cheerful New Yr’s Eve full of cheer, champagne and...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here