-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeras_cnn.py
84 lines (69 loc) · 2.61 KB
/
keras_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
import os
import common
import matplotlib.pyplot as plt
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import EarlyStopping
batch_size = 10
num_classes = 2
epochs = 50
save_dir = os.path.join(os.getcwd(), 'saved_models')
model_name = 'keras_apk_trained_model.h5'
# The data, split between train and test sets
(x_train, y_train), (x_test, y_test) = common.load_dataset_small()
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
# Building model
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', activation='relu',
input_shape=x_train.shape[1:]))
model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
# # Training
# opt = keras.optimizers.SGD(lr=0.005, decay=1e-4)
# model.compile(loss='categorical_crossentropy',
# optimizer=opt,
# metrics=['accuracy'])
# early_stopping = EarlyStopping(monitor='value_loss', patience=3, verbose=1)
# history = model.fit(x_train, y_train,
# batch_size=batch_size,
# epochs=epochs,
# verbose=1,
# validation_data=(x_test, y_test),
# shuffle=True,
# callbacks=[early_stopping]
# )
# # # Plot accuracy/loss to epochs
# # # history - dict of ['acc', 'loss', 'val_acc', 'val_loss']
# plt.plot(range(0, epochs), history.history['val_acc'])
# plt.xlabel('Epochs')
# plt.ylabel('Accuracy')
# plt.show()
# # plt.plot(range(0, epochs), history.history['val_loss'])
# # plt.xlabel('Epochs')
# # plt.ylabel('Loss')
# # plt.show()
# # Save model and weights
# if not os.path.isdir(save_dir):
# os.makedirs(save_dir)
# model_path = os.path.join(save_dir, model_name)
# model.save(model_path)
# print('Saved trained model at %s ' % model_path)
# scores = model.evaluate(x_test, y_test, verbose=1)
# print('Test loss:', scores[0])
# print('Test accuracy:', scores[1])