GithubHelp home page GithubHelp logo

imfing / keras-flask-deploy-webapp Goto Github PK

View Code? Open in Web Editor NEW
1.2K 30.0 475.0 101 KB

:smiley_cat: Pretty & simple image classifier app template. Deploy your own trained model or pre-trained model (VGG, ResNet, Densenet) to a web app using Flask in 10 minutes.

License: Apache License 2.0

Python 24.41% CSS 21.71% JavaScript 29.15% HTML 23.00% Dockerfile 1.72%
keras tensorflow flask deployment webapp deep-learning pre-trained

keras-flask-deploy-webapp's Introduction

Deploy Keras Model with Flask as Web App in 10 Minutes

Contributions Welcome

A minimal and customizable repo to deploy your image models as web app easily.

Getting Started

  • Quick run with Docker:
    docker run --rm -p 5000:5000 ghcr.io/imfing/keras-flask-deploy-webapp:latest
  • Go to http://localhost:5000 and enjoy ๐ŸŽ‰

Screenshot:

New Features ๐Ÿ”ฅ

  • Enhanced, mobile-friendly UI
  • Support image drag-and-drop
  • Use vanilla JavaScript, HTML and CSS. No jQuery or Bootstrap
  • Switch to TensorFlow 2.x and tf.keras by default
  • Upgrade Docker base image to Python 3.11


Run with Docker

Use prebuilt image

$ docker run --rm -p 5000:5000 ghcr.io/imfing/keras-flask-deploy-webapp:latest

Build locally

With Docker, you can quickly build and run the entire application in minutes ๐Ÿณ

# 1. First, clone the repo
$ git clone https://github.com/imfing/keras-flask-deploy-webapp.git
$ cd keras-flask-deploy-webapp

# 2. Build Docker image
$ docker build -t keras_flask_app .

# 3. Run!
$ docker run -it --rm -p 5000:5000 keras_flask_app

Open http://localhost:5000 and wait till the webpage is loaded.

Local Installation

It's easy to install and run it on your computer.

# 1. First, clone the repo
$ git clone https://github.com/imfing/keras-flask-deploy-webapp.git
$ cd keras-flask-deploy-webapp

# 2. Install Python packages
$ pip install -r requirements.txt

# 3. Run!
$ python app.py

Open http://localhost:5000 and have fun. ๐Ÿ˜ƒ


Customization

It's also easy to customize and include your models in this app.

Note Also consider gradio or streamlit to create complicated web apps for ML models.

Details

Use your own model

Place your trained .h5 file saved by model.save() under models directory.

Check the commented code in app.py.

Use other pre-trained model

See Keras applications for more available models such as DenseNet, MobilNet, NASNet, etc.

Check this section in app.py.

UI Modification

Modify files in templates and static directory.

index.html for the UI and main.js for all the behaviors.

More Resources

Building a simple Keras + deep learning REST API

keras-flask-deploy-webapp's People

Contributors

dependabot[bot] avatar imfing avatar physhik avatar robertdefilippi avatar s-gbz avatar xtellurian 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

keras-flask-deploy-webapp's Issues

ValueError has detected

hi,
When I run the app, I got the error as below,
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\ProgramData\Anaconda3\lib\site-packages\flask_compat.py", line 35, in reraise
raise value
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functionsrule.endpoint
File "C:\download\keras-flask-deploy-webapp-master\app.py", line 75, in upload
preds = model_predict(file_path, model)
File "C:\download\keras-flask-deploy-webapp-master\app.py", line 52, in model_predict
preds = model.predict(x)
File "C:\download\keras-flask-deploy-webapp-master\venv\lib\site-packages\keras\engine\training.py", line 1164, in predict
self._make_predict_function()
File "C:\download\keras-flask-deploy-webapp-master\venv\lib\site-packages\keras\engine\training.py", line 554, in _make_predict_function
**kwargs)
File "C:\download\keras-flask-deploy-webapp-master\venv\lib\site-packages\keras\backend\tensorflow_backend.py", line 2744, in function
return Function(inputs, outputs, updates=updates, **kwargs)
File "C:\download\keras-flask-deploy-webapp-master\venv\lib\site-packages\keras\backend\tensorflow_backend.py", line 2546, in init
with tf.control_dependencies(self.outputs):
File "C:\download\keras-flask-deploy-webapp-master\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 5004, in control_dependencies
return get_default_graph().control_dependencies(control_inputs)
File "C:\download\keras-flask-deploy-webapp-master\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 4543, in control_dependencies
c = self.as_graph_element(c)
File "C:\download\keras-flask-deploy-webapp-master\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 3490, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:\download\keras-flask-deploy-webapp-master\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 3569, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("fc1000/Softmax:0", shape=(?, 1000), dtype=float32) is not an element of this graph.

another problem.! plz help

it is OK to load a trained model now, but another problem happens, I used the same conda environment to deploy the project. dont know how to solve..
File "/home/ljs/anaconda3/envs/zk/lib/python3.7/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 792, in validate_kwargs
raise TypeError(error_message, kwarg)
TypeError: ('Keyword argument not understood:', 'keep_dim')

