GithubHelp home page GithubHelp logo

ibm / max-fast-neural-style-transfer Goto Github PK

View Code? Open in Web Editor NEW
50.0 26.0 21.0 6.5 MB

Generate a new image that mixes the content of a source image with the style of another image.

Home Page: https://developer.ibm.com/code/exchanges/models/

License: Apache License 2.0

Python 91.97% Dockerfile 8.03%
machine-learning docker-image pytorch machine-learning-models

max-fast-neural-style-transfer's Introduction

Build Status Website Status

IBM Developer Model Asset Exchange: Fast Neural Style Transfer

This repository contains code to instantiate and deploy an image style transfer model. This model generates a new image that mixes the content of an input image with the style of another image. The model consists of a deep feed-forward convolutional net using a ResNet architecture, trained with a perceptual loss function between a dataset of content images and a given style image. The model was trained on the COCO 2014 data set and 4 different style images. The input to the model is an image, and the output is a stylized image.

The model is based on the Pytorch Fast Neural Style Transfer Example. The model files are hosted on IBM Cloud Object Storage. The code in this repository deploys the model as a web service in a Docker container. This repository was developed as part of the IBM Developer Model Asset Exchange and the public API is powered by IBM Cloud.

Model Metadata

Domain Application Industry Framework Training Data Input Data Format
Vision Style Transfer General Pytorch COCO 2014 Image (PNG/JPG/TIFF)

References

Licenses

Component License Link
This repository Apache 2.0 LICENSE
Model Weights BSD-3-Clause Pytorch Examples LICENSE
Model Code (3rd party) BSD-3-Clause Pytorch Examples LICENSE
Test assets Various Samples README

Pre-requisites:

Note: this model can be very memory intensive. If you experience crashes (such as the model API process terminating with a Killed message), ensure your docker container has sufficient resources allocated (for example you may need to increase the default memory limit on Mac or Windows).

  • docker: The Docker command-line interface. Follow the installation instructions for your system.
  • The minimum recommended resources for this model is 6 GB Memory and 4 CPUs.

Deployment options

Deploy from Quay

To run the docker image, which automatically starts the model serving API, run:

$ docker run -it -p 5000:5000 quay.io/codait/max-fast-neural-style-transfer

This will pull a pre-built image from the Quay.io container registry (or use an existing image if already cached locally) and run it. If you'd rather checkout and build the model locally you can follow the run locally steps below.

Deploy on Red Hat OpenShift

You can deploy the model-serving microservice on Red Hat OpenShift by following the instructions for the OpenShift web console or the OpenShift Container Platform CLI in this tutorial, specifying quay.io/codait/max-fast-neural-style-transfer as the image name.

Deploy on Kubernetes

You can also deploy the model on Kubernetes using the latest docker image on Quay.

On your Kubernetes cluster, run the following commands:

$ kubectl apply -f https://raw.githubusercontent.com/IBM/MAX-Fast-Neural-Style-Transfer/master/max-fast-neural-style-transfer.yaml

The model will be available internally at port 5000, but can also be accessed externally through the NodePort.

A more elaborate tutorial on how to deploy this MAX model to production on IBM Cloud can be found here.

Run Locally

  1. Build the Model
  2. Deploy the Model
  3. Use the Model
  4. Development
  5. Cleanup

1. Build the Model

Clone this repository locally. In a terminal, run the following command:

$ git clone https://github.com/IBM/MAX-Fast-Neural-Style-Transfer.git

Change directory into the repository base folder:

$ cd MAX-Fast-Neural-Style-Transfer

To build the docker image locally, run:

$ docker build -t max-fast-neural-style-transfer .

All required model assets will be downloaded during the build process. Note that currently this docker image is CPU only (we will add support for GPU images later).

2. Deploy the Model

To run the docker image, which automatically starts the model serving API, run:

$ docker run -it -p 5000:5000 max-fast-neural-style-transfer

3. Use the Model

The API server automatically generates an interactive Swagger documentation page. Go to http://localhost:5000 to load it. From there you can explore the API and also create test requests.

Use the model/predict endpoint to load a test image (you can use one of the test images from the samples folder) and get a stylized image back from the API. You can select the style model to use with the model querystring argument. The available options are mosaic (the default model), candy, rain_princess and udnie. See the Pytorch example for more details.

Swagger Doc Screenshot

You can also test it on the command line, for example:

$ curl -F "image=@samples/bridge.jpg" -XPOST http://localhost:5000/model/predict?model=udnie > result.jpg

You can then open the stylized result image on your machine in the tool of your choice, which should look like the image below.

CLI Screenshot

4. Development

