-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
executable file
·78 lines (61 loc) · 1.97 KB
/
model.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
import os
import numpy as np
import tensorflow as tf
from utils import img_to_array, unison_shuffled_copies
from config import *
def load_images(dataset_location):
samples_0 = dataset_location + label_0
samples_1 = dataset_location + label_1
data_x = []
data_y = []
# ---- Images to arrays of numbers ----
# Images containing empty parking spots
images_0 = os.listdir(samples_0)
images_1 = os.listdir(samples_1)
data_x = np.ndarray(shape=(len(images_0 + images_1), width, height, channels), dtype=np.float32)
data_y = np.ndarray(shape=(len(images_0 + images_1)), dtype=np.float32)
i = 0
errors = 0
for img in images_0:
img_path = samples_0 + img
try:
img_arr = img_to_array(img_path)
data_x[i] = img_arr
data_y[i] = 0.
i += 1
except ValueError:
print(img, '<--- Does not work')
errors += 1
# Images containing occupied parking spots
for img in images_1:
img_path = samples_1 + img
try:
img_arr = img_to_array(img_path)
data_x[i] = img_arr
data_y[i] = 1.
i += 1
except ValueError:
print(img, '<--- Does not work')
errors += 1
data_x = np.array(data_x)
data_y = np.array(data_y)
if errors != 0:
data_x = data_x[:-errors]
data_y = data_y[:-errors]
data_x, data_y = unison_shuffled_copies(data_x, data_y)
return data_x, data_y
def train():
data_x, data_y = load_images(train_dataset)
test_x, test_y = load_images(test_dataset)
model = tf.keras.models.Sequential([
tf.keras.layers.Convolution2D(32, 3, 3, input_shape=(width, height, 3), activation=tf.nn.relu),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dense(2, activation=tf.nn.softmax)
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
print('Training model...')
model.fit(data_x, data_y, epochs=5)
model.save('model.h5')
return model