ladowanie pola z pliku GA, rozpoznawanie zdjec na polu, decyzja o podlaniu

This commit is contained in:
tafit0902 2024-06-07 00:02:36 +02:00
parent 0becd1b1f6
commit 51a5b13669
7 changed files with 77 additions and 11 deletions

21
App.py
View File

@ -10,6 +10,7 @@ import Ui
import BFS
import AStar
import neuralnetwork
import json
bfs1_flag=False
@ -20,8 +21,9 @@ Astar2 = False
if bfs3_flag or Astar or Astar2:
Pole.stoneFlag = True
TreeFlag=False
nnFlag=True
nnFlag=False
newModel=False
finalFlag = True
pygame.init()
show_console=True
@ -43,7 +45,15 @@ def init_demo(): #Demo purpose
old_info=""
traktor.draw_tractor()
time.sleep(2)
if not finalFlag:
pole.randomize_colors(nnFlag)
else:
population = 120
iterat = 2500
roulette = True
with open(f'pole_pop{population}_iter{iterat}_{roulette}.json', 'r') as file:
garden_data = json.load(file)
pole.setPlantsByList(garden_data)
traktor.draw_tractor()
start_flag=True
while True:
@ -128,11 +138,16 @@ def init_demo(): #Demo purpose
print_to_console("sieć nuronowa nauczona")
print('model został wygenerowany')
else:
model = neuralnetwork.loadModel('model.pth')
model = neuralnetwork.loadModel('model_500_hidden.pth')
print_to_console("model został załądowny")
testset = neuralnetwork.getDataset(False)
print(neuralnetwork.accuracy(model, testset))
traktor.snake_move_predict_plant(pole, model)
traktor.snake_move_predict_plant(pole, model, headers=['Coords','Real plant','Predicted plant','Result','Fertilizer'], actions=[traktor.fertilize_slot])
if(finalFlag):
pass
model = neuralnetwork.loadModel('model_500_hidden.pth')
Tractor.drzewo.treeLearn()
traktor.snake_move_predict_plant(pole, model, headers=['Coords','Real plant','Predicted plant','Result','Decision'], actions=[traktor.irigate_slot_NN])
start_flag=False
# demo_move()
old_info=get_info(old_info)

View File

@ -8,7 +8,7 @@ class Drzewo:
self.tree=self.treeLearn()
def treeLearn(self):
csvdata=pandas.read_csv('Data/dataTree.csv')
csvdata=pandas.read_csv('Data/dataTree2.csv')
#csvdata = pandas.read_csv('Data/dataTree2.csv')
x=csvdata[atributes]
decision=csvdata['action']

View File

@ -80,3 +80,25 @@ def getRandomImageFromDataBase():
image = pygame.image.load(imgPath)
image=pygame.transform.scale(image,(dCon.CUBE_SIZE,dCon.CUBE_SIZE))
return image, label, imgPath
def getSpedifiedImageFromDatabase(label):
folderPath = f"dataset/test/{label}"
files = os.listdir(folderPath)
random_image = random.choice(files)
imgPath = os.path.join(folderPath, random_image)
while imgPath in imagePathList:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
label = random.choice(neuralnetwork.labels)
folderPath = f"dataset/test/{label}"
files = os.listdir(folderPath)
random_image = random.choice(files)
imgPath = os.path.join(folderPath, random_image)
imagePathList.append(imgPath)
image = pygame.image.load(imgPath)
image=pygame.transform.scale(image,(dCon.CUBE_SIZE,dCon.CUBE_SIZE))
return image, label, imgPath

View File

@ -62,6 +62,14 @@ class Pole:
continue
else:
self.slot_dict[coordinates].set_random_plant(nn)
def setPlantsByList(self, plantList):
pygame.display.update()
time.sleep(3)
for coordinates in self.slot_dict:
if(coordinates==(0,0)):
continue
else:
self.slot_dict[coordinates].set_specifided_plant(plantList[coordinates[1]][coordinates[0]])
def change_color_of_slot(self,coordinates,color): #Coordinates must be tuple (x,y) (left top slot has cord (0,0) ), color has to be from defined in Colors.py or custom in RGB value (R,G,B)
self.get_slot_from_cord(coordinates).color_change(color)

