This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
densenet.py
56 lines (47 loc) · 1.71 KB
/
densenet.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms, models
from torch.autograd import Variable
import visdom
from se import train_se, train_normal, test_se, test_normal
# variables
cuda = torch.cuda.is_available()
batch_size = 64
# load data
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
train_loader = torch.utils.data.DataLoader(
datasets.CIFAR10('data/cifar10', train=True, download=True,
transform=transform),
batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(
datasets.CIFAR10('data/cifar10', train=False, transform=transform),
batch_size=batch_size, shuffle=True)
class CifarDenseNet(nn.Module):
def __init__(self):
super(CifarDenseNet, self).__init__()
densenet = models.densenet121()
self.dense_head = nn.Sequential(*list(densenet.children())[:-1])
self.dense1 = nn.Linear(1024, 10)
def forward(self, x):
x = self.dense_head(x)
x = x.view(-1, 1024)
x = self.dense1(x)
return F.log_softmax(x)
if __name__ == '__main__':
vis = visdom.Visdom(port=6006)
print("densenet")
model1, model2 = CifarDenseNet(), CifarDenseNet()
if cuda:
model1.cuda()
model2.cuda()
models = train_se(model1, 300, 6, 0.1, train_loader, vis)
print("snapshot ensemble")
test_se(CifarDenseNet, models, 5, test_loader)
print("---")
print("normal way")
normal_model = train_normal(model2, 300, train_loader, vis)
test_normal(normal_model, test_loader)