Giter VIP home page Giter VIP logo

tensorflow / tfjs Goto Github PK

View Code? Open in Web Editor NEW
18.1K 330.0 1.9K 168.44 MB

A WebGL accelerated JavaScript library for training and deploying ML models.

Home Page: https://js.tensorflow.org

License: Apache License 2.0

Shell 0.51% TypeScript 80.14% JavaScript 8.52% Python 3.77% C++ 4.51% HTML 0.92% CSS 0.19% Java 0.03% Ruby 0.01% Objective-C 0.04% Batchfile 0.01% C 0.01% Starlark 1.32% Dockerfile 0.01%
deep-learning typescript webgl javascript machine-learning neural-network deep-neural-network gpu-acceleration wasm web-assembly

tfjs's Introduction

TensorFlow.js

TensorFlow.js is an open-source hardware-accelerated JavaScript library for training and deploying machine learning models.

Develop ML in the Browser
Use flexible and intuitive APIs to build models from scratch using the low-level JavaScript linear algebra library or the high-level layers API.

Develop ML in Node.js
Execute native TensorFlow with the same TensorFlow.js API under the Node.js runtime.

Run Existing models
Use TensorFlow.js model converters to run pre-existing TensorFlow models right in the browser.

Retrain Existing models
Retrain pre-existing ML models using sensor data connected to the browser or other client-side data.

About this repo

This repository contains the logic and scripts that combine several packages.

APIs:

Backends/Platforms:

If you care about bundle size, you can import those packages individually.

If you are looking for Node.js support, check out the TensorFlow.js Node directory.

Examples

Check out our examples repository and our tutorials.

Gallery

Be sure to check out the gallery of all projects related to TensorFlow.js.

Pre-trained models

Be sure to also check out our models repository where we host pre-trained models on NPM.

Benchmarks

  • Local benchmark tool. Use this webpage tool to collect the performance related metrics (speed, memory, etc) of TensorFlow.js models and kernels on your local device with CPU, WebGL or WASM backends. You can benchmark custom models by following this guide.
  • Multi-device benchmark tool. Use this tool to collect the same performance related metrics on a collection of remote devices.

Getting started

There are two main ways to get TensorFlow.js in your JavaScript project: via script tags or by installing it from NPM and using a build tool like Parcel, WebPack, or Rollup.

via Script Tag

Add the following code to an HTML file:

<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script>


    <!-- Place your code in the script tag below. You can also use an external .js file -->
    <script>
      // Notice there is no 'import' statement. 'tf' is available on the index-page
      // because of the script tag above.

      // Define a model for linear regression.
      const model = tf.sequential();
      model.add(tf.layers.dense({units: 1, inputShape: [1]}));

      // Prepare the model for training: Specify the loss and the optimizer.
      model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

      // Generate some synthetic data for training.
      const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
      const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

      // Train the model using the data.
      model.fit(xs, ys).then(() => {
        // Use the model to do inference on a data point the model hasn't seen before:
        // Open the browser devtools to see the output
        model.predict(tf.tensor2d([5], [1, 1])).print();
      });
    </script>
  </head>

  <body>
  </body>
</html>

Open up that HTML file in your browser, and the code should run!

via NPM

Add TensorFlow.js to your project using yarn or npm. Note: Because we use ES2017 syntax (such as import), this workflow assumes you are using a modern browser or a bundler/transpiler to convert your code to something older browsers understand. See our examples to see how we use Parcel to build our code. However, you are free to use any build tool that you prefer.

import * as tf from '@tensorflow/tfjs';

// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));

// Prepare the model for training: Specify the loss and the optimizer.
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

// Train the model using the data.
model.fit(xs, ys).then(() => {
  // Use the model to do inference on a data point the model hasn't seen before:
  model.predict(tf.tensor2d([5], [1, 1])).print();
});

See our tutorials, examples and documentation for more details.

Importing pre-trained models

We support porting pre-trained models from:

Various ops supported in different backends

Please refer below :

Find out more

TensorFlow.js is a part of the TensorFlow ecosystem. For more info:

Thanks, BrowserStack, for providing testing support.

tfjs's People

Contributors

ahmedsabie avatar annxingyuan avatar axinging avatar bileschi avatar caisq avatar chunnienc avatar davidsoergel avatar dependabot[bot] avatar dsmilkov avatar ericdnielsen avatar gyagp avatar haoyunfeix avatar hujiajie avatar jameswex avatar jgartman avatar jinjingforever avatar lewuathe avatar lina128 avatar linchenn avatar manrajgrover avatar mattsoulanille avatar nkreeger avatar pforderique avatar pvaneck avatar pyu10055 avatar qjia7 avatar rthadur avatar syt123450 avatar tafsiri avatar xhcao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tfjs's Issues

web cannot access local file.

From @cyilu on March 29, 2018 3:38

As the security of web, it can't access local file. Therefore, it's difficult to train in the js.

Any help will be appreciated!

Copied from original issue: tensorflow/tfjs-core#908

Add tf.layers.conv2d_transpose support in SavedModel converter

From @carlthome on April 2, 2018 10:32

