-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel_ResNet_final.py
More file actions
206 lines (153 loc) · 6.05 KB
/
Copy pathModel_ResNet_final.py
File metadata and controls
206 lines (153 loc) · 6.05 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python
# coding: utf-8
# # ResNet Model
import numpy as np
from keras.models import Model
import keras.backend as K
from Model_ResNet_helper import *
from preprocessing import load_images as l_i
from preprocessing import load_file as l_f
from PIL import Image
from os import listdir
import random
data_file_name = "sampled_data.csv"
path_training = "train/images/" # path from current folder to training images
#path_testing = "data/test/" # path from current folder to testing images
path_testing = "train/images/" # path from current folder to testing images
### Set training and testing details #################################################
m = l_f.check_num_images(data_file_name)
m_training = 200 #20000
m_testing = 100 #1000
num_epochs = 3 #50
num_batches = 2 #32
### Train Data #######################################################################
np.random.seed(1)
### Shuffle the m examples
list_of_rows = np.arange(1, m+1, 1)
np.random.shuffle(list_of_rows)
### Take a random m_training examples and load the data
img_dict = l_f.load_file(data_file_name, list_of_rows[:m_training])
### Load and process the images
(train_x, train_y) = l_i.load_images(path_training, img_dict)
### Normalize image vectors
train_x = train_x / 255
### Convert to one hot vector
print(train_x.shape)
print(train_y.shape)
### Test Data #######################################################################
### Take a random m_testing examples and load the data
#img_dict = l_f.load_file(data_file_name, list_of_rows[m_training:m_testing])
img_dict_test = l_f.load_file(data_file_name, list_of_rows[m_training:m_training + m_testing])
### Load and process the images
(test_x, test_y) = l_i.load_images(path_testing, img_dict_test)
#print(test_y)
### Normalize image vectors
test_x = test_x / 255
### Convert to one hot vector
print(test_x.shape)
print(test_y.shape)
model = ResNet50(input_shape = (64, 64, 3)) #changed classes from 4 to 2
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train_x, train_y, epochs = num_epochs, batch_size = num_batches)
## To save the weights
# serialize model to JSON
model_json = model.to_json()
with open("model_uptodate.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model_uptodate_weights.h5")
print("Saved model to disk")
# How to load the saved pre-trained weights
# load json and create model
# json_file = open('model_uptodate.json', 'r')
# loaded_model_json = json_file.read()
# json_file.close()
# loaded_model = model_from_json(loaded_model_json)
# # load weights into new model
# loaded_model.load_weights("model_uptodate_weights.h5")
# print("Loaded model from disk")
# loaded_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Note: remember to run line 106 to recompile the loaded model
evaluation = model.evaluate(test_x, test_y)
print(evaluation)
print ("Loss = " + str(evaluation[0]))
print ("Test Accuracy = " + str(evaluation[1]))
import csv
## To compute only accuracy
# error_analysis_num = 1 # update so it doesn't overwrite previous csv files
# list_of_row_nums = list_of_rows[m_training:m_training + m_testing]
# preds = model.predict(test_x)
# with open("error_analysis" + str(error_analysis_num) + ".csv", "w", newline = "") as csv_write_file:
# line = csv.writer(csv_write_file)
# headers = ["img_name", "uid", "subtype", "prediction"]
# line.writerow(headers)
# with open(data_file_name) as csv_file:
# csv_reader = csv.reader(csv_file, delimiter=',')
# line_count = 0
# sample_count = 0
# for row in csv_reader:
# if (row[0] == ""):
# break
# if line_count in list_of_row_nums:
# img_name = row[17]
# uid = row[20]
# subtype = row[19]
# prediction = str(int(round(preds[sample_count][0])))
# sample_count += 1
# if (prediction != subtype):
# # Save information to csv file
# new_entry = [img_name, uid, subtype, prediction]
# line.writerow(new_entry)
# line_count += 1
## To compute precision and recall
error_analysis_num = 1 # update so it doesn't overwrite previous csv files
list_of_row_nums = list_of_rows[m_training:m_training + m_testing]
preds = model.predict(test_x)
TP = 0
TN = 0
FP = 0
FN = 0
with open("error_analysis" + str(error_analysis_num) + ".csv", "w", newline = "") as csv_write_file:
line = csv.writer(csv_write_file)
headers = ["img_name", "uid", "subtype", "prediction"]
line.writerow(headers)
with open(data_file_name) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
sample_count = 0
for row in csv_reader:
if (row[0] == ""):
break
if line_count in list_of_row_nums:
img_name = row[17]
uid = row[20]
subtype = row[19]
prediction = str(int(round(preds[sample_count][0])))
sample_count += 1
if (prediction != subtype):
# Save information to csv file
new_entry = [img_name, uid, subtype, prediction]
line.writerow(new_entry)
# Update FP / FN
if (prediction == "1"):
FP += 1
else:
FN += 1
else:
if (prediction == "1"):
TP += 1
else:
TN += 1
line_count += 1
print("TP: " + str(TP))
print("TN: " + str(TN))
print("FP: " + str(FP))
print("FN: " + str(FN))
accuracy = round((TP + TN) / (TP + TN + FP + FN), 2)
print("Accuracy: " + str(accuracy))
precision = round(TP / (TP + FN), 2)
print("Precision: " + str(precision))
recall = round(TP / (TP + FP), 2)
print("Recall: " + str(recall))
F1 = (2 * precision * recall) / (precision + recall)
print("F1: " + str(F1))