To run the Flask API app in debug mode, edit config.py to set DEBUG = True under the application settings. You will then need to rebuild the docker image (see step 1).

5. Cleanup

To stop the Docker container, type CTRL + C in your terminal.

Resources and Contributions

If you are interested in contributing to the Model Asset Exchange project or have any queries, please follow the instructions here.

max-fast-neural-style-transfer's People

Contributors

ajbozarth avatar animeshsingh avatar bdwyer2 avatar dependabot[bot] avatar djalova avatar frreiss avatar mlnick avatar ptitzler avatar splovyt avatar xuhdev avatar xwu0226 avatar yil532 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

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

max-fast-neural-style-transfer's Issues

Model only supports jpeg

Currently the model only supports jpegs and not pngs but it's not made clear or documented. We should either update it to support png or clearly document that it only supports jpeg (e.g. IBM/MAX-Object-Detector/pull/10 added png support to Object Detector)

Running the Container on Mac

There is an issue with this model running on Mac. Docker does not dynamically manage RAM usage on Mac and varying image sizes as an affect on RAM consumption. This causes the model to not be able to fetch the stylized image from Post request. Images usually above 3MB require 20GB of RAM on the 2017 MacBook Pro. I think the Note should be changed to inform that on Macs RAM allocation for the docker container should be variable to the image's size that you want to stylize.

Deployment to Red Hat OpenShift fails due to permission error

Traceback (most recent call last):
--
  | File "app.py", line 18, in <module>
  | from api import ModelMetadataAPI, ModelPredictAPI
  | File "/workspace/api/__init__.py", line 18, in <module>
  | from .predict import ModelPredictAPI  # noqa
  | File "/workspace/api/predict.py", line 23, in <module>
  | model_wrapper = ModelWrapper()
  | File "/workspace/core/model.py", line 55, in __init__
  | model = torch.load(model_path, map_location=lambda storage, loc: storage)  # cpu only for now ...
  | File "/opt/conda/lib/python3.6/site-packages/torch/serialization.py", line 265, in load
  | f = open(f, 'rb')
  | PermissionError: [Errno 13] Permission denied: 'assets/mosaic.pth'

Remove open command from README

README.md contains this line:
$ curl -F "image=@assets/bridge.jpg" -XPOST http://localhost:5000/model/predict?model=udnie > result.jpg && open result.jpg

however open is a command specific to OSX, we should remove this or specify it only works on Mac

GPU support

Is there a way to config current code to run through GPU?

inconsistent link to COS directory for model files

the README contains this link to the model files:

this works. however, it is different than the link in the Dockerfile:

i would expect both of these to point to the same set of model files.
in addition, both of the links should be pointing to the new US cross region:

s3.us.cloud-object-storage.appdomain.cloud

because the old one has been deprecated:

s3-api.us-geo.objectstorage.softlayer.net

inconsistent link to COS directory for model files

the README contains this link to the model files:

this works. however, it is different than the link in the Dockerfile:

i would expect both of these to point to the same set of model files.
in addition, both of the links should be pointing to the new US cross region:

s3.us.cloud-object-storage.appdomain.cloud

because the old one has been deprecated:

s3-api.us-geo.objectstorage.softlayer.net

Add ability to scale uploaded image

Based on a discussion with @MLnick we should add a new scale param that can be used to scale down the uploaded image before running prediction. This would fix the issue raised in #2 as well as allow users to control the returned image quality based on their available resources. We should include a value that denotes not to use scaling (such as -1) and find a good default value that will allow the model to work on a more common deployment (usually 4-8GB). We may even want to look at image size when determining weather to scale at all (why scale small images).

This would entail:

  • Adding scaling functionality to wrapper (before prediction)
  • Determine a good default value
  • Add scale param to API

Timeout exceeded while awaiting headers

Hello, just a heads up that there seems to be a timeout error when requesting the docker image:

$ docker run -it -p 5000:5000 quay.io/codait/max-fast-neural-style-transfer
Unable to find image 'quay.io/codait/max-fast-neural-style-transfer:latest' locally
docker: Error response from daemon: Head https://quay.io/v2/codait/max-fast-neural-style-transfer/manifests/latest: Get https://quay.io/v2/auth?scope=repository%3Acodait%2Fmax-fast-neural-style-transfer%3Apull&service=quay.io: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).

And same when building locally:

$ docker build -t max-fast-neural-style-transfer .
Sending build context to Docker daemon  2.298MB
Step 1/16 : FROM quay.io/codait/max-base:v1.1.3
Head https://quay.io/v2/codait/max-base/manifests/v1.1.3: Get https://quay.io/v2/auth?scope=repository%3Acodait%2Fmax-base%3Apull&service=quay.io: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

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.