Giter VIP home page Giter VIP logo

arbitrary-image-stylization-tfjs's Introduction

Arbitrary style transfer in TensorFlow.js

This repository contains an implementation of arbitrary style transfer running fully inside the browser using TensorFlow.js.

Demo website: https://reiinakano.github.io/arbitrary-image-stylization-tfjs

Blog post with more details: https://magenta.tensorflow.org/blog/2018/12/20/style-transfer-js/

Stylize an image

stylize

Combine styles

combine

FAQ

What is this?

This is an implementation of an arbitrary style transfer algorithm running purely in the browser using TensorFlow.js. As with all neural style transfer algorithms, a neural network attempts to "draw" one picture, the Content (usually a photograph), in the style of another, the Style (usually a painting).

Although other browser implementations of style transfer exist, they are normally limited to a pre-selected handful of styles, due to the requirement that a separate neural network must be trained for each style image.

Arbitrary style transfer works around this limitation by using a separate style network that learns to break down any image into a 100-dimensional vector representing its style. This style vector is then fed into another network, the transformer network, along with the content image, to produce the final stylized image.

I have written a blog post explaining this project in more detail.

Is my data safe? Can you see my pictures?

Your data and pictures here never leave your computer! In fact, this is one of the main advantages of running neural networks in your browser. Instead of sending us your data, we send you both the model and the code to run the model. These are then run by your browser.

What are all these different models?

The original paper uses an Inception-v3 model as the style network, which takes up ~36.3MB when ported to the browser as a FrozenModel.

In order to make this model smaller, a MobileNet-v2 was used to distill the knowledge from the pretrained Inception-v3 style network. This resulted in a size reduction of just under 4x, from ~36.3MB to ~9.6MB, at the expense of some quality.

For the transformer network, the original paper uses a model using plain convolution layers. When ported to the browser, this model takes up 7.9MB and is responsible for the majority of the calculations during stylization.

In order to make the transformer model more efficient, most of the plain convolution layers were replaced with depthwise separable convolutions. This reduced the model size to 2.4MB, while drastically improving the speed of stylization.

This demo lets you use any combination of the models, defaulting to the MobileNet-v2 style network and the separable convolution transformer network.

How big are the models I'm downloading?

The distilled style network is ~9.6MB, while the separable convolution transformer network is ~2.4MB, for a total of ~12MB. Since these models work for any style, you only have to download them once!

How does style combination work?

Since each style can be mapped to a 100-dimensional style vector by the style network, we simply take a weighted average of the two to get a new style vector for the transformer network.

This is also how we are able to control the strength of stylization. We take a weighted average of the style vectors of both content and style images and use it as input to the transformer network.

Running locally for development

This project uses Yarn for dependencies.

To run it locally, you must install Yarn and run the following command at the repository's root to get all the dependencies.

yarn run prep

Then, you can run

yarn run start

You can then browse to localhost:9966 to view the application.

Credits

This demo could not have been done without the following:

As a final note, I'd love to hear from people interested in making a suite of tools for artistically manipulating images, kind of like Magenta Studio but for images. Please reach out if you're planning to build/are building one out!

arbitrary-image-stylization-tfjs's People

Contributors

k1ic avatar reiinakano 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

arbitrary-image-stylization-tfjs's Issues

Sharing the original SavedModel

Hi Nakano-san,

Do you mind sharing the original float SavedModel prior to being converted to TensorFlow.js?

I'm interested in converting the model to TF Lite and use on mobile. It'd be great if I can build on top of your work instead of rebuilding what you have done.

Thanks.

camera work in local run but not work in production aws ec2

When I run on local the camera is work. but when I run on server aws EC2 I got this when I run the camera

Uncaught TypeError: navigator.getUserMedia is not a function
    at HTMLDivElement.<anonymous> (bundle.js:248)
    at HTMLDivElement.dispatch (jquery.min.js:2)
    at HTMLDivElement.y.handle (jquery.min.js:2)
    at Object.trigger (jquery.min.js:2)
    at HTMLDivElement.<anonymous> (jquery.min.js:2)
    at Function.each (jquery.min.js:2)
    at w.fn.init.each (jquery.min.js:2)
    at w.fn.init.trigger (jquery.min.js:2)
    at HTMLDivElement.r (bootstrap.min.js:6)
    at HTMLDivElement.i (jquery.min.js:2)

Can you advice , how to fix it?

