【问题标题】:Keras, TensorFlow : "TypeError: Cannot interpret feed_dict key as Tensor"Keras,TensorFlow:“TypeError:无法将 feed_dict 键解释为张量”
【发布时间】:2018-07-30 06:32:39
【问题描述】:

我正在尝试使用 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]

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

感谢您阅读这篇文章。

【问题讨论】:

标签: python tensorflow keras


【解决方案1】:

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

  1. 使用clear_session()函数

    from keras import backend as K
    

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

    K.clear_session()
    
  2. 致电_make_predict_function()

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

    model._make_predict_function()
    

    explanation

  3. 禁用线程:

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

    python manage.py runserver --nothreading 
    

    烧瓶使用这个:

    flask run --without-threads
    

如果以上解决方案都不起作用,请检查这些链接keras issue#6462keras issue#2397

【讨论】:

  • 我遇到了这个问题。我确实在 django 应用程序中运行了我的模型。运行没有线程的应用程序解决了这个问题。
  • 无线程运行服务器不是解决方案!那是最坏教派的黑客攻击
  • K.clear_session() 帮助我成功运行了 load_model()。谢谢!
  • K.clear_session() 也帮助了我。谢谢你的精彩解释
猜你喜欢
  • 2017-04-08
  • 2017-08-06
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多