属性错误:“张量”对象没有属性“输出”

问题描述 投票:0回答:1

我经常获取图像分类模型的热图,以了解我的模型如何做出调整模型的决策。但不幸的是我遇到了一个错误,我不知道如何正确修复它。

import numpy as np
import tensorflow as tf
from tensorflow import keras

# Display
from IPython.display import Image, display
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from keras.preprocessing.image import load_img
from keras.preprocessing import image
import numpy as np
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
from tensorflow.keras import models
from timeit import default_timer as timer
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from keras.models import load_model

model_builder = keras.applications.xception.Xception
img_size = (299, 299)
preprocess_input = keras.applications.xception.preprocess_input
decode_predictions = keras.applications.xception.decode_predictions

img_path = "box.jpg"

def make_gradcam_heatmap(
    img_array, model, last_conv_layer_name, classifier_layer_names
):
    # First, we create a model that maps the input image to the activations
    # of the last conv layer
    last_conv_layer = layer_output = model.get_layer('vgg16').get_layer(last_conv_layer_name).output
    last_conv_layer_model = keras.Model(model.inputs, last_conv_layer.output)

# Prepare image
img_array = preprocess_input(get_img_array(img_path, size=img_size))

# Make model
model = models.load_model("Box_Model_Augmented(15.11).h5")

last_conv_layer_name = "block5_conv3"
classifier_layer_names = ["global_max_pooling2d", "predictions"]

# Generate class activation heatmap
heatmap = make_gradcam_heatmap(
    img_array, model, last_conv_layer_name, classifier_layer_names
)

python keras deep-learning tf.keras keras-layer
1个回答
0
投票

您似乎遇到了与从 TensorFlow 导入 Keras 相关的错误。该问题可能是由于 Keras 的导入方式造成的。您应该使用 fromtensorflow.keras 从 TensorFlow 导入 Keras,而不是使用 from keras。

以下是修改代码的方法:

from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import load_model
© www.soinside.com 2019 - 2024. All rights reserved.