-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·100 lines (85 loc) · 3.26 KB
/
train.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
import time
import numpy as np
from gen_link import gen_link_text_and_image
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
from keras.models import model_from_json
K.set_image_dim_ordering('tf')
time_start=time.time()
seed = 7
np.random.seed(seed)
charlist = [chr(i) for i in range(97,123)] + [ str(i) for i in range(10)]
IMAGE_HEIGHT=60
IMAGE_WIDTH=160
MAX_LETTERS=6
CHAR_SET_LEN=len(charlist)
# generate training and validation data
def get_next_batch(batch_size=128):
batch_x = np.zeros([batch_size, IMAGE_HEIGHT, IMAGE_WIDTH])
batch_y = np.zeros([batch_size, MAX_LETTERS*CHAR_SET_LEN])
for i in range(batch_size):
text, image = gen_link_text_and_image()
batch_x[i,:] = image
batch_y[i,:] = text
return batch_x, batch_y
def cnn_model():
# create model
num_classes = CHAR_SET_LEN*MAX_LETTERS
model = Sequential()
#con layer 1
model.add(Conv2D(32, (5, 5), strides=(2, 2), padding='same', input_shape=(IMAGE_HEIGHT, IMAGE_WIDTH, 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2),strides=(2, 2),padding='same'))
model.add(Dropout(0.2))
#con layer 2
model.add(Conv2D(64, (5, 5), padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
#con layer 3
model.add(Conv2D(64, (5, 5), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2),strides=(2, 2),padding='same'))
model.add(Dropout(0.2))
#flatten for next layer
model.add(Flatten())
#full connection layer
model.add(Dense(2160, activation='relu'))
#out layer
model.add(Dense(num_classes, activation='sigmoid'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
# build the model
model = cnn_model()
(X_train, y_train) = get_next_batch(60000)
X_train = X_train.reshape(X_train.shape[0], IMAGE_HEIGHT, IMAGE_WIDTH, 1).astype('float32')
X_train = X_train / 255
(X_test, y_test) = get_next_batch(10000)
X_test = X_test.reshape(X_test.shape[0], IMAGE_HEIGHT, IMAGE_WIDTH, 1).astype('float32')
X_test = X_test / 255
# load model
# json_file = open('my_model_architecture.json', 'r')
# loaded_model_json = json_file.read()
# json_file.close()
# model = model_from_json(loaded_model_json)
# model.load_weights('my_model_weights.h5')
# model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# scores = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=100, batch_size=400, verbose=2)
while True:
scores = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=1000, verbose=2)
vacc = scores.history['val_acc'][-1]
acc = scores.history['acc'][-1]
if acc >= 0.995 and vacc >=0.999:
break
#save model and weight
print('start to save model to files')
json_string = model.to_json()
open('my_model_architecture.json','w').write(json_string)
model.save_weights('my_model_weights.h5')
print('model saved')
time_end=time.time()
print('totally cost',time_end-time_start,'s')