简体   繁体   中英

Pytorch: ValueError: Too many dimensions: 3 > 2. 9/opt/anaconda3/envs/pytorch/lib/python3.7/site-packages/PIL/Image.py in fromarray(obj, mode)

I am using MNIST data to run my python using pytorch. I like to train only partial data for digits 0 and 1. when I try to print the size of the first image it runs into this error:

ValueError: too many dimensions: 3 > 2

I am very new to Python. The program runs fine if I don't segment the training data. Here is the code snippet

subset_indices = ((train_data.train_labels == 0) + (train_data.train_labels == 1)).nonzero()
train_loader = torch.utils.data.DataLoader(train_data,batch_size=batch_size, shuffle=False,sampler=SubsetRandomSampler(subset_indices))

The error is due to the fact that you are passing a 3 dimensional array into the function Image.fromarray which is likely set in the wrong mode. You need to make sure mode is set to RGB so that it looks like Image.fromarray(data, mode='RGB') .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM