1

enter image description hereI have a tensor of shape (125, 3, 128, 128):

  • 125 frames
  • 3 channels (RGB)
  • each frame 128 x 128 size.
  • values in the tensor are in the range [0,1].

I want to display the video of these 125 frames, using Pytorch in Google Colab. How can I do that?

1 Answers1

1

One way to enable inline animations in Colab is using jshtml:

from matplotlib import rc
rc('animation', html='jshtml')

With this enabled, you can then plot your animation like so (note you will need to permute your image tensors to get them in PIL/matplotlib format):

import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

imgs = torch.rand(10,3,128,128)
imgs = imgs.permute(0,2,3,1) # Permuting to (Bx)HxWxC format
frames = [[ax.imshow(imgs[i])] for i in range(len(imgs))]

ani = animation.ArtistAnimation(fig, frames)
ani

enter image description here

iacob
  • 20,084
  • 6
  • 92
  • 119
  • Thank you, but it is not working for my code. Am I going wrong anywhere? How can i put screenshot here? – SRI VINOD PALACHARLA Apr 28 '21 at 09:41
  • 1
    @SRIVINODPALACHARLA can you be a bit more explicit about what is going wrong? Don't add a screenshot, add a [minimal reproduceable example](https://stackoverflow.com/help/minimal-reproducible-example) of your code and the error output. – iacob Apr 28 '21 at 09:44
  • for i,[inp] in enumerate(train_loader, 0): print(inp.shape) #(8,3,128,128) from matplotlib import rc rc('animation', html='jshtml') import matplotlib.pyplot as plt import matplotlib.animation as animation inp = inp.permute(0,2,3,1) fig, ax = plt.subplots() frames = [[ax.imshow(inp[i])] for i in range(len(inp))] ani = animation.ArtistAnimation(fig, frames) ani break – SRI VINOD PALACHARLA Apr 28 '21 at 09:46
  • 1
    @SRIVINODPALACHARLA your code appears to run fine on Colab for me with `inp = torch.rand(8,3,128,128)`. What exactly is the error / what looks wrong? – iacob Apr 28 '21 at 09:50
  • ouput is just a white square box with black border. When I use torch.rand, it is working as you said. but when i am using train loader it is not. Can i mail u screenshot? – SRI VINOD PALACHARLA Apr 28 '21 at 09:57
  • @SRIVINODPALACHARLA The issue appears to be in your data then - the code I have posted expects RGB data either in the normalised [0,1] range or [0,255]. Can you add an example of your data to the question? – iacob Apr 28 '21 at 10:00
  • My data values in [0,1]. I was able to output jpg images. Can i use animation inside a for loop right for multiple videos? – SRI VINOD PALACHARLA Apr 28 '21 at 10:06
  • @SRIVINODPALACHARLA oh I get the for loop now - yeah that's the issue. Remove the break, and plot one animation per cell. – iacob Apr 28 '21 at 10:09
  • I didn't get one animation per cell, how can I do that, please can you tell? – SRI VINOD PALACHARLA Apr 28 '21 at 10:13
  • @SRIVINODPALACHARLA Add an example of your data (i.e. some samples from `train_loader`) to your question. – iacob Apr 28 '21 at 10:15
  • I added clear photo, can you suggest one "animation per cell" edit you stated earlier? – SRI VINOD PALACHARLA Apr 28 '21 at 10:31
  • @SRIVINODPALACHARLA Save your animation objects to a container, and plot each one in a new cell. I'm not aware of a way to plot multiple animations in the same output in Colab (also, remove the break statement). – iacob Apr 28 '21 at 10:34