I'm working on a CycleGAN project which relies on strided convolutions (as do most GANs, and many other models). It's written in TensorFlow's Python API with tf.layers.conv2d_transpose.

I saw that TensorFlow.js has transposed strided convolutions but when converting the SavedModel with tensorflowjs_converter I get stuck with

Unsupported Ops in the model
StridedSlice

I guess I could rewrite the forward pass directly in TensorFlow.js but I'm curious why the converter didn't work? I guess it's because conv2dTranspose is fused while tf.layers.conv2d_transpose relies on a separate StridedSlice op.

Copied from original issue: tensorflow/tfjs-converter#68

Improve browser support by changing fetch mode arraybuffer?

From @thebillkidy on April 2, 2018 8:43

Hi,

So I tried to run the mnist example in firefox and instantly got the error on the 'arraybuffer' mode while using fetch (see: https://github.com/tensorflow/tfjs-examples/blob/master/mnist-core/data.js#L87).

Wouldn't it be better to change this code to:

const labelsRequest = fetch(MNIST_LABELS_PATH).then((res) => res.arrayBuffer());
const [imgResponse, labelsResponse] = await Promise.all([imgRequest, labelsRequest]);
this.datasetLabels = new Uint8Array(labelsResponse);

Happy to make a pull request if required :)

Copied from original issue: tensorflow/tfjs-examples#47

Implement the gradient for depthwiseConv2d.

From @nsthorat on March 24, 2018 18:0

Currently we don't have the gradient written. It should be strictly easier than conv2d, so should be easy.

This is important for training in browsers where compute is limited.

Copied from original issue: tensorflow/tfjs-core#892

Typo in BatchNormalization layer?

TensorFlow.js version

       "@tensorflow/tfjs-layers": "0.1.2"

Describe the problem or feature request

      https://github.com/tensorflow/tfjs-layers/blob/master/src/layers/normalization.ts#L228
      In the `normalizeInference()`, current code is `const broadcastGamma = this.center ? K.reshape(this.gamma.read(), broadcastShape) : null;` 
      it differs from the implementation in Keras which states `if self.scale: broadcast_gamma = K.reshape(self.gamma, broadcast_shape)`.

    I'm not entirely sure if this is a typo.

Why parseGrayscaleImageData and parseRGBImageData of xhrdataset have the same function?

From @cyilu on March 27, 2018 6:41

I think these two function are alike:
`
function parseGrayscaleImageData(
data: Uint8Array|Uint8ClampedArray, result: Uint8Array,
resultOffset: number): void {
let idx = resultOffset;
for (let i = 0; i < data.length; i += 4) {
result[idx++] = data[i];
}
}

function parseRGBImageData(
data: Uint8Array|Uint8ClampedArray, result: Uint8Array,
resultOffset: number): void {
let idx = resultOffset;
for (let i = 0; i < data.length; i += 4) {
result[idx] = data[i];
result[idx + 1] = data[i + 1];
result[idx + 2] = data[i + 2];
idx += 3;
}
}`

Copied from original issue: tensorflow/tfjs-core#902

demos broken on master

From @physicsrob on March 30, 2018 17:54

Demos are broken on master after moving some of the files out of the repo. Also the 'yarn prep' command is gone, which means users need to run yarn from the root directory and the demos directory, which I don't think is particularly obvious.

I'm sure this is part of the big shuffle and will be resolved shortly.

One question: Is there any sort of slack/irc channel for this project? It'd be nice to be able to communicate in a less formal manner than creating issues.

Copied from original issue: tensorflow/tfjs-core#911

Examples should generate ES6+ code.

From @nsthorat on March 16, 2018 19:57

It would be nice to have examples generate ES6+ code so we can actually debug and not deal with async/await polyfills which make the call stack quite muddled.

Copied from original issue: tensorflow/tfjs-examples#14

NPM Support

If you would like to get help from the community, our Google
group
might be a
better fit.

TensorFlow.js version

Browser version

Describe the problem or feature request

Code to reproduce the bug / link to feature request

yarn test fails

From @dsmilkov on April 2, 2018 14:57

Cloned the repo at master. Ran yarn and yarn test and got:

Chrome 65.0.3325 (Mac OS X 10.13.3) ERROR
  {
    "message": "An error was thrown in afterAll\nUncaught SyntaxError: Unexpected token :",
    "str": "An error was thrown in afterAll\nUncaught SyntaxError: Unexpected token :"
  }
Firefox 59.0.0 (Mac OS X 10.13.0) ERROR
  {
    "message": "An error was thrown in afterAll\nSyntaxError: unexpected token: ':'",
    "str": "An error was thrown in afterAll\nSyntaxError: unexpected token: ':'"
  }

Copied from original issue: tensorflow/tfjs-converter#69

Add support for additional random tensors

From @jgartman on March 28, 2018 1:17

TensorFlow.js currently supports tensors of random samples drawn from normal, truncated normal or uniform distributions and it looks like multinomial sampling is being added back to the api soon. Tensorflow’s random_ops supports all of these as well as the poisson, and gamma distributions. I was wondering if it would be worthwhile to support these in TensorFlow.js as well?

Thanks

