Keras, TensorFlow : TypeError: Cannot interpret feed_dict key as Tensor

2024/5/20 13:56:49

I am trying to use keras fune-tuning to develop image classify applications. I deployed that application to a web server and the image classification is succeeded.

However, when the application is used from two or more computers at the same time, the following error message appears and the application doesn't work.

TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(3, 3, 3, 64), dtype=float32) is not an element of this graph.

Here is my code for image classification.

img_height, img_width = 224, 224
channels = 3input_tensor = Input(shape=(img_height, img_width, channels))
vgg19 = VGG19(include_top=False, weights='imagenet', input_tensor=input_tensor)fc = Sequential()
fc.add(Flatten(input_shape=vgg19.output_shape[1:]))
fc.add(Dense(256, activation='relu'))
fc.add(Dropout(0.5))
fc.add(Dense(3, activation='softmax'))model = Model(inputs=vgg19.input, outputs=fc(vgg19.output))model.load_weights({h5_file_path})model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])img = image.load_img({image_file_path}, target_size=(img_height, img_width))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = x / 255.0pred = model.predict(x)[0]

How can I run this application in multiple computers at the same time?

Thank you for reading this post.

Answer

I found that there are a couple of workarounds, depending on various context:

  1. Using clear_session() function:

    from keras import backend as K
    

    Then do following at the beginning or at the end of the function, after predicting all the data:

    K.clear_session()
    
  2. Calling _make_predict_function():

    After you load your trained model call:

    model._make_predict_function()
    

    See explanation

  3. Disable threading:

    If you are running django server use this command:

    python manage.py runserver --nothreading 
    

    For flask use this:

    flask run --without-threads
    

If none of the above solutions work, check these links keras issue#6462, keras issue#2397

https://en.xdnf.cn/q/72929.html

Related Q&A

How to get matplotlib to place lines accurately?

By default, matplotlib plot can place lines very inaccurately.For example, see the placement of the left endpoint in the attached plot. Theres at least a whole pixel of air that shouldnt be there. In f…

Using Flask as pass through proxy for file upload?

Its for app engines blobstore since its upload interface generates a temporary endpoint every time. Id like to take the comlexity out of frontend, Flask would take the post request and forward it to th…

What does printing an empty line do?

I know this question may well be the silliest question youve heard today, but to me it is a big question at this stage of my programming learning.Why is the second empty line needed in this Python code…

Django - how do I _not_ dispatch a signal?

I wrote some smart generic counters and managers for my models (to avoid select count queries etc.). Therefore I got some heavy logic going on for post_save. I would like to prevent handling the signa…

Python 3 Decoding Strings

I understand that this is likely a repeat question, but Im having trouble finding a solution.In short I have a string Id like to decode:raw = "\x94my quote\x94" string = decode(raw)expected f…

how get context react using django

i need get context in react using django but i cant do itthis is my code in my jsx <h1>{{titulo}}</h1> <h2>{{ejemplo}}</h2>in my template:{% load staticfiles %} <!DOCTYPE ht…

Copy certain files from one folder to another using python

I am trying to copy only certain files from one folder to another. The filenames are in a attribute table of a shapefile. I am successful upto writing the filenames into a .csv file and list the column…

I want to create django popup form in my project [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

SVG to PNG with custom fonts in Python

Im using Cairo/RSVG based solution for rasterizing SVG to PNG. Its already beeb described on StackOverflow in Convert SVG to PNG in Python. However, this solution doesnt seem to work with custom fonts.…

How to solve an equation with variables in a matrix in Python?

im coding in Pyhon, and Im working on stereo-correlation. I want to resolve this equation : m = K.T.Mm,K,M are know.where :M is the homogeneous coordinate of a point in the cartesian coordinate system…