neural_network #25

Merged
s481834 merged 12 commits from neural_network into refactor 2024-06-04 13:23:20 +02:00
4 changed files with 18 additions and 5 deletions
Showing only changes of commit 26c99ddc55 - Show all commits

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
__pycache__/ __pycache__/
.idea/ .idea/
tree.png tree.png
dataset/
dataset.zip

View File

@ -66,6 +66,9 @@ def getRandomImageFromDataBase():
imgPath = os.path.join(folderPath, random_image) imgPath = os.path.join(folderPath, random_image)
while imgPath in imagePathList: while imgPath in imagePathList:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
label = random.choice(neuralnetwork.labels) label = random.choice(neuralnetwork.labels)
folderPath = f"dataset/test/{label}" folderPath = f"dataset/test/{label}"
files = os.listdir(folderPath) files = os.listdir(folderPath)

View File

@ -197,6 +197,9 @@ class Tractor:
count = 0 count = 0
for i in range(initPos[1], dCon.NUM_Y): for i in range(initPos[1], dCon.NUM_Y):
for j in range(initPos[0], dCon.NUM_X): for j in range(initPos[0], dCon.NUM_X):
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if self.slot.imagePath != None: if self.slot.imagePath != None:
predictedLabel = nn.predictLabel(self.slot.imagePath, model) predictedLabel = nn.predictLabel(self.slot.imagePath, model)
print(str("Coords: ({:02d}, {:02d})").format(self.slot.x_axis, self.slot.y_axis), "real:", self.slot.label, "predicted:", predictedLabel, "correct" if (self.slot.label == predictedLabel) else "incorrect", 'nawożę za pomocą:', nn.fertilizer[predictedLabel]) print(str("Coords: ({:02d}, {:02d})").format(self.slot.x_axis, self.slot.y_axis), "real:", self.slot.label, "predicted:", predictedLabel, "correct" if (self.slot.label == predictedLabel) else "incorrect", 'nawożę za pomocą:', nn.fertilizer[predictedLabel])

View File

@ -9,12 +9,12 @@ from PIL import Image
import random import random
imageSize = (128, 128) imageSize = (128, 128)
labels = ['beetroot', 'carrot', 'potato'] # musi być w kolejności alfabetycznej labels = ['carrot', 'potato', 'tomato'] # musi być w kolejności alfabetycznej
fertilizer = {labels[0]: 'kompost', labels[1]: 'saletra amonowa', labels[2]: 'superfosfat'} fertilizer = {labels[0]: 'kompost', labels[1]: 'saletra amonowa', labels[2]: 'superfosfat'}
torch.manual_seed(42) torch.manual_seed(42)
device = torch.device('cuda') if torch.cuda.is_available () else torch.device('cpu') device = torch.device('cuda')
def getTransformation(): def getTransformation():
transform=transforms.Compose([ transform=transforms.Compose([
@ -67,9 +67,11 @@ def getModel():
return model return model
def saveModel(model, path): def saveModel(model, path):
print("Saving model")
torch.save(model.state_dict(), path) torch.save(model.state_dict(), path)
def loadModel(path): def loadModel(path):
print("Loading model")
model = getModel() model = getModel()
model.load_state_dict(torch.load(path)) model.load_state_dict(torch.load(path))
return model return model
@ -83,6 +85,8 @@ def trainNewModel(n_iter=100, batch_size=256):
def predictLabel(imagePath, model): def predictLabel(imagePath, model):
image = Image.open(imagePath).convert("RGB") image = Image.open(imagePath).convert("RGB")
image = preprocess_image(image) image = preprocess_image(image)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
with torch.no_grad(): with torch.no_grad():
model.eval() # Ustawienie modelu w tryb ewaluacji model.eval() # Ustawienie modelu w tryb ewaluacji
output = model(image) output = model(image)
@ -97,9 +101,10 @@ def predictLabel(imagePath, model):
def preprocess_image(image): def preprocess_image(image):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
transform = getTransformation() transform = getTransformation()
image = transform(image).unsqueeze(0) # Dodanie wymiaru batch_size image = transform(image).unsqueeze(0) # Add batch dimension
image = image.to(device) # Move the image tensor to the same device as the model
return image return image