首页 文章

Keras,TensorFlow:“TypeError:无法将feed_dict键解释为Tensor”

提问于
浏览
2

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

但是,当同时从两台或多台计算机使用该应用程序时,会出现以下错误消息,并且该应用程序不起作用 .

TypeError:无法将feed_dict键解释为Tensor:Tensor Tensor(“Placeholder:0”,shape =(3,3,3,64),dtype = float32)不是此图的元素 .

这是我的图像分类代码 .

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]

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

感谢您阅读这篇文章 .

1 回答

  • 0

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

    • Using clear_session() function
    from keras import backend as K
    

    然后在预测所有数据之后,在函数的开头或结尾处执行以下操作:

    K.clear_session()
    
    • Calling _make_predict_function()

    加载训练有素的模型调用后:

    model._make_predict_function()
    

    explanation

    • Disable threading:

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

    python manage.py runserver --nothreading
    

    对于烧瓶使用这个:

    flask run --without-threads
    

    如果以上解决方案均无效,请查看以下链接keras issue#6462keras issue#2397

相关问题