/
train_n_test_n_save.py
166 lines (137 loc) · 6.13 KB
/
train_n_test_n_save.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import tensorflow as tf # deep learning library. Tensors are just multi-dimensional arrays
import numpy as np
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm
import re
#---------------------------------------------------------------------------------
#----------------------------- READ AND PREPATE DATA -----------------------------
#---------------------------------------------------------------------------------
DATASET_PATH = "dataset"
DATADIR = DATASET_PATH+"/"+"samples"
LABELFILE = DATASET_PATH+"/"+"labels"+"/"+"labels.txt"
TRAINING_AMOUNT = 40000
TESTING_AMOUNT = 2000
training_data_samples = []
def create_training_samples():
#for imgName in range(1, 194776):
path = os.path.join(DATADIR)
IMG_SIZE = 100
for img in tqdm(range(0, TRAINING_AMOUNT)):
try:
img_array = cv2.imread(os.path.join(path,str(img)+".jpeg") ,cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
training_data_samples.append(new_array)
except Exception as e:
print(e)
pass
test_data_samples = []
def create_test_samples():
#for imgName in range(1, 194776):
path = os.path.join(DATADIR)
IMG_SIZE = 100
for img in tqdm(range(TRAINING_AMOUNT, TRAINING_AMOUNT+TESTING_AMOUNT)):
try:
img_array = cv2.imread(os.path.join(path,str(img)+".jpeg") ,cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
test_data_samples.append(new_array)
except Exception as e:
print(e)
pass
training_data_labels = []
def create_training_labels():
count = 0
img_name = ""
with open(LABELFILE) as f:
for line in f:
if count >= TRAINING_AMOUNT:
break
array_of_words = line.split(',')
training_data_labels.append(int(array_of_words[1]))
img_name = array_of_words[0]
count += 1
test_data_labels = []
def create_test_labels():
count = 0
img_name = ""
with open(LABELFILE) as f:
for line in f:
if count >= TRAINING_AMOUNT and count < TRAINING_AMOUNT+TESTING_AMOUNT:
array_of_words = line.split(',')
test_data_labels.append(int(array_of_words[1]))
img_name = array_of_words[0]
count += 1
create_training_samples()
create_training_labels()
create_test_samples()
create_test_labels()
x_train = training_data_samples
y_train = np.zeros((TRAINING_AMOUNT,), dtype=int)
for index, val in enumerate(training_data_labels):
y_train[index] = val
x_test = test_data_samples
y_test = np.zeros((TESTING_AMOUNT,), dtype=int)
for index, val in enumerate(test_data_labels):
y_test[index] = val
#---------------------------------------------------------------------------------
#----------------------------- READ AND PREPATE DATA -----------------------------
#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
#------------------------------ CREATE MODEL AND TRAIN ---------------------------
#---------------------------------------------------------------------------------
x_train = tf.keras.utils.normalize(x_train, axis=1) # scales data between 0 and 1
x_test = tf.keras.utils.normalize(x_test, axis=1) # scales data between 0 and 1
model = tf.keras.models.Sequential() # a basic feed-forward model
model.add(tf.keras.layers.Flatten()) # takes our 28x28 and makes it 1x784
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu)) # a simple fully-connected layer, 128 units, relu activation
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu)) # a simple fully-connected layer, 128 units, relu activation
model.add(tf.keras.layers.Dense(2, activation=tf.nn.softmax)) # our output layer. 10 units for 10 classes. Softmax for probability distribution
model.compile(optimizer='adam', # Good default optimizer to start with
loss='sparse_categorical_crossentropy', # how will we calculate our "error." Neural network aims to minimize loss.
metrics=['accuracy']) # what to track
model.fit(x_train, y_train, epochs=5) # train the model
val_loss, val_acc = model.evaluate(x_test, y_test) # evaluate the out of sample data with model
print(val_loss) # model's loss (error)
print(val_acc) # model's accuracy
#---------------------------------------------------------------------------------
#------------------------------ CREATE MODEL AND TRAIN ---------------------------
#---------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------
# ----------------------------- save the model ---------------------------------------
# ------------------------------------------------------------------------------------
'''
# save the model to desk
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize the weights
model.save_weights("model.h5")
print(" ----- saved model to disk")
'''
# serialize model to YAML
model_yaml = model.to_yaml()
with open("model.yaml", "w") as yaml_file:
yaml_file.write(model_yaml)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
# ------------------------------------------------------------------------------------
# ----------------------------- save the model ---------------------------------------
# ------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
#------------------------------ TEST ---------------------------------------------
#---------------------------------------------------------------------------------
predictions = model.predict(x_test)
print("---------------------------------")
print("----------- TEST ----------------")
print("---------------------------------")
count = 0
for prediction in predictions:
if np.argmax(prediction) == 1:
print(TRAINING_AMOUNT+count, '--', np.argmax(prediction), '--', y_test[count])
count += 1
#---------------------------------------------------------------------------------
#------------------------------ TEST ---------------------------------------------
#---------------------------------------------------------------------------------