diff --git a/Network3.py b/Network3.py new file mode 100644 index 0000000..2eb4c23 --- /dev/null +++ b/Network3.py @@ -0,0 +1,63 @@ +import tensorflow as tf +from tensorflow import keras +from keras import layers + +# Load and preprocess the dataset +# Assuming you have three folders named 'class1', 'class2', and 'class3' +# each containing images of their respective classes + +data_dir = 'Training/' +image_size = (100, 100) +batch_size = 32 + +train_ds = tf.keras.preprocessing.image_dataset_from_directory( + data_dir, + validation_split=0.2, + subset="training", + seed=123, + image_size=image_size, + batch_size=batch_size, +) + +val_ds = tf.keras.preprocessing.image_dataset_from_directory( + data_dir, + validation_split=0.2, + subset="validation", + seed=123, + image_size=image_size, + batch_size=batch_size, +) + +class_names = train_ds.class_names +num_classes = len(class_names) + + +# Create the model +model = keras.Sequential([ + layers.Rescaling(1./255, input_shape=(100, 100, 3)), + layers.Conv2D(16, 3, padding='same', activation='relu'), + layers.MaxPooling2D(), + layers.Conv2D(32, 3, padding='same', activation='relu'), + layers.MaxPooling2D(), + layers.Conv2D(64, 3, padding='same', activation='relu'), + layers.MaxPooling2D(), + layers.Flatten(), + layers.Dense(128, activation='relu'), + layers.Dense(num_classes) +]) + +# Compile the model +model.compile(optimizer='adam', + loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), + metrics=['accuracy']) + +# Train the model +epochs = 10 +model.fit( + train_ds, + validation_data=val_ds, + epochs=epochs +) + +# Save the trained model +model.save('trained_model') \ No newline at end of file diff --git a/Tester2.py b/Tester2.py index f6dd52e..6f0188d 100644 --- a/Tester2.py +++ b/Tester2.py @@ -10,7 +10,7 @@ train_ds = tf.keras.utils.image_dataset_from_directory(train_data_dir, validatio val_ds = tf.keras.utils.image_dataset_from_directory(train_data_dir, validation_split=0.2, subset="validation", seed=123, batch_size=32, image_size=(100, 100)) -model = keras.models.load_model("trained_model") +model = keras.models.load_model("trained_model.h5") predictions = model.predict(val_ds.take(32)) diff --git a/mode2.h5 b/mode2.h5 new file mode 100644 index 0000000..b462b21 Binary files /dev/null and b/mode2.h5 differ diff --git a/network2.py b/network2.py new file mode 100644 index 0000000..8e35e3e --- /dev/null +++ b/network2.py @@ -0,0 +1,52 @@ +import numpy as np +import tensorflow as tf +from tensorflow import keras +from keras import layers +import matplotlib.pyplot as plt + +def normalize(image, label): + return image / 255, label + +train_data_dir = "Training/" + +train_ds = tf.keras.utils.image_dataset_from_directory(train_data_dir, + validation_split=0.2, subset="training", seed=123, batch_size=32, + image_size=(100, 100), color_mode='grayscale') + +val_ds = tf.keras.utils.image_dataset_from_directory(train_data_dir, + validation_split=0.2, subset="validation", seed=123, batch_size=32, + image_size=(100, 100), color_mode='grayscale') + +class_names = train_ds.class_names +print(class_names) + +train_ds = train_ds.map(normalize) +val_ds = val_ds.map(normalize) + + +model = keras.Sequential([ + layers.Conv2D(64, (3, 3), activation='relu', input_shape=(100, 100, 1)), + layers.MaxPool2D((2, 2)), + + layers.Conv2D(128, (3, 3), activation='relu'), + layers.MaxPool2D((2, 2)), + + layers.Conv2D(256, (3, 3), activation='relu'), + layers.MaxPool2D((2, 2)), + + layers.Flatten(), + layers.Dense(1024, activation='relu'), + layers.Dense(1, activation='sigmoid') +]) + +print(model.summary()) + +model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) + +trainHistory = model.fit(train_ds, epochs=10, validation_data=val_ds) + +(loss, accuracy) = model.evaluate(val_ds) +print(loss) +print(accuracy) + +model.save("mode2.h5") \ No newline at end of file diff --git a/testerZBS.py b/testerZBS.py new file mode 100644 index 0000000..6e40f27 --- /dev/null +++ b/testerZBS.py @@ -0,0 +1,57 @@ +import os +import numpy as np +import tensorflow as tf +from tensorflow import keras + +# Load the trained model +model = keras.models.load_model('trained_model.h5') + +# Load the class names +class_names = ['Empty', 'Food','People'] + +# Load and preprocess the validation dataset +data_dir = "Training/" +image_size = (100, 100) +batch_size = 32 + +val_ds = tf.keras.preprocessing.image_dataset_from_directory( + data_dir, + validation_split=0.2, + subset="validation", + seed=123, + image_size=image_size, + batch_size=batch_size, +) + +# Select 20 random images from the validation set +val_images = [] +val_labels = [] +for images, labels in val_ds.unbatch().shuffle(1000).take(60): + val_images.append(images) + val_labels.append(labels) + +# Make predictions on the random images +errorcount = 0 +for i in range(60): + test_image = val_images[i] + test_label = val_labels[i] + test_image = np.expand_dims(test_image, axis=0) + test_image = test_image / 255.0 # Normalize the image + + # Make predictions + predictions = model.predict(test_image) + predicted_class_index = np.argmax(predictions[0]) + predicted_class = class_names[predicted_class_index] + + true_class = class_names[test_label] + + direct = 'Results/' + filename = predicted_class + str(i) + '.jpeg' + tf.keras.preprocessing.image.save_img(direct+filename, val_images[i]) + if predicted_class != true_class: + errorcount += 1 + print('Image', i+1) + print('True class:', true_class) + print('Predicted class:', predicted_class) + print() +print('Error count: ', errorcount) \ No newline at end of file diff --git a/trained_model.h5 b/trained_model.h5 new file mode 100644 index 0000000..805575b Binary files /dev/null and b/trained_model.h5 differ