- def frame2seq(x):
- z = encoder(x)
- ze_bool = (quantizer(z) >= 0).long()
- seq = (
- ze_bool.permute(0, 2, 3, 1).reshape(ze_bool.size(0), -1, ze_bool.size(1))
- * pow2
- ).sum(-1)
- return seq
-
- def seq2frame(seq, T=1e-2):
- zd_bool = (seq[:, :, None] // pow2) % 2
- zd_bool = zd_bool.reshape(zd_bool.size(0), z_h, z_w, -1).permute(0, 3, 1, 2)
- logits = decoder(zd_bool * 2.0 - 1.0)
- logits = logits.reshape(
- logits.size(0), -1, 3, logits.size(2), logits.size(3)
- ).permute(0, 2, 3, 4, 1)
- results = torch.distributions.categorical.Categorical(
- logits=logits / T
- ).sample()
- return results
-
- return train_input, test_input, frame2seq, seq2frame
+ logger(f"vqae input {train_input[0].size()} output {z[0].size()}")
+
+ def frame2seq(input, batch_size=25):
+ seq = []
+ p = pow2.to(device)
+ for x in input.split(batch_size):
+ x = x.to(device)
+ z = encoder(x)
+ ze_bool = (quantizer(z) >= 0).long()
+ output = (
+ ze_bool.permute(0, 2, 3, 1).reshape(
+ ze_bool.size(0), -1, ze_bool.size(1)
+ )
+ * p
+ ).sum(-1)
+
+ seq.append(output)
+
+ return torch.cat(seq, dim=0)
+
+ def seq2frame(input, batch_size=25, T=1e-2):
+ frames = []
+ p = pow2.to(device)
+ for seq in input.split(batch_size):
+ seq = seq.to(device)
+ zd_bool = (seq[:, :, None] // p) % 2
+ zd_bool = zd_bool.reshape(zd_bool.size(0), z_h, z_w, -1).permute(0, 3, 1, 2)
+ logits = decoder(zd_bool * 2.0 - 1.0)
+ logits = logits.reshape(
+ logits.size(0), -1, 3, logits.size(2), logits.size(3)
+ ).permute(0, 2, 3, 4, 1)
+ output = torch.distributions.categorical.Categorical(
+ logits=logits / T
+ ).sample()
+
+ frames.append(output)
+
+ return torch.cat(frames, dim=0)
+
+ return train_input, train_actions, test_input, test_actions, frame2seq, seq2frame