-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hindi Character Recognition.py
70 lines (63 loc) · 2.59 KB
/
Hindi Character 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
import numpy as np
from keras import layers
from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from keras.utils import np_utils, print_summary
import pandas as pd
from keras.models import Sequential
from keras.callbacks import ModelCheckpoint
import keras.backend as K
data=pd.read_csv("copy of data.csv")
dataset=np.array(data)
np.random.shuffle(dataset)
X=dataset
Y=dataset
X=X[:, 0:1024]
Y=Y[:,1024]
X_train=X[0:70000,:]
X_train=X_train/255
X_test=X[70000:72001,:]
X_test=X_test/255
#Rehsape
Y=Y.reshape(Y.shape[0],1)
Y_train=Y[0:70000, :]
Y_train=Y_train.T
Y_test=Y[70000:72001, :]
Y_test=Y_test.T
print("Number of training examples = "+str(X_train.shape[0]))
print("Number of test examples = "+str(X_test.shape[0]))
print("X_train shape:"+str(X_train.shape))
print("Y_train shape:"+str(Y_train.shape))
print("X_test shape:"+str(X_test.shape))
print("Y_test shape:"+str(Y_test.shape))
image_x=32
image_y=32
train_y=np_utils.to_categorical(Y_train)
test_y=np_utils.to_categorical(Y_test)
train_y=train_y.reshape(train_y.shape[1], train_y.shape[2])
test_y=test_y.reshape(test_y.shape[1], test_y.shape[2])
X_train=X_train.reshape(X_train.shape[0], image_x, image_y, 1)
X_test=X_test.reshape(X_test.shape[0], image_x, image_y, 1)
print("X_train shape : "+str(X_train.shape))
print("Y_train shape : "+str(train_y.shape))
#Building the Model
def keras_model(image_x, image_y):
num_of_classes=37
model=Sequential()
model.add(Conv2D(filters=32, kernel_size=(5,5), input_shape=(image_x, image_y, 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'))
model.add(Conv2D(64, (5,5), activation='relu'))
model.add(MaxPooling2D(pool_size=(5,5), strides=(5,5), padding='same'))
model.add(Flatten())
model.add(Dense(num_of_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
filepath="devanagari.h5"
checkpoint1=ModelCheckpoint(filepath, monitor="val_acc", verbose=1, save_best_only=True, mode='max')
callbacks_list=[checkpoint1]
return model, callbacks_list
model, callbacks_list=keras_model(image_x, image_y)
model.fit(X_train, train_y, validation_data=(X_test, test_y), epochs=10, batch_size=64, callbacks=callbacks_list)
scores=model.evaluate(X_test, test_y, verbose=0)
print("CNN Error : %.2f%%" %(100-scores[1]*100))
print_summary(model)
model.save('devanagari.h5')