-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd_main.py
130 lines (111 loc) · 3.26 KB
/
d_main.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
# -*- coding: utf-8 -*-
"""
Created on Sat May 18 10:10:25 2019
@author: xyj77
"""
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras import optimizers
from keras.utils import plot_model
from keras.callbacks import TensorBoard
from keras.preprocessing.image import ImageDataGenerator
from model.LeNet import *
from model.resnet import *
from model.Vgg import *
from model.DenseNet import *
from keras import backend as K
if('tensorflow' == K.backend()):
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
# Hyper-parameter
DATA_PATH = './data'
LOG_PATH = './log'
IMG_SIZE = 32
CLASS = 9
BATCH_SIZE = 32
ITERATION = 20
# MODEL = 'LeNet'
# MODEL = 'VGG'
MODEL = 'ResNet'
# MODEL = 'DenseNet'
# MODEL = 'SENet'
# build network
if MODEL == 'LeNet':
EPOCH = 500
model = LeNet(in_shape=(IMG_SIZE,IMG_SIZE,1), n_class=CLASS)
elif MODEL == 'VGG':
EPOCH = 200
model = Vgg19(in_shape=(IMG_SIZE,IMG_SIZE,1), n_class=CLASS)
elif MODEL == 'ResNet':
EPOCH = 400
model = ResnetBuilder.build_resnet_18((1, IMG_SIZE, IMG_SIZE), CLASS)
elif MODEL == 'DenseNet':
EPOCH = 200
model = DenseNet(in_shape=(IMG_SIZE,IMG_SIZE,1), n_class=CLASS)
elif MODEL == 'SENet':
EPOCH = 200
model = DenseNet(in_shape=(IMG_SIZE,IMG_SIZE,1), n_class=CLASS)
adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, amsgrad=False)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
# show model
plot_model(model, to_file='./images/'+ MODEL +'_model.png')
print(model.summary())
# set callback
tb_cb = TensorBoard(log_dir=LOG_PATH)
cbks = [tb_cb]
# using real-time data augmentation
print('Using real-time data augmentation.')
datagen = ImageDataGenerator(
rescale=1./255,
# featurewise_center=True,
rotation_range=30,
horizontal_flip=True,
validation_split=0.2)
train = datagen.flow_from_directory(
DATA_PATH,
target_size=(IMG_SIZE, IMG_SIZE),
color_mode='grayscale',
batch_size=BATCH_SIZE,
class_mode='categorical',
subset='training')
validation = datagen.flow_from_directory(
DATA_PATH,
target_size=(IMG_SIZE, IMG_SIZE),
color_mode='grayscale',
batch_size=BATCH_SIZE,
class_mode='categorical',
subset='validation')
# start train
print('Using ' + MODEL + 'to predict gestures!')
history = model.fit_generator(
generator=train,
steps_per_epoch=ITERATION,
epochs=EPOCH,
callbacks=cbks,
validation_data=validation,
validation_steps=ITERATION)
# save model
model.save('./model/'+ MODEL +'_model.h5')
# visualization
# Acc
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='best')
plt.savefig('./images/'+ MODEL +'_acc.png')
plt.clf()
# Loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='best')
plt.savefig('./images/'+ MODEL +'_loss.png')
plt.close()