Keras,TensorFlow:"TypeError:无法将feed_dict键解释为Tensor" [英] Keras, TensorFlow : "TypeError: Cannot interpret feed_dict key as Tensor"

查看:149
本文介绍了Keras,TensorFlow:"TypeError:无法将feed_dict键解释为Tensor"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用keras fune-tuning开发图像分类应用程序. 我将该应用程序部署到Web服务器,并且图像分类成功.

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:无法将feed_dict键解释为张量:Tensor Tensor("Placeholder:0",shape =(3,3,3,64),dtype = float32)不是此图的元素.

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 = 3

input_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.0

pred = model.predict(x)[0]

如何在多台计算机上同时运行此应用程序?

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

感谢您阅读这篇文章.

推荐答案

我发现有两种解决方法,具体取决于各种上下文:

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

  1. 使用clear_session()函数:

  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()

  • 呼叫_make_predict_function() :

  • Calling _make_predict_function():

    加载经过训练的模型调用后:

    After you load your trained model call:

    model._make_predict_function()
    

    请参见说明

    禁用线程:

    如果您正在运行django服务器,请使用以下命令:

    If you are running django server use this command:

    python manage.py runserver --nothreading 
    

    对于烧瓶使用此:

    flask run --without-threads
    

  • 如果以上解决方案均无效,请检查以下链接 keras问题#6462 keras问题#2397

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

    这篇关于Keras,TensorFlow:"TypeError:无法将feed_dict键解释为Tensor"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