Closed emilwidlund closed 6 years ago
Looks like the server is trying to load the model a second time within the same tensorflow session.
Shouldn't be too hard to fix. Is it possible for you to share your code with me?
Thanks for your reply! I'm basically trying to build a simple REST-api that takes an image and run the prediction against it and then return the sfw/nsfw scores.
Here is the simple code so far:
from flask import Flask, request, jsonify
from PIL import Image
from open_nsfw.classify_nsfw import classify
import io
app = Flask(__name__)
@app.route('/', methods=['POST'])
def process_image():
if request.method == 'POST':
# Declare the form-image as variable 'file'
file = request.files['image']
# Open the image with the PIL-module
image = Image.open(file)
# Create an empty bytesIO which will hold the image in memory
temp = io.BytesIO()
# Save the image to the empty bytesIO-object
# This way, we are able to save the image in memory with a reduced quality
image.save(temp, format='JPEG', quality=40)
prediction = classify(temp.getvalue(), 'open_nsfw/data/open_nsfw-weights.npy')
return jsonify(
sfw=float(prediction[0]),
nsfw=float(prediction[1])
)
This also meant that I slightly modified your code to take image data instead of an image path reference. This issue is not related to my modification though.
The error you've posted above means that model.build
is getting called twice within the same tensorflow session, like:
model = OpenNsfwModel()
with tf.Session() as sess:
...
model.build() # fine
...
model.build() # error
....
I guess the interesting part of your code is in the classify
method. Maybe the session is not getting closed or reused when using gunicorn?
You could also try to create a new graph for each session:
with tf.Session(graph=tf.Graph()) as sess:
instead of with tf.Session() as sess:
Thanks a bunch! The latter solution worked fine by creating a new graph each session. Thanks!
This model works perfectly fine with Flask and its internal development server. But as soon as I use it with a production server like Gunicorn, it seems to throw a Tensorflow-error complaining about the use of a certain variable.
Here's the full stack. Thanks.