View File

@ -50,6 +50,11 @@ class Slot:
self.plant=Roslina.Roslina(self.label)
self.set_image()
def set_specifided_plant(self, plant):
self.plant_image, self.label, self.imagePath = self.specified_plant_dataset(plant)
self.plant=Roslina.Roslina(self.label)
self.set_image()
def set_image(self):
if self.plant_image is None:
self.plant_image = self.image_loader.return_random_plant()
@ -75,6 +80,8 @@ class Slot:
return self.image_loader.return_random_plant()
def random_plant_dataset(self):
return Image.getRandomImageFromDataBase()
def specified_plant_dataset(self, plant):
return Image.getSpedifiedImageFromDatabase(plant)
def return_plant(self):
return self.plant

View File

@ -30,6 +30,7 @@ class Tractor:
DIRECTION_SOUTH = 'S'
DIRECTION_WEST = 'W'
DIRECTION_EAST = 'E'
def __init__(self,slot,screen, osprzet,clock,bfs2_flag):
self.tractor_images = {
Tractor.DIRECTION_NORTH: pygame.transform.scale(pygame.image.load('images/traktorN.png'),
@ -193,8 +194,7 @@ class Tractor:
self.turn_left()
print("podlanych slotów: ", str(counter))
def snake_move_predict_plant(self, pole, model):
headers=['Coords','Real plant','Predicted plant','Result','Fertilizer']
def snake_move_predict_plant(self, pole, model, headers, actions = None):
print(format_string_nn.format(*headers))
initPos = (self.slot.x_axis, self.slot.y_axis)
count = 0
@ -207,9 +207,11 @@ class Tractor:
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(format_string_nn.format(f"{self.slot.x_axis,self.slot.y_axis}",self.slot.label,predictedLabel,"correct" if (self.slot.label == predictedLabel) else "incorrect",nn.fertilizer[predictedLabel]))
# print(format_string_nn.format(f"{self.slot.x_axis,self.slot.y_axis}",self.slot.label,predictedLabel,"correct" if (self.slot.label == predictedLabel) else "incorrect",nn.fertilizer[predictedLabel]))
for a in actions:
a(predictedLabel)
if self.slot.label != predictedLabel:
self.slot.mark_visited()
# self.slot.mark_visited()
count += 1
self.move_forward(pole, False)
if i % 2 == 0 and i != dCon.NUM_Y - 1:
@ -220,7 +222,19 @@ class Tractor:
self.turn_left()
self.move_forward(pole, False)
self.turn_left()
print(f"Dobrze nawiezionych roślin: {20*12-count}, źle nawiezionych roślin: {count}")
print(f"Dobrze rozpoznanych roślin: {20*12-count}, źle rozpoznanych roślin: {count}")
def fertilize_slot(self, predictedLabel):
print(format_string_nn.format(f"{self.slot.x_axis,self.slot.y_axis}",self.slot.label,predictedLabel,"correct" if (self.slot.label == predictedLabel) else "incorrect",nn.fertilizer[predictedLabel]))
if self.slot.label != predictedLabel:
self.slot.mark_visited()
def irigate_slot_NN(self, predictedLabel):
attributes=self.get_attributes()
decision = drzewo.makeDecision(attributes)
print(format_string_nn.format(f"{self.slot.x_axis,self.slot.y_axis}",self.slot.label,predictedLabel,"correct" if (self.slot.label == predictedLabel) else "incorrect",decision))
condition.cycle()
self.waterLevel = random.randint(0, 100)
def snake_move(self,pole,x,y):
next_slot_coordinates=(x,y)

View File

@ -77,7 +77,7 @@ def saveModel(model, path):
def loadModel(path):
print("Loading model")
model = getModel()
model.load_state_dict(torch.load(path))
model.load_state_dict(torch.load(path, map_location=torch.device('cpu'))) # musiałem tutaj dodać to ładowanie z mapowaniem na cpu bo u mnie CUDA nie działa wy pewnie możecie to usunąć
return model
def trainNewModel(n_iter=100, batch_size=256):