-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn.py
87 lines (68 loc) · 2.58 KB
/
cnn.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
# Image Classification
# Import libraries
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import GlobalAveragePooling2D
from keras.layers import Dense
# Initalize CNN
classifier = Sequential()
# Add 2 convolution layers
classifier.add(Conv2D(filters=32, kernel_size=(3,3), input_shape=(64, 64, 3), activation='relu'))
classifier.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
# Add pooling layer
classifier.add(MaxPooling2D(pool_size=(2,2)))
# Add 2 more convolution layers
classifier.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
classifier.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
# Add max pooling layer
classifier.add(MaxPooling2D(pool_size=(2,2)))
# Add 2 more convolution layers
classifier.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
classifier.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
# Add max pooling layer
classifier.add(MaxPooling2D(pool_size=(2,2)))
# Add global average pooling layer
classifier.add(GlobalAveragePooling2D())
# Add full connection
classifier.add(Dense(units=2, activation='softmax'))
# Compiling the ANN
classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Fit CNN to images
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_set = train_datagen.flow_from_directory(
'dataset/training_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')
test_set = test_datagen.flow_from_directory(
'dataset/test_set',
target_size=(64, 64),
batch_size=32,
class_mode='categorical')
classifier.fit_generator(
train_set,
steps_per_epoch=8000,
epochs=25,
validation_data=test_set,
validation_steps=2000)
classifier.save('model_categorical_complex.h5')
# Test accuracy of classifier
def test_accuracy(classifier, test_set, steps):
num_correct = 0
num_guesses = 0
for i in range(steps):
a = test_set.next()
guesses = classifier.predict(a[0])
correct = a[1]
for index in range(len(guesses)):
num_guesses += 1
if round(guesses[index][0]) == correct[index]:
num_correct += 1
return num_correct, num_guesses