Changed method for accuracy calculation:

This commit is contained in:
Cezary Adamczak 2021-06-20 15:04:51 +02:00
parent 3898a3bcab
commit 14795cdc5e

View File

@ -1,63 +1,47 @@
import PIL import torch
import torchvision
import torchvision.transforms as transforms import torchvision.transforms as transforms
import torch.nn as nn
from AI import neural_network import torch.nn.functional as F
import torch.optim as optim
import numpy as np
from matplotlib.pyplot import imshow
import os
import PIL
import numpy as np
from matplotlib.pyplot import imshow
import neural_network
from matplotlib.pyplot import imshow
# wcześniej grinder.py # wcześniej grader.py
# Get accuracy for neural_network model 'network_model.pth' # Get accuracy for neural_network model 'network_model.pth'
def NN_accuracy(): def NN_accuracy():
# Create the model # Create the model
model = neural_network.Net() net = neural_network.Net()
# Load state_dict # Load state_dict
neural_network.load_network_from_structure(model) neural_network.load_network_from_structure(net)
# Create the preprocessing transformation here
transform = transforms.Compose([neural_network.Negative(), transforms.ToTensor()])
# load your image(s)
img = PIL.Image.open('../src/test/0_100.jpg')
img2 = PIL.Image.open('../src/test/1_100.jpg')
img3 = PIL.Image.open('../src/test/4_100.jpg')
img4 = PIL.Image.open('../src/test/5_100.jpg')
# Transform
input = transform(img)
input2 = transform(img2)
input3 = transform(img3)
input4 = transform(img4)
# unsqueeze batch dimension, in case you are dealing with a single image
input = input.unsqueeze(0)
input2 = input2.unsqueeze(0)
input3 = input3.unsqueeze(0)
input4 = input4.unsqueeze(0)
# Set model to eval # Set model to eval
model.eval() net.eval()
# Get prediction device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
output = model(input)
output2 = model(input2)
output3 = model(input3)
output4 = model(input4)
print(output) folderlist = os.listdir(os.path.dirname(__file__) + "\\test")
index = output.cpu().data.numpy().argmax()
print(index)
print(output2) tested = 0
index = output2.cpu().data.numpy().argmax() correct = 0
print(index)
print(output3) for folder in folderlist:
index = output3.cpu().data.numpy().argmax() for file in os.listdir(os.path.dirname(__file__) + "\\test\\" + folder):
print(index) if neural_network.result_from_network(net, os.path.dirname(__file__) + "\\test\\" + folder + "\\" + file) == folder:
correct += 1
tested += 1
else:
tested += 1
print(output4) print(correct/tested)
index = output4.cpu().data.numpy().argmax()
print(index)
if __name__ == "__main__": if __name__ == "__main__":