Dimensions of the model

Hi!

Great work!

I just only wanted to tell you that we just recently ported this model to tfjs from here

We haven't modified the use of the InceptionV3. After the parameters optimisation the total model (we don't expose it as two parts) is just only about 25MB in total and the model is able to run (like yours) if the browser can handle enough RAM creating the WebGL context (usually limited to 512MB).

So your model is 17MB in total and the model with inceptionV3 is just only 25MB.

Of course that's not the REAL memory consumed by the model on inference.

Again, great work!

How to increase number of iterations?

I feel like this needs to run through more iterations than it does - the stylization strength isn't high enough for me, all the images wind up looking roughly the same.

Can I also use these models for a react native application?

I have been playing around and most of the implementations that I have found are browser based. The only mobile applications there are use the tflite versions of the magenta arbitrary style transfer from tfhub. I just wanted to build a react native application and so hoped if I could use these models in the app and get a good and fast result or not.

How about migrate the model to the mobile?

Great demo for arbitrary style transfer!
The progress from fixed style images to totally free choices is really exciting. It seems that you are more likely to create a demo with js? Inspired by this repo, I want to have a try with migrating the model to the mobile like Prisma, just for practice, no commercial use : ). Since I am not familiar with the tensorflow.js apis, there are few questions:

Is there any way to integrate the .js with android frame?
Or the possible solution is taking use of the original model (before combined with js) and trying to reduce it with apis like tensorflow lite?

Looking for your help : )

where is port 9966 configured?

I thought I knew yarn/node/javascript. ¯_(ツ)_/¯

yarn run start : checking package.json to see what is executed.

package.json shows
"scripts": {
"start": "budo main.js:dist/bundle.js --live --host localhost",

main.js does not contain a reference to port 9966
directory dist is empty. bundle.js does not exist, yet my localhost is running fine.

Where should I be looking?

Running this thru command line

Hi, is it possible to run this thru a command line way.

run image_001 and convert with style image, store result in folder

I'm trying to find a low latency kind of library for style transfer to do such a thing I tried ML5 P5js but that doesn't work because I can't train a model.

Your solution does work but I don't want to use the interface. I just want to use command line way for automation.

Any help would be greatly appriciated.

Photographic style transfer

Hi! This is great! I have a proposition for you. In recent years began to appear the algorithms for fast photo-realistic imitation of any style. Why not make a browser version of this algorithm? This is no less interesting than simple styling, but there are only a few similar projects.

incorrect stylized image when the screen resolution isn't 1920x1080

Hi, I tested the style transfer result in the demo website and found that I could not get correct stylized image when the screen resolution isn't 1920x1080. For example, when the screen resolution is 2160x1440, the generated image looks like this:
screen
while it works for another computer with 1920x1080 resolution. Are there any possible reasons about this issue?

Larger images

I've tried to increase the size of the source image (changed index.html) to produce larger stylised images but I can't seem to get beyond 1000x1000. I get an error: "GL ERROR :GL_OUT_OF_MEMORY : glBufferData: cannot allocate more than 1GB". Any suggestions?

distilled tf savedModel

Hi @reiinakano, thanks for this example, we tried running the TFJS model in our electron application, it is a bit slower in the browser. We are planning to write a native node module with onnxruntime, just wondering is there a place we could find the original distilled TF model before the TFJS conversion, which might help us to test. Thanks!

How to reproduce style from deeplearn.js

On the "old" deeplearn.js demo page, one can generate a very nice image when combining the face content image with the udnie style image. This is the result:
face-udnie
The paining style is very clearly applied to the image of the face.
I'd like to reproduce this result via the new arbitrary-image-stylization-tfjs library. I've been playing around on the demo page for quite some time, trying all kinds of settings, but I cannot get close to this result.
Which values do I need to select to get (close) to the image shown above?

How to change content size slider in local? Need bigger images

What it says on the tin, I'd like for the image output to be larger than the slider allows for, so I'm trying to edit it, but the references in the main js file don't reveal anything so I'm at a bit of a loss. (My background is python and AI/ML, not nodejs.)

Tensorflow and Tensorflowjs version

Hello @reiinakano,
Thanks for your great repository. Can you tell me which version of Tensorflow and Tensorflowjs have you used?
I want to convert Tensorflowjs model to TFLite model but I have some issue.

Look forward to hearing from you soon,
Thanks

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.