File size: 7,390 Bytes
a560c26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# coding=utf-8
# Copyright 2023 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Video module library."""

import functools
from typing import Any, Callable, Dict, Iterable, Mapping, NamedTuple, Optional, Tuple, Union

from flax import linen as nn
import jax.numpy as jnp
from invariant_slot_attention.lib import utils
from invariant_slot_attention.modules import misc

Shape = Tuple[int]

DType = Any
Array = Any  # jnp.ndarray
ArrayTree = Union[Array, Iterable["ArrayTree"], Mapping[str, "ArrayTree"]]  # pytype: disable=not-supported-yet
ProcessorState = ArrayTree
PRNGKey = Array
NestedDict = Dict[str, Any]


class CorrectorPredictorTuple(NamedTuple):
  corrected: ProcessorState
  predicted: ProcessorState


class Processor(nn.Module):
  """Recurrent processor module.

  This module is scanned (applied recurrently) over the sequence dimension of
  the input and applies a corrector and a predictor module. The corrector is
  only applied if new inputs (such as a new image/frame) are received and uses
  the new input to correct its internal state.

  The predictor is equivalent to a latent transition model and produces a
  prediction for the state at the next time step, given the current (corrected)
  state.
  """
  corrector: Callable[[ProcessorState, Array], ProcessorState]
  predictor: Callable[[ProcessorState], ProcessorState]

  @functools.partial(
      nn.scan,  # Scan (recurrently apply) over time axis.
      in_axes=(1, 1, nn.broadcast),  # (inputs, padding_mask, train).
      out_axes=1,
      variable_axes={"intermediates": 1},  # Stack intermediates along seq. dim.
      variable_broadcast="params",
      split_rngs={"params": False, "dropout": True})
  @nn.compact
  def __call__(self, state, inputs,
               padding_mask,
               train):

    # Only apply corrector if we receive new inputs.
    if inputs is not None:
      corrected_state = self.corrector(state, inputs, padding_mask, train=train)
    # Otherwise simply use previous state as input for predictor.
    else:
      corrected_state = state

    # Always apply predictor (i.e. transition model).
    predicted_state = self.predictor(corrected_state, train=train)

    # Prepare outputs in a format compatible with nn.scan.
    new_state = predicted_state
    outputs = CorrectorPredictorTuple(
        corrected=corrected_state, predicted=predicted_state)
    return new_state, outputs


class SAVi(nn.Module):
  """Video model consisting of encoder, recurrent processor, and decoder."""

  encoder: Callable[[], nn.Module]
  decoder: Callable[[], nn.Module]
  corrector: Callable[[], nn.Module]
  predictor: Callable[[], nn.Module]
  initializer: Callable[[], nn.Module]
  decode_corrected: bool = True
  decode_predicted: bool = True

  @nn.compact
  def __call__(self, video, conditioning = None,
               continue_from_previous_state = False,
               padding_mask = None,
               train = False):
    """Performs a forward pass on a video.

    Args:
      video: Video of shape `[batch_size, n_frames, height, width, n_channels]`.
      conditioning: Optional jnp.ndarray used for conditioning the initial state
        of the recurrent processor.
      continue_from_previous_state: Boolean, whether to continue from a previous
        state or not. If True, the conditioning variable is used directly as
        initial state.
      padding_mask: Binary mask for padding video inputs (e.g. for videos of
        different sizes/lengths). Zero corresponds to padding.
      train: Indicating whether we're training or evaluating.

    Returns:
      A dictionary of model predictions.
    """
    processor = Processor(
        corrector=self.corrector(), predictor=self.predictor())  # pytype: disable=wrong-arg-types

    if padding_mask is None:
      padding_mask = jnp.ones(video.shape[:-1], jnp.int32)

    # video.shape = (batch_size, n_frames, height, width, n_channels)
    # Vmapped over sequence dim.
    encoded_inputs = self.encoder()(video, padding_mask, train)  # pytype: disable=not-callable
    if continue_from_previous_state:
      assert conditioning is not None, (
          "When continuing from a previous state, the state has to be passed "
          "via the `conditioning` variable, which cannot be `None`.")
      init_state = conditioning[:, -1]  # We currently only use last state.
    else:
      # Same as above but without encoded inputs.
      init_state = self.initializer()(
          conditioning, batch_size=video.shape[0], train=train)  # pytype: disable=not-callable

    # Scan recurrent processor over encoded inputs along sequence dimension.
    _, states = processor(init_state, encoded_inputs, padding_mask, train)
    # type(states) = CorrectorPredictorTuple.
    # states.corrected.shape = (batch_size, n_frames, ..., n_features).
    # states.predicted.shape = (batch_size, n_frames, ..., n_features).

    # Decode latent states.
    decoder = self.decoder()  # Vmapped over sequence dim.
    outputs = decoder(states.corrected,
                      train) if self.decode_corrected else None  # pytype: disable=not-callable
    outputs_pred = decoder(states.predicted,
                           train) if self.decode_predicted else None  # pytype: disable=not-callable

    return {
        "states": states.corrected,
        "states_pred": states.predicted,
        "outputs": outputs,
        "outputs_pred": outputs_pred,
    }


class FrameEncoder(nn.Module):
  """Encoder for single video frame, vmapped over time axis."""

  backbone: Callable[[], nn.Module]
  pos_emb: Callable[[], nn.Module] = misc.Identity
  reduction: Optional[str] = None
  output_transform: Callable[[], nn.Module] = misc.Identity

  # Vmapped application of module, consumes time axis (axis=1).
  @functools.partial(utils.time_distributed, in_axes=(1, 1, None))
  @nn.compact
  def __call__(self, inputs, padding_mask = None,
               train = False):
    del padding_mask  # Unused.

    # inputs.shape = (batch_size, height, width, n_channels)
    x = self.backbone()(inputs, train=train)

    x = self.pos_emb()(x)

    if self.reduction == "spatial_flatten":
      batch_size, height, width, n_features = x.shape
      x = jnp.reshape(x, (batch_size, height * width, n_features))
    elif self.reduction == "spatial_average":
      x = jnp.mean(x, axis=(1, 2))
    elif self.reduction == "all_flatten":
      batch_size, height, width, n_features = x.shape
      x = jnp.reshape(x, (batch_size, height * width * n_features))
    elif self.reduction is not None:
      raise ValueError("Unknown reduction type: {}.".format(self.reduction))

    output_block = self.output_transform()

    if hasattr(output_block, "qkv_size"):
      # Project to qkv_size if used transformer.
      x = nn.relu(nn.Dense(output_block.qkv_size)(x))

    x = output_block(x, train=train)
    return x