4

I am using keras' pre-trained model and the error came up when calling ResNet50(weights='imagenet'). I have the following code in flask server:

def getVGG16Prediction(img_path):

    model = VGG16(weights='imagenet', include_top=True)
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    pred = model.predict(x)
    return sort(decode_predictions(pred, top=3)[0])


def getResNet50Prediction(img_path):

    model = ResNet50(weights='imagenet') #ERROR HERE
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    preds = model.predict(x)
    return decode_predictions(preds, top=3)[0]

when calling in in main, it works fine

if __name__ == "__main__":
    STATIC_PATH = os.getcwd()+"/static"
    print(getVGG16Prediction(STATIC_PATH+"/18.jpg"))
    print(getResNet50Prediction(STATIC_PATH+"/18.jpg"))

however, the ValueError rises when I call it from the flask POST function:

@app.route("/uploadMultipleImages", methods=["POST"])
def uploadMultipleImages():
    uploaded_files = request.files.getlist("file[]")
    weight = request.form.get("weight")

    for file in uploaded_files:
        path = os.path.join(STATIC_PATH, file.filename)
        file.save(os.path.join(STATIC_PATH, file.filename))
        result = getResNet50Prediction(path)

The full error is as follow:

ValueError: Tensor("cond/pred_id:0", dtype=bool) must be from the same graph as Tensor("batchnorm/add_1:0", shape=(?, 112, 112, 64), dtype=float32)

Any comment or suggestion is highly appreciated. Thank you.

matchifang
  • 5,190
  • 12
  • 47
  • 76

2 Answers2

7

you'll need open different session and specify which graph goes with each session, else Keras will replace each graph as default.

from tensorflow import Graph, Session, load_model
from Keras import backend as K

Loading the graphs:

graph1 = Graph()
    with graph1.as_default():
        session1 = Session()
        with session1.as_default():
            model = load_model(foo.h5)

graph2 = Graph()
    with graph2.as_default():
        session2 = Session()
        with session2.as_default():
            model2 = load_model(foo2.h5)

Predicting/Using the graphs:

K.set_session(session1)
    with graph1.as_default():
        result = model.predict(data)
Megan Hardy
  • 397
  • 4
  • 12
2

The problem here is your loop. You're trying to generate a new graph in each iteration.

This line

model = ResNet50(weights='imagenet')

Should only be called once. So either define it as a global variable or create it before and pass it as a parameter to getResNet50Prediction()

gidim
  • 2,314
  • 20
  • 23
  • Thank you for your answer gidim. I tried defining it as a global variable, and I got another error - if you could have a look at: http://stackoverflow.com/questions/41991756/valueerror-tensor-is-not-an-element-of-this-graph Thank you. – matchifang Feb 01 '17 at 23:28