-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
52 lines (46 loc) · 1.47 KB
/
utils.py
File metadata and controls
52 lines (46 loc) · 1.47 KB
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
import numpy as np
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plot
import os
import errno
# Preprocessing function to subtract ImageNet mean RGB values from new images
# to keep weights coherent during transfer learning
def meanSubtraction(x):
x = x.astype(np.float32)
means = np.array([123.68, 116.779, 103.939], dtype=np.float32).reshape((1,1,3))
x -= means
return x
# Helper function to get new top layers configuration
def obtainNewTopLayers(input_shape, num_classes):
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
return model
# Helper function to plot and save graphs about the training
def plotGraph(graphData, title, xlabel, ylabel, legend, savePath):
for (i, data) in enumerate(graphData):
plot.plot(data)
plot.title(title)
plot.ylabel(ylabel)
plot.xlabel(xlabel)
if legend != None:
plot.legend(legend, loc='lower right')
plot.savefig(savePath)
plot.close()
# Helper function to organize folder structure
def createDirIfNotExisting(path):
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
# Raise exception if training data is not found
def checkDirs(paths):
for (i, path) in enumerate(paths):
if not os.path.isdir(path):
raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), path)