-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResNet50CNN.py
161 lines (135 loc) · 5.19 KB
/
ResNet50CNN.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
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications.mobilenet import preprocess_input
from keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D
from keras.optimizers import Adam, SGD
from keras.layers import Flatten, Dense, Dropout
from tensorflow.keras import Model
from tensorflow.keras.layers import BatchNormalization
import seaborn as sns
import MobileNetCNN
import numpy as np
import matplotlib.plyplot as plt
def base_model_ResNet():
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
for layer in model.layers: #freeze all layers
layer.trainable = False
return model
x_train = []
x_test = []
y_train = []
y_test = []
acc = []
val_acc = []
lo = []
val_lo = []
from sklearn.model_selection import KFold
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
folds = np.array(['fold0','fold1','fold2','fold3','fold4','fold5','fold6','fold7','fold8','fold9'])
load_dir = "Spectrograms/"
accuracies = []
losses = []
kf = KFold(n_splits=10)
#skf = StratifiedKFold(n_splits=10)
count = 0
for train_index, test_index in kf.split(folds): #Splits into training and testing sets
x_train, y_train = [], []
for ind in train_index:
param = os.path.join(load_dir,'fold'+str(ind),"*png")
features,labels = MobileNetCNN.extract(param)
x_train += features
y_train += labels
test_param = os.path.join(load_dir,'fold'+str(test_index[0]),"*png")
test_features, test_labels = MobileNetCNN.extract(test_param)
x_test += test_features
y_test += test_labels
x_train_norm = np.array(x_train) / 255
x_test_norm = np.array(x_test) / 255
#one hot encode
y_train_encoded = to_categorical(y_train)
y_test_encoded = to_categorical(y_test)
model = base_model_ResNet()
model.compile(optimizer='rmsprop', loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train_norm,y_train_encoded,validation_data=(x_test_norm, y_test_encoded),batch_size=10,epochs=5)
#determine which layer to slice at for freeze/unfreeze
###
for i, layer in enumerate(model.layers):
print(i, layer.name)
###
for layer in model.layers[:67]:
layer.trainable = False
for layer in model.layers[67:]:
layer.trainable = True
# we need to recompile the model for these modifications to take effect
model.compile(optimizer=Adam(lr=1e-5), loss='categorical_crossentropy',metrics=['accuracy'])
#model.summary()
history = model.fit(x_train_norm,y_train_encoded, validation_data= (x_test_norm, y_test_encoded), batch_size=10,epochs=15)
'''
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.savefig("Accuracy_Graph_"+str(count)+"_.png")
plt.close()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.savefig("Loss_Graph_"+str(count)+"_.png")
plt.close()
'''
acc.append(history.history['accuracy'])
val_acc.append(history.history['val_accuracy'])
lo.append(history.history['loss'])
val_lo.append(history.history['val_loss'])
#model.save("Model" + str(count) + ".h5")
l, a = model.evaluate(x_test_norm,y_test_encoded,verbose = 0)
accuracies.append(a)
losses.append(l)
print(accuracies)
print(losses)
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
y_predicted = model.predict(x_test_norm)
con_mat_df = confusion_matrix(y_test_encoded.argmax(axis=1), y_predicted.argmax(axis=1))
figure = plt.figure(figsize=(8, 8))
sns.heatmap(con_mat_df,annot=True,cmap=plt.cm.Blues)
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.savefig("Confusion_Matrix_CNN"+str(count)+".png")
plt.close()
count += 1
print("Displaying Classification Report")
classes = ["0-Voices","1-Locomotion","2-Digestive","3-Elements","4-Animals","5-Cook_App","6-Clean-App","7-Vent_App","8-Furniture","9-Instruments"]
print(classification_report(y_test_encoded.argmax(axis=1), y_predicted.argmax(axis=1), target_names=classes))
x_train.clear()
x_test.clear()
y_train.clear()
y_test.clear()
print(acc)
print(val_acc)
print(lo)
print(val_lo)
print("Average 10 Folds Accuracy:" + str((np.mean(accuracies))))
fig = plt.figure(figsize = (10, 5))
all_folds = ["fold0","fold1","fold2","fold3","fold4","fold5","fold6","fold7","fold8","fold9"]
# creating the bar plot
plt.bar(all_folds, accuracies, color ='maroon',
width = 0.4)
plt.xlabel("Fold No")
plt.ylabel("Accuracy")
plt.title("Accuracy of Each Fold")
plt.show()
plt.close()