From 328b171e5a48599ed93ecc28aa4313012dfa16ab Mon Sep 17 00:00:00 2001 From: Lewy Date: Tue, 15 Jun 2021 01:36:18 +0200 Subject: [PATCH] file namechange - changed name of files to represent inside - added main to seperate processes - changed raw code to functions --- AI/NN_accuracy.py | 64 ++++++++++++++++++++++++++++++++++++++++++++ AI/grader.py | 56 -------------------------------------- AI/id3.py | 6 +++-- AI/neural_network.py | 3 ++- 4 files changed, 70 insertions(+), 59 deletions(-) create mode 100644 AI/NN_accuracy.py delete mode 100644 AI/grader.py diff --git a/AI/NN_accuracy.py b/AI/NN_accuracy.py new file mode 100644 index 0000000..9ab14a6 --- /dev/null +++ b/AI/NN_accuracy.py @@ -0,0 +1,64 @@ +import PIL +import torchvision.transforms as transforms + +from AI import neural_network + + +# wcześniej grinder.py +# Get accuracy for neural_network model 'network_model.pth' +def NN_accuracy(): + # Create the model + model = neural_network.Net() + + # Load state_dict + neural_network.load_network_from_structure(model) + + # 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 + model.eval() + + # Get prediction + output = model(input) + output2 = model(input2) + output3 = model(input3) + output4 = model(input4) + + print(output) + index = output.cpu().data.numpy().argmax() + print(index) + + print(output2) + index = output2.cpu().data.numpy().argmax() + print(index) + + print(output3) + index = output3.cpu().data.numpy().argmax() + print(index) + + print(output4) + index = output4.cpu().data.numpy().argmax() + print(index) + + +if __name__ == "__main__": + NN_accuracy() diff --git a/AI/grader.py b/AI/grader.py deleted file mode 100644 index 7b0057a..0000000 --- a/AI/grader.py +++ /dev/null @@ -1,56 +0,0 @@ -import PIL -import torchvision.transforms as transforms - -from AI import neural_network - -# Create the model -model = neural_network.Net() - -# Load state_dict -neural_network.load_network_from_structure(model) - -# 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 -model.eval() - -# Get prediction -output = model(input) -output2 = model(input2) -output3 = model(input3) -output4 = model(input4) - -print(output) -index = output.cpu().data.numpy().argmax() -print(index) - -print(output2) -index = output2.cpu().data.numpy().argmax() -print(index) - -print(output3) -index = output3.cpu().data.numpy().argmax() -print(index) - -print(output4) -index = output4.cpu().data.numpy().argmax() -print(index) diff --git a/AI/id3.py b/AI/id3.py index 68b9348..f7b382d 100644 --- a/AI/id3.py +++ b/AI/id3.py @@ -120,5 +120,7 @@ def pretty_print(root, n): pretty_print(child[0], n + 1) -tree = treelearn(cases, attributes, 0) -pretty_print(tree, 0) +# Get view of decision_tree.py +if __name__ == "__main__": + tree = treelearn(cases, attributes, 0) + pretty_print(tree, 0) diff --git a/AI/neural_network.py b/AI/neural_network.py index 0910476..a2d340e 100644 --- a/AI/neural_network.py +++ b/AI/neural_network.py @@ -27,7 +27,7 @@ def plotdigit(image): transform = transforms.Compose([Negative(), transforms.ToTensor()]) -train_set = torchvision.datasets.ImageFolder(root='train', transform=transform) +train_set = torchvision.datasets.ImageFolder(root='../src/train', transform=transform) classes = ("apple", "potato") BATCH_SIZE = 2 @@ -99,6 +99,7 @@ def load_network_from_structure(network): network.load_state_dict(torch.load('network_model.pth')) +# Create network_model.pth if __name__ == "__main__": print(torch.cuda.is_available()) training_network()