Copied from original issue: tensorflow/tfjs-core#903

tf.keep() does not work for nested tidy()s.

From @nsthorat on March 25, 2018 20:35

Specifically this does not work if you are trying to keep a Tensor allocated in a parent tidy().

let b;
tf.tidy(() => {
  const a = tf.scalar(1);
  tf.tidy(() => {
    b = tf.keep(a);
  });
});

// b is disposed.

Copied from original issue: tensorflow/tfjs-core#897

Add Support for Deconvolution Layer for Keras model

From @clover113 on April 2, 2018 8:9

Currently, when converting keras model with deconvolution2d layer, the converter went fine. However, when importing the model in Tensorflow.js, it outputs "Error: Unknown layer: Conv2DTranspose".
Also, conv2dtranspose is supported in tensorflow.js.

Copied from original issue: tensorflow/tfjs-converter#67

Investigate calling tf.nextFrame() in model.fit() between batches.

Calling tf.nextFrame() in a browser context will release the UI thread so the page can be responsive. This should have no impact on performance for medium-large sized models, though will negatively impact small models (where forward / backprop is < 16ms).

Some benchmarks from @Cais

No tf.nextFrame() (today)

image

tf.nextFrame() between batches

image

Notice that there is no performance impact for rows 2+ (the first row slows down by about a frame, which is expected since the forward / backwards pass is sub-frame).

After launch, we can see what type of models people typically use and come back to this discussion.

MobileNet does not return activations as commented

From @jaxball on March 28, 2018 17:18

models/squeezenet has a predictWithActivation function that returns both named activation and pre-softmax logits. models/mobilenet only has a predict function which returns a 1D tensor, but its function comment appears identical to that for SqueezeNet, stating that

predict(input: dl.Tensor3D): dl.Tensor1D
Infer through MobileNet, assumes variables have been loaded. This does standard ImageNet pre-processing before inferring through the model. This method returns named activations as well as pre-softmax logits.

Can someone verify correctness of the comment and shed light on how to get the activations from MobileNet?

Copied from original issue: tensorflow/tfjs-core#907

Property 'print' does not exist on type 'Tensor<Rank>[]'.

If you would like to get help from the community, our Google
group
might be a
better fit.

TensorFlow.js version

0.6.1

Browser version

Describe the problem or feature request

I use Typescript environment (Angular 5)

I trying to run this code from thehttps://js.tensorflow.org/#getting-started

 import * as tf from '@tensorflow/tfjs';

  // Define a model for linear regression.
  const model = tf.sequential();
  model.add(tf.layers.dense({units: 1, inputShape: [1]}));

  // Prepare the model for training: Specify the loss and the optimizer.
  model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

  // Generate some synthetic data for training.
  const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
  const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

  // Train the model using the data.
  model.fit(xs, ys).then(() => {
    // Use the model to do inference on a data point the model hasn't seen before:
    model.predict(tf.tensor2d([5], [1, 1])).print();
  });

3

ERROR in src/app/app.component.ts(29,47): error TS2339: Property 'print' does not exist on type 'Tensor<Rank> | Tensor<Rank>[]'.
  Property 'print' does not exist on type 'Tensor<Rank>[]'.

I think is the model.d.ts file please add the print method inside this file

Code to reproduce the bug / link to feature request

pytorch model loader

deeplearn.js supported some level of conversion of pytorch models via python scripts

Will this functionality be ported to tfjs or is this out of scope now that the framework is aiming for a tensorflow only solution?

Handling of Assert Ops ?

From @rodrigob on March 21, 2018 20:11

The SavedModel I am trying to convert generated, amongst others; the following message:

Unsupported Ops in the model: Assert

I am wondering if tfjs-converted should not simply ignore Assert ops, and at best generate a warning rather than an error ? (I guess the same would go for Print ops)

Copied from original issue: tensorflow/tfjs-converter#42

Uniqueified Tensor names break tf.loadModel()

If I do tf.loadModel() twice within one session, the second time fails. The reason is that the Tensor names on the second load are uniqueified, e.g. my_tensor => my_tensor_1, so the new Tensor names no longer match the keys in the weights manifest.

Implement tensor.round

From @nbardy on March 24, 2018 20:24

dl.floor and dl.ceil are implemented round is missing.

Copied from original issue: tensorflow/tfjs-core#894

Add "repeat" op & gradients / tf.image.resizeBilinear gradients

TensorFlow.js version

0.6.1

Browser version

Chrome 56.0.2924.87 (Electron 1.6.11)

Feature request

I'm interested in building convolutional generative networks / autoencoders, and am looking for a way to upsample tensors. However, tf.image.resizeBilinear doesn't seem to have its gradient implemented yet, and I couldn't find an element-wise "repeat" op to use in its place.

Having these ops would be very helpful, thanks! Great work on tfjs, awesome library.

A list of prereqs could be handy

From @sgserg on April 2, 2018 17:16

Examples work neither on OSX 10.9 (errors out on unsupported node version)
nor in iOS 11:

beb1db23-6dcf-4596-b6cb-7a8966edc864

Copied from original issue: tensorflow/tfjs-examples#49

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.