-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
145 lines (106 loc) · 3.99 KB
/
Copy pathtrain.py
File metadata and controls
145 lines (106 loc) · 3.99 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
# python3
# -*- coding: utf-8 -*-
#
# ===============================================
# Author: Scott A. Soifer
# Email: sas2412@columbia.edu
# Email: soifer00@gmail.com
# Date Created: Sun May 1 6:26:34 EST 2022
# ======================================================
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import Model
from tensorflow.keras import metrics
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import pandas as pd
import csv
from datetime import datetime
import numpy as np
import pathlib
import os
from sklearn.model_selection import train_test_split
from preprocess import load_glyphs
def gen_data(file_dir):
labels, imgs = load_glyphs(file_dir)
train_imgs, test_imgs, train_labels, test_labels = train_test_split(imgs, labels)
# train_imgs, test_imgs = train_imgs / 255.0, test_imgs / 255.0
# train_imgs_ds = tf.data.Dataset.from_tensor_slices(train_imgs)
# train_labels_ds = tf.data.Dataset.from_tensor_slices(train_labels)
# train_ds = tf.data.Dataset.zip((train_imgs_ds, train_labels_ds))
# train_ds = train_ds.batch(16)
train_imgs = tf.expand_dims(train_imgs, 3)
test_imgs = tf.expand_dims(test_imgs, 3)
train_datagen = ImageDataGenerator(
# rotation_range=3,
# width_shift_range=0.1,
# height_shift_range=0.1,
# shear_range=0.5,
# zoom_range=0.2,
# fill_mode='nearest'
)
test_datagen = ImageDataGenerator()
train_generator = train_datagen.flow(train_imgs, train_labels, shuffle=True, batch_size=16)
test_generator = train_datagen.flow(test_imgs, test_labels, batch_size=16)
return train_generator, test_generator
def build_model():
model = keras.Sequential(
[
keras.Input(shape=(28, 28, 1)),
layers.Conv2D(32, (3, 3), activation='relu'),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dropout(0.5),
layers.Dense(256, activation='relu'),
layers.Dense(27, activation='softmax')
]
)
return model
def plot(history, save=True):
# The history object contains results on the training and test
# sets for each epoch
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
# Get the number of epochs
epochs = range(len(acc))
fig1 = plt.figure()
plt.title('Training and validation accuracy')
plt.plot(epochs, acc, color='blue', label='Train')
plt.plot(epochs, val_acc, color='orange', label='Val')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
fig2 = plt.figure()
plt.title('Training and validation loss')
plt.plot(epochs, loss, color='blue', label='Train')
plt.plot(epochs, val_loss, color='orange', label='Val')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
if save:
timestamp = datetime.now().strftime('%Y%m%d_%I.%M.%S %p')
fig1.savefig(f'outputs/acc_graph_{timestamp}.png')
fig2.savefig(f'outputs/loss_graph_{timestamp}.png')
def train(data_file_dir):
train_generator, test_generator = gen_data(data_file_dir)
model = build_model()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# history = model.fit(train_ds, validation_data=(test_imgs, test_labels), epochs=10)
history = model.fit(train_generator, validation_data=test_generator, epochs=15,
steps_per_epoch=train_generator.n//train_generator.batch_size,
validation_steps=test_generator.n//test_generator.batch_size)
# model.save("/content/ocr_model_english.h5")
model.save("outputs/ocr_model_hebrew.h5")
return history
def main():
# history = train(data_file_dir="data/heb_dataset/")
# plot(history, save=True)
pass
if __name__=="__main__":
main()