Docker run issue

Hello. Thanks for great repository.

There is an issue when I containerized the Flask application. When I run docker build command, it simply builds and creates an image but when I run the docker image as a container it's stuck in the line which it tries to download default Keras ResNet50 model.

capture

and so localhost:5000 is not answering. I have modified the docker file like follows:

FROM ubuntu:latest
RUN apt-get update -y && apt-get install -y python python-dev python-pip python-virtualenv build-essential && rm -rf /var/lib/apt/lists/

COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]
*

docker exits just after starting

Thank you for your quick responses and all the edits done today! I have tried new docker right now, but the problem is it quits just after the start. I tried building docker both with model in folder Models (exactly I used h5 model from teachable machine by google) and without it, but nevertheless - simply exits. As for local install:

Traceback (most recent call last):
  File "app.py", line 5, in <module>
    from flask import Flask, redirect, url_for, request, render_template, Response, jsonify, redirect
ImportError: No module named flask

Even though it is installed within requirements.txt

Value Error: decode predictions

ValueError: decode_predictions expects a batch of predictions (i.e. a 2D array of shape (samples, 1000)). Found array with shape: (1, 2)

172.17.0.1 - - [2020-10-05 15:30:02] "POST /predict HTTP/1.1" 500 411 0.227771

Please I need help, once I run the docker build with my model I just get a 500 error them the terminal trows back this error above.

Problem using another .h5

Hi I tried your project and I liked it ;)

I tried also to change another .h5 file to create only a binary classification (dogs vs cats) but uncommenting the lines of the code to do it and putting my.h5 in the model folder, I have different problems. Can you explain me which should be the features to create a .h5 file to work on it ?
For example I tried to used this .h5 file (https://drive.google.com/open?id=19yICdtSbU_YkQBRxJ2if9KJwUL1oY5xs) from this tutorial (https://deeplizard.com/learn/video/XgzxH6G-ufA) to create only a binary classification.

Can you explain me how to re-edit it to work with this tutorial?

Thanks in advance and let me know.

I really appreciate your project ๐Ÿ‘๐Ÿป

A problem when I run my own model

When I run my own model,which is not a ResNet or other classical model, I follow the comment code to change the model path and load my model, I get the error as follow,ValueError: decode_predictions expects a batch of predictions (i.e. a 2D array of shape (samples, 1000)). Found array with shape: (1, 1)),what should I do to deal with this issue?

AttributeError: 'str' object has no attribute 'decode'

File "/home/anaconda3/envs/tf12/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 645, in load_weights_from_hdf5_group
original_keras_version = f.attrs['keras_version'].decode('utf8')

Object detection

Hello ,
Can you please make a tutorial of object detection problem ??

a error occurred !

MODEL_PATH = '/home/ljs/keras-flask-deploy-webapp/models/densenet121.h5'
File "/home/ljs/anaconda3/envs/zk/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 175, in load_model_from_hdf5
raise ValueError('No model found in config file.')

when I plan to use a downloaded keras model, above problem occurred, please help me.

ValueError : Cannot create group in read only mode.

Hi @mtobeiyf ,

While using my own trained model, I am facing this issue:
runfile('C:/Users/Singhkri/Videos/keras-flask-deploy-webapp-master/app.py', wdir='C:/Users/Singhkri/Videos/keras-flask-deploy-webapp-master')
Using TensorFlow backend.
Traceback (most recent call last):

File "", line 1, in
runfile('C:/Users/Singhkri/Videos/keras-flask-deploy-webapp-master/app.py', wdir='C:/Users/Singhkri/Videos/keras-flask-deploy-webapp-master')

File "C:\Users\Singhkri\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\Users\Singhkri\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Singhkri/Videos/keras-flask-deploy-webapp-master/app.py", line 17, in
model = load_model('models/model.h5')

File "C:\Users\Singhkri\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)

File "C:\Users\Singhkri\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\saving.py", line 221, in _deserialize_model
model_config = f['model_config']

File "C:\Users\Singhkri\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\utils\io_utils.py", line 302, in getitem
raise ValueError('Cannot create group in read only mode.')

ValueError: Cannot create group in read only mode.

It would be helpful if you can help in sorting out this.

Unable to recreate the application

I've been struggling for hours trying to get this sample to build and run.

Docker container works great, and let's me easily see what this program does. In fact I can shell into the running docker container and verify the python/pip setup, as well as the tooling listed in the requirements.txt. I can verify that the docker container is using python 3.6.12 and pip 21.0.1

I did a whole lot of testing, and unless I had python 3.6.12 AND pip 21.0.1 I could not get a pass on pip install -r requirements.txt

Consider this an issue raised. You absolutely have to have Python version 3.6.12 and pip version 21.0.1 to successfully reproduce this sample. Can I recommend that info be added to the coversheet README.md file? In fact, that might be a good action to take to close this open issue.

Note: I used pyenv for managing the python versions... After bringing Python to 3.6.12 I then had to $ pip install --upgrade pip (which brought pip from 18.1 to 21.0.1)

