File size: 829 Bytes
5425743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec1ad30
5425743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec1ad30
5425743
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
import os
import imageio
import numpy as np

import torch
import torchvision

from einops import rearrange


def save_videos_grid(
    videos: torch.Tensor, 
    save_path: str = 'output',
    path: str = 'output.gif', 
    rescale=False, 
    n_rows=4, 
    duration=3
):
    videos = rearrange(videos, "b c t h w -> t b c h w")
    outputs = []
    for x in videos:
        x = torchvision.utils.make_grid(x, nrow=n_rows)
        x = x.transpose(0, 1).transpose(1, 2).squeeze(-1)
        if rescale:
            x = (x + 1.0) / 2.0  # -1,1 -> 0,1
        x = (x * 255).numpy().astype(np.uint8)
        outputs.append(x)
        
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    
    
    imageio.mimsave(os.path.join(save_path, path), outputs, duration=duration)
    return os.path.join(save_path, path)