diff --git a/NeuralNetwork.md b/NeuralNetwork.md new file mode 100644 index 0000000..6608d9e --- /dev/null +++ b/NeuralNetwork.md @@ -0,0 +1,70 @@ +# DSZI_Survival - Sieć Neuronowa +### Autor: Jonathan Spaczyński + +## Cel zastosowania w projekcie +W projekcie DSZI_Survival sieć neuronowa użyta jest do podejmowania decyzji przez agenta. +Decyzja polega na rozpoznawaniu zdjęć owoców (jabłka i gruszki). W przypadku nie rozpoznania owocu przez +agenta, dochodzi do zatrucia i agent umiera/przegrywa. + +## Przygotowanie danych + +* **Krok 1** Przechowywane zdjęcia owoców muszą przejść przez proces zamiany zdjęcia (.jpg) na dane, które +mogą być wykorzystane przez sieć neuronową. + +```python +CATEGORIES = ["Apple", "Pear"] +IMG_SIZE = 64 + +training_data = [] + + +def create_training_data(): + for category in CATEGORIES: + path = os.path.join(DATADIR, category) + class_num = CATEGORIES.index(category) + for img in os.listdir(path): + try: + img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE) + new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) + training_data.append([new_array, class_num]) + except Exception as e: + pass +``` +zdjęcia są przechowywane w tablicy training_data wraz z klasyfikacją (class_num) odpowiadającą jakim typem owocu jest zdjęcie + +* **Krok 2** Bardzo ważnym krokiem jest pomieszanie danych. W przeciwnym wypadku nasz model po ciągłym otrzymywanie danych +reprezentujących tylko jedną kategorię owoców mógłby się wyuczyć, aby tylko zgadywać tą kategorię. +```python +random.shuffle(training_data) +``` +* **Krok 3** Ostatnim krokiem jest zaktualizowanie danych w taki sposób żeby były z przedziału +od 0 d 255 (reprezentacja koloru danego pixela) +```python +X = X / 255.0 +``` +## Kilka słów na temat danych +* **Ilość Danych** Do trenowania modelu wykorzystałem 8568 zdjęć gruszek i jabłek +z czego mniej więcej połowa danych była jednym z typów ww. owoców, a druga połowa +reprezentowała pozostałą kategorią +* **Dane wykorzystane do obliczenia skutecznośći** stanowiły małą i oddzielną część danych wykorzystanych do trenowania. +## Model +* **Dane wejściowe:** Dane o kształcie 64x64 reprezentujące pixele w zdjęciach owoców +* **Warstwa ukryta:** Składająca się z 128 "neuronów" wykorzystującą sigmoid jako funkcję aktywacyjną +* **Warstwa wyjściowa:** Składająca się z 2 "neuronów" reprezentujących gruszkę i jabłko +* **Stała ucząca:** 0.001 + +```python +model = tf.keras.Sequential([ + tf.keras.layers.Flatten(input_shape=(64, 64)), + tf.keras.layers.Dense(128, activation=tf.nn.sigmoid), + tf.keras.layers.Dense(2, activation=tf.nn.sigmoid) + ]) + +model.compile(tf.keras.optimizers.Adam(lr=0.001), + loss="sparse_categorical_crossentropy", + metrics=["accuracy"]) +``` +## Osiągniecia modelu +* **Trafność:** 86.4% +* **Strata:** 0.312 + diff --git a/data/AI_data/Nn_images/Apple/1 (115).jpg b/data/AI_data/Nn_images/Apple/1 (115).jpg new file mode 100644 index 0000000..2d0247d Binary files /dev/null and b/data/AI_data/Nn_images/Apple/1 (115).jpg differ diff --git a/data/AI_data/Nn_images/Apple/1 (151).jpg b/data/AI_data/Nn_images/Apple/1 (151).jpg new file mode 100644 index 0000000..a6714cd Binary files /dev/null and b/data/AI_data/Nn_images/Apple/1 (151).jpg differ diff --git a/data/AI_data/Nn_images/Apple/1 (27).jpg b/data/AI_data/Nn_images/Apple/1 (27).jpg new file mode 100644 index 0000000..b4beb40 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/1 (27).jpg differ diff --git a/data/AI_data/Nn_images/Apple/1 (37).jpg b/data/AI_data/Nn_images/Apple/1 (37).jpg new file mode 100644 index 0000000..e6ae8a2 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/1 (37).jpg differ diff --git a/data/AI_data/Nn_images/Apple/1 (53).jpg b/data/AI_data/Nn_images/Apple/1 (53).jpg new file mode 100644 index 0000000..ea92f8b Binary files /dev/null and b/data/AI_data/Nn_images/Apple/1 (53).jpg differ diff --git a/data/AI_data/Nn_images/Apple/1 (8).jpg b/data/AI_data/Nn_images/Apple/1 (8).jpg new file mode 100644 index 0000000..cf083eb Binary files /dev/null and b/data/AI_data/Nn_images/Apple/1 (8).jpg differ diff --git a/data/AI_data/Nn_images/Apple/1 (84).jpg b/data/AI_data/Nn_images/Apple/1 (84).jpg new file mode 100644 index 0000000..23a0e40 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/1 (84).jpg differ diff --git a/data/AI_data/Nn_images/Apple/2 (129).jpg b/data/AI_data/Nn_images/Apple/2 (129).jpg new file mode 100644 index 0000000..e261713 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/2 (129).jpg differ diff --git a/data/AI_data/Nn_images/Apple/2 (19).jpg b/data/AI_data/Nn_images/Apple/2 (19).jpg new file mode 100644 index 0000000..0d8d41c Binary files /dev/null and b/data/AI_data/Nn_images/Apple/2 (19).jpg differ diff --git a/data/AI_data/Nn_images/Apple/2 (59).jpg b/data/AI_data/Nn_images/Apple/2 (59).jpg new file mode 100644 index 0000000..ad2a21f Binary files /dev/null and b/data/AI_data/Nn_images/Apple/2 (59).jpg differ diff --git a/data/AI_data/Nn_images/Apple/2 (94).jpg b/data/AI_data/Nn_images/Apple/2 (94).jpg new file mode 100644 index 0000000..1393b29 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/2 (94).jpg differ diff --git a/data/AI_data/Nn_images/Apple/3 (10).jpg b/data/AI_data/Nn_images/Apple/3 (10).jpg new file mode 100644 index 0000000..a0ab15c Binary files /dev/null and b/data/AI_data/Nn_images/Apple/3 (10).jpg differ diff --git a/data/AI_data/Nn_images/Apple/3 (130).jpg b/data/AI_data/Nn_images/Apple/3 (130).jpg new file mode 100644 index 0000000..fdaf7c9 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/3 (130).jpg differ diff --git a/data/AI_data/Nn_images/Apple/3 (35).jpg b/data/AI_data/Nn_images/Apple/3 (35).jpg new file mode 100644 index 0000000..1c3b2b6 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/3 (35).jpg differ diff --git a/data/AI_data/Nn_images/Apple/3 (69).jpg b/data/AI_data/Nn_images/Apple/3 (69).jpg new file mode 100644 index 0000000..0d3eba0 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/3 (69).jpg differ diff --git a/data/AI_data/Nn_images/Apple/3 (98).jpg b/data/AI_data/Nn_images/Apple/3 (98).jpg new file mode 100644 index 0000000..98d6db1 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/3 (98).jpg differ diff --git a/data/AI_data/Nn_images/Apple/37_100.jpg b/data/AI_data/Nn_images/Apple/37_100.jpg new file mode 100644 index 0000000..901d28b Binary files /dev/null and b/data/AI_data/Nn_images/Apple/37_100.jpg differ diff --git a/data/AI_data/Nn_images/Apple/4 (129).jpg b/data/AI_data/Nn_images/Apple/4 (129).jpg new file mode 100644 index 0000000..f9d58eb Binary files /dev/null and b/data/AI_data/Nn_images/Apple/4 (129).jpg differ diff --git a/data/AI_data/Nn_images/Apple/4 (163).jpg b/data/AI_data/Nn_images/Apple/4 (163).jpg new file mode 100644 index 0000000..40a8bac Binary files /dev/null and b/data/AI_data/Nn_images/Apple/4 (163).jpg differ diff --git a/data/AI_data/Nn_images/Apple/4 (36).jpg b/data/AI_data/Nn_images/Apple/4 (36).jpg new file mode 100644 index 0000000..6726421 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/4 (36).jpg differ diff --git a/data/AI_data/Nn_images/Apple/4 (4).jpg b/data/AI_data/Nn_images/Apple/4 (4).jpg new file mode 100644 index 0000000..a37e380 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/4 (4).jpg differ diff --git a/data/AI_data/Nn_images/Apple/4 (64).jpg b/data/AI_data/Nn_images/Apple/4 (64).jpg new file mode 100644 index 0000000..ffb1cff Binary files /dev/null and b/data/AI_data/Nn_images/Apple/4 (64).jpg differ diff --git a/data/AI_data/Nn_images/Apple/4 (95).jpg b/data/AI_data/Nn_images/Apple/4 (95).jpg new file mode 100644 index 0000000..8af5fec Binary files /dev/null and b/data/AI_data/Nn_images/Apple/4 (95).jpg differ diff --git a/data/AI_data/Nn_images/Apple/5 (129).jpg b/data/AI_data/Nn_images/Apple/5 (129).jpg new file mode 100644 index 0000000..027d337 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/5 (129).jpg differ diff --git a/data/AI_data/Nn_images/Apple/5 (33).jpg b/data/AI_data/Nn_images/Apple/5 (33).jpg new file mode 100644 index 0000000..f142269 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/5 (33).jpg differ diff --git a/data/AI_data/Nn_images/Apple/5 (61).jpg b/data/AI_data/Nn_images/Apple/5 (61).jpg new file mode 100644 index 0000000..ad87ab4 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/5 (61).jpg differ diff --git a/data/AI_data/Nn_images/Apple/5 (92).jpg b/data/AI_data/Nn_images/Apple/5 (92).jpg new file mode 100644 index 0000000..e9cb7b4 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/5 (92).jpg differ diff --git a/data/AI_data/Nn_images/Apple/6 (104).jpg b/data/AI_data/Nn_images/Apple/6 (104).jpg new file mode 100644 index 0000000..0cccaa8 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/6 (104).jpg differ diff --git a/data/AI_data/Nn_images/Apple/6 (132).jpg b/data/AI_data/Nn_images/Apple/6 (132).jpg new file mode 100644 index 0000000..fa542c8 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/6 (132).jpg differ diff --git a/data/AI_data/Nn_images/Apple/6 (163).jpg b/data/AI_data/Nn_images/Apple/6 (163).jpg new file mode 100644 index 0000000..52bd1e3 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/6 (163).jpg differ diff --git a/data/AI_data/Nn_images/Apple/6 (42).jpg b/data/AI_data/Nn_images/Apple/6 (42).jpg new file mode 100644 index 0000000..017b0fa Binary files /dev/null and b/data/AI_data/Nn_images/Apple/6 (42).jpg differ diff --git a/data/AI_data/Nn_images/Apple/6 (5).jpg b/data/AI_data/Nn_images/Apple/6 (5).jpg new file mode 100644 index 0000000..c7604c7 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/6 (5).jpg differ diff --git a/data/AI_data/Nn_images/Apple/6 (76).jpg b/data/AI_data/Nn_images/Apple/6 (76).jpg new file mode 100644 index 0000000..89f2937 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/6 (76).jpg differ diff --git a/data/AI_data/Nn_images/Apple/7 (114).jpg b/data/AI_data/Nn_images/Apple/7 (114).jpg new file mode 100644 index 0000000..5839950 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/7 (114).jpg differ diff --git a/data/AI_data/Nn_images/Apple/7 (145).jpg b/data/AI_data/Nn_images/Apple/7 (145).jpg new file mode 100644 index 0000000..4733fa2 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/7 (145).jpg differ diff --git a/data/AI_data/Nn_images/Apple/7 (24).jpg b/data/AI_data/Nn_images/Apple/7 (24).jpg new file mode 100644 index 0000000..4966694 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/7 (24).jpg differ diff --git a/data/AI_data/Nn_images/Apple/7 (49).jpg b/data/AI_data/Nn_images/Apple/7 (49).jpg new file mode 100644 index 0000000..f788e93 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/7 (49).jpg differ diff --git a/data/AI_data/Nn_images/Apple/7 (77).jpg b/data/AI_data/Nn_images/Apple/7 (77).jpg new file mode 100644 index 0000000..8867231 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/7 (77).jpg differ diff --git a/data/AI_data/Nn_images/Apple/8 (115).jpg b/data/AI_data/Nn_images/Apple/8 (115).jpg new file mode 100644 index 0000000..9745e13 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/8 (115).jpg differ diff --git a/data/AI_data/Nn_images/Apple/8 (144).jpg b/data/AI_data/Nn_images/Apple/8 (144).jpg new file mode 100644 index 0000000..2aa0d55 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/8 (144).jpg differ diff --git a/data/AI_data/Nn_images/Apple/8 (21).jpg b/data/AI_data/Nn_images/Apple/8 (21).jpg new file mode 100644 index 0000000..2ccf93f Binary files /dev/null and b/data/AI_data/Nn_images/Apple/8 (21).jpg differ diff --git a/data/AI_data/Nn_images/Apple/8 (46).jpg b/data/AI_data/Nn_images/Apple/8 (46).jpg new file mode 100644 index 0000000..57156ea Binary files /dev/null and b/data/AI_data/Nn_images/Apple/8 (46).jpg differ diff --git a/data/AI_data/Nn_images/Apple/8 (65).jpg b/data/AI_data/Nn_images/Apple/8 (65).jpg new file mode 100644 index 0000000..fd1edb4 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/8 (65).jpg differ diff --git a/data/AI_data/Nn_images/Apple/8 (84).jpg b/data/AI_data/Nn_images/Apple/8 (84).jpg new file mode 100644 index 0000000..2ac7dd9 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/8 (84).jpg differ diff --git a/data/AI_data/Nn_images/Apple/8 (99).jpg b/data/AI_data/Nn_images/Apple/8 (99).jpg new file mode 100644 index 0000000..0e6e9f9 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/8 (99).jpg differ diff --git a/data/AI_data/Nn_images/Apple/84_100.jpg b/data/AI_data/Nn_images/Apple/84_100.jpg new file mode 100644 index 0000000..3b467d7 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/84_100.jpg differ diff --git a/data/AI_data/Nn_images/Apple/9 (123).jpg b/data/AI_data/Nn_images/Apple/9 (123).jpg new file mode 100644 index 0000000..a3694d2 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/9 (123).jpg differ diff --git a/data/AI_data/Nn_images/Apple/9 (151).jpg b/data/AI_data/Nn_images/Apple/9 (151).jpg new file mode 100644 index 0000000..806770a Binary files /dev/null and b/data/AI_data/Nn_images/Apple/9 (151).jpg differ diff --git a/data/AI_data/Nn_images/Apple/9 (161).jpg b/data/AI_data/Nn_images/Apple/9 (161).jpg new file mode 100644 index 0000000..6188b05 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/9 (161).jpg differ diff --git a/data/AI_data/Nn_images/Apple/9 (36).jpg b/data/AI_data/Nn_images/Apple/9 (36).jpg new file mode 100644 index 0000000..3cd656c Binary files /dev/null and b/data/AI_data/Nn_images/Apple/9 (36).jpg differ diff --git a/data/AI_data/Nn_images/Apple/9 (64).jpg b/data/AI_data/Nn_images/Apple/9 (64).jpg new file mode 100644 index 0000000..23f6aec Binary files /dev/null and b/data/AI_data/Nn_images/Apple/9 (64).jpg differ diff --git a/data/AI_data/Nn_images/Apple/9 (8).jpg b/data/AI_data/Nn_images/Apple/9 (8).jpg new file mode 100644 index 0000000..bf0a6e7 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/9 (8).jpg differ diff --git a/data/AI_data/Nn_images/Apple/9 (92).jpg b/data/AI_data/Nn_images/Apple/9 (92).jpg new file mode 100644 index 0000000..0470449 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/9 (92).jpg differ diff --git a/data/AI_data/Nn_images/Apple/r_327_100.jpg b/data/AI_data/Nn_images/Apple/r_327_100.jpg new file mode 100644 index 0000000..110dc75 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/r_327_100.jpg differ diff --git a/data/AI_data/Nn_images/Apple/r_5_100.jpg b/data/AI_data/Nn_images/Apple/r_5_100.jpg new file mode 100644 index 0000000..4838cb4 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/r_5_100.jpg differ diff --git a/data/AI_data/Nn_images/Apple/r_97_100.jpg b/data/AI_data/Nn_images/Apple/r_97_100.jpg new file mode 100644 index 0000000..c65ff96 Binary files /dev/null and b/data/AI_data/Nn_images/Apple/r_97_100.jpg differ diff --git a/data/AI_data/Nn_images/Pear/0_100.jpg b/data/AI_data/Nn_images/Pear/0_100.jpg new file mode 100644 index 0000000..d9c43d1 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/0_100.jpg differ diff --git a/data/AI_data/Nn_images/Pear/1 (12).jpg b/data/AI_data/Nn_images/Pear/1 (12).jpg new file mode 100644 index 0000000..9a1e605 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/1 (12).jpg differ diff --git a/data/AI_data/Nn_images/Pear/1 (130).jpg b/data/AI_data/Nn_images/Pear/1 (130).jpg new file mode 100644 index 0000000..b99df73 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/1 (130).jpg differ diff --git a/data/AI_data/Nn_images/Pear/1 (164).jpg b/data/AI_data/Nn_images/Pear/1 (164).jpg new file mode 100644 index 0000000..b88c2b6 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/1 (164).jpg differ diff --git a/data/AI_data/Nn_images/Pear/1 (2).jpg b/data/AI_data/Nn_images/Pear/1 (2).jpg new file mode 100644 index 0000000..412fc31 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/1 (2).jpg differ diff --git a/data/AI_data/Nn_images/Pear/1 (43).jpg b/data/AI_data/Nn_images/Pear/1 (43).jpg new file mode 100644 index 0000000..9640765 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/1 (43).jpg differ diff --git a/data/AI_data/Nn_images/Pear/1 (71).jpg b/data/AI_data/Nn_images/Pear/1 (71).jpg new file mode 100644 index 0000000..d3b2a26 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/1 (71).jpg differ diff --git a/data/AI_data/Nn_images/Pear/1 (99).jpg b/data/AI_data/Nn_images/Pear/1 (99).jpg new file mode 100644 index 0000000..a031155 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/1 (99).jpg differ diff --git a/data/AI_data/Nn_images/Pear/2 (116).jpg b/data/AI_data/Nn_images/Pear/2 (116).jpg new file mode 100644 index 0000000..5ce8bae Binary files /dev/null and b/data/AI_data/Nn_images/Pear/2 (116).jpg differ diff --git a/data/AI_data/Nn_images/Pear/2 (13).jpg b/data/AI_data/Nn_images/Pear/2 (13).jpg new file mode 100644 index 0000000..d820cf3 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/2 (13).jpg differ diff --git a/data/AI_data/Nn_images/Pear/2 (144).jpg b/data/AI_data/Nn_images/Pear/2 (144).jpg new file mode 100644 index 0000000..92c3c26 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/2 (144).jpg differ diff --git a/data/AI_data/Nn_images/Pear/2 (172).jpg b/data/AI_data/Nn_images/Pear/2 (172).jpg new file mode 100644 index 0000000..6a42930 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/2 (172).jpg differ diff --git a/data/AI_data/Nn_images/Pear/2 (197).jpg b/data/AI_data/Nn_images/Pear/2 (197).jpg new file mode 100644 index 0000000..5c35685 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/2 (197).jpg differ diff --git a/data/AI_data/Nn_images/Pear/2 (225).jpg b/data/AI_data/Nn_images/Pear/2 (225).jpg new file mode 100644 index 0000000..0a5d495 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/2 (225).jpg differ diff --git a/data/AI_data/Nn_images/Pear/2 (41).jpg b/data/AI_data/Nn_images/Pear/2 (41).jpg new file mode 100644 index 0000000..b2686ae Binary files /dev/null and b/data/AI_data/Nn_images/Pear/2 (41).jpg differ diff --git a/data/AI_data/Nn_images/Pear/2 (54).jpg b/data/AI_data/Nn_images/Pear/2 (54).jpg new file mode 100644 index 0000000..6ef0d1b Binary files /dev/null and b/data/AI_data/Nn_images/Pear/2 (54).jpg differ diff --git a/data/AI_data/Nn_images/Pear/2 (91).jpg b/data/AI_data/Nn_images/Pear/2 (91).jpg new file mode 100644 index 0000000..0bd9c3b Binary files /dev/null and b/data/AI_data/Nn_images/Pear/2 (91).jpg differ diff --git a/data/AI_data/Nn_images/Pear/250_100.jpg b/data/AI_data/Nn_images/Pear/250_100.jpg new file mode 100644 index 0000000..0ea6638 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/250_100.jpg differ diff --git a/data/AI_data/Nn_images/Pear/3 (114).jpg b/data/AI_data/Nn_images/Pear/3 (114).jpg new file mode 100644 index 0000000..dc8a177 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/3 (114).jpg differ diff --git a/data/AI_data/Nn_images/Pear/3 (142).jpg b/data/AI_data/Nn_images/Pear/3 (142).jpg new file mode 100644 index 0000000..498e41c Binary files /dev/null and b/data/AI_data/Nn_images/Pear/3 (142).jpg differ diff --git a/data/AI_data/Nn_images/Pear/3 (170).jpg b/data/AI_data/Nn_images/Pear/3 (170).jpg new file mode 100644 index 0000000..cbe176e Binary files /dev/null and b/data/AI_data/Nn_images/Pear/3 (170).jpg differ diff --git a/data/AI_data/Nn_images/Pear/3 (18).jpg b/data/AI_data/Nn_images/Pear/3 (18).jpg new file mode 100644 index 0000000..c5324fb Binary files /dev/null and b/data/AI_data/Nn_images/Pear/3 (18).jpg differ diff --git a/data/AI_data/Nn_images/Pear/3 (204).jpg b/data/AI_data/Nn_images/Pear/3 (204).jpg new file mode 100644 index 0000000..531bbef Binary files /dev/null and b/data/AI_data/Nn_images/Pear/3 (204).jpg differ diff --git a/data/AI_data/Nn_images/Pear/3 (219).jpg b/data/AI_data/Nn_images/Pear/3 (219).jpg new file mode 100644 index 0000000..d8406fa Binary files /dev/null and b/data/AI_data/Nn_images/Pear/3 (219).jpg differ diff --git a/data/AI_data/Nn_images/Pear/3 (220).jpg b/data/AI_data/Nn_images/Pear/3 (220).jpg new file mode 100644 index 0000000..491c17e Binary files /dev/null and b/data/AI_data/Nn_images/Pear/3 (220).jpg differ diff --git a/data/AI_data/Nn_images/Pear/3 (52).jpg b/data/AI_data/Nn_images/Pear/3 (52).jpg new file mode 100644 index 0000000..5618da5 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/3 (52).jpg differ diff --git a/data/AI_data/Nn_images/Pear/3 (86).jpg b/data/AI_data/Nn_images/Pear/3 (86).jpg new file mode 100644 index 0000000..5760581 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/3 (86).jpg differ diff --git a/data/AI_data/Nn_images/Pear/4 (1).jpg b/data/AI_data/Nn_images/Pear/4 (1).jpg new file mode 100644 index 0000000..ac5bf8f Binary files /dev/null and b/data/AI_data/Nn_images/Pear/4 (1).jpg differ diff --git a/data/AI_data/Nn_images/Pear/4 (133).jpg b/data/AI_data/Nn_images/Pear/4 (133).jpg new file mode 100644 index 0000000..8af62d6 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/4 (133).jpg differ diff --git a/data/AI_data/Nn_images/Pear/4 (28).jpg b/data/AI_data/Nn_images/Pear/4 (28).jpg new file mode 100644 index 0000000..0a283ac Binary files /dev/null and b/data/AI_data/Nn_images/Pear/4 (28).jpg differ diff --git a/data/AI_data/Nn_images/Pear/4 (62).jpg b/data/AI_data/Nn_images/Pear/4 (62).jpg new file mode 100644 index 0000000..c5cf620 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/4 (62).jpg differ diff --git a/data/AI_data/Nn_images/Pear/4 (99).jpg b/data/AI_data/Nn_images/Pear/4 (99).jpg new file mode 100644 index 0000000..d8823b5 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/4 (99).jpg differ diff --git a/data/AI_data/Nn_images/Pear/5 (125).jpg b/data/AI_data/Nn_images/Pear/5 (125).jpg new file mode 100644 index 0000000..ac8ca16 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/5 (125).jpg differ diff --git a/data/AI_data/Nn_images/Pear/5 (153).jpg b/data/AI_data/Nn_images/Pear/5 (153).jpg new file mode 100644 index 0000000..b6b3e00 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/5 (153).jpg differ diff --git a/data/AI_data/Nn_images/Pear/5 (190).jpg b/data/AI_data/Nn_images/Pear/5 (190).jpg new file mode 100644 index 0000000..0a45446 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/5 (190).jpg differ diff --git a/data/AI_data/Nn_images/Pear/5 (218).jpg b/data/AI_data/Nn_images/Pear/5 (218).jpg new file mode 100644 index 0000000..c5340a9 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/5 (218).jpg differ diff --git a/data/AI_data/Nn_images/Pear/5 (32).jpg b/data/AI_data/Nn_images/Pear/5 (32).jpg new file mode 100644 index 0000000..c94b245 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/5 (32).jpg differ diff --git a/data/AI_data/Nn_images/Pear/5 (4).jpg b/data/AI_data/Nn_images/Pear/5 (4).jpg new file mode 100644 index 0000000..e747810 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/5 (4).jpg differ diff --git a/data/AI_data/Nn_images/Pear/5 (63).jpg b/data/AI_data/Nn_images/Pear/5 (63).jpg new file mode 100644 index 0000000..e3908d7 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/5 (63).jpg differ diff --git a/data/AI_data/Nn_images/Pear/5 (94).jpg b/data/AI_data/Nn_images/Pear/5 (94).jpg new file mode 100644 index 0000000..3bdc444 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/5 (94).jpg differ diff --git a/data/AI_data/Nn_images/Pear/6 (117).jpg b/data/AI_data/Nn_images/Pear/6 (117).jpg new file mode 100644 index 0000000..c2caaf2 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/6 (117).jpg differ diff --git a/data/AI_data/Nn_images/Pear/6 (145).jpg b/data/AI_data/Nn_images/Pear/6 (145).jpg new file mode 100644 index 0000000..fb3064f Binary files /dev/null and b/data/AI_data/Nn_images/Pear/6 (145).jpg differ diff --git a/data/AI_data/Nn_images/Pear/6 (170).jpg b/data/AI_data/Nn_images/Pear/6 (170).jpg new file mode 100644 index 0000000..0b633d4 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/6 (170).jpg differ diff --git a/data/AI_data/Nn_images/Pear/6 (186).jpg b/data/AI_data/Nn_images/Pear/6 (186).jpg new file mode 100644 index 0000000..5fd5dff Binary files /dev/null and b/data/AI_data/Nn_images/Pear/6 (186).jpg differ diff --git a/data/AI_data/Nn_images/Pear/6 (214).jpg b/data/AI_data/Nn_images/Pear/6 (214).jpg new file mode 100644 index 0000000..120fd78 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/6 (214).jpg differ diff --git a/data/AI_data/Nn_images/Pear/6 (30).jpg b/data/AI_data/Nn_images/Pear/6 (30).jpg new file mode 100644 index 0000000..c4f52dc Binary files /dev/null and b/data/AI_data/Nn_images/Pear/6 (30).jpg differ diff --git a/data/AI_data/Nn_images/Pear/6 (58).jpg b/data/AI_data/Nn_images/Pear/6 (58).jpg new file mode 100644 index 0000000..40412f3 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/6 (58).jpg differ diff --git a/data/AI_data/Nn_images/Pear/6 (86).jpg b/data/AI_data/Nn_images/Pear/6 (86).jpg new file mode 100644 index 0000000..b852663 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/6 (86).jpg differ diff --git a/data/AI_data/Nn_images/Pear/7 (117).jpg b/data/AI_data/Nn_images/Pear/7 (117).jpg new file mode 100644 index 0000000..74da0b8 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/7 (117).jpg differ diff --git a/data/AI_data/Nn_images/Pear/7 (148).jpg b/data/AI_data/Nn_images/Pear/7 (148).jpg new file mode 100644 index 0000000..2449557 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/7 (148).jpg differ diff --git a/data/AI_data/Nn_images/Pear/7 (164).jpg b/data/AI_data/Nn_images/Pear/7 (164).jpg new file mode 100644 index 0000000..760f7e6 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/7 (164).jpg differ diff --git a/data/AI_data/Nn_images/Pear/7 (33).jpg b/data/AI_data/Nn_images/Pear/7 (33).jpg new file mode 100644 index 0000000..d3d1152 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/7 (33).jpg differ diff --git a/data/AI_data/Nn_images/Pear/7 (61).jpg b/data/AI_data/Nn_images/Pear/7 (61).jpg new file mode 100644 index 0000000..de6d9e3 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/7 (61).jpg differ diff --git a/data/AI_data/Nn_images/Pear/7 (8).jpg b/data/AI_data/Nn_images/Pear/7 (8).jpg new file mode 100644 index 0000000..c87f925 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/7 (8).jpg differ diff --git a/data/AI_data/Nn_images/Pear/7 (92).jpg b/data/AI_data/Nn_images/Pear/7 (92).jpg new file mode 100644 index 0000000..6bb99bb Binary files /dev/null and b/data/AI_data/Nn_images/Pear/7 (92).jpg differ diff --git a/data/AI_data/Nn_images/Pear/r_148_100.jpg b/data/AI_data/Nn_images/Pear/r_148_100.jpg new file mode 100644 index 0000000..80633e9 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/r_148_100.jpg differ diff --git a/data/AI_data/Nn_images/Pear/r_257_100.jpg b/data/AI_data/Nn_images/Pear/r_257_100.jpg new file mode 100644 index 0000000..cab9e57 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/r_257_100.jpg differ diff --git a/data/AI_data/Nn_images/Pear/r_74_100.jpg b/data/AI_data/Nn_images/Pear/r_74_100.jpg new file mode 100644 index 0000000..b73f635 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/r_74_100.jpg differ diff --git a/data/AI_data/Nn_images/Pear/r_8_100.jpg b/data/AI_data/Nn_images/Pear/r_8_100.jpg new file mode 100644 index 0000000..eadbe60 Binary files /dev/null and b/data/AI_data/Nn_images/Pear/r_8_100.jpg differ diff --git a/mygraph.png b/mygraph.png new file mode 100644 index 0000000..816966b Binary files /dev/null and b/mygraph.png differ diff --git a/src/AI/NeuralNetwork/load_images.py b/src/AI/NeuralNetwork/load_images.py new file mode 100644 index 0000000..3e11ac9 --- /dev/null +++ b/src/AI/NeuralNetwork/load_images.py @@ -0,0 +1,54 @@ +import numpy as np +import matplotlib.pyplot as plt +import os +import cv2 +import random +import pickle + +DATADIR = "/home/paul/Desktop/TEST" +CATEGORIES = ["Apple", "Pear"] +IMG_SIZE = 64 + +training_data = [] + + +def create_training_data(): + for category in CATEGORIES: + path = os.path.join(DATADIR, category) + class_num = CATEGORIES.index(category) + for img in os.listdir(path): + try: + img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE) + new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) + training_data.append([new_array, class_num]) + except Exception as e: + pass + + +create_training_data() +print("amount of training data: " + str(len(training_data))) +random.shuffle(training_data) + +X = [] +y = [] + +for features, label in training_data: + X.append(features) + y.append(label) +X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1) +y = np.array(y) + + +pickle_out = open("X_test.pickle.pickle", "wb") +pickle.dump(X, pickle_out) +pickle_out.close() + +pickle_out = open("y_test.pickle.pickle", "wb") +pickle.dump(y, pickle_out) +pickle_out.close() + + +""" +pickle_in = open("X.pickle", "rb") +X = pickle.load(pickle_in) +""" \ No newline at end of file diff --git a/src/AI/NeuralNetwork/model.h5 b/src/AI/NeuralNetwork/model.h5 new file mode 100644 index 0000000..f1c4c2c Binary files /dev/null and b/src/AI/NeuralNetwork/model.h5 differ diff --git a/src/AI/NeuralNetwork/predict_image.py b/src/AI/NeuralNetwork/predict_image.py new file mode 100644 index 0000000..67e3eb9 --- /dev/null +++ b/src/AI/NeuralNetwork/predict_image.py @@ -0,0 +1,79 @@ +import os +from random import randint +import cv2 +from tensorflow.keras.models import load_model +import matplotlib.pyplot as plt +import numpy as np + + +class NN: + def __init__(self): + self.model = load_model('src/AI/NeuralNetwork/model.h5') + self.IMG_SIZE = 64 + self.game_images = [] + self.CATEGORIES = ["Apple", "Pear"] + self.X = [] + self.y = [] + self.load_images() + self.images_used = [] + + def show_picture(self, picture_id): + plt.imshow(self.game_images[picture_id][0]) + plt.savefig("mygraph.png") + #print("this is: " + self.CATEGORIES[self.game_images[picture_id][1]]) + + show_img = cv2.imread("mygraph.png") + cv2.imshow('image', show_img) + cv2.waitKey(0) + cv2.destroyAllWindows() + + def load_images(self): + DATADIR = "data/AI_data/Nn_images" + for category in self.CATEGORIES: + path = os.path.join(DATADIR, category) + class_num = self.CATEGORIES.index(category) + for img in os.listdir(path): + img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE) + new_array = cv2.resize(img_array, (self.IMG_SIZE, self.IMG_SIZE)) + self.game_images.append([new_array, class_num]) + + for features, label in self.game_images: + self.X.append(features) + self.y.append(label) + self.X = np.array(self.X).reshape(-1, self.IMG_SIZE, self.IMG_SIZE, 1) + self.X = self.X / 255.0 + + def predict_image(self): + while True: + random_num = randint(0, len(self.game_images) - 1) + if random_num not in self.images_used: + self.images_used.append(random_num) + break + self.show_picture(random_num) + predictions = self.model.predict(self.X) + print("this is my prediction: " + self.CATEGORIES[np.argmax(predictions[random_num])]) + if np.argmax(predictions[random_num]) == self.y[random_num]: + return True, self.CATEGORIES[np.argmax(predictions[random_num])] + else: + return False, self.CATEGORIES[np.argmax(predictions[random_num])] + +""" +pm = CNN() +print(pm.predict_image()) +print() +print(pm.predict_image()) +print() +print(pm.predict_image()) +print() +print(pm.predict_image()) +print() +print(pm.predict_image()) +print() +print(pm.predict_image()) +print() +print(pm.predict_image()) +print() + + + +""" \ No newline at end of file diff --git a/src/AI/NeuralNetwork/train_model.py b/src/AI/NeuralNetwork/train_model.py new file mode 100644 index 0000000..e2ac45a --- /dev/null +++ b/src/AI/NeuralNetwork/train_model.py @@ -0,0 +1,33 @@ +import tensorflow as tf +from tensorflow.keras.models import Sequential +from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D +import pickle +from tensorflow.keras.models import load_model +import numpy as np + +X = pickle.load(open("X.pickle", "rb")) +y = pickle.load(open("y.pickle", "rb")) + +X_test = pickle.load(open("X_test.pickle", "rb")) +y_test = pickle.load(open("y_test.pickle", "rb")) + +X = X / 255.0 + +model = tf.keras.Sequential([ + tf.keras.layers.Flatten(input_shape=(64, 64)), + tf.keras.layers.Dense(128, activation=tf.nn.relu), + tf.keras.layers.Dense(2, activation=tf.nn.softmax) + ]) + +model.compile(optimizer="adam", + loss="sparse_categorical_crossentropy", + metrics=["accuracy"]) + +model.fit(X, y, epochs=5) +test_loss, test_acc = model.evaluate(X_test, y_test) + +print("Accuracy: " + str(test_acc)) +print("Loss: " + str(test_loss)) + +model.save('model.h5') + diff --git a/src/game/Game.py b/src/game/Game.py index f3e84ec..8e6f46e 100644 --- a/src/game/Game.py +++ b/src/game/Game.py @@ -5,7 +5,8 @@ from pathlib import Path import os from random import sample from random import shuffle - +import pickle +import cv2 import pygame from src.AI.Affinities import Affinities @@ -16,6 +17,8 @@ from src.AI.DecisionTrees.projectSpecificClasses.SurvivalClassification import S from src.AI.GA import geneticAlgorithm from src.AI.GaTravelingForHerbs.GeneticAlgorithm import GeneticAlgorithm from src.AI.GaTravelingForHerbs.Traveling import Traveling, START_COORD, END_COORD +from src.AI.NeuralNetwork.predict_image import NN +from src.entities.Enums import Classifiers from src.entities.Player import Player from src.game.EventManager import EventManager from src.game.Map import Map @@ -101,6 +104,11 @@ class Game: filesPath) + os.sep + "data" + os.sep + "AI_data" + os.sep + "dt_exmpls" + os.sep + "dt_examples" dtExampleManager = ExamplesManager(examplesFilePath) dtExampleManager.generateExamples() + + elif argv[1] == "NN": + print("Running Neural Network for image classification") + self.run_neural_network(filesPath) + # Traveling ga algorithm elif argv[1] == "ga_travel": self.travelRun(filesPath) @@ -109,6 +117,112 @@ class Game: print("Invalid game mode. \n Possible options: test, ga") exit(1) + def run_neural_network(self, filesPath, pauseAfterDecision=False): + + image_predictor = NN() + + self.running = True + print("Initializing screen, params: " + str(self.config["window"]) + "...", end=" ") + + # Vertical rotation is unsupported due to UI layout + if self.config["window"]["height"] > self.config["window"]["width"]: + print("The screen cannot be in a vertical orientation. Exiting...") + exit(1) + + # Read examples to decision tree learning + examplesFilePath = str( + filesPath) + os.sep + "data" + os.sep + "AI_data" + os.sep + "dt_exmpls" + os.sep + "dt_examples" + examplesManager = ExamplesManager(examplesFilePath) + examples = examplesManager.readExamples() + + # Create decision tree + survivalDecisionTree = SurvivalDT(DT.inductiveDecisionTreeLearning(examples, + AttrDefs.allAttributesDefinitions, + SurvivalClassification.FOOD, + SurvivalClassification)) + + print("\nDecision tree: \n") + DecisionTree.printTree(survivalDecisionTree.entityPickingDecisionTree, 0) + print() + + # Initialize timers + # Virtual timer to track in-game time + self.ingameTimer = Timer() + self.ingameTimer.startClock() + + # Initialize screen + self.screen = Screen(self, self.config["window"]) + print("OK") + + self.initializeMap(filesPath) + + # Initialize the player + self.player = Player((15, 15), self.map.tileSize, Affinities(0.3, 0.6, 0.1, 0.5)) + self.map.addEntity(self.player, DONTADD=True) + + pause = False + decisionsMade = 0 + + # main loop without user input + while True: + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + exit(0) + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_SPACE: + pause = not pause + + if pause or not self.running: + pass + + else: + # Tick the timers + self.ingameTimer.updateTime(self.pgTimer.tick()) + self.screen.ui.updateTime() + + # If player is dead write information to console and break main loop + if not self.player.alive: + self.screen.ui.updateOnDeath(self.player) + self.screen.ui.console.printToConsole( + "Score: {}".format(str(decisionsMade + self.player.movePoints))) + self.screen.ui.console.printToConsole("Decisions made {}. Movements made {}.". + format(decisionsMade, self.player.movePoints)) + self.spritesList.update() + self.spritesList.draw(self.screen.pygameScreen) + pygame.display.flip() + self.running = False + + # Choose target for player using decision tree + if self.player.movementTarget is None: + pickedEntity = survivalDecisionTree.pickEntity(self.player, self.map) + if pickedEntity.classifier == Classifiers.FOOD: + result = image_predictor.predict_image() + self.screen.ui.console.printToConsole("I think it is a: " + result[1]) + if result[0]: + self.screen.ui.console.printToConsole("I was right") + pass + else: + self.screen.ui.console.printToConsole("I was wrong") + self.player.alive = False + continue + + self.player.gotoToTarget(survivalDecisionTree.pickEntity(self.player, self.map), self.map) + decisionsMade += 1 + if pauseAfterDecision: + pause = True + + self.screen.ui.updateBarsBasedOnPlayerStats(self.player.statistics) + + # Call update() method for each entity + self.spritesList.update() + + # Draw all sprites + self.spritesList.draw(self.screen.pygameScreen) + + # Flip the display + pygame.display.flip() + def initializePygame(self): """ Initializes all pygame members.