python - Tensor Tensor("predictions/Softmax:0", shape=(?, 1000), dtype=float32) 不是该图的元素
问题描述
我正在尝试遵循一个关于如何使用预训练的 VGG 模型进行图像分类的简单教程。我拥有的代码:
from keras.applications.vgg16 import VGG16
from keras.preprocessing.image import load_img, img_to_array
from keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np
class KerasModel(object):
def __init__(self):
self.model = VGG16()
def evaluate(self):
image = load_img('mug.jpg', target_size=(224,224))
image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
image = preprocess_input(image)
yhat = self.model.predict(image)
label = decode_predictions(yhat)
label = label[0][0]
return ('%s (%.2f%%)' % (label[1]), label[2]*100)
这给出了错误:Tensor Tensor("predictions/Softmax:0", shape=(?, 1000), dtype=float32) is not an element of this graph。
在搜索了这个错误之后,我得到了这个代码:
from keras.applications.vgg16 import VGG16
from keras.preprocessing.image import load_img, img_to_array
from keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np
import tensorflow as tf
graph = tf.get_default_graph()
class KerasModel(object):
def __init__(self):
self.model = VGG16()
def evaluate(self):
image = load_img('mug.jpg', target_size=(224,224))
image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
image = preprocess_input(image)
with graph.as_default():
yhat = self.model.predict(image)
label = decode_predictions(yhat)
label = label[0][0]
return ('%s (%.2f%%)' % (label[1]), label[2]*100)
但这仍然会导致相同的错误。有人可以帮我吗?我不明白我做错了什么,因为该教程似乎对每个人都有效。
型号总结:
_________________________________________________________________
xvision | Layer (type) Output Shape Param #
xvision | =================================================================
xvision | input_1 (InputLayer) (None, 224, 224, 3) 0
xvision | _________________________________________________________________
xvision | block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
xvision | _________________________________________________________________
xvision | block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
xvision | _________________________________________________________________
xvision | block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
xvision | _________________________________________________________________
xvision | block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
xvision | _________________________________________________________________
xvision | block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
xvision | _________________________________________________________________
xvision | block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
xvision | _________________________________________________________________
xvision | block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
xvision | _________________________________________________________________
xvision | block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
xvision | _________________________________________________________________
xvision | block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
xvision | _________________________________________________________________
xvision | block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
xvision | _________________________________________________________________
xvision | block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
xvision | _________________________________________________________________
xvision | block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
xvision | _________________________________________________________________
xvision | block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
xvision | _________________________________________________________________
xvision | block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
xvision | _________________________________________________________________
xvision | block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
xvision | _________________________________________________________________
xvision | block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
xvision | _________________________________________________________________
xvision | block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
xvision | _________________________________________________________________
xvision | block5_pool (MaxPooling2D) (None, 7, 7, 512) 0
xvision | _________________________________________________________________
xvision | flatten (Flatten) (None, 25088) 0
xvision | _________________________________________________________________
xvision | fc1 (Dense) (None, 4096) 102764544
xvision | _________________________________________________________________
xvision | fc2 (Dense) (None, 4096) 16781312
xvision | _________________________________________________________________
xvision | predictions (Dense) (None, 1000) 4097000
xvision | =================================================================
xvision | Total params: 138,357,544
xvision | Trainable params: 138,357,544
xvision | Non-trainable params: 0
xvision | _________________________________________________________________
xvision | None
解决方案
似乎 Keras 不是线程安全的,因此您需要在每个线程中初始化模型。修复正在调用:_make_predict_function()
它确实对我有用。这是一个干净的例子:
from keras.models import load_model
def load_model():
model = load_model('./my_model.h5')
model._make_predict_function()
print('model loaded') # just to keep track in your server
return model
希望这可以帮助。
推荐阅读
- pandas - 替换数据框中的公式(熊猫)
- python-3.x - APIError(code=-1099):未找到、未经身份验证或未经授权
- vue.js - 如何以编程方式扩展 v-data-table 组件中的所有行(Vuetify 1.5 版)
- python - 在 RStudio Cloud 中使用网状结构时出错:AttributeError:模块 'altair' 没有属性 'Chart'
- elasticsearch - 从 Elasticsearh 中的嵌套和聚合中删除选定的过滤器以执行过滤搜索
- php - 使用 NGINX 反向代理作为子域通过 Docker 安装 Bookstack 的完整示例
- react-native - React Native Scrollview ref 回来未定义(大部分)
- c# - C# - Linq 通过其他属性在列表属性中选择
- android - setVisibility 只能随机工作
- java - 页面旋转时如何匹配pdf坐标?