add neural network

This commit is contained in:
Zuzanna Witoch 2021-06-20 17:04:20 +02:00
parent bb970960d0
commit 571a127782
17883 changed files with 26921 additions and 0 deletions

View File

@ -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

View File

@ -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
View 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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 239 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 219 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 239 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 125 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 308 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 177 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 243 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 110 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 177 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

View File

@ -0,0 +1,3 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=D:\studia\semestr4\si\laby\archive.zip

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 KiB

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