add neural network
@ -72,6 +72,7 @@ def get_cost(node, goal: Mushroom, board):
|
||||
weight = 1000
|
||||
elif isinstance((board.board[node.x][node.y]), Mushroom) and board.dt.predict(board.board[node.x][node.y]):
|
||||
weight = 1000
|
||||
#board.nn.predict()
|
||||
else:
|
||||
weight = 2
|
||||
|
||||
|
@ -2,6 +2,7 @@ import random
|
||||
|
||||
from core.constans import *
|
||||
from core.decision_tree import DecisionTree
|
||||
from core.neural_network import Neural_network
|
||||
from core.tree import Tree
|
||||
from core.mushroom import Mushroom
|
||||
from core.agent import Agent
|
||||
@ -22,6 +23,8 @@ class Board:
|
||||
self.update_free_spaces()
|
||||
self.dt = DecisionTree(0.05)
|
||||
self.dt.learn()
|
||||
self.nn = Neural_network()
|
||||
self.nn.build_model()
|
||||
|
||||
self.agent = Agent(self.free_spaces.pop(), self.square_size)
|
||||
|
||||
|
97
core/neural_network.py
Normal file
@ -0,0 +1,97 @@
|
||||
import os, random
|
||||
import numpy as np
|
||||
import joblib
|
||||
import tensorflow as tf
|
||||
|
||||
from tensorflow import keras
|
||||
from tensorflow.python.keras.layers.convolutional import Conv2D
|
||||
from tensorflow.python.keras.layers.core import Dense, Flatten
|
||||
from tensorflow.python.keras.models import Sequential
|
||||
from tensorflow.python.keras.preprocessing.image_dataset import load_image
|
||||
|
||||
|
||||
class Neural_network:
|
||||
|
||||
def __init__(self):
|
||||
|
||||
#check physical devices
|
||||
physical_devices = tf.config.experimental.list_physical_devices('GPU')
|
||||
print("Num GPUs Available: ", len(physical_devices))
|
||||
tf.config.experimental.set_memory_growth(physical_devices[0], enable=True)
|
||||
|
||||
def build_model(self,image_size = (25,25),batch_size = 8):
|
||||
#Generate a Dataset
|
||||
self.train_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
||||
"core/resources/data/train",
|
||||
validation_split=0.2,
|
||||
subset="training",
|
||||
shuffle = False,
|
||||
image_size=image_size,
|
||||
batch_size=batch_size,
|
||||
)
|
||||
self.val_ds = tf.keras.preprocessing.image_dataset_from_directory(
|
||||
"core/resources/data/validate",
|
||||
validation_split=0.2,
|
||||
subset="validation",
|
||||
shuffle = False,
|
||||
image_size=image_size,
|
||||
batch_size=batch_size,
|
||||
)
|
||||
|
||||
self.class_names = self.train_ds.class_names
|
||||
print(self.class_names)
|
||||
|
||||
#Configure the dataset
|
||||
AUTOTUNE = tf.data.AUTOTUNE
|
||||
train_ds = self.train_ds.prefetch(buffer_size=AUTOTUNE)
|
||||
val_ds = self.val_ds.prefetch(buffer_size=AUTOTUNE)
|
||||
|
||||
input_shape=(25,25,3)
|
||||
#create model
|
||||
model = Sequential()
|
||||
model.add(Conv2D(16, kernel_size=(5), activation='relu', input_shape=input_shape))
|
||||
model.add(Conv2D(32, kernel_size=(5, 5), activation='relu'))
|
||||
model.add(Conv2D(64, kernel_size=(5, 5), activation='relu'))
|
||||
model.add(Conv2D(128, kernel_size=(5, 5), activation='relu'))
|
||||
model.add(Flatten())
|
||||
model.add(Dense(16, activation='relu'))
|
||||
model.add(Dense(9, activation='softmax'))
|
||||
|
||||
model.compile(
|
||||
optimizer=keras.optimizers.Adam(),
|
||||
loss="sparse_categorical_crossentropy",
|
||||
metrics=["accuracy"],
|
||||
)
|
||||
|
||||
#model.summary()
|
||||
#Training the model
|
||||
model.fit(
|
||||
train_ds, epochs=15, validation_data=val_ds,
|
||||
)
|
||||
|
||||
self.model = model
|
||||
self.model.save("core/saved_model.h5")
|
||||
joblib.dump(self.class_names, fr"saved_model_classes.joblib")
|
||||
|
||||
# load an image and predict the class
|
||||
def predict(self):
|
||||
folder="core/resources/data/Mushrooms/jadalne"
|
||||
|
||||
filename=random.choice(os.listdir(folder))
|
||||
print(filename)
|
||||
|
||||
# load the image
|
||||
img = keras.preprocessing.image.load_img(
|
||||
filename, grayscale=False, color_mode='rgb',
|
||||
target_size=None,interpolation='nearest'
|
||||
)
|
||||
# convert to array
|
||||
img_array = keras.preprocessing.image.img_to_array(img)
|
||||
img_array = np.array([img_array])
|
||||
# load model
|
||||
self.model = tf.keras.models.load_model("saved_model.h5")
|
||||
self.class_names = joblib.load("saved_model_classes.joblib")
|
||||
# predict the class
|
||||
predictions = self.model.predict(img_array)
|
||||
result = tf.nn.softmax(predictions[0])
|
||||
print(result)
|
BIN
core/resources/data/Mushrooms/0000_av7f-8yVUNY.jpg
Normal file
After Width: | Height: | Size: 239 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0001_eCt6Eq3hCgg.jpg
Normal file
After Width: | Height: | Size: 168 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0001_yB5GiXfgyRU.jpg
Normal file
After Width: | Height: | Size: 219 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0002_FVNX1Bdwf4s.jpg
Normal file
After Width: | Height: | Size: 212 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0002_MoLhNp2kIAI.jpg
Normal file
After Width: | Height: | Size: 239 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0003_9A6sBrZ3ZaE.jpg
Normal file
After Width: | Height: | Size: 146 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0003_f0DLNgJYt3M.jpg
Normal file
After Width: | Height: | Size: 174 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0004_35PYkvNjkns.jpg
Normal file
After Width: | Height: | Size: 91 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0004_gigZhorw0Eo.jpg
Normal file
After Width: | Height: | Size: 158 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0005_S57OF1cMyDs.jpg
Normal file
After Width: | Height: | Size: 125 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0005_ewWGUA7-KvU.jpg
Normal file
After Width: | Height: | Size: 121 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0006_Vh24Gx3FuuY.jpg
Normal file
After Width: | Height: | Size: 308 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0007_klZ5Wbedtx8.jpg
Normal file
After Width: | Height: | Size: 99 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0008_AKTJIixVK88.jpg
Normal file
After Width: | Height: | Size: 151 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0009_2hEUS66s6Gw.jpg
Normal file
After Width: | Height: | Size: 168 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/000_ePQknW8cTp8.jpg
Normal file
After Width: | Height: | Size: 72 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/000_jY-KIKiHVjQ.jpg
Normal file
After Width: | Height: | Size: 51 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/0010_7M6YHrGZ3aM.jpg
Normal file
After Width: | Height: | Size: 248 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/001_0eat5sScleY.jpg
Normal file
After Width: | Height: | Size: 148 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/001_2jP9N_ipAo8.jpg
Normal file
After Width: | Height: | Size: 162 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/001_iKuK1p9EOJ0.jpg
Normal file
After Width: | Height: | Size: 134 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/001_jNbj1WMvR-8.jpg
Normal file
After Width: | Height: | Size: 123 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/002_-WkK-Cd2TLA.jpg
Normal file
After Width: | Height: | Size: 61 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/002_Td9tM0040uI.jpg
Normal file
After Width: | Height: | Size: 197 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/002_hNh3aQSH-ZM.jpg
Normal file
After Width: | Height: | Size: 138 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/002_pJY3-9Ttfto.jpg
Normal file
After Width: | Height: | Size: 142 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/002_yBYBSt8vVoU.jpg
Normal file
After Width: | Height: | Size: 177 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/003_4AurAO4Jil8.jpg
Normal file
After Width: | Height: | Size: 180 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/003_Irz75i3SKq4.jpg
Normal file
After Width: | Height: | Size: 126 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/003_Iy7eywnsU1w.jpg
Normal file
After Width: | Height: | Size: 243 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/003_eIMrvDdKleY.jpg
Normal file
After Width: | Height: | Size: 66 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/003_ysU9uALX9uI.jpg
Normal file
After Width: | Height: | Size: 124 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/004_JB7q8HblzL8.jpg
Normal file
After Width: | Height: | Size: 70 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/004_Syi3NxxviC0.jpg
Normal file
After Width: | Height: | Size: 105 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/004_eMDf5BvLP0A.jpg
Normal file
After Width: | Height: | Size: 220 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/004_tcyA4HuN_Jw.jpg
Normal file
After Width: | Height: | Size: 141 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/004_wIVV5id0aOg.jpg
Normal file
After Width: | Height: | Size: 116 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/005_K5H990g_JZQ.jpg
Normal file
After Width: | Height: | Size: 216 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/005_PboNeVptEe0.jpg
Normal file
After Width: | Height: | Size: 172 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/005_QJIGT6Nm-dE.jpg
Normal file
After Width: | Height: | Size: 110 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/005_rf92ELmZl9U.jpg
Normal file
After Width: | Height: | Size: 132 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/005_sUqyy4Yb9VY.jpg
Normal file
After Width: | Height: | Size: 53 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/005_w9ZVoplkTZ8.jpg
Normal file
After Width: | Height: | Size: 190 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/005_wsn6CwNhHq0.jpg
Normal file
After Width: | Height: | Size: 154 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/006_1_E6AXBJqn4.jpg
Normal file
After Width: | Height: | Size: 127 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/006_862rFWvLb4I.jpg
Normal file
After Width: | Height: | Size: 72 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/006_VpJGlCCcEuA.jpg
Normal file
After Width: | Height: | Size: 177 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/006_wR0nc64hb7I.jpg
Normal file
After Width: | Height: | Size: 174 KiB |
@ -0,0 +1,3 @@
|
||||
[ZoneTransfer]
|
||||
ZoneId=3
|
||||
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip
|
BIN
core/resources/data/Mushrooms/006_yYLHXSJGfrk.jpg
Normal file
After Width: | Height: | Size: 167 KiB |