gpt4 book ai didi

python - 凯拉斯, tensorflow : "TypeError: Cannot interpret feed_dict key as Tensor"

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:13 25 4
gpt4 key购买 nike

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

但是,当同时从两台或多台计算机使用该应用程序时,会出现以下错误消息并且该应用程序无法运行。

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.

这是我的图像分类代码。

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. 使用 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 使用这个:

    flask run --without-threads

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

关于python - 凯拉斯, tensorflow : "TypeError: Cannot interpret feed_dict key as Tensor",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51588186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com