Many thanks for sharing this repo. You've helped me a whole lot understand classification within python

ImportError: Could not import PIL.Image. The use of load_img requires PIL.

Hi,

FYI, after selected image and click predict, no prediction results after waiting for 1 minute, found error below.
raise ImportError('Could not import PIL.Image. '
ImportError: Could not import PIL.Image. The use of load_img requires PIL.

solution
apt-get install python-pillow
reference python-pillow/Pillow#3088

my env
ubuntu18.04
python 3.5 <- same error after "apt-get install python-pillow"
python 3.6 <- error solved after "apt-get install python-pillow"

test solution
test with Python 3.6.8 (default, Aug 20 2019, 17:12:48) [GCC 8.3.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>> from PIL import Image as pil_image

Update to Python 3

as python 2 will be depreciating in 2019 . Its good to have a updated system.

Displaying result

Hi,

I'm using this repository for a segmentation based web-app and I was wondering if there was a way to display the output image from the neural network as opposed to json/text data?

Thanks!

Problems with running in Docker

Hello, thank you for your great job! The problem I have is I can't run this project. I use Docker but each time get this error:

Traceback (most recent call last):
  File "app.py", line 7, in <module>
    from gevent.pywsgi import WSGIServer
  File "/usr/local/lib/python3.6/dist-packages/gevent/__init__.py", line 87, in <module>
    from gevent._hub_local import get_hub
  File "/usr/local/lib/python3.6/dist-packages/gevent/_hub_local.py", line 101, in <module>
    import_c_accel(globals(), 'gevent.__hub_local')
  File "/usr/local/lib/python3.6/dist-packages/gevent/_util.py", line 105, in import_c_accel
    mod = importlib.import_module(cname)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "src/gevent/__greenlet_primitives.pxd", line 12, in init gevent.__hub_local
ValueError: greenlet.greenlet size changed, may indicate binary incompatibility. Expected 128 from C header, got 40 from PyObject

I was trying to update gevent, I have the latest version of greenlet.
I also tried local installm but seem to get the same problems at first, that's why switched to Docker as usually it is much easier to run.

What do I have to do? Thank you!

expected conv2d_1

I'm getting

ValueError: Error when checking input: expected conv2d_1_input to have shape (150, 150, 3) but got array with shape (224, 224, 3)

after clicking 'Predict' using my own model trained with Keras.

Attached is the model.h5 if anyone is interested in trying it out.

Tensorflow: 1.12.0
Keras: 2.2.4
Python: 3.5.2

model.zip

Implement_for_Object_detection

Hi,

I want to deploy my object detection using the same approach. First I will upload the original image on click of predict, the model will process and gives me an annotated image. Then i need to upload it the webpage next to the original image.

I have all the required python code. Can you help me how/what changes i need to do in HTML or js file?

MODEL_PATH = 'models/your_model.h5' gives error

afshan@afshan-VirtualBox:~/keras-flask-deploy-webapp$ python3 app.py
/usr/local/lib/python3.5/dist-packages/h5py/init.py:36: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type.
from ._conv import register_converters as _register_converters
Using TensorFlow backend.
Traceback (most recent call last):
File "app.py", line 27, in
model = load_model(MODEL_PATH)
File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 268, in load_model
raise ValueError('No model found in config file.')
ValueError: No model found in config file.

I am getting error ValueError: No model found in config file when I pass save.h5 file to FILE_PATH.

Any one can help??

Deploying to Digital Ocean as an app. Keras load_model not working

Has anybody come an issue deploying a custom model (h5 or saved_model format) and receiving the following when trying to load the model using tf.keras.models.load_model:

radiographyml | 17:42:33 Traceback (most recent call last): radiographyml | 17:42:33 File "app.py", line 53, in <module> radiographyml | 17:42:33 model = load_model(MODEL_PATH) radiographyml | 17:42:33 File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py", line 186, in load_model radiographyml | 17:42:33 loader_impl.parse_saved_model(filepath) radiographyml | 17:42:33 File "/usr/local/lib/python3.7/site-packages/tensorflow/python/saved_model/loader_impl.py", line 113, in parse_saved_model radiographyml | 17:42:33 constants.SAVED_MODEL_FILENAME_PB)) radiographyml | 17:42:33 OSError: SavedModel file does not exist at: ./models/my_model.h5/{saved_model.pbtxt|saved_model.pb}

The model is quite big, and I'm using git lfs to upload it to github. The repo is synced to Digital Ocean and refreshes each time a new commit is made. I've been stuck for a while here, trying different models, versions of TF, python, filepaths etc etc

ValueError: Error when checking input: expected conv2d_1_input to have shape

I had loaded my own model as mentioned by you. While my training I had resized images with 28*28.
I have changed this img size in your app.py also but now I am getting error as mentioned below:

"ValueError: Error when checking input: expected conv2d_1_input to have shape (28, 28, 3) but got array with shape (28, 28, 4)"
Can you please tell me how can I convert 28,28,4 to 28,28,3 so that I could get predictions as per my model ?

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.