I did try it and the accuracy is 45 percent.
with torch.no_grad():
correct=0
total=0
for images,labels in valid_loader:
images=images.to(device)
labels=labels.to(device)
op=F.conv2d(images,model[‘layer1.0.weight’],padding=1)
op=z1(op)
op=F.conv2d(op,model[‘layer1.3.weight’],padding=1)
op=z1(op)
op=zm(op)
op=F.conv2d(op,model[‘layer2.0.weight’],padding=1)
op=z2(op)
op=F.conv2d(op,model[‘layer2.3.weight’],padding=1)
op=z2(op)
op=zm(op)
op=F.conv2d(op,model[‘layer3.0.weight’],padding=1)
op=z3(op)
op=F.conv2d(op,model[‘layer3.3.weight’],padding=1)
op=z3(op)
op=zm(op)
op=op.reshape(op.size(0),-1)
op=F.dropout(op,False)
op=F.linear(op,model[‘layer4.1.weight’])
op=F.relu(op)
op=F.dropout(op,False)
op=F.linear(op,model[‘layer4.4.weight’])
op=F.softmax(op,dim=1)
_,predicted=torch.max(op,1)
total+=labels.size(0)
correct+=(predicted==labels).sum().item()
print(‘The validation accuracy is {}%’.format((correct/total)*100))
Lets say the trained model is model=ConvNet.to(device)
Instead of storing and loading the state_dict is there a direct way of storing the model instance itself so that I could do the following:
with torch.no_grad():
correct=0
total=0
for images,labels in valid_loader:
images=images.to(device)
labels=labels.to(device)
op=model(images) #loaded from a file
_,predicted=torch.max(op,1)
total+=labels.size(0)
correct+=(predicted==labels).sum().item()
print(‘The validation accuracy is {}%’.format((correct/total)*100))