diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3034.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3034.jpg deleted file mode 100644 index 0e788ab..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3034.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3035.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3035.jpg deleted file mode 100644 index cbb72c4..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3035.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3036.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3036.jpg deleted file mode 100644 index 30690c7..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3036.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3037.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3037.jpg deleted file mode 100644 index 0fc90cc..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3037.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3038.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3038.jpg deleted file mode 100644 index 1aa00dc..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3038.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3039.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3039.jpg deleted file mode 100644 index bd9d153..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3039.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3040.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3040.jpg deleted file mode 100644 index ead5757..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3040.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3041.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3041.jpg deleted file mode 100644 index 9c8c917..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3041.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3042.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3042.jpg deleted file mode 100644 index 98c0091..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3042.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3043.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3043.jpg deleted file mode 100644 index e2c5cc6..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3043.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3044.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3044.jpg deleted file mode 100644 index 561a634..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3044.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3045.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3045.jpg deleted file mode 100644 index bda746f..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3045.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3046.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3046.jpg deleted file mode 100644 index 6a55e05..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3046.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3047.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3047.jpg deleted file mode 100644 index 41fa85b..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3047.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3048.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3048.jpg deleted file mode 100644 index f3f9b9a..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3048.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3049.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3049.jpg deleted file mode 100644 index a257146..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3049.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3050.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3050.jpg deleted file mode 100644 index f5d4d2e..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3050.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3051.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3051.jpg deleted file mode 100644 index d503eb0..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3051.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3052.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3052.jpg deleted file mode 100644 index 98b9d24..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3052.jpg and /dev/null differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3053.jpg b/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3053.jpg deleted file mode 100644 index 519c3a3..0000000 Binary files a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3053.jpg and /dev/null differ diff --git a/machine_learning/neuralModel.h5 b/machine_learning/neuralModel.h5 index d008213..aa65f50 100644 Binary files a/machine_learning/neuralModel.h5 and b/machine_learning/neuralModel.h5 differ diff --git a/machine_learning/neuralNetwork.py b/machine_learning/neuralNetwork.py index e533f30..145cafd 100644 --- a/machine_learning/neuralNetwork.py +++ b/machine_learning/neuralNetwork.py @@ -1,8 +1,11 @@ +import os from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense train_data_dir = "garbage_photos" +location = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) +train_data_dir = os.path.join(location, train_data_dir) input_shape = (150, 150, 3) num_classes = 5 diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3014.jpg b/machine_learning/photos_not_from_train_set/1.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3014.jpg rename to machine_learning/photos_not_from_train_set/1.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3022.jpg b/machine_learning/photos_not_from_train_set/10.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3022.jpg rename to machine_learning/photos_not_from_train_set/10.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3023.jpg b/machine_learning/photos_not_from_train_set/11.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3023.jpg rename to machine_learning/photos_not_from_train_set/11.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3024.jpg b/machine_learning/photos_not_from_train_set/12.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3024.jpg rename to machine_learning/photos_not_from_train_set/12.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3026.jpg b/machine_learning/photos_not_from_train_set/13.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3026.jpg rename to machine_learning/photos_not_from_train_set/13.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3027.jpg b/machine_learning/photos_not_from_train_set/14.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3027.jpg rename to machine_learning/photos_not_from_train_set/14.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3028.jpg b/machine_learning/photos_not_from_train_set/15.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3028.jpg rename to machine_learning/photos_not_from_train_set/15.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3029.jpg b/machine_learning/photos_not_from_train_set/16.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3029.jpg rename to machine_learning/photos_not_from_train_set/16.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3030.jpg b/machine_learning/photos_not_from_train_set/17.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3030.jpg rename to machine_learning/photos_not_from_train_set/17.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3031.jpg b/machine_learning/photos_not_from_train_set/18.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3031.jpg rename to machine_learning/photos_not_from_train_set/18.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3032.jpg b/machine_learning/photos_not_from_train_set/19.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3032.jpg rename to machine_learning/photos_not_from_train_set/19.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3015.jpg b/machine_learning/photos_not_from_train_set/2.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3015.jpg rename to machine_learning/photos_not_from_train_set/2.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3033.jpg b/machine_learning/photos_not_from_train_set/20.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3033.jpg rename to machine_learning/photos_not_from_train_set/20.jpg diff --git a/machine_learning/photos_not_from_train_set/21.jpg b/machine_learning/photos_not_from_train_set/21.jpg new file mode 100644 index 0000000..249a027 Binary files /dev/null and b/machine_learning/photos_not_from_train_set/21.jpg differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3016.jpg b/machine_learning/photos_not_from_train_set/3.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3016.jpg rename to machine_learning/photos_not_from_train_set/3.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3017.jpg b/machine_learning/photos_not_from_train_set/4.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3017.jpg rename to machine_learning/photos_not_from_train_set/4.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3018.jpg b/machine_learning/photos_not_from_train_set/5.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3018.jpg rename to machine_learning/photos_not_from_train_set/5.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3019.jpg b/machine_learning/photos_not_from_train_set/6.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3019.jpg rename to machine_learning/photos_not_from_train_set/6.jpg diff --git a/machine_learning/photos_not_from_train_set/7.jpg b/machine_learning/photos_not_from_train_set/7.jpg new file mode 100644 index 0000000..e09c474 Binary files /dev/null and b/machine_learning/photos_not_from_train_set/7.jpg differ diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3020.jpg b/machine_learning/photos_not_from_train_set/8.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3020.jpg rename to machine_learning/photos_not_from_train_set/8.jpg diff --git a/machine_learning/garbage_photos/photos_not_from_train_set/IMG_3021.jpg b/machine_learning/photos_not_from_train_set/9.jpg similarity index 100% rename from machine_learning/garbage_photos/photos_not_from_train_set/IMG_3021.jpg rename to machine_learning/photos_not_from_train_set/9.jpg diff --git a/movement.py b/movement.py index e726f90..27aee02 100644 --- a/movement.py +++ b/movement.py @@ -2,7 +2,7 @@ import joblib from sklearn.calibration import LabelEncoder from agentActionType import AgentActionType import time -from garbage import GarbageType, RecognizedGarbage +from garbage import Garbage, GarbageType, RecognizedGarbage from garbageCan import GarbageCan from turnCar import turn_left_orientation, turn_right_orientation from garbageTruck import GarbageTruck @@ -14,6 +14,12 @@ import pygame from bfs import find_path_to_nearest_can from agentState import AgentState +import tensorflow as tf +from keras.models import load_model +import keras.utils as image +from keras.optimizers import Adam +import numpy as np + def collect_garbage(game_context: GameContext) -> None: while True: @@ -30,11 +36,12 @@ def collect_garbage(game_context: GameContext) -> None: pass def _recognize_garbage(dust_car: GarbageTruck, can: GarbageCan) -> None: - loaded_model = joblib.load('machine_learning/model.pkl') + tree_model = joblib.load('machine_learning/model.pkl') + optimizer = Adam(learning_rate=0.001) + neural_model = load_model('machine_learning/neuralModel.h5', compile=False) + neural_model.compile(optimizer=optimizer) for garbage in can.garbage: - attributes = [garbage.shape, garbage.flexibility, garbage.does_smell, garbage.weight, garbage.size, garbage.color, garbage.softness, garbage.does_din] - encoded = attributes_to_floats(attributes) - predicted_class = loaded_model.predict([encoded])[0] + predicted_class = predict_class(garbage, tree_model, neural_model) garbage_type: GarbageType = None if predicted_class == 'PAPER': garbage_type = GarbageType.PAPER @@ -50,6 +57,35 @@ def _recognize_garbage(dust_car: GarbageTruck, can: GarbageCan) -> None: recognized_garbage = RecognizedGarbage(garbage, garbage_type) dust_car.sort_garbage(recognized_garbage) +def predict_class(garbage: Garbage, tree_model, neural_model) -> str: + if garbage.img is None: + return predict_class_from_tree(garbage, tree_model) + return predict_class_from_neural_model(garbage, neural_model) + +def predict_class_from_tree(garbage: Garbage, tree_model) -> str: + attributes = [garbage.shape, garbage.flexibility, garbage.does_smell, garbage.weight, garbage.size, garbage.color, garbage.softness, garbage.does_din] + encoded = attributes_to_floats(attributes) + return tree_model.predict([encoded])[0] + +def predict_class_from_neural_model(garbage: Garbage, neural_model) -> str: + img = image.load_img(garbage.img, target_size=(150, 150)) + img_array = image.img_to_array(img) + img_array = np.expand_dims(img_array, axis=0) + img_array /= 255. + + predictions = neural_model.predict(img_array) + prediction = np.argmax(predictions[0]) + if prediction == 0: + return "BIO" + if prediction == 1: + return "GLASS" + if prediction == 2: + return "MIXED" + if prediction == 3: + return "PAPER" + if prediction == 4: + return "PLASTIC_AND_METAL" + def attributes_to_floats(attributes: list[str]) -> list[float]: output: list[float] = [] if attributes[0] == 'Longitiudonal': diff --git a/startup.py b/startup.py index 6cc7222..25f1cee 100644 --- a/startup.py +++ b/startup.py @@ -36,12 +36,19 @@ def create_city() -> City: streets = create_streets() trashcans = create_trashcans() bumps = create_speed_bumps() - garbage_pieces = create_garbage_pieces() + garbage_pieces = create_garbage_pieces_witout_imgs() garbage_pieces_counter = 0 for s in streets: city.add_street(s) for t in trashcans: - for i in range(4): + for _ in range(4): + t.add_garbage(garbage_pieces[garbage_pieces_counter]) + garbage_pieces_counter = garbage_pieces_counter + 1 + city.add_can(t) + garbage_pieces = create_garbage_pieces_with_images() + garbage_pieces_counter = 0 + for t in trashcans: + for _ in range(3): t.add_garbage(garbage_pieces[garbage_pieces_counter]) garbage_pieces_counter = garbage_pieces_counter + 1 city.add_can(t) @@ -50,16 +57,21 @@ def create_city() -> City: return city -def create_garbage_pieces() -> List[Garbage]: +def create_garbage_pieces_witout_imgs() -> List[Garbage]: garbage_pieces = [] with open('machine_learning/garbage_infill.csv', 'r') as file: lines = file.readlines() for line in lines[1:]: param = line.strip().split(',') garbage_pieces.append( - Garbage('img', param[0], param[1], param[2], param[3], param[4], param[5], param[6], param[7].strip())) + Garbage(None, param[0], param[1], param[2], param[3], param[4], param[5], param[6], param[7].strip())) return garbage_pieces +def create_garbage_pieces_with_images() -> list[Garbage]: + garbage_pieces = [] + for i in range(1, 22): + garbage_pieces.append(Garbage('machine_learning/photos_not_from_train_set/' + str(i) + '.jpg', None, None, None, None, None, None, None, None)) + return garbage_pieces def create_streets() -> List[Street]: streets = []