-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
81 lines (66 loc) · 1.75 KB
/
train_model.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
# %%
import cv2
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
# %% [markdown]
# Train the model
# %%
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
# %%
def binarize(img):
img = image.img_to_array(img, dtype='uint8')
binarized = np.expand_dims(cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2), -1)
inverted_binary_img = ~binarized
return inverted_binary_img
# %%
data_dir = 'archive/data/extracted_new_images2'
batch_size = 32
img_height = 45
img_width = 45
# %%
train_datagen = ImageDataGenerator(
preprocessing_function=binarize)
# %%
train_generator = train_datagen.flow_from_directory(
data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
color_mode="grayscale",
class_mode="categorical",
seed=123)
# %%
# Class names
class_names = [k for k,v in train_generator.class_indices.items()]
class_names
print(class_names)
# %%
num_classes = 23
model = tf.keras.Sequential([
tf.keras.layers.Input((45, 45, 1)),
tf.keras.layers.Rescaling(1./255),
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(num_classes)
])
# %%
model.summary()
# %%
model.compile(
optimizer='adam',
loss=tf.losses.CategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# %%
model.fit(
train_generator,
epochs=5
)
# %%
model.save('eqn-detect-model2.h5')