-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevanagriOCR2.py
110 lines (91 loc) · 2.67 KB
/
devanagriOCR2.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
import os
import h5py
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential
from keras.layers import Convolution2D,MaxPooling2D,Activation, Dropout, Flatten, Dense
trainDataGen = ImageDataGenerator(
rotation_range = 5,
width_shift_range = 0.1,
height_shift_range = 0.1,
rescale = 1.0/255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = False,
fill_mode = 'nearest')
test_datagen = ImageDataGenerator(rescale=1./255)
trainGenerator = trainDataGen.flow_from_directory(
"/home/owner/Downloads/DevanagariHandwrittenCharacterDataset/Train",
target_size = (32,32),
batch_size = 32,
color_mode = "grayscale",
class_mode = "categorical")
prev = ""
labels = ["ka","kha","ga","gha","kna","cha","chha","ja","jha","yna","t`a","t`ha","d`a","d`ha","adna","ta","tha","da","dha","na","pa","pha","ba","bha","ma","yaw","ra","la","waw","sha","shat","sa","ha","aksha","tra","gya","0","1","2","3","4","5","6","7","8","9"]
count = 0;
'''for i in trainGenerator.classes:
if prev == labels[i]:
count = count+1
continue;
print count
print labels[i]
count = 1
prev = labels[i]
print count
'''
validation_generator = test_datagen.flow_from_directory(
"/home/owner/Downloads/DevanagariHandwrittenCharacterDataset/Test",
target_size=(32,32),
batch_size=32,
color_mode = "grayscale",
class_mode= 'categorical')
model = Sequential()
model.add(Convolution2D(filters = 32,
kernel_size = (3,3),
strides = 1,
activation = "relu",
input_shape = (32,32,1)))
model.add(Convolution2D(filters = 32,
kernel_size = (3,3),
strides = 1,
activation = "relu",
input_shape = (32,32,1)))
model.add(MaxPooling2D(pool_size=(2, 2),
strides=(2, 2),
padding="same"))
model.add(Convolution2D(filters = 64,
kernel_size = (3,3),
strides = 1,
activation = "relu"))
model.add(Convolution2D(filters = 64,
kernel_size = (3,3),
strides= 1,
activation = "relu"))
model.add(MaxPooling2D(pool_size=(2, 2),
strides=(2, 2),
padding="same"))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128,
activation = "relu",
kernel_initializer = "uniform"))
model.add(Dense(64,
activation = "relu",
kernel_initializer = "uniform"))
model.add(Dense(46,
activation = "softmax",
kernel_initializer = "uniform"))
model.compile(optimizer = "adam",
loss = "categorical_crossentropy",
metrics = ["accuracy"])
print model.summary()
model.fit_generator(
trainGenerator,
nb_epoch = 20,
steps_per_epoch = 2444,
validation_data = validation_generator,
validation_steps = 432,
use_multiprocessing = True
)
model.save("DevaModel.h5")