-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkan-lenet.py
136 lines (110 loc) · 3.76 KB
/
kan-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
from efficient_kan import KAN
# Train on MNIST
import cv2 as cv
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from tqdm import tqdm
# Load MNIST
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]
)
trainset = torchvision.datasets.MNIST(
root="./data", train=True, download=True, transform=transform
)
valset = torchvision.datasets.MNIST(
root="./data", train=False, download=True, transform=transform
)
trainloader = DataLoader(trainset, batch_size=64, shuffle=True)
valloader = DataLoader(valset, batch_size=64, shuffle=False)
# functions to show an image
def imshow(img):
npimg = img.numpy()
npimg = np.transpose(npimg, (1, 2, 0))
cv.imshow('image',npimg)
cv.waitKey(0)
cv.destroyAllWindows()
#show some images
images, labels = next(iter(trainloader))
imshow(torchvision.utils.make_grid(images))
#model with only convolution layers
class LeNetKAN(nn.Module):
def __init__(self):
super().__init__()
self.feature = nn.Sequential(
#1
nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, stride=1, padding=2), # 28*28->32*32-->28*28
nn.Tanh(),
nn.AvgPool2d(kernel_size=2, stride=2), # 14*14
#2
nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5, stride=1), # 10*10
nn.Tanh(),
nn.AvgPool2d(kernel_size=2, stride=2), # 5*5
)
self.classifier = nn.Sequential(
nn.Flatten(),
KAN([16*5*5, 120, 84, 10])
)
def forward(self, x):
return self.classifier(self.feature(x))
# Define model
model = LeNetKAN()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
# Define optimizer
optimizer = optim.AdamW(model.parameters(), lr=1e-3, weight_decay=1e-4)
# Define learning rate scheduler
scheduler = optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.8)
# Define loss
criterion = nn.CrossEntropyLoss()
for epoch in range(10):
# Train
model.train()
with tqdm(trainloader) as pbar:
for i, (images, labels) in enumerate(pbar):
images = images.view(-1, 1, 28, 28).to(device)
optimizer.zero_grad()
output = model(images)
loss = criterion(output, labels.to(device))
loss.backward()
optimizer.step()
accuracy = (output.argmax(dim=1) == labels.to(device)).float().mean()
pbar.set_postfix(loss=loss.item(), accuracy=accuracy.item(), lr=optimizer.param_groups[0]['lr'])
# Validation
model.eval()
val_loss = 0
val_accuracy = 0
with torch.no_grad():
for images, labels in valloader:
images = images.view(-1, 1, 28, 28).to(device)
output = model(images)
val_loss += criterion(output, labels.to(device)).item()
val_accuracy += (
(output.argmax(dim=1) == labels.to(device)).float().mean().item()
)
val_loss /= len(valloader)
val_accuracy /= len(valloader)
# Update learning rate
scheduler.step()
print(
f"Epoch {epoch + 1}, Val Loss: {val_loss}, Val Accuracy: {val_accuracy}"
)
for images, labels in valloader:
images = images.view(-1, 1, 28, 28).to(device)
output = model(images)
print("output")
print(output)
print(output.shape)
mylabels = labels.to(device)
print("labels")
print(mylabels)
print(mylabels.shape)
break
# Export to ONNX
dummy_input = torch.randn(1, 1, 28, 28, device=device)
onnx_program = torch.onnx.dynamo_export(model, dummy_input)
onnx_program.save("kanocr.onnx")