forked from microsoft/ccf-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaper_models.py
More file actions
90 lines (81 loc) · 3.05 KB
/
paper_models.py
File metadata and controls
90 lines (81 loc) · 3.05 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
"""
Model architectures aligned with FL_Clients.ipynb / paper experiments.
MNIST & Fashion-MNIST: 2x Conv + MaxPool CNN.
CIFAR-10: deeper CNN. HAR: MLP on sensor features.
"""
from __future__ import annotations
import numpy as np
import tensorflow as tf
def build_paper_model(dataset: str, num_classes: int = 10) -> tf.keras.Model:
ds = dataset.lower().replace("-", "_")
if ds in ("mnist", "fashion_mnist"):
model = tf.keras.Sequential(
[
tf.keras.layers.Conv2D(
64, kernel_size=3, activation="relu", input_shape=(28, 28, 1)
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(32, kernel_size=3, activation="relu"),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(num_classes, activation="softmax"),
],
name=f"CCFL_CNN_{ds}",
)
model.compile(
optimizer="adam",
loss="categorical_crossentropy",
metrics=["accuracy"],
)
return model
if ds == "cifar10":
model = tf.keras.Sequential(
[
tf.keras.layers.Conv2D(32, 3, activation="relu", input_shape=(32, 32, 3)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(64, 3, activation="relu"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(64, 3, activation="relu"),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(num_classes, activation="softmax"),
],
name="CCFL_CIFAR10_CNN",
)
model.compile(
optimizer="adam",
loss="categorical_crossentropy",
metrics=["accuracy"],
)
return model
if ds == "har":
model = tf.keras.Sequential(
[
tf.keras.layers.Dense(256, activation="relu", input_shape=(561,)),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dense(num_classes, activation="softmax"),
],
name="CCFL_HAR_MLP",
)
model.compile(
optimizer="adam",
loss="categorical_crossentropy",
metrics=["accuracy"],
)
return model
raise ValueError(f"Unknown dataset: {dataset}")
def preprocess_batch(
dataset: str, x: np.ndarray, y: np.ndarray, num_classes: int
) -> tuple[np.ndarray, np.ndarray]:
ds = dataset.lower().replace("-", "_")
x = x.astype(np.float32)
if ds in ("mnist", "fashion_mnist"):
if x.ndim == 3:
x = x[..., np.newaxis]
if ds == "cifar10" and x.ndim == 3:
pass
y_oh = tf.keras.utils.to_categorical(y, num_classes)
return x, y_oh.astype(np.float32)