keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
62.13k stars 19.49k forks source link

tensorflow.python.framework.errors_impl.InvalidArgumentError: Tensor conv2d_1_input:0, specified in either feed_devices or fetch_devices was not found in the Graph #13901

Closed Ananthan4451 closed 4 years ago

Ananthan4451 commented 4 years ago

Hi, i'm using keras and tensorflow for this code

from flask import Flask, request, jsonify, render_template
import numpy
from PIL import Image
import os
import tensorflow.keras
from werkzeug.utils import secure_filename
from keras.models import load_model

app = Flask(__name__)

model = load_model('traffic_classifier.h5')
model._make_predict_function()
classes = { 1:'Speed limit (20km/h)',
            2:'Speed limit (30km/h)',      
            3:'Speed limit (50km/h)',       
            4:'Speed limit (60km/h)',      
            5:'Speed limit (70km/h)',    
            6:'Speed limit (80km/h)',      
            7:'End of speed limit (80km/h)',     
            8:'Speed limit (100km/h)',    
            9:'Speed limit (120km/h)',     
           10:'No passing',   
           11:'No passing veh over 3.5 tons',     
           12:'Right-of-way at intersection',     
           13:'Priority road',    
           14:'Yield',     
           15:'Stop',       
           16:'No vehicles',       
           17:'Veh > 3.5 tons prohibited',       
           18:'No entry',       
           19:'General caution',     
           20:'Dangerous curve left',      
           21:'Dangerous curve right',   
           22:'Double curve',      
           23:'Bumpy road',     
           24:'Slippery road',       
           25:'Road narrows on the right',  
           26:'Road work',    
           27:'Traffic signals',      
           28:'Pedestrians',     
           29:'Children crossing',     
           30:'Bicycles crossing',       
           31:'Beware of ice/snow',
           32:'Wild animals crossing',      
           33:'End speed + passing limits',      
           34:'Turn right ahead',     
           35:'Turn left ahead',       
           36:'Ahead only',      
           37:'Go straight or right',      
           38:'Go straight or left',      
           39:'Keep right',     
           40:'Keep left',      
           41:'Roundabout mandatory',     
           42:'End of no passing',      
           43:'End no passing veh > 3.5 tons' }

@app.route('/')
def index():
    # Main page
    return render_template('index.html')

@app.route('/traffic')
def traffic():
    # Main page
    return render_template('traffic.html')

@app.route('/sleep')
def sleep():
    # Main page
    return render_template('sleep.html')

@app.route('/predict',methods=['POST'])
def predict():
    '''
    For rendering results on HTML GUI
    '''
    if request. method == "POST":
        #image=request. form["fileupload"]

        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)  

    image = Image.open(file_path)
    image = image.resize((30,30))
    image = numpy.expand_dims(image, axis=0)
    image = numpy.array(image)

    pred = model.predict_classes([image])[0]

    sign = classes[pred+1]

    return render_template('traffic.html', prediction_text='This sign represents {}'.format(sign))

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

it shows the error tensorflow.python.framework.errors_impl.InvalidArgumentError: Tensor conv2d_1_input:0, specified in either feed_devices or fetch_devices was not found in the Graph

please help me on this

Ananthan4451 commented 4 years ago

solved it by adding

config = tensorflow.ConfigProto( device_count={'GPU': 1}, intra_op_parallelism_threads=1, allow_soft_placement=True )

config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.6

session = tensorflow.Session(config=config) keras.backend.set_session(session)

model = load_model('traffic_classifier.h5') model._make_predict_function()

Shahadbal commented 7 months ago

I encountered the same error and when I adding your solution I encounter another error FailedPreconditionError: Error while reading resource variable conv2d_1/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/conv2d_1/bias/class tensorflow::Var does not exist. [[{{node conv2d_1/BiasAdd/ReadVariableOp}}]]

I'm working on conda environment without utilizing GPU, working on Flask and tensorflow = 1.14