-
Notifications
You must be signed in to change notification settings - Fork 25
/
model_build.py
125 lines (86 loc) · 3.44 KB
/
model_build.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
# -*- coding: utf-8 -*-
"""Detection
"""
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
from keras.models import load_model
import os
import numpy
K.set_image_dim_ordering('th')
from tensorflow.contrib.learn.python.learn.datasets.mnist import extract_images, extract_labels
image_folder = './result/image/'
# In[baselineK_model]
#Defining the model
def baselineK_model():
# create model
model = Sequential()
model.add(Conv2D(32, (3,3), input_shape=(1,28,28), activation='relu'))
model.add(Conv2D(32,(3,3),strides=(1,1),padding="same"))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64,(3,3),strides=(1,1),padding="same"))
model.add(Activation('relu'))
model.add(Conv2D(64,(3,3),strides=(1,1),padding="same"))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32,(3,3),strides=(1,1),padding="same"))
model.add(Activation('relu'))
model.add(Conv2D(32,(3,3),strides=(1,1),padding="same"))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
# In[Main]
if not (os.path.exists('./gzip/Mnist1L_5Conv.h5')):
with open('./gzip/emnist-letters-train-images-idx3-ubyte.gz', 'rb') as c:
train_images = extract_images(c)
train_images=train_images[0:]
c.close()
print("Loaded training images")
with open('./gzip/emnist-letters-train-labels-idx1-ubyte.gz', 'rb') as c:
y_train = extract_labels(c)
y_train=y_train[0:]
c.close()
print("Loaded training labels")
with open('./gzip/emnist-letters-test-images-idx3-ubyte.gz', 'rb') as f:
test_images = extract_images(f)
test_images=test_images[0:]
c.close()
print("Loaded testing images")
with open('./gzip/emnist-letters-test-labels-idx1-ubyte.gz', 'rb') as f:
y_test = extract_labels(f)
y_test=y_test[0:]
f.close()
print("Loaded testing labels")
test_images=test_images.astype('float32')
train_images = train_images.astype('float32')
train_images = train_images.reshape(train_images.shape[0], 1, 28, 28).astype('float32')
test_images = test_images.reshape(test_images.shape[0], 1, 28, 28).astype('float32')
seed = 7
numpy.random.seed(seed)
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
#if not (os.path.exists('./gzip/Mnist1L_5Conv.h5')):
print("No model found, creating new")
model = baselineK_model()
print(model.summary())
# Fit the model
model.fit(train_images, y_train, validation_data=(test_images, y_test), epochs=8, batch_size=30, verbose=2)
model.save('./gzip/Mnist1L_5Conv.h5')
else:
print('Model found')
model = load_model('./gzip/Mnist1L_5Conv.h5')
print(model.summary())
# Final evaluation of the model
scores = model.evaluate(test_images, y_test, verbose=0)
print("CNN Error: %.2f%%" % (100-scores[1]*100))