neural network (incomplete)
This commit is contained in:
parent
1f7b9c780c
commit
3dea3909dd
@ -6,7 +6,7 @@ MAX_ROWS = 15
|
|||||||
MAX_COLS = 25
|
MAX_COLS = 25
|
||||||
|
|
||||||
mapping = {
|
mapping = {
|
||||||
'd': 10,
|
'd': 2,
|
||||||
's': 20,
|
's': 20,
|
||||||
'r': 999
|
'r': 999
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ def treelearn():
|
|||||||
# visualize and display decision tree
|
# visualize and display decision tree
|
||||||
data = tree.export_graphviz(decision_tree, out_file=None, feature_names=attributes)
|
data = tree.export_graphviz(decision_tree, out_file=None, feature_names=attributes)
|
||||||
graph = pydotplus.graph_from_dot_data(data)
|
graph = pydotplus.graph_from_dot_data(data)
|
||||||
graph.write_png(os.path.join('resources', 'decision_tree.png'))
|
# graph.write_png(os.path.join('resources', 'decision_tree.png'))
|
||||||
img = pltimg.imread(os.path.join('resources', 'decision_tree.png'))
|
img = pltimg.imread(os.path.join('resources', 'decision_tree.png'))
|
||||||
imgplot = plt.imshow(img)
|
imgplot = plt.imshow(img)
|
||||||
|
|
||||||
|
58
neural.py
Normal file
58
neural.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
import tensorflow as tf
|
||||||
|
import glob
|
||||||
|
import numpy as np
|
||||||
|
import os
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
from sklearn.model_selection import train_test_split
|
||||||
|
from tensorflow import keras
|
||||||
|
|
||||||
|
images = []
|
||||||
|
labels = []
|
||||||
|
|
||||||
|
class_names = ['fragile', 'priority', 'skull']
|
||||||
|
|
||||||
|
image_dir = 'C:\\Users\\mateu\\Desktop\\SI projekt\\resources'
|
||||||
|
image_size = (32, 32)
|
||||||
|
|
||||||
|
for class_index, class_name in enumerate(class_names):
|
||||||
|
class_path = os.path.join(image_dir, class_name)
|
||||||
|
file_names = os.listdir(class_path)
|
||||||
|
print(class_path)
|
||||||
|
for file_name in file_names:
|
||||||
|
image_path = os.path.join(class_path, file_name)
|
||||||
|
image = Image.open(image_path).convert('L')
|
||||||
|
image = image.resize(image_size)
|
||||||
|
image_array = np.array(image) / 255.0
|
||||||
|
images.append(image_array)
|
||||||
|
labels.append(class_index)
|
||||||
|
|
||||||
|
images = np.array(images)
|
||||||
|
labels = np.array(labels)
|
||||||
|
|
||||||
|
X_train, X_test, y_train, y_test = train_test_split(images, labels, test_size=0.2, random_state=42)
|
||||||
|
|
||||||
|
num_classes = 3
|
||||||
|
y_train_encoded = keras.utils.to_categorical(y_train, num_classes)
|
||||||
|
y_test_encoded = keras.utils.to_categorical(y_test, num_classes)
|
||||||
|
|
||||||
|
model = keras.Sequential([
|
||||||
|
keras.layers.Flatten(input_shape=image_size),
|
||||||
|
keras.layers.Dense(64, activation='relu'),
|
||||||
|
keras.layers.Dense(num_classes, activation='softmax')
|
||||||
|
])
|
||||||
|
|
||||||
|
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
||||||
|
|
||||||
|
batch_size = 32
|
||||||
|
epochs = 10
|
||||||
|
|
||||||
|
model.fit(X_train, y_train_encoded, batch_size=batch_size, epochs=epochs,
|
||||||
|
validation_data=(X_test, y_test_encoded))
|
||||||
|
|
||||||
|
test_loss, test_acc = model.evaluate(X_test, y_test_encoded)
|
||||||
|
print('Test accuracy:', test_acc)
|
||||||
|
|
||||||
|
predictions = model.predict(X_test)
|
||||||
|
|
||||||
|
model.save('resources/model.h5')
|
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
BIN
resources/model.h5
Normal file
BIN
resources/model.h5
Normal file
Binary file not shown.
BIN
resources/package/8.jpg
Normal file
BIN
resources/package/8.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 116 KiB |
BIN
resources/package/9.jpg
Normal file
BIN
resources/package/9.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 100 KiB |
25
test.py
Normal file
25
test.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
|
from tensorflow import keras
|
||||||
|
|
||||||
|
saved_model_path = 'resources/model.h5'
|
||||||
|
model = keras.models.load_model(saved_model_path)
|
||||||
|
|
||||||
|
|
||||||
|
image_path = 'resources/indeks-fragile.png'
|
||||||
|
image_size = (32, 32)
|
||||||
|
|
||||||
|
image = Image.open(image_path).convert('L')
|
||||||
|
image = image.resize(image_size)
|
||||||
|
image_array = np.array(image) / 255.0
|
||||||
|
input_data = np.expand_dims(image_array, axis=0)
|
||||||
|
|
||||||
|
|
||||||
|
predictions = model.predict(input_data)
|
||||||
|
predicted_class = np.argmax(predictions, axis=1)
|
||||||
|
|
||||||
|
|
||||||
|
class_names = ['fragile', 'priority', 'skull']
|
||||||
|
predicted_label = class_names[predicted_class[0]]
|
||||||
|
|
||||||
|
print('Predicted class:', predicted_label)
|
Loading…
Reference in New Issue
Block a user