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

Ruchi_Tiwari: <p>#!pip3 install torch==1.5.0 torchvision==1.6.0</p>
<p>import torch</p>
<p>import torchvision</p>
<p>import torchvision.transforms as transforms</p>
<p>import torch.nn as nn</p>
<p>import torch.nn.functional as F</p>
<p>import torch.optim as optim</p>
<p>import os</p>
<p>from torch.utils.data import DataLoader</p>
<p>import torch.quantization</p>
<p>from torch.quantization import QuantStub, DeQuantStub</p>
<p>import torchvision.models as models</p>
<p>import json# Define the transforms and load the MNIST dataset<br>
transform = transforms.Compose(<br>
[transforms.ToTensor(),<br>
transforms.Normalize((0.5,), (0.5,))])</p>
<p>trainset = torchvision.datasets.MNIST(root=‘./data’, train=True,<br>
download=True, transform=transform)<br>
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64,<br>
shuffle=True, num_workers=16, pin_memory=True)</p>
<p>testset = torchvision.datasets.MNIST(root=‘./data’, train=False,<br>
download=True, transform=transform)<br>
testloader = torch.utils.data.DataLoader(testset, batch_size=64,<br>
shuffle=False, num_workers=16, pin_memory=True)model = models.resnet50(pretrained=False)def change_layers(model):<br>
model.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)<br>
model.fc = nn.Linear(2048, 10, bias=True)<br>
return model<br>
change_layers(model)if(torch.cuda.is_available()):<br>
model = model.cuda()<br>
criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)import time</p>
<p>print(‘Training…’)<br>
total = 0<br>
correct = 0<br>
start = time.time()</p>
<p>for epoch in range(10):</p>
<pre><code>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()
</code></pre>
<p>print(‘Training Completed in: {} secs’.format(time.time()-start))<br>
print(‘Training accuracy: {} %’.format((correct/total)*100))<br>
print(‘Testing…’)<br>
total = 0<br>
correct = 0<br>
start = time.time()</p>
<h1><a name="set-the-model-to-evaluation-mode-1" class="anchor" href="#set-the-model-to-evaluation-mode-1"></a>Set the model to evaluation mode</h1>
<p>model.eval()</p>
<p>with torch.no_grad():<br>
for data in testloader:<br>
images, labels = data</p>
<pre><code>    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()
</code></pre>
<p>print(‘Testing Completed in: {} secs’.format(time.time()-start))<br>
print(‘Test accuracy : {} %’.format((correct/total)*100))</p>
<h1><a name="save-the-trained-model-2" class="anchor" href="#save-the-trained-model-2"></a>Save the trained model</h1>
<p>torch.save(model.state_dict(), ‘mnist_resnet.pth’)</p>
<p>print(“Trained model saved as ‘mnist_resnet.pth’”)<br>
import torch<br>
import torchvision.models as models</p>
<h1><a name="load-the-trained-model-architecture-3" class="anchor" href="#load-the-trained-model-architecture-3"></a>Load the trained model architecture</h1>
<p><span class="hashtag-raw">#model</span> = models.resnet50(pretrained=False)</p>
<p>model = models.resnet50(pretrained=False)  # where num_classes will be different<br>
model.state_dict(torch.load(‘/content/mnist_resnet.pth’))  # load<br>
model.eval()</p>
<h1><a name="rest-of-your-cod-4" class="anchor" href="#rest-of-your-cod-4"></a>Rest of your cod</h1>
<h1><a name="define-a-function-to-test-the-model-accuracy-5" class="anchor" href="#define-a-function-to-test-the-model-accuracy-5"></a>Define a function to test the model accuracy</h1>
<p>def test_accuracy(model, dataloader):<br>
model.eval()<br>
correct = 0<br>
total = 0<br>
with torch.no_grad():<br>
for images, labels in dataloader:<br>
if torch.cuda.is_available():<br>
images, labels = images.cuda(), labels.cuda()<br>
outputs = model(images)<br>
_, predicted = torch.max(outputs, 1)<br>
total += labels.size(0)<br>
correct += (predicted == labels).sum().item()<br>
accuracy = (correct / total) * 100<br>
return accuracy<br>
model.qconfig = torch.quantization.default_qconfig</p>
<p>print(model.qconfig)</p>
<p>torch.quantization.prepare(model, inplace=True)</p>
<p>print(‘Post Training Quantization Prepare: Inserting Observers’)</p>
<p>print(‘\n Conv1: After observer insertion \n\n’, model.conv1)</p>
<p>torch.quantization.convert(model, inplace=True)</p>
<h1><a name="define-the-device-cpu-or-gpu-6" class="anchor" href="#define-the-device-cpu-or-gpu-6"></a>Define the device (CPU or GPU)</h1>
<p>device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)</p>
<h1><a name="move-the-traced-model-to-the-desired-device-7" class="anchor" href="#move-the-traced-model-to-the-desired-device-7"></a>Move the traced model to the desired device</h1>
<p>model.to(device)# Test the traced model<br>
total = 0<br>
correct = 0<br>
start = time.time()</p>
<p>with torch.no_grad():<br>
for data in testloader:<br>
images, labels = data<br>
images, labels = images.to(device), labels.to(device)</p>
<pre><code>    outputs = model(images)
    _, predicted = torch.max(outputs.data, 1)
    total += labels.size(0)
    correct += (predicted == labels).sum().item()
</code></pre>
<p>print(‘Testing Completed in: {} secs’.format(time.time()-start))<br>
print(‘Test accuracy : {:.2f}% - {}’.format((correct/total)*100, ‘Quantized’))</p>
<h1><a name="save-the-traced-model-8" class="anchor" href="#save-the-traced-model-8"></a>Save the traced model</h1>
<h2><a name="modelsavemnist_resnet_quantized_tracedpth-printtraced-quantized-model-saved-as-mnist_resnet_quantized_tracedpth-9" class="anchor" href="#modelsavemnist_resnet_quantized_tracedpth-printtraced-quantized-model-saved-as-mnist_resnet_quantized_tracedpth-9"></a>model.save(‘mnist_resnet_quantized_traced.pth’)<br>
print(“Traced quantized model saved as ‘mnist_resnet_quantized_traced.pth’”)</h2>
<p>NotImplementedError                       Traceback (most recent call last)<br>
 in &lt;cell line: 6&gt;()<br>
9         images, labels = images.to(device), labels.to(device)<br>
10<br>
—&gt; 11         outputs = model(images)<br>
12         _, predicted = torch.max(outputs.data, 1)<br>
13         total += labels.size(0)</p>
<p>5 frames<br>
/usr/local/lib/python3.10/dist-packages/torch/_ops.py in <strong>call</strong>(self, *args, **kwargs)<br>
500         # We save the function ptr as the <code>op</code> attribute on<br>
501         # OpOverloadPacket to access it here.<br>
 → 502         return self._op(*args, **kwargs or {})<br>
503<br>
504     # TODO: use this to make a <strong>dir</strong>  in the last i am getting error kindly please help me for this</p>