-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectronet.py
137 lines (122 loc) · 4.56 KB
/
spectronet.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
# @Author: nilanjan
# @Date: 2018-11-20T19:34:48+05:30
# @Email: [email protected]
# @Filename: spectronet.py
# @Last modified by: nilanjan
# @Last modified time: 2018-11-22T23:09:30+05:30
# @Copyright: Nilanjan Daw
from keras import layers, models
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras import optimizers
from keras import callbacks
import csv
import os
import shutil
def makeTrainDataset():
base_dir = 'training/'
normal = base_dir + "normal/"
abnormal = base_dir + "abnormal/"
with open('training.csv') as file:
dataset = csv.reader(file, delimiter=',')
for data in dataset:
if data[1] == '-1':
filename = data[0] + ".png"
src = base_dir + filename
if os.path.isfile(src):
dst = normal + filename
shutil.move(src, dst)
elif data[1] == '1':
filename = data[0] + ".png"
src = base_dir + filename
if os.path.isfile(src):
dst = abnormal + filename
shutil.move(src, dst)
def makeValidationDataset():
base_dir = 'validation/'
normal = base_dir + "normal/"
abnormal = base_dir + "abnormal/"
with open('validation.csv') as file:
dataset = csv.reader(file, delimiter=',')
for data in dataset:
if data[1] == '-1':
filename = data[0] + ".png"
src = base_dir + filename
if os.path.isfile(src):
dst = normal + filename
shutil.move(src, dst)
elif data[1] == '1':
filename = data[0] + ".png"
src = base_dir + filename
if os.path.isfile(src):
dst = abnormal + filename
shutil.move(src, dst)
def defineModel():
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(240, 360, 3)))
model.add(layers.MaxPool2D(2, 2))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPool2D(2, 2))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPool2D(2, 2))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPool2D(2, 2))
model.add(layers.Flatten())
model.add(layers.Dropout(0.1))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
print(model.summary())
model.compile(loss='binary_crossentropy',
optimizer=optimizers.RMSprop(lr=0.04), metrics=['acc'])
return model
print("Compiling model...")
model = defineModel()
print("making training dataset...")
makeTrainDataset()
print("making validation dataset")
makeValidationDataset()
train_dir = 'training'
validation_dir = 'validation'
train_datagen = ImageDataGenerator(
rescale=1./255,
horizontal_flip=True,
fill_mode='nearest'
)
validation_datagen = ImageDataGenerator(
rescale=1./255,
featurewise_center=True,
featurewise_std_normalization=True
)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(240, 360),
batch_size=20,
class_mode='binary'
)
validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(240, 360),
batch_size=20,
class_mode='binary'
)
csv_logger = callbacks.CSVLogger('log.csv', append=True, separator=';')
filepath = "weights/weights-{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = callbacks.ModelCheckpoint(
filepath, monitor='val_acc', save_best_only=True, mode='max')
tensorboard = callbacks.TensorBoard(log_dir='./logs', histogram_freq=0, batch_size=32, write_graph=True,
write_grads=False, write_images=True, embeddings_freq=0,
embeddings_layer_names=None, embeddings_metadata=None)
history = model.fit_generator(
train_generator,
steps_per_epoch=100,
epochs=5,
validation_data=validation_generator,
validation_steps=50,
callbacks=[csv_logger, tensorboard, checkpoint]
)
model.save('spectronet.h5')