Thanks to visit codestin.com
Credit goes to github.com

Skip to content

tape.watch DenseNet121 on a layer's output throws ValueError #104521

@khteh

Description

@khteh

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

No

Source

binary

TensorFlow version

2.20.0

Custom code

Yes

OS platform and distribution

Ubuntu 25.10

Mobile device

No response

Python version

3.13.7

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

    tape.watch(spatial_map_layer)
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/usr/src/Python/pAIthon/.venv/lib/python3.13/site-packages/tensorflow/python/eager/backprop.py", line 873, in watch
    for t in _extract_tensors_and_variables(tensor):
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "/usr/src/Python/pAIthon/.venv/lib/python3.13/site-packages/tensorflow/python/eager/backprop.py", line 700, in _extract_tensors_and_variables
    raise ValueError(f"Passed in object {obj} of type {type(obj).__name__!r}"
                     f", not tf.Tensor or tf.Variable or ExtensionType.")
ValueError: Passed in object <KerasTensor shape=(None, None, None, 1024), dtype=float32, sparse=False, ragged=False, name=keras_tensor_424> of type 'KerasTensor', not tf.Tensor or tf.Variable or ExtensionType.

Standalone code to reproduce the issue

base_model = DenseNet121(weights='models/densenet.hdf5', include_top=False)
        print("Loaded DenseNet")
        # add a global spatial average pooling layer
        x = base_model.output
        x = GlobalAveragePooling2D()(x)
        # and a logistic layer
        predictions = Dense(len(self._labels), activation="sigmoid")(x)
        print("Added layers")

        self._model = Model(inputs=base_model.input, outputs=predictions)

        def get_weighted_loss(neg_weights, pos_weights, epsilon=1e-7):
            def weighted_loss(y_true, y_pred):
                # L(X, y) = −w * y log p(Y = 1|X) − w *  (1 − y) log p(Y = 0|X)
                # from https://arxiv.org/pdf/1711.05225.pdf
                loss = 0
                for i in range(len(neg_weights)):
                    loss -= (neg_weights[i] * y_true[:, i] * tf.math.log(y_pred[:, i] + epsilon) + 
                            pos_weights[i] * (1 - y_true[:, i]) * tf.math.log(1 - y_pred[:, i] + epsilon))
                loss = tf.math.reduce_sum(loss)
                return loss
            return weighted_loss
        self._model.compile(
            loss=get_weighted_loss(self._neg_weights, self._pos_weights),
            optimizer=Adam(),
        )
        self._model.load_weights("models/pretrained_model.h5")


        preprocessed_input = self._load_image_normalize(img_path, self._mean, self._std)
        layer_name='conv5_block16_concat'
        with tf.GradientTape() as tape:
            predictions = self._model.predict(preprocessed_input)
            spatial_map_layer = self._model.get_layer(layer_name).output
            # Watch the intermediate layer's output
            tape.watch(spatial_map_layer)

Relevant log output

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions