-
Notifications
You must be signed in to change notification settings - Fork 0
/
monkeyModel.py
111 lines (87 loc) · 4.2 KB
/
monkeyModel.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
import numpy as np
import time
import matplotlib.pyplot as plt
import tensorflow as tf
from keras.models import load_model
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
# parameters
IMG = 200
IMG_SIZE = [IMG, IMG]
numOfClasses = 10
batchSize = 32
EPOCHS = 30
# building the model
model = tf.keras.models.Sequential ([
#convolution layers
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(IMG,IMG,3)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(IMG,IMG,3)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(IMG,IMG,3)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(IMG,IMG,3)),
tf.keras.layers.MaxPooling2D(2,2),
# flattening
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.5), #prevent overfitting, randomly 50% of input units
# fully connected layers
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(numOfClasses, activation='softmax')
])
print(model.summary())
# compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# get the data
trainingFolder = "C:/Users/huydu/introtoml/monkey_class_proj/monkeyClassification/images/training"
validationFolder = "C:/Users/huydu/introtoml/monkey_class_proj/monkeyClassification/images/validation"
trainingGenerator = ImageDataGenerator( rescale = 1. / 255, #augment/enrich images to get more data
rotation_range = 20,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
training_set = trainingGenerator.flow_from_directory( trainingFolder,
shuffle = True,
target_size = IMG_SIZE,
batch_size = batchSize,
class_mode = 'categorical')
validationGenerator = ImageDataGenerator( rescale = 1. / 255)
validation_set = validationGenerator.flow_from_directory( validationFolder,
shuffle = False, ####################
target_size = IMG_SIZE,
batch_size = batchSize,
class_mode = 'categorical')
# get step number (rounding up)
trainingSteps = np.ceil(training_set.samples / batchSize)
validationSteps = np.ceil(validation_set.samples / batchSize)
# store best model found during training
bestModel_file = "C:/Users/huydu/introtoml/monkey_class_proj/monkeyClassification/bestMonkeyModel.h5"
bestModel = ModelCheckpoint(bestModel_file, monitor='val_accuracy', verbose=1, save_best_only=True)
# train the model
history = model.fit( training_set,
validation_data = validation_set,
epochs = EPOCHS,
steps_per_epoch = trainingSteps,
validation_steps = validationSteps,
verbose = 1,
callbacks = [bestModel])
# evaluate the model
valResults = model.evaluate(validation_set)
print(valResults)
print(model.metrics_names)
# chart the results
trainAcc = history.history['accuracy']
validationAcc = history.history['val_accuracy']
trainLoss = history.history['loss']
validationLoss = history.history['val_loss']
actualEpochs = range(len(trainAcc))
print("Actual Epochs: "+ str(actualEpochs))
plt.plot(actualEpochs, trainAcc, 'r', label="Training Accuracy")
plt.plot(actualEpochs, validationAcc, 'b', label="Validation Accuracy")
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.title('Training and Validation Accuracy vs Epochs')
plt.show()