IceCube
IceCube

Reputation: 423

3D tensor as an image in torch

How can I save 3D tensor as an image in torch ?

I'm using image Package: https://github.com/torch/image/blob/master/doc/saveload.md

To save 3D tensor as an image:

  image.save('train100.jpg', trainData[100])

when loading back:

x = image.load('train100.jpg'.jpg' )

I get that x is different from the original tensor and the saved image is all white.

this is the code:

require 'torch'
require 'image'
local trainset = torch.load('cifar.torch/cifar10-train.t7')
local testset = torch.load('cifar.torch/cifar10-test.t7')

local trainData = trainset.data:float() 
local trainLabels = trainset.label:float():add(1)

testData = testset.data:float()
testLabels = testset.label:float():add(1)

image.save('train100.jpg', trainData[100])

x = image.load('train100.jpg' )
print(trainData[100])
print(x)

Upvotes: 0

Views: 1095

Answers (2)

Soubhi M. Hadri
Soubhi M. Hadri

Reputation: 143

You can do that using torchvision.utils.save_image

Check here for the function parameters.

Upvotes: 0

IceCube
IceCube

Reputation: 423

this was solved by normalizing the tensor:

image.save('train100.jpg', trainData[100])

Upvotes: 0

Related Questions