(activations, segments)
return sample_generator()
Helper function when defining generators for *_conditional tallies.
    Transforms a batch of activations and segmentations into a
    sequence of conditional statistics, i.e., activations that
    are at the same location as the segmentation label.
    Both activations nad segments should be 4d tensors with
    the same sample, y, and x dimensions.  Segments can be
    a multilabel segmentation.  The zero segmentation value is
    assumed to be unused.

    Returns a generator for a sequence of (condition, (sample, unit)-tensor)
    listing every condition present in the segments, along with the
    set of activations overlapping that condition.  The activation tensor
    is 2d in (sample, unit) order, where sample is the number of samples
    with for the condition.
Helper function when defining generators for *_conditional tallies.
    Transforms a batch of activations and segmentations into a
    sequence of conditional statistics, i.e., activations that
    are at the same location as the segmentation label.
    Both activations nad segments should be 4d tensors with
    the same sample, y, and x dimensions.  Segments can be
    a multilabel segmentation.  The zero segmentation value is
    assumed to be unused.
Helper function when defining generators for * _conditional tallies . Transforms a batch of activations and segmentations into a sequence of conditional statistics i . e . activations that are at the same location as the segmentation label . Both activations nad segments should be 4d tensors with the same sample y and x dimensions . Segments can be a multilabel segmentation . The zero segmentation value is assumed to be unused .
def conditional_samples(activations, segments):
    '''
    Helper function when defining generators for *_conditional tallies.
    Transforms a batch of activations and segmentations into a
    sequence of conditional statistics, i.e., activations that
    are at the same location as the segmentation label.
    Both activations nad segments should be 4d tensors with
    the same sample, y, and x dimensions.  Segments can be
    a multilabel segmentation.  The zero segmentation value is
    assumed to be unused.

    Returns a generator for a sequence of (condition, (sample, unit)-tensor)
    listing every condition present in the segments, along with the
    set of activations overlapping that condition.  The activation tensor
    is 2d in (sample, unit) order, where sample is the number of samples
    with for the condition.
    '''
    channels = activations.shape[1]
    activations_by_channel = activations.permute(0, 2, 3, 1).contiguous()
    segcounts = segments.view(-1).bincount()
    conditions = (segcounts[1:].nonzero() + 1)[:, 0]
    def sample_generator():
        # First yield the full set of activations, unconditioned
        yield (0, activations_by_channel.view(-1, channels))
        # Then a set of activations for each condition present in the image
        for condition in conditions:
            mask = (segments == condition).max(1)[0][...,None]
            mask = mask.expand(activations_by_channel.shape)
            yield (condition.item(),
                    activations_by_channel[mask].view(-1, channels))
    return sample_generator()
def conditional_samples ( activations , segments ) : channels = activations . shape [ 1 ] activations_by_channel = activations . permute ( 0 , 2 , 3 , 1 ) . contiguous ( ) segcounts = segments . view ( - 1 ) . bincount ( ) conditions = ( segcounts [ 1 : ] . nonzero ( ) + 1 ) [ : , 0 ] def sample_generator ( ) : # First yield the full set of activations, unconditioned yield ( 0 , activations_by_channel . view ( - 1 , channels ) ) # Then a set of activations for each condition present in the image for condition in conditions : mask = ( segments == condition ) . max ( 1 ) [ 0 ] [ ... , None ] mask = mask . expand ( activations_by_channel . shape ) yield ( condition . item ( ) , activations_by_channel [ mask ] . view ( - 1 , channels ) ) return sample_generator ( )