| 119 | self.resample = nn.Identity() |
| 120 | |
| 121 | def forward(self, x, feat_cache=None, feat_idx=[0]): |
| 122 | b, c, t, h, w = x.size() |
| 123 | if self.mode == 'upsample3d': |
| 124 | if feat_cache is not None: |
| 125 | idx = feat_idx[0] |
| 126 | if feat_cache[idx] is None: |
| 127 | feat_cache[idx] = 'Rep' |
| 128 | feat_idx[0] += 1 |
| 129 | else: |
| 130 | |
| 131 | cache_x = x[:, :, -CACHE_T:, :, :].clone() |
| 132 | if cache_x.shape[2] < 2 and feat_cache[ |
| 133 | idx] is not None and feat_cache[idx] != 'Rep': |
| 134 | # cache last frame of last two chunk |
| 135 | cache_x = torch.cat([ |
| 136 | feat_cache[idx][:, :, -1, :, :].unsqueeze(2).to( |
| 137 | cache_x.device), cache_x |
| 138 | ], |
| 139 | dim=2) |
| 140 | if cache_x.shape[2] < 2 and feat_cache[ |
| 141 | idx] is not None and feat_cache[idx] == 'Rep': |
| 142 | cache_x = torch.cat([ |
| 143 | torch.zeros_like(cache_x).to(cache_x.device), |
| 144 | cache_x |
| 145 | ], |
| 146 | dim=2) |
| 147 | if feat_cache[idx] == 'Rep': |
| 148 | x = self.time_conv(x) |
| 149 | else: |
| 150 | x = self.time_conv(x, feat_cache[idx]) |
| 151 | feat_cache[idx] = cache_x |
| 152 | feat_idx[0] += 1 |
| 153 | |
| 154 | x = x.reshape(b, 2, c, t, h, w) |
| 155 | x = torch.stack((x[:, 0, :, :, :, :], x[:, 1, :, :, :, :]), |
| 156 | 3) |
| 157 | x = x.reshape(b, c, t * 2, h, w) |
| 158 | t = x.shape[2] |
| 159 | x = rearrange(x, 'b c t h w -> (b t) c h w') |
| 160 | x = self.resample(x) |
| 161 | x = rearrange(x, '(b t) c h w -> b c t h w', t=t) |
| 162 | |
| 163 | if self.mode == 'downsample3d': |
| 164 | if feat_cache is not None: |
| 165 | idx = feat_idx[0] |
| 166 | if feat_cache[idx] is None: |
| 167 | feat_cache[idx] = x.clone() |
| 168 | feat_idx[0] += 1 |
| 169 | else: |
| 170 | cache_x = x[:, :, -1:, :, :].clone() |
| 171 | x = self.time_conv( |
| 172 | torch.cat([feat_cache[idx][:, :, -1:, :, :], x], 2)) |
| 173 | feat_cache[idx] = cache_x |
| 174 | feat_idx[0] += 1 |
| 175 | return x |
| 176 | |
| 177 | def init_weight(self, conv): |
| 178 | conv_weight = conv.weight |