-
Notifications
You must be signed in to change notification settings - Fork 97
/
utils.py
90 lines (74 loc) · 2.54 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
# Layers
from keras.layers import Dense, Activation, Flatten, Dropout
from keras import backend as K
# Other
from keras import optimizers
from keras import losses
from keras.optimizers import SGD, Adam
from keras.models import Sequential, Model
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras.models import load_model
# Utils
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random, glob
import os, sys, csv
import cv2
import time, datetime
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(class_list_file):
class_list = []
with open(class_list_file, '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.plot(epochs, acc, 'r.')
plt.plot(epochs, val_acc, 'r')
plt.title('Training and validation accuracy')
# plt.figure()
# plt.plot(epochs, loss, 'r.')
# plt.plot(epochs, val_loss, 'r-')
# plt.title('Training and validation loss')
plt.show()
plt.savefig('acc_vs_epochs.png')