Compare commits

..

3 Commits

Author SHA1 Message Date
Aliaksei Brown
3c5b05a7bb nn update 3 2023-06-05 16:18:20 +02:00
Aliaksei Brown
50292376e7 added: trained_model option 2023-06-05 01:30:39 +02:00
Aliaksei Brown
ffe1ff9565 added: datasets with vegies, two neural network training models 2023-06-05 00:42:39 +02:00
21021 changed files with 323 additions and 393 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.idea
.DS_Store
__pycache__

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

121
main.py
View File

@ -4,90 +4,10 @@ import random
import land
import tractor
import blocks
import nn
import astar_search
import neural_network.inference
from pygame.locals import *
examples = [
['piasek', 'sucha', 'jalowa', 'żółty'],
['czarnoziem', 'wilgotna', 'bogata', 'brazowa'],
['rzedzina', 'wilgotna', 'bogata', 'zielona'],
['gleby murszowe', 'wilgotna', 'bogata', 'szara'],
['pustynne gleby', 'sucha', 'jalowa', 'pomarańczowa'],
['torfowiska', 'sucha', 'jalowa', 'czerwona']
]
attributes = ['typ_gleby', 'wilgotność', 'zawartość_składników', 'kolor']
# Tworzenie obiektu TreeLearn i nauka drzewa decyzyjnego
# tree_learner = TreeLearn()
# default_class = 'nieznane'
# tree_learner.train(examples, attributes, default_class)
class TreeLearn:
def __init__(self):
self.tree = None
def train(self, examples, attributes, default_class):
self.tree = self.build_tree(examples, attributes, default_class)
def build_tree(self, examples, attributes, default_class):
if not examples:
return Node(default_class)
if self.all_same_class(examples):
return Node(examples[0][-1])
if not attributes:
class_counts = self.get_class_counts(examples)
default_class = max(class_counts, key=class_counts.get)
return Node(default_class)
best_attribute = self.choose_attribute(examples, attributes)
root = Node(best_attribute)
attribute_values = self.get_attribute_values(examples, best_attribute)
for value in attribute_values:
new_examples = self.filter_examples(examples, best_attribute, value)
new_attributes = attributes[:]
new_attributes.remove(best_attribute)
new_default_class = max(self.get_class_counts(new_examples), key=lambda k: class_counts.get(k, 0))
subtree = self.build_tree(new_examples, new_attributes, new_default_class)
root.add_child(value, subtree)
return root
def all_same_class(self, examples):
return len(set([example[-1] for example in examples])) == 1
def get_class_counts(self, examples):
class_counts = {}
for example in examples:
class_label = example[-1]
class_counts[class_label] = class_counts.get(class_label, 0) + 1
return class_counts
def choose_attribute(self, examples, attributes):
# Placeholder for attribute selection logic
return attributes[0]
def get_attribute_values(self, examples, attribute):
return list(set([example[attribute] for example in examples]))
def filter_examples(self, examples, attribute, value):
return [example for example in examples if example[attribute] == value]
class Node:
def __init__(self, label):
self.label = label
self.children = {}
def add_child(self, value, child):
self.children[value] = child
import numpy as np
class Game:
@ -105,6 +25,8 @@ class Game:
self.grass_body = []
self.red_block = [] #aim block
#self.one_body = []
self.fawn_seed_body = []
self.fawn_wheat_body = []
@ -141,6 +63,15 @@ class Game:
# self.potato = blocks.Blocks(self.surface, self.cell_size)
# self.potato.locate_soil('black earth', 6, 1, [])
#class_names = ['Pumpkin', 'Tomato', 'Carrot']
self.neural_network = nn.NNModel("neural_network/save/second_model.pth")
# self.pumpkin_batch = self.neural_network.input_image("resources/pampkin.png")
# self.tomato_batch = self.neural_network.input_image("resources/tomato.png")
# self.carrot_batch = self.neural_network.input_image("resources/carrot.png")
self.tractor = tractor.Tractor(self.surface, self.cell_size)
self.tractor.draw()
@ -152,13 +83,10 @@ class Game:
clock = pygame.time.Clock()
move_tractor_event = pygame.USEREVENT + 1
pygame.time.set_timer(move_tractor_event, 500) # tractor moves every 1000 ms
pygame.time.set_timer(move_tractor_event, 100) # tractor moves every 1000 ms
tractor_next_moves = []
astar_search_object = astar_search.Search(self.cell_size, self.cell_number)
veggies = dict()
veggies_debug = dict()
while running:
clock.tick(60) # manual fps control not to overwork the computer
for event in pygame.event.get():
@ -186,28 +114,21 @@ class Game:
random_x = random.randrange(0, self.cell_number * self.cell_size, 50)
random_y = random.randrange(0, self.cell_number * self.cell_size, 50)
print("Generated target: ",random_x, random_y)
#aim-blue block
if self.red_block:
self.red_block.pop()
self.red_block.append([random_x/50, random_y/50])
self.path_image = "resources/2.png"
self.aim_batch = self.neural_network.input_image(self.path_image)
self.predicate = self.neural_network.predicte(self.aim_batch)
# below line should be later moved into tractor.py
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
#bandaid to know about stones
tractor_next_moves = astar_search_object.astarsearch(
[self.tractor.x, self.tractor.y, angles[self.tractor.angle]], [random_x, random_y], self.stone_body, self.flower_body)
current_veggie = next(os.walk('./neural_network/images/test'))[1][random.randint(0, len(next(os.walk('./neural_network/images/test'))[1])-1)]
if(current_veggie in veggies_debug):
veggies_debug[current_veggie]+=1
else:
veggies_debug[current_veggie] = 1
current_veggie_example = next(os.walk(f'./neural_network/images/test/{current_veggie}'))[2][random.randint(0, len(next(os.walk(f'./neural_network/images/test/{current_veggie}'))[2])-1)]
predicted_veggie = neural_network.inference.main(f"./neural_network/images/test/{current_veggie}/{current_veggie_example}")
if predicted_veggie in veggies:
veggies[predicted_veggie]+=1
else:
veggies[predicted_veggie] = 1
print("Debug veggies: ", veggies_debug, "Predicted veggies: ", veggies)
else:
self.tractor.move(tractor_next_moves.pop(0)[0], self.cell_size, self.cell_number)
elif event.type == QUIT:

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Some files were not shown because too many files have changed in this diff Show More