-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
75 lines (64 loc) · 2.06 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
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
# Before run this script, you should complete the annotation and save it in pascal voc format
# An annotation software named labelImg: $ pip install labelImg
# The scripe will train and save a new model
# batch_size, epochs and validation_split depends on the number of pictures
import tensorflow as tf
# ----------Custom Inputs for Scripts----------
# Define some parameters for the loader
batch_size = 1
target_size = (640, 640)
# Point to the dataset location
data_dir = 'pic_source'
# The number of classes
num_classes = 2
# Validation split
val_sp = 0.3
# ----------That's ALL You Need to Do----------
# Generate train dataset
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=val_sp,
subset="training",
seed=123,
image_size=target_size,
batch_size=batch_size)
# Generate validation dataset
val_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=val_sp,
subset="validation",
seed=123,
image_size=target_size,
batch_size=batch_size)
# Standardize the data, make values to be in the [0,1]
normalization_layer = tf.keras.layers.Rescaling(1./255)
normalized_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
image_batch, labels_batch = next(iter(normalized_ds))
# Train a model
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)
])
model.compile(
optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(
train_ds,
validation_data=val_ds,
epochs=1
)
# Save the new trained model as keras format
model.save('model.h5')
# Access the loss and accuracy
loss, accuracy = model.evaluate(val_ds)
print(loss,accuracy)
print('Training work has Done!')