-
Notifications
You must be signed in to change notification settings - Fork 51
/
signatue_recognition.py
161 lines (113 loc) · 4.43 KB
/
signatue_recognition.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import keras
from sklearn.model_selection import train_test_split
TEST_DIR='E:/Python/signatue_recognition/data/test/'
SIGNATURE_CLASSES = ['A', 'B', 'C','D','E','F','K','L','M','N','O','P']
import os, random
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import log_loss
from sklearn.preprocessing import LabelEncoder
import matplotlib.pyplot as plt
from matplotlib import ticker
#import seaborn as sns
#%matplotlib inline
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Convolution2D, MaxPooling2D, ZeroPadding2D, Dense, Activation
from keras.optimizers import SGD, Adagrad
from keras.callbacks import EarlyStopping
from keras.utils import np_utils
from keras.optimizers import RMSprop, Adam
from keras import backend as K
ROWS = 190
COLS = 160
CHANNELS = 3
TRAIN_DIR="E:/Python/signatue_recognition/data/train/"
def root_mean_squared_error(y_true, y_pred):
"""
RMSE loss function
"""
return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1))
def get_images(fish):
"""Load files from train folder"""
fish_dir = TRAIN_DIR+'{}'.format(fish)
images = [fish+'/'+im for im in os.listdir(fish_dir)]
return images
def read_image(src):
import os
from scipy import misc
filepath=src
im=misc.imread(filepath)
import scipy.misc as mc
return mc.imresize(im,(ROWS,COLS))
files = []
y_all = []
for fish in SIGNATURE_CLASSES:
fish_files = get_images(fish)
files.extend(fish_files)
y_fish = np.tile(fish, len(fish_files))
y_all.extend(y_fish)
print("{0} photos of {1}".format(len(fish_files), fish))
y_all = np.array(y_all)
print(len(files))
print(len(y_all))
X_all = np.ndarray((len(files), ROWS, COLS, CHANNELS), dtype=np.uint8)
for i, im in enumerate(files):
X_all[i] = read_image(TRAIN_DIR+im)
if i%1000 == 0: print('Processed {} of {}'.format(i, len(files)))
print(X_all.shape)
y_all = LabelEncoder().fit_transform(y_all)
y_all = np_utils.to_categorical(y_all)
from sklearn.model_selection import train_test_split
X_train, X_valid, y_train, y_valid = train_test_split(X_all, y_all,
test_size=14, random_state=23,
stratify=y_all)
optimizer = RMSprop(lr=1e-4)
objective = 'categorical_crossentropy'
def center_normalize(x):
return (x - K.mean(x)) / K.std(x)
print('1')
model = Sequential()
model.add(Activation(activation=center_normalize, input_shape=(ROWS, COLS, CHANNELS)))
model.add(Convolution2D(64, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3, border_mode='valid'))
model.add(Activation('relu'))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(96, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(96, 3, 3, border_mode='valid'))
model.add(Activation('relu'))
model.add(ZeroPadding2D(padding=(1, 1)))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(128, 2, 2, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(128, 2, 2, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(len(SIGNATURE_CLASSES)))
model.add(Activation('sigmoid'))
adam = Adam(lr=0.0001)
model.compile(optimizer=adam, loss=root_mean_squared_error)
early_stopping = EarlyStopping(monitor='val_loss', patience=4, verbose=1, mode='auto')
model.fit(X_train, y_train, batch_size=64, nb_epoch=3,
validation_split=0.1, verbose=1, shuffle=True, callbacks=[early_stopping])
preds = model.predict(X_valid, verbose=1)
print("Validation Log Loss: {}".format(log_loss(y_valid, preds)))
test_files = [im for im in os.listdir(TEST_DIR)]
test = np.ndarray((len(test_files), ROWS, COLS, CHANNELS), dtype=np.uint8)
for i, im in enumerate(test_files):
test[i] = read_image(TEST_DIR+im)
test_preds = model.predict(test, verbose=1)
submission = pd.DataFrame(test_preds, columns=SIGNATURE_CLASSES)
submission.insert(0, 'image', test_files)
submission.head()
submission.to_csv('E:/Python/signatue_recognition/signatureResults.csv',index=False)