-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn.py
100 lines (89 loc) · 3.55 KB
/
cnn.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
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torch.nn.functional as F
import numpy as np
import torch.utils.data as td
import random, time
# Define hyperparameters
batch_size = 64
test_batch_size = 64
epochs = 40
size_input = 3072
eta = 0.01
momentum = 0.9
# Import data
def cifar_loaders(batch_size, shuffle_test=False):
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.225, 0.225, 0.225])
train = datasets.CIFAR10('./', train=True, download=True,
transform=transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32, 4),
transforms.ToTensor(),
normalize,
]))
test = datasets.CIFAR10('./', train=False,
transform=transforms.Compose([transforms.ToTensor(), normalize]))
train_loader = torch.utils.data.DataLoader(train, batch_size=batch_size,
shuffle=True, pin_memory=True)
test_loader = torch.utils.data.DataLoader(test, batch_size=batch_size,
shuffle=shuffle_test, pin_memory=True)
return train_loader, test_loader
train_loader, _ = cifar_loaders(batch_size)
_, test_loader = cifar_loaders(test_batch_size)
class CNN_ReLu(nn.Module):
def __init__(self):
super(CNN_ReLu, self).__init__()
self.conv1 = nn.Conv2d(3,64,kernel_size=5,stride=1,padding=2)
self.conv2 = nn.Conv2d(64,256,kernel_size=4,stride=2,padding=0)
self.conv3 = nn.Conv2d(256,512, kernel_size=3,stride=2,padding=0)
self.conv4 = nn.Conv2d(512,1024, kernel_size=5,stride=2,padding=0)
self.fc1 = nn.Linear(1024*2*2,4096)
self.fc2 = nn.Linear(4096,4096)
self.fc3 = nn.Linear(4096,10)
def forward(self,x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = x.view(-1,1024*2*2)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
model = CNN_ReLu()
use_cuda = True
if use_cuda and torch.cuda.is_available():
model.cuda()
crit = nn.CrossEntropyLoss()
optim = torch.optim.SGD(model.parameters(), lr = eta, momentum=momentum)
for epoch in range(epochs):
total_loss = 0
for i, (images, labels) in enumerate(train_loader):
if use_cuda and torch.cuda.is_available():
images = images.cuda()
labels = labels.cuda()
optim.zero_grad()
train_output = model(images)
loss = crit(train_output,labels)
loss.backward()
optim.step()
total_loss=total_loss+loss
num_batches = i+1
epoch_loss = total_loss / num_batches
print('Epoch: (%d/%d), Loss = %.4f' % (epoch + 1, epochs, epoch_loss))
accurate = 0.
tot = 0.
for images, labels in test_loader:
if use_cuda and torch.cuda.is_available():
images = images.cuda()
labels = labels.cuda()
test_output = model(images)
_, prediction=torch.max(test_output,1)
accurate += (prediction == labels).sum()
tot += images.shape[0]
print('Accuracy of the model on the test images: %f %%' % (100 * (accurate.float() / tot)))