AttributeError: 'Tensor' object has no attribute '__array_interface__'

Does anyone happen to know how to solve this?

AttributeError:
'Tensor' object has no attribute '__array_interface__'

I wrote a custom function to extract exactly one class of class “category” (an int) from the dataset usps, and my code is:

dataset_tgt = datasets.USPS(root='./data',
                              train=True,
                              transform=transform,
                              download=True)
dataset_tgt.data, dataset_tgt.targets = get_particular_class(dataset, category, 'usps')

dataloader_tgt = torch.utils.data.DataLoader(dataset_tgt, batch_size=batch_size,
                                         shuffle=True)

and the get_particular_class function is defined as

def get_particular_class(dataset, category, order):

    print('getting class {} in dataset {}'.format(category, order))
    try:
        targets = dataset.targets
    except:
        targets = dataset.labels

    data = dataset.data

    new_targets = []
    new_data = []
    for target, sample in zip(targets, data):
        if target == category:
            new_targets.append(target)
            if order == 'svhn':
                new_data.append(sample.transpose(2,1,0))
            else:
                new_data.append(sample)
    return new_data, new_targets

And the dataloader is able to load the updated dataset_tgt (USPS) just fine. However, in training loop, it gives this error.

generate_fake_mnist_usps.py 134 <module>
for i, (data, data_tgt) in enumerate(zip(dataloader, cycle(dataloader_tgt)), 0):

dataloader.py 435 __next__
data = self._next_data()

dataloader.py 475 _next_data
data = self._dataset_fetcher.fetch(index)  # may raise StopIteration

fetch.py 44 fetch
data = [self.dataset[idx] for idx in possibly_batched_index]

fetch.py 44 <listcomp>
data = [self.dataset[idx] for idx in possibly_batched_index]

usps.py 80 __getitem__
img = Image.fromarray(img, mode='L')

Image.py 2739 fromarray
arr = obj.__array_interface__

AttributeError:
'Tensor' object has no attribute '__array_interface__'

It seems Image.fromarray receives a tensor now instead of a np.array:

img = torch.randn(3, 24, 24)
Image.fromarray(img)
# > AttributeError: 'Tensor' object has no attribute '__array_interface__'

so I guess your get_particular_class manipulates the internal data structure and converts them into tensors?