forked from mjbigdel/SignatureDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMycode_ML_Project.py
More file actions
148 lines (106 loc) · 4.4 KB
/
Copy pathMycode_ML_Project.py
File metadata and controls
148 lines (106 loc) · 4.4 KB
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
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 24 13:00:18 2018
@author: Manoochehr
"""
import cv2
import glob
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from keras.utils import np_utils
from sklearn.model_selection import train_test_split
from keras.layers import Conv1D, MaxPooling1D, Flatten, Dense, Activation, Dropout
from keras.models import Sequential
from keras import backend as K
from keras.optimizers import RMSprop, Adam
from keras.callbacks import EarlyStopping
from keras.layers.normalization import BatchNormalization
from keras.optimizers import SGD
from sklearn.metrics import log_loss
#from SignatureCroping import getSignatureFromPage
filenames = glob.glob("DatasetSig1/*/*.png")
imagesX = [cv2.imread(img,cv2.IMREAD_GRAYSCALE) for img in filenames]
y_all = []
for file in filenames:
_,imgY,_ = file.split("\\")
y_all.append(imgY)
X_all = []
for img in imagesX:
_,thresh1 = cv2.threshold(img,220,1,cv2.THRESH_BINARY_INV)
img = thresh1 * img
img = cv2.resize(img,(220, 150))
X_all.append(img)
y_all = np.array(y_all)
X_all = np.asarray(X_all)
plt.imshow(X_all[0,:,:])
#plt.imshow(X_all[35])
# One Hot Encoding Labels
y_all = LabelEncoder().fit_transform(y_all)
y_all = np_utils.to_categorical(y_all)
X_train, X_valid, y_train, y_valid = train_test_split(X_all, y_all,
test_size=0.1, random_state=23,
stratify=y_all)
def root_mean_squared_error(y_true, y_pred):
return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1))
optimizer = RMSprop(lr=1e-4)
objective = 'categorical_crossentropy'
def center_normalize(x):
return (x - K.mean(x)) / K.std(x)
print('1')
classifier = Sequential()
#classifier.add(Activation( activation=center_normalize, input_shape=(220, 150, 1)))
classifier.add(Conv1D(96, 11, strides=4, padding= 'valid',input_shape=(150, 220)))
classifier.add(BatchNormalization())
classifier.add(Activation( activation='relu'))
classifier.add(MaxPooling1D(3, strides=2, padding='same'))
classifier.add(Conv1D(256, 5, strides=1, padding= 'same'))
classifier.add(BatchNormalization())
classifier.add(Activation( activation='relu'))
classifier.add(MaxPooling1D(3, strides=2, padding='same'))
classifier.add(Conv1D(384, 3, strides=1, padding= 'same'))
classifier.add(BatchNormalization())
classifier.add(Activation( activation='relu'))
classifier.add(Conv1D(384, 3, strides=1, padding= 'same'))
classifier.add(BatchNormalization())
classifier.add(Activation( activation='relu'))
classifier.add(Conv1D(256, 3, strides=1, padding= 'same'))
classifier.add(BatchNormalization())
classifier.add(Activation(activation='relu'))
classifier.add(MaxPooling1D(3, strides=2, padding='same'))
classifier.add(Flatten())
classifier.add(Dense(units = 2048))
classifier.add(BatchNormalization())
classifier.add(Activation(activation='relu'))
classifier.add(Dense(units = 2048))
classifier.add(BatchNormalization())
classifier.add(Activation( activation='relu'))
classifier.add(Dense(units = 10 , activation='softmax')) # 10 is number of targets.
#classifier.add(Dense(units = 10, activation = 'sigmoid'))
adam = Adam(lr=0.001)
# For a mean squared error regression problem
# For a multi-class classification problem
sgd = SGD(lr=0.0001, decay=1e-4, momentum=0.9, nesterov=True, clipnorm=1.)
classifier.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
classifier.fit(X_train, y_train,
epochs=10,
batch_size=32)
score = classifier.evaluate(X_valid, y_valid, batch_size=32)
preds = classifier.predict(X_valid, verbose=1)
#
#
# with a Sequential model
get_nth_layer_output = K.function([classifier.layers[0].input],
[classifier.layers[-2].output])
FeatureLayer = get_nth_layer_output([X_train])[0]
FeatureLayer = np.asarray(FeatureLayer)
a = np.histogram(FeatureLayer)
plt.hist(a, bins='auto') # arguments are passed to np.histogram
plt.title("Histogram with 'auto' bins")
plt.show()
#from sklearn.svm import SVC
#
#clf = SVC(kernel = 'linear',random_state=0,gamma='scale', decision_function_shape='ovo')
#clf.fit(FeatureLayer, y_valid)