-
Notifications
You must be signed in to change notification settings - Fork 2
/
NeuralNetwork.py
105 lines (83 loc) · 2.94 KB
/
NeuralNetwork.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
from tensorflow import keras;
from tensorflow.keras import layers;
from tensorflow.keras.models import Sequential;
import time
from skimage.io import imread, imsave
# IMAGE_DATASET_DIR = './ClusteredImages'
IMAGE_DATASET_DIR = './UnclusteredImages'
# OUTPUT_DATASET_DIR = './ClusteredNoFuelCapData'
if __name__ == "__main__":
#
batch_size = 32
img_height = 180
img_width = 180
trainingDataset = tf.keras.preprocessing.image_dataset_from_directory(
IMAGE_DATASET_DIR,
validation_split=0.2,
subset="training",
seed=1,
image_size=(img_height, img_width),
batch_size=batch_size
)
validationDataset = tf.keras.preprocessing.image_dataset_from_directory(
IMAGE_DATASET_DIR,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size
)
# can print the classe names with this
class_names = trainingDataset.class_names
# speeds up the i/o of reading and writing to files
AUTOTUNE = tf.data.experimental.AUTOTUNE
trainingDataset = trainingDataset.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
validationDataset = validationDataset.cache().prefetch(buffer_size=AUTOTUNE)
num_classes = len(class_names)
model = Sequential([
layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
layers.Conv2D(16, 3, padding='same', activation='sigmoid'),
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)
])
model.compile(
optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy']
)
model.summary()
# train the model
epochs = 100;
history = model.fit(
trainingDataset,
validation_data = validationDataset,
epochs = epochs
)
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()