Loading MNIST from Pytorch

Rjacob: <p>Hi Folks,<br>
I have a query that how would you be able to load MNIST dataset from pytorch and filter out number 9 or 5 from it?<br>
I am learning pytorch so I would appreciate if you can share the code with me.</p>

Eta_C: <p><a href="https://pytorch.org/docs/1.1.0/_modules/torchvision/datasets/mnist.html" rel="noopener nofollow ugc">torchvision MNIST Dataset</a></p>

J_Johnson: <p>This is based on CIFAR10,</p>
<p><a href="https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html</a></p>
<p>but you can change that to:<br>
<code>trainset=torchvision.dataset.MNIST(**KWARGS)</code></p>
<p>For filtering out a particular category, you may need to define a custom DataLoader:</p>
<p><a href="https://pytorch.org/tutorials/beginner/basics/data_tutorial.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/beginner/basics/data_tutorial.html</a></p>
<p>and this is good to read, too:</p>
<p><a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/beginner/data_loading_tutorial.html</a></p>

Rjacob: <p>I am following the below approach-<br>
train_data = torchvision.datasets.MNIST("./", train=True, transform=transforms.ToTensor(), download=True)</p>
<p>test_data_xy = torchvision.datasets.MNIST("./", train=False, transform=transforms.ToTensor(), download=True)</p>
<p>idx_train = train_data.train_labels!=5</p>
<p>idx_test= test_data_xy.train_labels!=5</p>
<p>train_data.train_labels = train_data .train_labels[idx_train]</p>
<p>test_data_xy.train_data = test_data_xy.train_data[idx_test]</p>
<p>Getting an error as-<br>
AttributeError: can’t set attribute.</p>
<p>No clue what needs to be done</p>

Karthik_Ganesan: <p>Note that <code>torchvision.datasets.MNIST</code> returns a class of type <code>'torchvision.datasets.mnist.MNIST'</code>, not an actual list you an iterate over. You then have to use a dataloader to access the individual data points.</p>
<p>This is the code provided in the example to load MNIST. Can you try this and see?</p>
<pre><code class="lang-auto">transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])

batch_size = 4
trainset = torchvision.datasets.MNIST(root='./data/', train=True, download=False, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=2)

testset = torchvision.datasets.MNIST(root='./data/', train=False, download=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=2)
</code></pre>

Rjacob: <p>Hi Karthik, the code works, but I am not sure if your code has removed digit 5 from the dataset?</p>

Karthik_Ganesan: <p>Hi Ronish, no, I used all the classes available. As J_Johnson said above, you should checkout the link on datasets and dataloaders to create a custom one which excludes the 5 class.</p>
<p>The way you are doing it won’t work since <code>train_data</code> and <code>test_data_xy</code> are not just lists you can directly modify.</p>

Novin_Nouri: <p>you can download dataset images from this <a href="https://www.kaggle.com/datasets/novinnouri/mnist-dataset/data" rel="noopener nofollow ugc">link</a></p>
<p>You can manually change your dataset with this <a href="https://github.com/novin-nouri/MNIST_dataset/tree/main?tab=readme-ov-file" rel="noopener nofollow ugc">Github</a> repository</p>