0

This is my first time using services of Heroku. I am trying to deploy a Image Classifier flask app on Heroku. I have took help from other post but no result so far. It is running perfectly on local machine.

File Structure:

ImageClassifier_DeepLearning
 |
 + static
 |      |
 |      + style.css
 |
 + templates
 |       |
 |       + index.html
 |       + index2.html
 |       + show.html
 |       + upload.html
 |
 + uploads
 |       | 
 |       +.jpg,.jpeg ....
 |
 + Procfile
 |
 + requirements.txt
 |
 + app.py
 |
 + model2.h5

Procfile:

web: gunicorn --bind 0.0.0.0:$PORT app: app

requirements.txt:

gunicorn==20.0.4
keras
numpy==1.18.5
h5py==2.10.0
pillow==7.1.2
flask_uploads
Flask==1.1.2
protobuf>=3.8.0
tensorflow==1.0.0
Werkzeug==1.0.1
scipy==1.4.1
scikit-image==0.16.2
setuptools>=41.0.0

Here is the snapshot of the error got from logs: enter image description here

heroku ps:

enter image description here

Thanks a ton if anyone can help!:)

app.py

import tensorflow as tf
import keras
from flask import Flask, render_template, request
from flask_uploads import UploadSet, configure_uploads,IMAGES
from scipy.misc import imsave, imread, imresize
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
from tensorflow.keras.models import load_model

import numpy as np
from werkzeug import secure_filename
import keras.models
import re
import sys
import os

app = Flask(__name__)

model = load_model('model2.h5')
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

global graph
graph = tf.compat.v1.get_default_graph()

photos = UploadSet('photos', IMAGES)

app.config['UPLOADED_PHOTOS_DEST'] = '.'
configure_uploads(app, photos)

def preprocess_image(file_path):
    img = image.load_img(file_path, target_size=(224,224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    return x

@app.route('/')
def index():
    return render_template("index.html")

@app.route('/upload', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        f = request.files['photo']

        basepath = os.path.dirname(__file__)
        file_path = os.path.join(
            basepath, 'uploads', secure_filename(f.filename))
        f.save(file_path)

        x = preprocess_image(file_path)

        out = model.predict(x)
        u = decode_predictions(out, top=3)[0]
        s1 = u[0][1]
        s2 = u[0][2]*100
        s3 = u[1][1]
        s4 = u[1][2]*100
        s5 = u[2][1]
        s6 = u[2][2]*100
        print(s1,s2,s3)
        return render_template("index2.html",s1=s1,s2=s2,s3=s3,s4=s4,s5=s5,s6=s6)

if __name__ == "__main__":
    port = int(os.environ.get('PORT', 8080))
    app.run(host='0.0.0.0', port=port)

Complete logs: enter image description here

Roopak
  • 31
  • 7
  • What's the CLI command you are using to run your project locally? – Tin Nguyen Aug 06 '20 at 07:14
  • @TinNguyen ```$ python app.py``` or i just run the app.py script in VS Code. – Roopak Aug 06 '20 at 07:18
  • Try to get it to run locally with `gunicorn` or adjust your `Procfile` to the command you are using locally. In your code you will have to provide logic to bind to the port `import os; PORT=os.environ.get("PORT", 8080)` – Tin Nguyen Aug 06 '20 at 07:24
  • @TinNguyen I made the changes as you suggested, running fine on local machine on port 8080 but while made the same changes in github, getting the same error: app crashed. – Roopak Aug 06 '20 at 08:00
  • show me your `app.py` – Tin Nguyen Aug 06 '20 at 08:03
  • I have added the app.py code in the post, thanks – Roopak Aug 06 '20 at 08:06
  • Provide more heroku logs. Specifically from `heroku[web.1]` it should have a traceback error of the reason why your app crashed. I guess one of your dependency isn't getting properly installed. – Tin Nguyen Aug 06 '20 at 08:06
  • I have added the complete logs ``` heroku logs --app deeplearning-classifier ``` in the post – Roopak Aug 06 '20 at 08:13
  • 1
    Your keras package requires tensorflow 2.2 or higher. Since it is running locally for you you can do `python -m pip freeze > requirements.txt` and commit that new `requirements.txt`. – Tin Nguyen Aug 06 '20 at 08:16
  • @TinNguyen I commited the new requirements.txt. Now the problem is ```Compiled slug size: 542.3M is too large (max is 500M)```. It's my free account that's why i put the tensorflow version==1.0.0 earlier. version 2.1.0 is above 400 mb. So, that's the problem. – Roopak Aug 06 '20 at 08:50
  • The 500 MB cannot be increased https://devcenter.heroku.com/changelog-items/1145 https://devcenter.heroku.com/articles/slug-compiler#slug-size Heroku may not be suited for your hosting needs. – Tin Nguyen Aug 06 '20 at 09:04
  • @TinNguyen Thanks tin for your advices,, can you suggest me some other services for hosting. – Roopak Aug 06 '20 at 09:10
  • If you are a Student -> Digital Ocean (you get $50 store credit from GitHub Education Pack); if you have a CC -> Google free tier cloud compute, AWS offered something similar; I personally host my stuff on Vultr and pay $2.5 a month for a VPS. – Tin Nguyen Aug 06 '20 at 09:13
  • @TinNguyen Thanks alot :) – Roopak Aug 06 '20 at 15:02

0 Answers0