-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyImageClassifier.py
88 lines (77 loc) · 3.01 KB
/
MyImageClassifier.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
import numpy as np
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from PIL import Image
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1=nn.Conv2d(3,6,5)
self.conv2=nn.Conv2d(6,16,5)
self.fc1=nn.Linear(16*5*5,120)
self.fc2=nn.Linear(120,84)
self.fc3=nn.Linear(84,10)
def forward(self, x):
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1,self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x=self.fc3(x)
return x
def num_flat_features(self, x):
size=x.size()[1:]
n_features=1
for i in size:
n_features*=i
return n_features
def train(batch_size, lr=0.001):
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
trainset = torchvision.datasets.CIFAR10(root='F:/CIFAR', train=True, download=False, transform=transform)
trainloader=torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=2)
net=Net()
criterion = nn.CrossEntropyLoss()
# optimizer = optim.SGD(net.parameters(), lr=lr, momentum=0.9)
optimizer = optim.Adam(net.parameters(), lr=lr)
for eppch in range(10):
for i,data in enumerate(trainloader,0):
inputs, labels = data
optimizer.zero_grad()
outputs=net(inputs)
loss=criterion(outputs,labels)
loss.backward()
optimizer.step()
print(eppch,"-",i+1,loss.item())
print("Finishing Training! ")
torch.save(net, "./model_adam.pkl")
def test(batch_size):
net = torch.load('model_adam.pkl')
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
testset = torchvision.datasets.CIFAR10(root='F:/CIFAR', train=False, download=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=True, num_workers=2)
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
total = 0
right = 0
with torch.no_grad():
for data in testloader:
total += batch_size
images, labels = data
outputs = net(images)
rate, index = torch.max(outputs.data, 1)
print("data:")
print(data)
print("outputs")
print(outputs)
print("result:")
print(rate,index)
for i in range(batch_size):
if (labels[i].item() == index[i]):
right += 1
print("accuracy:", round(right / total, 2))
if __name__=='__main__':
BATCHA_SIZE = 50
train(BATCHA_SIZE)
test(BATCHA_SIZE)