universal_attention/models.py
def attending_classifier(
    encoder: layers.Layer, dataset_and_info: data.DatasetAndInfo
) -> tf.keras.Model:
    """
    A classifier, made up of an encoder (possibly with attention) and a head
    to a fixed number of classes, run on fixed-size imagery.

    Parameters
    ----------
    encoder: tf.keras.layers.Layer
        A layer returning AttendingEncoderOutput.
    dataset_and_info: data.DatasetAndInfo
        A dataset and its information, used to decide shapes.

    Returns
    -------
    tf.keras.Model (tf.Tensor -> AttendingClassifierOutput)
        A model mapping images to class probabilities and attention maps.
    """
    splits, info = dataset_and_info
    img_shape = splits["train"].element_spec[0].shape[1:]
    num_classes = info.features["label"].num_classes

    image = tf.keras.Input(shape=(img_shape), name="image")
    features, attention = encoder(image)
    logits = layers.Dense(num_classes, name="prediction")(features)

    # Rename attention outputs
    attention = {
        key: layers.Lambda(lambda x: x, name=f"attention_{key}")(attention_map)
        for key, attention_map in attention.items()
    }

    return tf.keras.Model(image, AttendingClassifierOutput(logits, attention))

    A classifier, made up of an encoder (possibly with attention) and a head
    to a fixed number of classes, run on fixed-size imagery.

    Parameters
    ----------
    encoder: tf.keras.layers.Layer
        A layer returning AttendingEncoderOutput.
    dataset_and_info: data.DatasetAndInfo
        A dataset and its information, used to decide shapes.

    Returns
    -------
    tf.keras.Model (tf.Tensor -> AttendingClassifierOutput)
        A model mapping images to class probabilities and attention maps.
    
A classifier, made up of an encoder (possibly with attention) and a head
to a fixed number of classes, run on fixed-size imagery.

Parameters

tf.keras.layers.Layer
A layer returning AttendingEncoderOutput.
dataset_and_info: data.DatasetAndInfo
A dataset and its information, used to decide shapes.

Returns

tf.keras.Model (tf.Tensor -> AttendingClassifierOutput)
A model mapping images to class probabilities and attention maps.
A classifier made up of an encoder ( possibly with attention ) and a head to a fixed number of classes run on fixed - size imagery . Parameters tf . keras . layers . Layer A layer returning AttendingEncoderOutput . dataset_and_info : data . DatasetAndInfo A dataset and its information used to decide shapes . Returns tf . keras . Model ( tf . Tensor - > AttendingClassifierOutput ) A model mapping images to class probabilities and attention maps .
def attending_classifier(
    encoder: layers.Layer, dataset_and_info: data.DatasetAndInfo
) -> tf.keras.Model:
    splits, info = dataset_and_info
    img_shape = splits["train"].element_spec[0].shape[1:]
    num_classes = info.features["label"].num_classes
    image = tf.keras.Input(shape=(img_shape), name="image")
    features, attention = encoder(image)
    logits = layers.Dense(num_classes, name="prediction")(features)
    attention = {
        key: layers.Lambda(lambda x: x, name=f"attention_{key}")(attention_map)
        for key, attention_map in attention.items()
    }
    return tf.keras.Model(image, AttendingClassifierOutput(logits, attention))
def attending_classifier ( encoder : layers . Layer , dataset_and_info : data . DatasetAndInfo ) -> tf . keras . Model : splits , info = dataset_and_info img_shape = splits [ "train" ] . element_spec [ 0 ] . shape [ 1 : ] num_classes = info . features [ "label" ] . num_classes image = tf . keras . Input ( shape = ( img_shape ) , name = "image" ) features , attention = encoder ( image ) logits = layers . Dense ( num_classes , name = "prediction" ) ( features ) attention = { key : layers . Lambda ( lambda x : x , name = f"attention_{key}" ) ( attention_map ) for key , attention_map in attention . items ( ) } return tf . keras . Model ( image , AttendingClassifierOutput ( logits , attention ) )
A classifier, made up of an encoder (possibly with attention) and a head
to a fixed number of classes, run on fixed-size imagery.
A classifier made up of an encoder ( possibly with attention ) and a head to a fixed number of classes run on fixed - size imagery .
"""
    A classifier, made up of an encoder (possibly with attention) and a head
    to a fixed number of classes, run on fixed-size imagery.

    Parameters
    ----------
    encoder: tf.keras.layers.Layer
        A layer returning AttendingEncoderOutput.
    dataset_and_info: data.DatasetAndInfo
        A dataset and its information, used to decide shapes.

    Returns
    -------
    tf.keras.Model (tf.Tensor -> AttendingClassifierOutput)
        A model mapping images to class probabilities and attention maps.
    """ # Rename attention outputs
encoder
layers.Layer
dataset_and_info
data.DatasetAndInfo