Gradienten sind bei der Implementierung von GradCam keine

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Gradienten sind bei der Implementierung von GradCam keine

by Anonymous » 04 Mar 2025, 14:36

Ich versuche, Grad-Cam für ein TensorFlow 2.0-Modell (mit dem Keras-API erstellt) zu implementieren, aber die vom Band zurückgegebenen Gradienten sind immer keine. Xception-Modell von tf.keras.applications zur Debugie (kein Verhaltensunterschied, daher muss das Problem mit meinem Code liegen).

Code: Select all

    # model (not shown here) is Xception from tf.keras.applications
cam_model = tf.keras.models.Model(
[model.inputs],
[model.get_layer(conv_layer_name).output, model.output] # conv_layer_name = 'block14_sepconv2_act'
)

with tf.GradientTape() as tape:
conv_out, predictions = cam_model(image)
class_out = tf.argmax(predictions, axis=-1)

grads = tape.gradient(class_out, conv_out)

if grads is None: # grads is None
raise Exception("Grad cam has recorded no gradient")
< /code>
Das ist einfach genug, ich verstehe nicht, warum die Gradienten keine sind. Ich vermute, dass das Band möglicherweise nicht aufgenommen wird, aber angesichts des Colabs in https://colab.research.google.com/github/keras-team/keras-io/blob/master/examples/vision/ipynb/grad_cam_cam.ipynb. Es scheint nichts erforderlich zu sein. Layer. Hier ist der Modelldefinitionscode: < /p>
    backbone = VGG16(
include_top=False,
weights='imagenet',
input_shape=(*size, 3),
pooling='max'
)

backbone.trainable = False

net = Sequential()

for layer in backbone.layers:
net.add(layer)

net.add(Flatten())
net.add(Dense(256, activation='relu'))
net.add(Dense(128, activation='relu'))
net.add(Dense(len(CLASSES), activation='softmax'))
Dies ist in TensorFlow 2.8.0, auf GPU.

Top