-
Notifications
You must be signed in to change notification settings - Fork 1
/
tensorflow_model.py
249 lines (211 loc) · 8.83 KB
/
tensorflow_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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 1 11:43:33 2021
@author: Peter L'Oiseau
"""
#import neccesary libraries to build a classification model
import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
import random
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from keras.callbacks import EarlyStopping, ModelCheckpoint
# working directory
path = 'C:/Users/peter/Documents/baseball-databases/savant_video'
os.chdir(path)
# the dataset file or root folder path
dataset_path = 'C:/Users/peter/Documents/baseball-databases/savant_video/pics'
#standardize height and width of the pictures
IMG_HEIGHT = 64
IMG_WIDTH = 64
#set a batch size for training
batch_size = 32
#define the training and validation sets
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
dataset_path,
validation_split=0.2,
subset="training",
seed=12,
image_size=(IMG_HEIGHT, IMG_WIDTH),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
dataset_path,
validation_split=0.2,
subset="validation",
seed=12,
image_size=(IMG_HEIGHT, IMG_WIDTH),
batch_size=batch_size)
#ensure the classes have been imported
class_names = train_ds.class_names
print(class_names)
#visualize a sample of the pics to ensure they have loaded
plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
for i in range(9):
ax = plt.subplot(3, 3, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])
plt.axis("off")
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
dataset_path,
validation_split=0.2,
subset="training",
seed=123,
image_size=(IMG_HEIGHT, IMG_WIDTH),
batch_size=batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
dataset_path,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(IMG_HEIGHT, IMG_WIDTH),
batch_size=batch_size)
#keep the photos in cache to speed of modelling process
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
#normalize the photos to a specific size
normalization_layer = layers.experimental.preprocessing.Rescaling(1./255)
#change pixel values to a 0-1 scale
normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
image_batch, labels_batch = next(iter(normalized_ds))
first_image = image_batch[0]
# Notice the pixels values are now in `[0,1]`.
print(np.min(first_image), np.max(first_image))
#augment the data to create more data to build the model
data_augmentation = keras.Sequential(
[
layers.experimental.preprocessing.RandomFlip("horizontal",
input_shape=(IMG_HEIGHT,
IMG_WIDTH,
3)),
layers.experimental.preprocessing.RandomRotation(0.1),
layers.experimental.preprocessing.RandomZoom(0.1),
]
)
params = dict(epochs = [100,200], dropout=[.5,.35,.2,.1],
learning_rate=[.001,.005,.01], kernel=[2,3,4],
strides=[1,2,3], dilate=[1,2,3])
num_classes = 4
def get_new_model(lr, dropout, kernel, strides, dilate):
#initialize the model structure for a CNN
model = Sequential([
data_augmentation,
layers.experimental.preprocessing.Rescaling(1./255),
layers.Conv2D(32, (kernel,kernel), padding='same', activation='relu', strides=(strides,strides), dilation_rate=(dilate,dilate)),
#layers.BatchNormalization(),
layers.MaxPooling2D(),
layers.Dropout(dropout),
layers.Conv2D(64, (kernel,kernel), padding='same', activation='relu', strides=(strides,strides), dilation_rate=(dilate,dilate)),
#layers.BatchNormalization(),
layers.MaxPooling2D(),
layers.Dropout(dropout),
layers.Conv2D(128, (kernel,kernel), padding='same', activation='relu', strides=(strides,strides), dilation_rate=(dilate,dilate)),
#layers.BatchNormalization(),
layers.MaxPooling2D(),
layers.Dropout(dropout),
layers.Conv2D(256, (kernel,kernel), padding='same', activation='relu', strides=(strides,strides), dilation_rate=(dilate,dilate)),
#layers.BatchNormalization(),
layers.MaxPooling2D(),
layers.Dropout(dropout),
layers.Conv2D(512, (kernel,kernel), padding='same', activation='relu', strides=(strides,strides), dilation_rate=(dilate,dilate)),
#layers.BatchNormalization(),
layers.MaxPooling2D(),
layers.Dropout(dropout),
layers.Conv2D(1024, (kernel,kernel), padding='same', activation='relu', strides=(strides,strides), dilation_rate=(dilate,dilate)),
#layers.BatchNormalization(),
layers.MaxPooling2D(),
layers.Dropout(dropout),
layers.Flatten(),
layers.Dense(2048, activation='relu'),
layers.Dropout(dropout),
layers.Dense(4096, activation='relu'),
#layers.BatchNormalization(),
layers.Dropout(dropout),
layers.Dense(num_classes, activation='softmax')
])
#compile the model
model.compile(optimizer=keras.optimizers.Adam(learning_rate=lr),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
return model
#instantiate a random search where 20 of the combos are tried
combos = np.prod([len(x) for x in list(params.values())])
rand = random.sample(range(combos),100)
k=0
for kernel in list(params.values())[3]:
for strides in list(params.values())[4]:
for dilate in list(params.values())[5]:
for epochs in list(params.values())[0]:
for dropout in list(params.values())[1]:
for lr in list(params.values())[2]:
if k in rand:
#if passes random search, build and comile the model
try:
model=get_new_model(lr, dropout, kernel, strides, dilate)
#fit the model
history=model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs,
callbacks = EarlyStopping(monitor = 'val_loss', patience = 20))
val_acc = max(history.history['val_accuracy'])
par_set = ['adam' , epochs, dropout, lr, kernel, strides, dilate, val_acc]
row = pd.DataFrame([par_set])
row.to_csv('hyperpar.csv', mode='a', header=False, index=False)
except:
pass
else:
pass
k+=1
print(k)
#choose best model
hyper_par=pd.read_csv('hyperpar.csv')
optimizer, epochs, dropout, lr, kernel, strides, dilate, acc = hyper_par.loc[hyper_par['val_acc'].idxmax(), ]
#and build it
model = get_new_model(lr, dropout, kernel, strides, dilate)
wname = 'weights_0.hdf5'
#make sure to save the best weights
checkpoint = ModelCheckpoint(wname, monitor='val_accuracy',
save_best_only = True)
callbacks_list = [EarlyStopping(monitor = 'val_loss', patience = 50),
checkpoint]
#fit the model
history=model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs*5,
callbacks = callbacks_list)
model.load_weights(wname)
#visualize the training results to see where we may improve
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(len(acc))
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()
#predict test set and view confusion matrix
predictions = model.predict(val_ds)
predicted_categories = tf.argmax(predictions, axis=1)
true_categories = tf.concat([y for x, y in val_ds], axis=0)
print(tf.math.confusion_matrix(true_categories, predicted_categories))
print(class_names)
print(model.evaluate(val_ds))
#save the model
#!mkdir -p saved_model
model.save('keras_model.h5')