How can I convert tuple to tensor? I need it for the following code because the code is in the batch of size 4. Using append, did not work properly so I decided to use cat. for torch.cat I need tuple of tensors but im_path is a tuple itself and I want to convert it to tensor so I could pass it to torch.cat.
Basically at the end I want to get a flattened list of im_paths (this worked for class_probs --not showing the entire code)
###im_paths = []
im_paths = torch.Tensor()
class_probs = torch.Tensor()
with torch.no_grad():
for i, (inputs, classes, im_path) in enumerate(dataloaders['test']):
im_paths = im_paths.cuda()
class_probs = class_probs.cuda()
class_probs = torch.cat((class_probs, F.softmax(outputs, 1)))
###im_paths.append(im_path)
im_paths = torch.cat((im_paths, im_path))
here is an example of one of im_path
('10folds/10fold_9/test/2/10316.jpg', '10folds/10fold_9/test/7/10392.jpg', '10folds/10fold_9/test/2/90007.jpg', '10folds/10fold_9/test/2/10214.jpg')
which is a tuple
The following code:
im_paths = torch.Tensor()
with torch.no_grad():
for i, (inputs, classes, im_path) in enumerate(dataloaders['test']):
im_paths = im_paths.cuda()
im_paths = torch.cat((im_paths, im_path), )
Gives me the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-56-efb416e1f73a> in <module>()
18 for i, (inputs, classes, im_path) in enumerate(dataloaders['test']):
19 im_paths = im_paths.cuda()
---> 20 im_paths = torch.cat((im_paths, im_path), )
21
22 inputs = inputs.to(device)
TypeError: expected Tensor as element 1 in argument 0, but got tuple