forked from awslabs/ratex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlenet.py
166 lines (145 loc) · 5.38 KB
/
lenet.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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import copy
import torch
import ratex
import ratex.lazy_tensor_core.debug.metrics as metrics
import ratex.lazy_tensor_core.core.lazy_model as lm
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import lr_scheduler
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import time
import os
import copy
class TorchLeNet(nn.Module):
def __init__(self, input_shape=28, num_classes=10):
super(TorchLeNet, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, padding=2, bias=False)
self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5, bias=False)
self.linear1 = nn.Linear(((input_shape // 2 - 4) // 2) ** 2 * 16, 120)
self.linear2 = nn.Linear(120, 84)
self.linear3 = nn.Linear(84, num_classes)
def forward(self, x):
out = self.conv1(x)
out = torch.relu(out)
out = F.avg_pool2d(out, (2, 2), (2, 2))
out = self.conv2(out)
out = torch.relu(out) # pylint: disable=no-member
out = F.avg_pool2d(out, (2, 2), (2, 2))
out = torch.flatten(out, 1) # pylint: disable=no-member
out = self.linear1(out)
out = self.linear2(out)
out = self.linear3(out)
return out
def train(device, model, image_datasets):
dataloaders = {
x: torch.utils.data.DataLoader(
image_datasets[x], batch_size=1, shuffle=False, num_workers=1
)
for x in ["train", "val"]
}
dataset_sizes = {x: len(image_datasets[x]) for x in ["train", "val"]}
model.train()
criterion = lambda pred, true: nn.functional.nll_loss(nn.LogSoftmax(dim=-1)(pred), true)
num_epochs = 10
best_acc = 0.0
if device == "lazy":
model = ratex.jit.script(model)
model = model.to(device, dtype=torch.float32)
optimizer = optim.SGD(model.parameters(), lr=0.001)
for epoch in range(num_epochs):
print("Epoch {}/{}".format(epoch, num_epochs - 1))
print("-" * 10)
running_loss = 0.0
running_corrects = 0
# Iterate over data.
for inputs, labels in dataloaders["train"]:
inputs = inputs.to(device)
inputs.requires_grad = True
labels_one_hot = torch.tensor(np.eye(10, dtype=np.float32)[labels])
labels_one_hot = labels_one_hot.to(device) # One-hot
optimizer.zero_grad()
outputs = model(inputs)
# _, preds = torch.max(outputs, 1)
# loss = criterion(outputs, labels)
# adapting loss cacluation from
# https://www.programmersought.com/article/86167037001/
# this doesn't match nn.NLLLoss() exactly, but close...
loss = -torch.sum(outputs * labels_one_hot) / inputs.size(0)
loss.backward()
optimizer.step()
lm.mark_step()
running_loss += loss.item() * inputs.size(0)
# running_corrects += torch.sum(preds == labels.data)
epoch_loss = running_loss / dataset_sizes["train"]
epoch_acc = 0
print("{} Loss: {:.4f}".format("train", epoch_loss))
def infer(device, model, image_datasets):
dataloaders = {
x: torch.utils.data.DataLoader(
image_datasets[x], batch_size=1, shuffle=False, num_workers=1
)
for x in ["train", "val"]
}
dataset_sizes = {x: len(image_datasets[x]) for x in ["train", "val"]}
model.eval()
criterion = lambda pred, true: nn.functional.nll_loss(nn.LogSoftmax(dim=-1)(pred), true)
best_acc = 0.0
if device == "lazy":
model = ratex.jit.script(model)
model = model.to(device)
running_loss = 0.0
running_corrects = 0
# Iterate over data.
for inputs, labels in dataloaders["val"]:
inputs = inputs.to(device)
labels = labels.to(device)
outputs = model(inputs)
# _, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
# statistics
running_loss += loss.item() * inputs.size(0)
# running_corrects += torch.sum(preds == labels.data)
epoch_loss = running_loss / dataset_sizes["val"]
# epoch_acc = running_corrects.double() / dataset_sizes["train"]
epoch_acc = 0
print("{} Loss: {:.4f} Acc: {:.4f}".format("val", epoch_loss, epoch_acc))
def main():
model_mnm = TorchLeNet()
model_cpu = copy.deepcopy(model_mnm)
data_transforms = {
"train": transforms.Compose(
[
transforms.CenterCrop(28),
transforms.ToTensor(),
]
),
"val": transforms.Compose(
[
transforms.CenterCrop(28),
transforms.ToTensor(),
]
),
}
image_datasets = {
x: datasets.FakeData(
size=1, image_size=(1, 28, 28), num_classes=10, transform=data_transforms[x]
)
for x in ["train", "val"]
}
print("raf starts...")
train("lazy", model_mnm, image_datasets)
print("cpu starts...")
train("cpu", model_cpu, image_datasets)
# print("raf starts...")
# infer("raf", model_mnm, image_datasets)
# print("cpu starts...")
# infer("cpu", model_cpu, image_datasets)
# statistics
print(metrics.metrics_report())
if __name__ == "__main__":
main()