Ошибка тензорного потока: тензор потока. python .framework.errors_impl.InvalidArgumentError - PullRequest
0 голосов
/ 03 мая 2020

При попытке развернуть модель глубокого обучения с использованием flask, я получаю сообщение об ошибке: "tenorflow. python .framework.errors_impl.InvalidArgumentError: Tensor vgg16_input: 0, указанный в feed_devices или fetch_devices, не найден в График ". Я использую сохраненный файл модели в формате 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, а также другую пользовательскую модель, но обе показывают похожую ошибку.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...