Skip to content

Commit 6f2c8a9

Browse files
authored
task3-4
1 parent 689b702 commit 6f2c8a9

File tree

3 files changed

+227
-0
lines changed

3 files changed

+227
-0
lines changed
6.05 KB
Loading
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
import torch
2+
import torch.nn as nn
3+
import torch.nn.functional as F
4+
import torchvision
5+
import torchvision.transforms as transforms
6+
import torch.optim as optim
7+
8+
transform = transforms.Compose(
9+
[
10+
transforms.ToTensor(),
11+
transforms.Normalize(
12+
(0.5, 0.5, 0.5),
13+
(0.5, 0.5, 0.5)
14+
)
15+
]
16+
)
17+
18+
trainset = torchvision.datasets.CIFAR10(
19+
root='./data',
20+
train=True,
21+
download=False,
22+
transform=transform
23+
)
24+
25+
testset = torchvision.datasets.CIFAR10(
26+
root='./data',
27+
train=False,
28+
download=False,
29+
transform=transform
30+
)
31+
32+
trainloader = torch.utils.data.DataLoader(
33+
trainset,
34+
batch_size=4,
35+
shuffle=True,
36+
num_workers=2
37+
)
38+
39+
testloader = torch.utils.data.DataLoader(
40+
testset,
41+
batch_size=4,
42+
shuffle=False,
43+
num_workers=2
44+
)
45+
46+
classes = (
47+
'plane', 'car', 'bird', 'cat',
48+
'deer', 'dog', 'frog', 'horse', 'ship', 'truck'
49+
)
50+
51+
class Net(nn.Module):
52+
def __init__(self):
53+
super(Net, self).__init__()
54+
self.conv1 = nn.Conv2d(3, 6, 5)
55+
self.pool = nn.MaxPool2d(2, 2)
56+
self.conv2 = nn.Conv2d(6, 16, 5)
57+
self.fc1 = nn.Linear(16 * 5 * 5, 120)
58+
self.fc2 = nn.Linear(120, 84)
59+
self.fc3 = nn.Linear(84, 10)
60+
61+
def forward(self, x):
62+
x = self.pool(F.relu(self.conv1(x)))
63+
x = self.pool(F.relu(self.conv2(x)))
64+
x = x.view(-1, 16 * 5 * 5)
65+
x = F.relu(self.fc1(x))
66+
x = F.relu(self.fc2(x))
67+
x = self.fc3(x)
68+
return x
69+
70+
71+
net = Net()
72+
73+
loss_function = nn.CrossEntropyLoss()
74+
optimizer = optim.SGD(
75+
net.parameters(),
76+
lr=0.001
77+
)
78+
79+
for epoch in range(2):
80+
running_loss = 0.0
81+
for i, data in enumerate(trainloader, 0):
82+
# data = (inputs, labels)
83+
inputs, labels = data
84+
optimizer.zero_grad()
85+
86+
outputs = net(inputs)
87+
loss = loss_function(outputs, labels)
88+
loss.backward()
89+
optimizer.step()
90+
91+
running_loss = running_loss + loss.item()
92+
if i % 2000 == 1999:
93+
print(
94+
'[%d, %5d] loss: %.3f' %
95+
(epoch + 1, i+1, running_loss/2000)
96+
)
97+
running_loss = 0.0
98+
print("vola")
99+
100+
'''
101+
OUTPUT:
102+
number of output channels=16, kernel size=5, learning rate=0.001 epoch=2
103+
final loss ===========>[2, 12000] loss: 1.811
104+
Test Accuracy of plane: 50% (502/1000)
105+
Test Accuracy of car: 49% (495/1000)
106+
Test Accuracy of bird: 15% (157/1000)
107+
Test Accuracy of cat: 15% (151/1000)
108+
Test Accuracy of deer: 36% (362/1000)
109+
Test Accuracy of dog: 22% (222/1000)
110+
Test Accuracy of frog: 49% (495/1000)
111+
Test Accuracy of horse: 34% (348/1000)
112+
Test Accuracy of ship: 52% (520/1000)
113+
Test Accuracy of truck: 42% (426/1000)
114+
115+
Test Accuracy (Overall): 36% (3678/10000)
116+
117+
number of output channels=16, kernel size=5, learning rate=0.01 epoch=2
118+
final loss ===========>[2, 12000] loss: 1.255
119+
120+
Test Accuracy of plane: 60% (609/1000)
121+
Test Accuracy of car: 71% (719/1000)
122+
Test Accuracy of bird: 45% (453/1000)
123+
Test Accuracy of cat: 44% (445/1000)
124+
Test Accuracy of deer: 46% (469/1000)
125+
Test Accuracy of dog: 42% (423/1000)
126+
Test Accuracy of frog: 68% (681/1000)
127+
Test Accuracy of horse: 65% (659/1000)
128+
Test Accuracy of ship: 66% (660/1000)
129+
Test Accuracy of truck: 34% (344/1000)
130+
131+
Test Accuracy (Overall): 54% (5462/10000)
132+
133+
number of output channels=16, kernel size=3 learning rate=0.01 epoch=2
134+
final loss ===========>[2, 12000] loss: 1.270
135+
Test Accuracy of plane: 63% (632/1000)
136+
Test Accuracy of car: 74% (747/1000)
137+
Test Accuracy of bird: 41% (419/1000)
138+
Test Accuracy of cat: 36% (366/1000)
139+
Test Accuracy of deer: 61% (610/1000)
140+
Test Accuracy of dog: 30% (306/1000)
141+
Test Accuracy of frog: 74% (740/1000)
142+
Test Accuracy of horse: 62% (624/1000)
143+
Test Accuracy of ship: 62% (621/1000)
144+
Test Accuracy of truck: 49% (491/1000)
145+
146+
Test Accuracy (Overall): 55% (5556/10000)
147+
148+
number of output channels=32, kernel size=3 learning rate=0.01 epoch=2
149+
final loss ===========>[2, 12000] loss: 1.170
150+
Test Accuracy of plane: 64% (644/1000)
151+
Test Accuracy of car: 67% (673/1000)
152+
Test Accuracy of bird: 37% (376/1000)
153+
Test Accuracy of cat: 59% (593/1000)
154+
Test Accuracy of deer: 60% (603/1000)
155+
Test Accuracy of dog: 38% (381/1000)
156+
Test Accuracy of frog: 70% (706/1000)
157+
Test Accuracy of horse: 64% (646/1000)
158+
Test Accuracy of ship: 70% (707/1000)
159+
Test Accuracy of truck: 72% (728/1000)
160+
161+
Test Accuracy (Overall): 60% (6057/10000)
162+
163+
number of output channels=32, kernel size=3 learning rate=0.01 epoch=4
164+
final loss ===========>[[4, 12000] loss: 0.966
165+
166+
number of output channels=32, kernel size=3 learning rate=0.01 epoch=4 batch 5
167+
final loss ===========>[[6, 10000] loss: 0.799
168+
Test Accuracy of plane: 60% (606/1000)
169+
Test Accuracy of car: 83% (831/1000)
170+
Test Accuracy of bird: 49% (498/1000)
171+
Test Accuracy of cat: 38% (385/1000)
172+
Test Accuracy of deer: 71% (712/1000)
173+
Test Accuracy of dog: 69% (690/1000)
174+
Test Accuracy of frog: 73% (733/1000)
175+
Test Accuracy of horse: 68% (682/1000)
176+
Test Accuracy of ship: 80% (808/1000)
177+
Test Accuracy of truck: 65% (651/1000)
178+
179+
Test Accuracy (Overall): 65% (6596/10000)
180+
181+
number of output channels=32, kernel size=3 learning rate=0.01 epoch=4 batch 5 with padding
182+
final loss ===========>[6, 10000] loss: 0.718
183+
Test Accuracy of plane: 68% (689/1000)
184+
Test Accuracy of car: 64% (645/1000)
185+
Test Accuracy of bird: 57% (573/1000)
186+
Test Accuracy of cat: 48% (481/1000)
187+
Test Accuracy of deer: 58% (588/1000)
188+
Test Accuracy of dog: 63% (633/1000)
189+
Test Accuracy of frog: 83% (838/1000)
190+
Test Accuracy of horse: 65% (653/1000)
191+
Test Accuracy of ship: 82% (829/1000)
192+
Test Accuracy of truck: 75% (755/1000)
193+
194+
Test Accuracy (Overall): 66% (6684/10000)
195+
'''
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
from torchvision import transforms
4+
from PIL import Image
5+
6+
import torchvision.transforms.functional as F
7+
import torch
8+
9+
10+
11+
transform = transforms.Compose([
12+
transforms.Resize(255),
13+
transforms.CenterCrop(224),
14+
transforms.ColorJitter(brightness=1, contrast=1, saturation=0, hue=0),
15+
transforms.RandomVerticalFlip(),
16+
transforms.ToTensor(),
17+
transforms.Normalize(
18+
(0.5, 0.5, 0.5),
19+
(0.5, 0.5, 0.5)
20+
),
21+
22+
23+
])
24+
25+
26+
img=Image.open('image/index.jpeg')
27+
28+
img = transform(img)
29+
30+
31+
a = F.to_pil_image(img)
32+
a.show()

0 commit comments

Comments
 (0)