-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathANN.py
180 lines (143 loc) · 5.06 KB
/
ANN.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
from typing import Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
import pickle
import time
import numpy as np
import os
DTYPE = torch.float
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
BATCH_SIZE = 64
DATA_PATH = os.getenv('DATA_PATH')
NUM_OUTPUTS = 10 if os.getenv('NUM_OUTPUTS') is None else int(os.getenv('NUM_OUTPUTS'))
loader_g = torch.Generator()
loader_g.manual_seed(2023)
# Define VGG block
def vgg_block(num_convs, in_channels, out_channels):
layers = []
for _ in range(num_convs):
layers += [nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1), nn.BatchNorm2d(out_channels), nn.ReLU(inplace=True)]
in_channels = out_channels
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
return nn.Sequential(*layers)
# Define the VGG11bn model
class VGG11bn(nn.Module):
def __init__(self, num_classes=10):
super(VGG11bn, self).__init__()
self.features = nn.Sequential(
vgg_block(1, 3, 64),
vgg_block(1, 64, 128),
vgg_block(2, 128, 256),
vgg_block(2, 256, 512),
vgg_block(2, 512, 512),
)
self.classifier = nn.Sequential(
nn.Linear(512, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, num_classes),
)
def forward(self, x):
x = self.features(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x
# Data loading
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(
(0.4914, 0.4822, 0.4465),
(0.2023, 0.1994, 0.2010))
])
criterion = nn.CrossEntropyLoss()
# Define Network
def load_model(device=None):
return VGG11bn(num_classes=NUM_OUTPUTS)
def load_train_data(dataset):
return torch.utils.data.DataLoader(
dataset,
batch_size=BATCH_SIZE,
sampler=torch.utils.data.RandomSampler(dataset, generator=loader_g),
drop_last=True
)
def load_val_data(dataset):
return torch.utils.data.DataLoader(
dataset,
batch_size=BATCH_SIZE,
sampler=torch.utils.data.SequentialSampler(dataset),
drop_last=True
)
def load_client_data(node_id: int):
"""Load partition CIFAR10 data."""
with open(DATA_PATH, 'rb') as file:
# Load the data from the file
trainsets, valsets, _ = pickle.load(file)
return load_train_data(trainsets[node_id]), load_val_data(valsets[node_id])
def load_test_data():
"""Load test CIFAR10 data."""
with open(DATA_PATH, 'rb') as file:
# Load the data from the file
_, _, testset = pickle.load(file)
return load_val_data(testset)
def train(model, optimizer, trainloader, device, epoch):
model.train()
train_loss = 0.0
num_processed_samples = 0
start_time = time.time()
for inputs, labels in trainloader:
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
train_loss += loss.item()
batch_size = labels.shape[0]
num_processed_samples += batch_size
print(f'Train: train_loss={train_loss/len(trainloader):.6f}, samples/s={num_processed_samples / (time.time() - start_time):.3f}')
def test(model, data_loader, device):
model.eval()
correct = 0
total = 0
loss = 0
num_processed_samples = 0
start_time = time.time()
with torch.inference_mode():
for inputs, labels in data_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
loss += criterion(outputs, labels)
total += labels.size(0)
correct += (predicted == labels).sum().item()
batch_size = labels.shape[0]
num_processed_samples += batch_size
test_acc = 100 * correct / total
test_loss = float(loss / len(data_loader))
print(f'Test: test_acc={test_acc:.3f}, test_loss={test_loss:.6f}, samples/s={num_processed_samples / (time.time() - start_time):.3f}')
return test_loss, test_acc
def main():
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Centralized PyTorch training")
print("Load data")
trainloader, testloader = load_client_data(0)
net = load_model().to(DEVICE)
net.eval()
optimizer = torch.optim.AdamW(net.parameters(), lr=0.001, weight_decay=0.0)
print("Start training")
train(model=net, optimizer=optimizer, trainloader=trainloader, device=DEVICE, epoch=1)
print("Evaluate model")
loss, accuracy = test(model=net, data_loader=testloader, device=DEVICE)
print("Loss: ", loss)
print("Accuracy: ", accuracy)
if __name__ == "__main__":
main()