How to do inference/calculate accuracy on the entire test set

I am getting very slow inference rates by creating a test dataloader with batch size of 10k(Fashion MNIST)
Is there any better faster way to do the same

import numpy as np
import matplotlib.pyplot as plt
from torchvision import datasets,transforms
from torch import utils,nn,load,exp,autograd
count=0
class Model(nn.Module):
def init(self):
super().init()
self.l1 = nn.Linear(in_features=784,out_features=1000,bias=False)
self.l2= nn.Linear(in_features=1000,out_features=1000,bias=False)
self.l3 = nn.Linear(in_features=1000,out_features= 500,bias=False)
self.l4 = nn.Linear(in_features=500,out_features=200,bias=False)
self.l5 = nn.Linear(in_features=200,out_features=10,bias=False)
def forward(self,x):
x = x.view(x.shape[0],-1)
x = nn.functional.relu(self.l1(x))
x = nn.functional.relu(self.l2(x))
x = nn.functional.relu(self.l3(x))
x = nn.functional.relu(self.l4(x))
x = nn.functional.log_softmax(self.l5(x), dim=1)
return x
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))])
test_dataset = datasets.FashionMNIST(’~/.pytorch/F_MNIST_data/’, train = False, transform=transform, download = True)
test_dataloader = utils.data.DataLoader(test_dataset, shuffle=True, batch_size=10000)
def test(i):
global count
title_order = [‘T-shirt/top’,
‘Trouser’,
‘Pullover’,
‘Dress’,
‘Coat’,
‘Sandal’,
‘Shirt’,
‘Sneaker’,
‘Bag’,
‘Ankle Boot’]
dataiter = iter(test_dataloader)
images, labels = dataiter.next()
img = images[i]
lbl = labels[i]
with autograd.no_grad():
output = model.forward(img)
c_number=np.argmax(output)
if title_order[lbl]==title_order[c_number]:
count = count+1
model=Model()
model.load_state_dict(load(‘Adam_baseline.pth’))
model.eval()
images,labels = next(iter(test_dataloader))
for i in range(10000):
test(i)
if(i%100==0):
print(i,“images inferenced”)
acc=count/10000
print(“Predicted % for SGD baseline”,acc)

Can you post an indented code? :smiley:

https://colab.research.google.com/drive/1MCb4-E8oVAWkjiiuzZQvMrLGeqR5RwF-

Sorry my bad @Paulo_Augusto_Nino ! Here’s the same code as colab notebook