Tensorflow错误:tensorflow.python.framework.errors_impl.InvalidArgumentError

时间:2020-05-03 17:36:22

标签: tensorflow flask keras deep-learning

尝试使用Flask部署深度学习模型时,出现错误:“ tensorflow.python.framework.errors_impl.InvalidArgumentError:在图中未找到feed_devices或fetch_devices中指定的Tensor vgg16_input:0”。 我正在使用h5格式的保存的模型文件。 app.py文件为:

from __future__ import division, print_function
# coding=utf-8
import sys
import os
import glob
import re
import numpy as np
import tensorflow as tf
# Keras
from keras.applications.imagenet_utils import preprocess_input, decode_predictions
from keras.models import load_model
from keras.preprocessing import image

# Flask utils
from flask import Flask, redirect, url_for, request, render_template
from werkzeug.utils import secure_filename
from gevent.pywsgi import WSGIServer

# Define a flask app
app = Flask(__name__)

# Model saved with Keras model.save()
MODEL_PATH = 'C:/Users/HP WORLD/Downloads/my_model_1.h5'

# Load your trained model
model = load_model(MODEL_PATH)

model._make_predict_function()  

print('Model loaded. Check http://127.0.0.1:5000/')

def model_predict(img_path, model):
   img = image.load_img(img_path, target_size=(224, 224))
   # Preprocessing the image
   x = image.img_to_array(img)

   x = np.expand_dims(x, axis=0)


   preds = model.predict(x)
   return preds

@app.route('/', methods=['GET'])
def index():
    # Main page
    return render_template('index.html')


@app.route('/predict', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        # Get the file from post request
        f = request.files['file']

    # Save the file to ./uploads
    basepath = os.path.dirname(__file__)
    file_path = os.path.join(
        basepath, 'uploads', secure_filename(f.filename))
    f.save(file_path)

    # Make prediction
    preds = model_predict(file_path, model)


         # Convert to string
    if(int(preds[0][0])==1):
        return 'Mild'
    elif(int(preds[0][1])==1):
        return 'Moderate'
    elif(int(preds[0][2])==1):
        return 'No_DR'
    elif(int(preds[0][3])==1):
        return 'Proliferate_DR'
    else:
        return 'Severe'


return None


  if __name__ == '__main__':
    app.run(debug=True)

我尝试了预训练模型vgg16,也尝试了另一个自定义模型,但是两者都显示出相似的错误。

0 个答案:

没有答案