Hi , I have a network that extract a tensor with shape:(batch,channels,time,x,y)=>(batch, 3,16,56,56), and I want to save it as a video, so that the output will be a video.
I tried the naive way using:
torch.save(var,"path\try.mp4")
You can guess what happened… I’ll be happy if someone has an Idea, really need your help!
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.
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.
Here is a pyimagesearch tutorial of something similar
Edit: Well you’d have to convert your tensor to images beforehand. If you need help with that let us know
I wrote this example that should be able to convert tensors to images. It might not work 100% for you but it should help
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 -> 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
Edit: Perhaps OpenCV wants a numpy array when I think about it. You can convert the image to a numpy array with np_image = np.array(image)