npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-tensorflow

v2.2.0

Published

TensorflowJS based hooks for react.js

Downloads

63

Readme

React-Tensorflow

Typed with TypeScript codecov CircleCI npm twitter-badge github-star-badge

A library of React hooks and HOCs written in Typescript to use Tensorflow models in your application! 🤖🧠

Demo application with Code Examples

Installation

yarn add react-tensorflow
npm i react-tensorflow -S

Peer dependencies

Basic usage

import { useModel } from 'react-tensorflow'

const MyModelComponent = () => {
  const model = useModel({ modelUrl: `${PATH_TO_MODEL}` })

  // ...do something with the model

  return null
}

API

useModel

useModel({
  model?: any,
  modelUrl?: string,
  layers?: boolean,
  onLoadCallback?: (model: GraphModel | LayersModel | null) => void
}): GraphModel | LayersModel | null

If model or modelUrl is omitted useModel will look to find the ModelProvider as it's context for returning the model. When loading a model with this hook, the layers boolean is passed if your TF model should be loaded with the function tf.loadLayersModel otherwise it is assumed the model should be loaded with tf.loadGraphModel. If a model is loaded with modelUrl and an onLoadCallback function is provided, it will be called with the loaded model. This function is intended to be used as a warm up function that could look like this –

(model) => {
  const zeroTensor = tf.zeros([1, 300, 300, 3], 'int32');
  const result = await model.executeAsync(zeroTensor) as tf.Tensor[];
  await Promise.all(result.map(t => t.data()));
  result.map(t => t.dispose());
  zeroTensor.dispose();
}

ModelProvider

<ModelProvider url={string} layers={boolean} onLoadCallback={(model) => void}>
  <App />
</ModelProvider>

Wraps the children in a React Provider to be consumed by Context's in either the useModel hook or withModel HOC. The props passed to this provider are the same as the documented props for useModel.

withModel

withModel(Component: React.ComponentType): JSX.Element

Wraps the provided component in a React Context, passing the model give to the provider as a prop.

useWebcam

useWebcam (options?: {
    width?: number
    height?: number
    facingMode?: string
  }): [React.MutableRefObject<HTMLVideoElement>, tf.Tensor | null]

Provides a ref to be used on a video element, the hook then returns a tensor with shape [1, width, height, 3] where the width and height are either dictated by the element's width & height or the provided argument documented above. The options argument while documented above can infact take all the properties of the MediaStreamConstraints.

:point_right: All the following hooks use useModel under the hood, therefore accepting any of the args passed to useModel :point_left:

usePrediction

usePrediction (options?: {
  predictConfig?: {},
  useExecute?: boolean = false,
  outputName?: string,
  predictionFunction?: string,
  modelUrl?: string,
  layers?: boolean,
}): [(data: tf.Tensor) => void, tf.Tensor | tf.Tensor[] | tf.NamedTensorMap | null]

Provides a function to set the data you want to use to create a prediction. The data must be in the form of a tensor. It then returns a new tensor as the prediction using either the model set with the ModelProvider component or by passing a modelUrl as an argument as it uses useModel under the hood. You can then perform different actions such as normalizing the data for to classify the original input. By default usePrediction uses .predict, if you want to force the use of .executeAsync set useExecute: true and if you want to use a custom predict function, pass it's name via the predictionFunction key. If you're using a LayersModel you must set outputName.

:no_entry_sign: Using a @tensorflow/tfjs-models model with this hook will cause typescript errors if the model predicition method is called or will simply return null because the model either does not have an executeAsync or predict function or it does, and it has not returned a Tensor. :no_entry_sign:

:point_right: All the following hooks use usePrediction under the hood, therefore accepting any of the args passed to usePrediction :point_left:

useClassifier

useClassifer(options?: {
  classes?: {},
  returns?: number,
  modelUrl?: string,
  layers?: boolean,
}): [(data: tf.Tensor) => void, Array<{class: number, probability: number}> | Array<{class: string, probability: number}> | null]

Returns a function to set the data which must be in the form of a tensor. After prediction has been made, returns an array of classifications (be default, the array will have length 5). If the classes argument is provided, the class key in the returned array will be the class at the index of the prediction.

useObjectDetect

useObjectDetect(options?:{
  returns?: number,
  minConfidence?: number,
  classes?: {},
  width?: number,
  height?: number,
  modelUrl?: string,
  layers?: boolean,
}): [(data: tf.Tensor) => void, Array<{class: number, probability: number, boundingBox: number[]}> | Array<{class: string, probability: number, boundingBox: number[]}> | null]

Returns an array with index 0 being a function to set the data. This data must be in the form of a tensor. After a prediction has been made, returns an array of objects detected. Both height and width of the media must be provided to recieve values inside bounding box which will be an array of four numbers – [left, top, width, height]. minConfidence must be a value between 0 - 1. The hook then returns an array of detected objects (by default, the array will be length 5). If the classes argument is provided, the class key in the returned array will be the class at the index of the prediction as a string.

Contributing

Contributions are very welcome and wanted.

Before submitting a new pull request, please make sure:

  1. Consider if the pull-request should be going to the master branch or the latest release branch.
  2. If merging to master, you have updated the package.json version.
  3. You report your changes into the CHANGELOG file.
  4. make sure you run the test and build script before submitting your merge request.
  5. make sure you've added the documentation of your changes.