-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelLearning.py
More file actions
74 lines (37 loc) · 1.03 KB
/
modelLearning.py
File metadata and controls
74 lines (37 loc) · 1.03 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
base_dir = './dataset'
train_dir = f'{base_dir}/train'
val_dir = f'{base_dir}/val'
test_dir = f'{base_dir}/test'
batch_size = 32
train_ds = tf.keras.utils.image_dataset_from_directory(
train_dir,
label_mode='categorical',
image_size=(224, 224),
batch_size=batch_size,
shuffle=True
)
val_ds = tf.keras.utils.image_dataset_from_directory(
val_dir,
label_mode='categorical',
image_size=(224, 224),
batch_size=batch_size,
shuffle=True
)
test_ds = tf.keras.utils.image_dataset_from_directory(
test_dir,
label_mode='categorical',
image_size=(224, 224),
batch_size=batch_size,
shuffle=False
)
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
test_ds = test_ds.cache().prefetch(buffer_size=AUTOTUNE)
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=5
)
test_loss, test_accuracy = model.evaluate(test_ds)
print(f"Test accuracy: {test_accuracy:.2f}")