-
Notifications
You must be signed in to change notification settings - Fork 70
/
myVgg16.py
230 lines (202 loc) · 7.69 KB
/
myVgg16.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import tensorflow as tf
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
import math
import json
import sys
import keras
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, AveragePooling2D, ZeroPadding2D, Flatten, Activation, add
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras import initializers
from keras.engine import Layer, InputSpec
from keras import backend as K
from keras.utils import np_utils
from keras.optimizers import *
import numpy as np
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from utils import dataset as dataset
import argparse
import time
from datetime import timedelta
def build_dataset(data_directory, img_width):
X, y, tags = dataset.dataset(data_directory, int(img_width))
nb_classes = len(tags)
sample_count = len(y)
train_size = sample_count
print("train size : {}".format(train_size))
feature = X
label = np_utils.to_categorical(y, nb_classes)
return feature, label, nb_classes
def build_model(SHAPE, nb_classes, bn_axis, seed=None):
# We can't use ResNet50 directly, as it might cause a negative dimension
# error.
if seed:
np.random.seed(seed)
input_layer = Input(shape=SHAPE)
# block 1
x = Conv2D(64, (3, 3),
activation='relu',
padding='same',
name='block1_conv1')(input_layer)
x = Conv2D(64, (3, 3),
activation='relu',
padding='same',
name='block1_conv2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
# block 2
x = Conv2D(128, (3, 3),
activation='relu',
padding='same',
name='block2_conv1')(x)
x = Conv2D(128, (3, 3),
activation='relu',
padding='same',
name='block2_conv2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
# block 3
x = Conv2D(256, (3, 3),
activation='relu',
padding='same',
name='block3_conv1')(x)
x = Conv2D(256, (3, 3),
activation='relu',
padding='same',
name='block3_conv2')(x)
x = Conv2D(256, (3, 3),
activation='relu',
padding='same',
name='block3_conv3')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
# Block 4
x = Conv2D(512, (3, 3),
activation='relu',
padding='same',
name='block4_conv1')(x)
x = Conv2D(512, (3, 3),
activation='relu',
padding='same',
name='block4_conv2')(x)
x = Conv2D(512, (3, 3),
activation='relu',
padding='same',
name='block4_conv3')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
# Block 5
x = Conv2D(512, (3, 3),
activation='relu',
padding='same',
name='block5_conv1')(x)
x = Conv2D(512, (3, 3),
activation='relu',
padding='same',
name='block5_conv2')(x)
x = Conv2D(512, (3, 3),
activation='relu',
padding='same',
name='block5_conv3')(x)
# x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
x = Flatten(name='flatten')(x)
x = Dense(4096, activation='relu', name='fc1')(x)
x = Dense(4096, activation='relu', name='fc2')(x)
x = Dense(nb_classes, activation='softmax', name='predictions')(x)
model = Model(input_layer, x)
return model
def main():
start_time = time.monotonic()
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-i', '--input',
help='an input directory of dataset', required=True)
parser.add_argument('-d', '--dimension',
help='a image dimension', type=int, default=48)
parser.add_argument('-c', '--channel',
help='a image channel', type=int, default=3)
parser.add_argument('-e', '--epochs',
help='num of epochs', type=int, default=10)
parser.add_argument('-b', '--batch_size',
help='num of batch_size', type=int, default=64)
# parser.add_argument('-o', '--optimizer',
# help='choose the optimizer (rmsprop, adagrad, adadelta, adam, adamax, nadam)', default="adam")
parser.add_argument('-o', '--output',
help='a result file', type=str, default="hasilnya.txt")
args = parser.parse_args()
# dimensions of our images.
img_width, img_height = args.dimension, args.dimension
channel = args.channel
epochs = args.epochs
batch_size = args.batch_size
SHAPE = (img_width, img_height, channel)
bn_axis = 3 if K.image_dim_ordering() == 'tf' else 1
data_directory = args.input
period_name = data_directory.split('/')
print("loading dataset")
X_train, Y_train, nb_classes = build_dataset(
"{}/train".format(data_directory), args.dimension)
X_test, Y_test, nb_classes = build_dataset(
"{}/test".format(data_directory), args.dimension)
print("number of classes : {}".format(nb_classes))
model = build_model(SHAPE, nb_classes, bn_axis)
model.compile(optimizer=Adam(lr=1.0e-4),
loss='categorical_crossentropy', metrics=['accuracy'])
# Fit the model
model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs)
# Save Model or creates a HDF5 file
model.save('{}epochs_{}batch_vgg16_model_{}.h5'.format(
epochs, batch_size, data_directory.replace("/", "_")), overwrite=True)
# del model # deletes the existing model
predicted = model.predict(X_test)
y_pred = np.argmax(predicted, axis=1)
Y_test = np.argmax(Y_test, axis=1)
cm = confusion_matrix(Y_test, y_pred)
report = classification_report(Y_test, y_pred)
tn = cm[0][0]
fn = cm[1][0]
tp = cm[1][1]
fp = cm[0][1]
if tp == 0:
tp = 1
if tn == 0:
tn = 1
if fp == 0:
fp = 1
if fn == 0:
fn = 1
TPR = float(tp)/(float(tp)+float(fn))
FPR = float(fp)/(float(fp)+float(tn))
accuracy = round((float(tp) + float(tn))/(float(tp) +
float(fp) + float(fn) + float(tn)), 3)
specitivity = round(float(tn)/(float(tn) + float(fp)), 3)
sensitivity = round(float(tp)/(float(tp) + float(fn)), 3)
mcc = round((float(tp)*float(tn) - float(fp)*float(fn))/math.sqrt(
(float(tp)+float(fp))
* (float(tp)+float(fn))
* (float(tn)+float(fp))
* (float(tn)+float(fn))
), 3)
f_output = open(args.output, 'a')
f_output.write('=======\n')
f_output.write('{}epochs_{}batch_vgg16\n'.format(
epochs, batch_size))
f_output.write('TN: {}\n'.format(tn))
f_output.write('FN: {}\n'.format(fn))
f_output.write('TP: {}\n'.format(tp))
f_output.write('FP: {}\n'.format(fp))
f_output.write('TPR: {}\n'.format(TPR))
f_output.write('FPR: {}\n'.format(FPR))
f_output.write('accuracy: {}\n'.format(accuracy))
f_output.write('specitivity: {}\n'.format(specitivity))
f_output.write("sensitivity : {}\n".format(sensitivity))
f_output.write("mcc : {}\n".format(mcc))
f_output.write("{}".format(report))
f_output.write('=======\n')
f_output.close()
end_time = time.monotonic()
print("Duration : {}".format(timedelta(seconds=end_time - start_time)))
if __name__ == "__main__":
main()