-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
110 lines (89 loc) · 3.18 KB
/
utils.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
# Layers
from keras.layers import Dense, Activation, Flatten, Dropout
from keras import backend as K
# Other
from keras import optimizers
from keras.models import Sequential, Model
# Utils
import matplotlib as plt
plt.use("Agg")
import glob
import os, sys, csv
import cv2
def merge_topless_top_model(model_bottom, model_top):
model_bottom_last_layer = model_bottom.get_layer(model_bottom.layers[-1].name)
model_bottom_remodel = Model(inputs=model_bottom.input, outputs=model_bottom_last_layer.output)
new_model = Sequential()
new_model.add(model_bottom_remodel)
new_model.add(model_top)
return new_model
def get_square(image,square_size):
height,width=image.shape
if(height>width):
differ=height
else:
differ=width
differ+=4
mask = np.zeros((differ,differ), dtype="uint8")
x_pos=int((differ-width)/2)
y_pos=int((differ-height)/2)
mask[y_pos:y_pos+height,x_pos:x_pos+width]=image[0:height,0:width]
mask=cv2.resize(mask,(square_size,square_size),interpolation=cv2.INTER_AREA)
return mask
def save_class_list(class_list, model_name, dataset_name):
class_list.sort()
target=open("./checkpoints/" + model_name + "_" + dataset_name + "_class_list.txt",'w')
for c in class_list:
target.write(c)
target.write("\n")
def load_class_list(model_name, dataset_name):
class_list = []
with open("./checkpoints/" + model_name + "_" + dataset_name + "_class_list.txt", 'r') as csvfile:
file_reader = csv.reader(csvfile)
for row in file_reader:
class_list.append(row)
class_list.sort()
return class_list
# Get a list of subfolders in the directory
def get_subfolders(directory):
subfolders = os.listdir(directory)
subfolders.sort()
return subfolders
# Get number of files by searching directory recursively
def get_num_files(directory):
if not os.path.exists(directory):
return 0
cnt = 0
for r, dirs, files in os.walk(directory):
for dr in dirs:
cnt += len(glob.glob(os.path.join(r, dr + "/*")))
return cnt
# Add on new FC layers with dropout for fine tuning
def build_finetune_model(base_model, dropout, fc_layers, num_classes):
for layer in base_model.layers:
layer.trainable = False
x = base_model.output
x = Flatten()(x)
for fc in fc_layers:
x = Dense(fc, activation='relu')(x) # New FC layer, random init
x = Dropout(dropout)(x)
predictions = Dense(num_classes, activation='softmax')(x) # New softmax layer
finetune_model = Model(inputs=base_model.input, outputs=predictions)
return finetune_model
# Plot the training and validation loss + accuracy
def plot_training(history):
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.figure()
plt.plot(epochs, acc, 'r.')
plt.plot(epochs, val_acc, 'r')
plt.title('Training and validation accuracy')
plt.savefig('acc_vs_epochs.png')
plt.figure()
plt.plot(epochs, loss, 'r.')
plt.plot(epochs, val_loss, 'r-')
plt.title('Training and validation loss')
plt.savefig('loss_vs_epochs.png')