TypeError Traceback (most recent call last)
<ipython-input-8-767abd2c3c6d> in <module>()
402
403 print(conf_matrix)
--> 404 plt.imshow(torchvision.utils.make_grid(predictions))
1 frames
/usr/local/lib/python3.7/dist-packages/torchvision/utils.py in make_grid(tensor, nrow, padding, normalize, value_range, scale_each, pad_value, **kwargs)
44 if not (torch.is_tensor(tensor) or
45 (isinstance(tensor, list) and all(torch.is_tensor(t) for t in tensor))):
---> 46 raise TypeError(f'tensor or list of tensors expected, got {type(tensor)}')
47
48 if "range" in kwargs.keys():
TypeError: tensor or list of tensors expected, got <class 'numpy.ndarray'>
Hey @randino ,
Your code is wrong.
You are using mat to load your image and again assigning mat to a tensor with random value. So, no matter how many times you change the image path, you will get a random image. Let me explain.
This two lines will read the mat file.
mat = pjoin('./sample_data/', 'img.mat')
#print('Mat loading ...')
mat = sio.loadmat(mat) # image values are loaded into 'mat' variable
So, mat is now a numpy array image. But, this line will assign a tensor with random values, overwriting previous mat value.
mat = torch.randn(1, 1, 100, 100) # you are assigning a random tensor to 'mat' variable
So, when you try to show or save the image, you get a random image.
plt.imshow(torchvision.utils.make_grid(mat).permute(1, 2, 0) # this mat is a random tensor
You can try the following.
mat = pjoin('./sample_data/', 'img.mat')
#print('Mat loading ...')
mat = sio.loadmat(mat)
## extract image from mat
## .....
## .....
mat = torchvision.transforms.ToTensor()(mat)
#save image
torchvision.utils.save_image(mat, "image.png")
#show image
plt.imshow(torchvision.transforms.ToPILImage()(mat))
plt.show()
Hi @randino,
This is expected as mat files are dict files with keys being the MATLAB variables and values being the objects assigned to those variables.
Please refer to this answer.
Hi @randino ,
Please check the following minimal example on how to read, save and load image from mat file.
import scipy.io as sio
import matplotlib.pyplot as plt
import numpy as np
# taking the cifar-10 mat version for testing
mat = './cifar-10-batches-mat/data_batch_1.mat'
# loading mat file
mat = sio.loadmat(mat)
# finding the key values
print(mat.keys())
# output: dict_keys(['__header__', '__version__', '__globals__', 'data', 'labels', 'batch_label'])
# we will take the "data" key from the dict as it contains images
data = np.asarray(mat.get('data'))
# reshaping the raw data into image format (channel first approach)
data = data.reshape((-1, 3, 32, 32))
data = data.swapaxes(3, 2).swapaxes(1, 3) # swapping axes for image correction
data_image = data[0] # taking the first image of the dataset
print(data_image.shape)
# output: (32,32,3)
plt.imsave('./data_image.jpg', np.asarray(data_image)) # saving the image
# reading back
image = plt.imread('/home/la-belva/code_ground/data_image.jpg') # reading the saved image
print(image.shape)
# output: (32,32,3)