-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.py
More file actions
142 lines (90 loc) · 3.27 KB
/
Copy pathnet.py
File metadata and controls
142 lines (90 loc) · 3.27 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
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
import torch
import torchvision
import matplotlib.pyplot as plt
from torch import nn
from torch.utils.data import DataLoader
from torchvision import transforms
from tqdm import tqdm_notebook
import random
import numpy as np
def seed_everything(seed=42):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
seed_everything(42)
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5))])
trainset = torchvision.datasets.CIFAR10(root="./data", train=True, transform=transform, download=True)
train = DataLoader(trainset, shuffle=True, batch_size=128, num_workers=2)
testset = torchvision.datasets.CIFAR10(root="./data", train=False, transform=transform, download=True)
test = DataLoader(testset, shuffle=False, batch_size=128, num_workers=2)
#classes = trainset.classes
classes = ('airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
print(classes)
value = next(iter(test))[0]
class MyNet(nn.Module):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.conv1 = nn.Conv2d(in_channels=3, out_channels=128, kernel_size=5)
self.pool = nn.MaxPool2d(kernel_size=2,stride=2)
self.conv2 = nn.Conv2d(in_channels=128, out_channels=512, kernel_size=3)
self.fc1 = nn.Linear(512*6*6,256)
self.fc2 = nn.Linear(256, 128)
self.fc3 = nn.Linear(128, 10)
def forward(self, x):
x = self.pool(nn.functional.relu(self.conv1(x)))
x = self.pool(nn.functional.relu(self.conv2(x)))
#print(x.shape)
x = x.view(-1, 512*6*6)
x = nn.functional.relu(self.fc1(x))
x = nn.functional.relu(self.fc2(x))
x = self.fc3(x)
return x
device = "cuda" if torch.cuda.is_available() else "cpu"
net = MyNet().to(device)
optimizer = torch.optim.Adam(net.parameters(), lr=1e-3)
loss_fn = nn.CrossEntropyLoss()
net(value.to(device))
loss_values = []
for epoch in tqdm_notebook(range(10)):
loss_sum = 0
for i, batch in enumerate(tqdm_notebook(train)):
x_batch, y_batch = batch
x_batch = x_batch.to(device)
y_batch = y_batch.to(device)
optimizer.zero_grad()
y_pred = net(x_batch)
loss = loss_fn(y_pred, y_batch)
loss_sum += loss.item()
loss.backward()
optimizer.step()
loss_values.append(loss_sum/128)
print(f"epoch {epoch} loss: {loss_values[-1]:.2f}")
plt.plot(loss_values)
plt.show()
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
with torch.no_grad():
for data in test:
images, labels = data
y_pred = net(images.to(device))#.view(4, -1))
_, predicted = torch.max(y_pred, 1)
c = (predicted.cpu().detach() == labels).squeeze()
for i in range(128):
try:
label = labels[i]
class_correct[label] += c[i].item()
class_total[label] += 1
except:
continue
k=0
for i in range(10):
print('Accuracy of %5s : %2d %%' % (
classes[i], 100 * class_correct[i] / class_total[i]))
k += 100 * class_correct[i] / class_total[i]
print(f"Average Accuracy: {k/10:.2f}%")