【发布时间】:2021-06-28 20:41:45
【问题描述】:
我已经训练了一个模型来判断图像是对还是错(只有 2 个类),并且我使用了 keras 网站上的指南 GradCAM。
输入图像被重新整形为 (250, 250),然后通过将图像 numpy 数组除以 255 进行归一化。然后将其传递给模型的训练。
这是附加的代码。我遇到以下错误:Invalid reduction dimension (1 for input with 1 dimension(s) [Op:Mean]
数据
image = cv2.imread("/content/drive/MyDrive/SendO2/Train/correct/droidcam-20210128-152301.jpg")
image = cv2.resize(image, (250, 250))
image = image.astype('float32') / 255
image = np.expand_dims(image, axis=0)
型号
model = Sequential()
#Adding first convolutional layer
model.add(Conv2D(64, (3,3), activation="relu"))
#Adding maxpooling
model.add(MaxPooling2D((2,2)))
#Adding second convolutional layer and maxpooling
model.add(Conv2D(64, (3,3), activation="relu"))
model.add(MaxPooling2D((2,2)))
#Adding third convolutional layer and maxpooling
model.add(Conv2D(64, (3,3), activation="relu"))
model.add(MaxPooling2D((2,2)))
#Adding fourth convolutional layer and maxpooling
model.add(Conv2D(64, (3,3), activation="relu"))
model.add(MaxPooling2D((2,2)))
#Adding fifth convolutional layer and maxpooling
model.add(Conv2D(64, (3,3), activation="relu"))
model.add(MaxPooling2D((2,2)))
#Flattening the layers
model.add(Flatten())
model.add(Dense(128, input_shape = X.shape[1:], activation="relu"))
#Output Layer. Since, the image is right/wrong, only 2 neurons is needed.
model.add(Dense(2, activation = "softmax"))
# model.add(Dense(2, activation = "sigmoid"))
model.compile(optimizer = "adam", loss = "sparse_categorical_crossentropy", metrics = ["accuracy"])
GradCAM
def get_img_array(img_path, size):
# `img` is a PIL image of size 299x299
img = keras.preprocessing.image.load_img(img_path, target_size=size)
# `array` is a float32 Numpy array of shape (299, 299, 3)
array = keras.preprocessing.image.img_to_array(img)
# We add a dimension to transform our array into a "batch"
# of size (1, 299, 299, 3)
array = np.expand_dims(array, axis=0)
print(array.shape)
return array
def make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=None):
# First, we create a model that maps the input image to the activations
# of the last conv layer as well as the output predictions
grad_model = tf.keras.models.Model(
[model.inputs], [model.get_layer(last_conv_layer_name).output, model.output]
)
# Then, we compute the gradient of the top predicted class for our input image
# with respect to the activations of the last conv layer
with tf.GradientTape() as tape:
last_conv_layer_output, preds = grad_model(img_array)
if pred_index is None:
pred_index = tf.argmax(preds[0])
class_channel = preds[:, pred_index]
# This is the gradient of the output neuron (top predicted or chosen)
# with regard to the output feature map of the last conv layer
grads = tape.gradient(class_channel, last_conv_layer_output)
# This is a vector where each entry is the mean intensity of the gradient
# over a specific feature map channel
pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
# pooled_grads = tf.reduce_mean(grads)
# We multiply each channel in the feature map array
# by "how important this channel is" with regard to the top predicted class
# then sum all the channels to obtain the heatmap class activation
last_conv_layer_output = last_conv_layer_output[0]
heatmap = last_conv_layer_output @ pooled_grads[..., tf.newaxis]
heatmap = tf.squeeze(heatmap)
# For visualization purpose, we will also normalize the heatmap between 0 & 1
heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
return heatmap.numpy()
调整参数
img_size = (250, 250)
preprocess_input = keras.applications.xception.preprocess_input
decode_predictions = keras.applications.xception.decode_predictions
last_conv_layer_name = "dense_1"
# The local path to our target image
img_path = "/content/drive/MyDrive/SendO2/Train/correct/droidcam-20210128-152301.jpg"
preprocess_input = keras.applications.xception.preprocess_input
decode_predictions = keras.applications.xception.decode_predictions
display(Image(img_path))
运行它们
# Prepare image
img_array = preprocess_input(get_img_array(img_path, size=img_size))
# Make model
model = model_builder(weights="imagenet")
# Remove last layer's softmax
model.layers[-1].activation = None
# Print what the top predicted class is
preds = model.predict(img_array)
print("Predicted:", decode_predictions(preds, top=1)[0])
# Generate class activation heatmap
heatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name)
# Display heatmap
plt.matshow(heatmap)
plt.show()
如果有人能在这里帮助我,我将不胜感激。
【问题讨论】:
-
试试this。让我知道它是否适合您。
-
嘿,谢谢你的链接,但我收到了这个错误:
InvalidArgumentError: Invalid reduction dimension (1 for input with 1 dimension(s) [Op:Mean].我要问你的一件事是你的模型有 10 个输出神经元,而我的只有 2 个输出神经元。那么,您认为这会影响代码吗?错误:imgur.com/a/Wg9QfOS -
您如何尝试上述链接?如果可能,请分享一个 colab 链接。
-
抱歉回复晚了。请看一下答案。希望对您有所帮助。
标签: python-3.x tensorflow keras deep-learning neural-network