-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
46 lines (39 loc) · 1.12 KB
/
train.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
import PIL.Image
import tensorflow as tf
val_ds = tf.keras.utils.image_dataset_from_directory(
"./input/test",
validation_split=0.2,
subset="validation",
seed=123,
image_size=(25, 25),
batch_size=32)
train_ds = tf.keras.utils.image_dataset_from_directory(
"./input/train",
validation_split=0.2,
subset="training",
seed=123,
image_size=(25, 25),
batch_size=32)
num_classes = 2
model = tf.keras.Sequential([
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, activation="softmax")
])
model.compile(
optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(
train_ds,
validation_data=val_ds,
epochs=6
)
model.save('saved_model/my_model')