-
Notifications
You must be signed in to change notification settings - Fork 3
/
OCR.py
75 lines (51 loc) · 2.64 KB
/
OCR.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
import sys
import cv2
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense,Flatten ,Dropout
from keras.models import load_model
from keras.layers import Convolution2D, MaxPooling2D
import keras
import os
from sklearn import preprocessing
img_gen=ImageDataGenerator(rescale=1./255, validation_split=0.2)
train_set=img_gen.flow_from_directory('C:/Users/AMIN/Desktop/PLT/DATA',
target_size=(60,120),
batch_size=50,
class_mode='categorical',
color_mode='grayscale',
subset='training',
seed=1)
valid_set=img_gen.flow_from_directory('C:/Users/AMIN/Desktop/PLT/DATA',
target_size=(60,120),
batch_size=50,
class_mode='categorical',
color_mode='grayscale',
subset='validation',
seed=1)
model = Sequential()
model.add(Convolution2D(32,4, activation='relu',input_shape=(60,120,1)))
model.add(Convolution2D(32, 4, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(900, activation='relu'))
model.add(Dense( 9, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer=Adam(learning_rate=0.001, name='Adam'),
metrics=['accuracy'])
history = model.fit(x=train_set, validation_data=valid_set,
epochs=100, verbose=1)
model.save("model_jadid.h5")
print("مدل ذخیره شد")
x=max(history.history['accuracy'])
y=min(history.history['loss'])
xx=max(history.history['val_accuracy'])
yy=min(history.history['val_loss'])
print("amozesh-deghat", x)
print("test-deghat", xx)
# min_max_scaler = preprocessing.MinMaxScaler()
# train_set = min_max_scaler.fit_transform(train_set)
# valid_set = min_max_scaler.fit_transform(valid_set)