Output of the model is video , how can I save it?

username: barakb
cooked: <p>Hi , I have a network that extract a tensor with shape:(batch,channels,time,x,y)=&gt;(batch, 3,16,56,56), and I want to save it as a video, so that the output will be a video.</p>
<p>I tried the naive way using:</p>
<pre><code class="lang-auto">    torch.save(var,"path\try.mp4")

</code></pre>
<p>You can guess what happened… I’ll be happy if someone has an Idea, really need your help!</p>
<p>Thanks!</p>

username: Oli
cooked: <p>I haven’t done it myself but openCV has something called a VideoWriter. That should do the trick. There is also the ffmpeg module outside of Python.</p>
<p>The way I have done it in the past saving all the images in a folder and via a bash script and ffmpeg make a movie out of them. Not as clean, but yeah.</p>
<p>Here is a <a href="https://www.pyimagesearch.com/2016/02/22/writing-to-video-with-opencv/" rel="nofollow noopener">pyimagesearch</a> tutorial of something similar</p>
<p>Edit: Well you’d have to convert your tensor to images beforehand. If you need help with that let us know <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=7" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: barakb
cooked: <p>Thanks!<br>
I’ll try the opencv option tomorrow (It’s night time at my place).<br>
And yes I’ll be happy with some direction about turning tensor to images.</p>
<p>Thanks again!</p>

username: Oli
cooked: <p>I wrote this example that should be able to convert tensors to images. It might not work 100% for you but it should help <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=7" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<pre><code class="lang-auto">import torch
from torchvision import transforms

tensor_2_image = transforms.ToPILImage() # Converts to Image
output = torch.randn(2, 3, 16, 56, 56) # Tensor

for batch in output:
  batch = batch.permute(1, 0, 2, 3) # Change axises -&gt; 16,3,56,56
  images = [tensor_2_image(im) for im in batch] # Convert images
  for image in images:
    print(image)
    # Do what you want with your image here
</code></pre>
<p>Edit: Perhaps OpenCV wants a numpy array when I think about it. You can convert the image to a numpy array with <code>np_image = np.array(image)</code></p>

username: JuanFMontesinos
cooked: <p>Hi, I think imageio library (which is very well optimized, clean API) can write video just by giving a list of frames format and frame rate <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=7" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: barakb
cooked: <p>Thanks , I will try both of them.<br>
Have you tried or heard trying using pyffmpeg?</p>

username: Oli
cooked: <p>Cool, no but that sounds like a great option <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=7" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: ibro45
cooked: <p>I was looking for it now and found that torchvision allows you to write a tensor as a video. I suppose it wasn’t available a year ago, otherwise it would have been mentioned here for sure. Here’s the link <a href="https://pytorch.org/docs/stable/torchvision/io.html#torchvision.io.write_video" rel="nofollow noopener">https://pytorch.org/docs/stable/torchvision/io.html#torchvision.io.write_video</a></p>