Merge branch 'sieci-neuronowe-uczenie'

This commit is contained in:
Zofia Lorenc 2024-06-09 09:28:11 +02:00
commit 4595a5fe83
5 changed files with 80 additions and 5 deletions

3
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"recommendations": ["sbsnippets.pytorch-snippets"]
}

3
src/import torch.py Normal file
View File

@ -0,0 +1,3 @@
import torch
x = torch.rand(5, 3)
print(x)

View File

@ -1,4 +1,3 @@
import sys
import pygame import pygame
from field import Field from field import Field
import os import os

View File

@ -4,6 +4,10 @@ from kb import tractor_kb
import pytholog as pl import pytholog as pl
import random import random
from config import TILE_SIZE, FREE_TILES from config import TILE_SIZE, FREE_TILES
import torch
import torchvision.transforms as transforms
from PIL import Image
class Tile(pygame.sprite.Sprite): class Tile(pygame.sprite.Sprite):
@ -26,15 +30,39 @@ class Tile(pygame.sprite.Sprite):
self.set_type(random_vegetable) self.set_type(random_vegetable)
self.water_level = random.randint(1, 5) * 10 self.water_level = random.randint(1, 5) * 10
self.stage = 'planted' # wczesniej to była self.faza = 'posadzono' ale stwierdzilem ze lepiej po angielsku??? self.stage = 'planted' # wczesniej to była self.faza = 'posadzono' ale stwierdzilem ze lepiej po angielsku???
classes = [
"bób", "brokuł", "brukselka", "burak", "cebula",
"cukinia", "dynia", "fasola", "groch", "jarmuż",
"kalafior", "kalarepa", "kapusta", "marchew",
"ogórek", "papryka", "pietruszka", "pomidor",
"por", "rzepa", "rzodkiewka", "sałata", "seler",
"szpinak", "ziemniak"]
model = torch.load("veggies_recognition/best_model.pth")
mean = [0.5322, 0.5120, 0.3696]
std = [0.2487, 0.2436, 0.2531]
image_transforms = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(torch.Tensor(mean),torch.Tensor(std))
])
self.prediction = self.predict(model, image_transforms, self.image_path, classes)
else: else:
if random.randint(1, 10) % 3 == 0: if random.randint(1, 10) % 3 == 0:
self.set_type('water') self.set_type('water')
self.water_level = 100 self.water_level = 100
self.stage = 'no_plant' self.stage = 'no_plant'
self.prediction = 'water'
else: else:
self.set_type('grass') self.set_type('grass')
self.water_level = random.randint(1, 5) * 10 self.water_level = random.randint(1, 5) * 10
self.stage = 'no_plant' self.stage = 'no_plant'
self.prediction = 'grass'
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
@ -44,6 +72,15 @@ class Tile(pygame.sprite.Sprite):
def draw(self, surface): def draw(self, surface):
self.tiles.draw(surface) self.tiles.draw(surface)
def get_random_image_from_folder(self):
folder_path = f"veggies_recognition/veggies/testing/{self.type}"
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
random_file = random.choice(files)
image_path = folder_path + "/" + random_file
return image_path
def set_type(self, type): def set_type(self, type):
self.type = type self.type = type
if self.type == 'grass': if self.type == 'grass':
@ -51,9 +88,30 @@ class Tile(pygame.sprite.Sprite):
elif self.type == 'water': elif self.type == 'water':
image_path = "images/water.png" image_path = "images/water.png"
else: else:
image_path = f"images/vegetables/{self.type}.png" #image_path = f"images/vegetables/{self.type}.png"
image_path = self.get_random_image_from_folder()
if not os.path.exists(image_path): if not os.path.exists(image_path):
image_path = "images/question.jpg" image_path = "images/question.jpg"
self.image_path = image_path
self.image = pygame.image.load(image_path).convert() self.image = pygame.image.load(image_path).convert()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE)) self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
def predict(self, model, image_transforms, image_path, classes):
model = model.eval()
image = Image.open(image_path)
image = image.convert("RGB")
image = image_transforms(image).float()
image = image.unsqueeze(0)
output = model(image)
_, predicted = torch.max(output.data, 1)
result = classes[predicted.item()]
if result == "ziemniak":
result = 'marchew'
return result

View File

@ -67,7 +67,9 @@ class Tractor(pygame.sprite.Sprite):
neighbors.append('grass') neighbors.append('grass')
input_data = { input_data = {
'tile_type': self.get_current_tile().type, #tutaj będzie dostawał informację ze zdjęcia
'tile_type': self.get_current_tile().prediction,
#'tile_type': self.get_current_tile().type,
'water_level': self.get_current_tile().water_level, 'water_level': self.get_current_tile().water_level,
"plant_stage": self.get_current_tile().stage, "plant_stage": self.get_current_tile().stage,
"neighbor_N": neighbors[0], "neighbor_N": neighbors[0],
@ -180,6 +182,7 @@ class Tractor(pygame.sprite.Sprite):
if (self.get_current_tile().type != 'grass' or self.get_current_tile().type == 'water'): action = 'move' if (self.get_current_tile().type != 'grass' or self.get_current_tile().type == 'water'): action = 'move'
self.prev_action = action self.prev_action = action
match (action): match (action):
case ('move'): case ('move'):
pass pass
@ -240,7 +243,16 @@ class Tractor(pygame.sprite.Sprite):
self.get_current_tile().set_type('ziemniak') self.get_current_tile().set_type('ziemniak')
self.move_2() self.move_2()
#self.action_index += 1 #self.action_index += 1
print(action)
if self.get_current_tile().type == "grass":
print("Co jest faktycznie: trawa")
elif self.get_current_tile().type == "water":
print("Co jest faktycznie: woda")
else:
print("Rozpoznano: ", self.get_current_tile().prediction)
print("Co jest faktycznie: ", self.get_current_tile().type)
print("\n")
return return
def log_info(self): def log_info(self):