I am not able to test my quantized model with post training static quantization

#!pip3 install torch==1.5.0 torchvision==1.6.0

import torch

import torchvision

import torchvision.transforms as transforms

import torch.nn as nn

import torch.nn.functional as F

import torch.optim as optim

import os

from torch.utils.data import DataLoader

import torch.quantization

from torch.quantization import QuantStub, DeQuantStub

import torchvision.models as models

import json# Define the transforms and load the MNIST dataset
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])

trainset = torchvision.datasets.MNIST(root=‘./data’, train=True,
download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64,
shuffle=True, num_workers=16, pin_memory=True)

testset = torchvision.datasets.MNIST(root=‘./data’, train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64,
shuffle=False, num_workers=16, pin_memory=True)model = models.resnet50(pretrained=False)def change_layers(model):
model.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
model.fc = nn.Linear(2048, 10, bias=True)
return model
change_layers(model)if(torch.cuda.is_available()):
model = model.cuda()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)import time

print(‘Training…’)
total = 0
correct = 0
start = time.time()

for epoch in range(10):

for i, data in enumerate(trainloader, 1):
    images, labels = data

    if(torch.cuda.is_available()):
        images = images.cuda()
        labels = labels.cuda()

    optimizer.zero_grad()
    outputs = model(images)

    _, predicted = torch.max(outputs.data, 1)
    total += labels.size(0)
    correct += (predicted == labels).sum().item()

    loss = criterion(outputs, labels)
    if(i%100 == 0):
        print('Epoch: {} Batch: {} loss: {}'.format(epoch, i, loss.item()))

    loss.backward()
    optimizer.step()

print(‘Training Completed in: {} secs’.format(time.time()-start))
print(‘Training accuracy: {} %’.format((correct/total)*100))
print(‘Testing…’)
total = 0
correct = 0
start = time.time()

Set the model to evaluation mode

model.eval()

with torch.no_grad():
for data in testloader:
images, labels = data

    if torch.cuda.is_available():
        images = images.cuda()
        labels = labels.cuda()

    outputs = model(images)
    _, predicted = torch.max(outputs.data, 1)
    total += labels.size(0)
    correct += (predicted == labels).sum().item()

print(‘Testing Completed in: {} secs’.format(time.time()-start))
print(‘Test accuracy : {} %’.format((correct/total)*100))

Save the trained model

torch.save(model.state_dict(), ‘mnist_resnet.pth’)

print(“Trained model saved as ‘mnist_resnet.pth’”)
import torch
import torchvision.models as models

Load the trained model architecture

#model = models.resnet50(pretrained=False)

model = models.resnet50(pretrained=False) # where num_classes will be different
model.state_dict(torch.load(‘/content/mnist_resnet.pth’)) # load
model.eval()

Rest of your cod

Define a function to test the model accuracy

def test_accuracy(model, dataloader):
model.eval()
correct = 0
total = 0
with torch.no_grad():
for images, labels in dataloader:
if torch.cuda.is_available():
images, labels = images.cuda(), labels.cuda()
outputs = model(images)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = (correct / total) * 100
return accuracy
model.qconfig = torch.quantization.default_qconfig

print(model.qconfig)

torch.quantization.prepare(model, inplace=True)

print(‘Post Training Quantization Prepare: Inserting Observers’)

print(‘\n Conv1: After observer insertion \n\n’, model.conv1)

torch.quantization.convert(model, inplace=True)

Define the device (CPU or GPU)

device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)

Move the traced model to the desired device

model.to(device)# Test the traced model
total = 0
correct = 0
start = time.time()

with torch.no_grad():
for data in testloader:
images, labels = data
images, labels = images.to(device), labels.to(device)

    outputs = model(images)
    _, predicted = torch.max(outputs.data, 1)
    total += labels.size(0)
    correct += (predicted == labels).sum().item()

print(‘Testing Completed in: {} secs’.format(time.time()-start))
print(‘Test accuracy : {:.2f}% - {}’.format((correct/total)*100, ‘Quantized’))

Save the traced model

model.save(‘mnist_resnet_quantized_traced.pth’)
print(“Traced quantized model saved as ‘mnist_resnet_quantized_traced.pth’”)

NotImplementedError Traceback (most recent call last)
in <cell line: 6>()
9 images, labels = images.to(device), labels.to(device)
10
—> 11 outputs = model(images)
12 _, predicted = torch.max(outputs.data, 1)
13 total += labels.size(0)

5 frames
/usr/local/lib/python3.10/dist-packages/torch/_ops.py in call(self, *args, **kwargs)
500 # We save the function ptr as the op attribute on
501 # OpOverloadPacket to access it here.
→ 502 return self._op(*args, **kwargs or {})
503
504 # TODO: use this to make a dir in the last i am getting error kindly please help me for this