forked from drakesvoboda/DistributedTrainingExperiments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVHN.py
More file actions
80 lines (68 loc) · 2.5 KB
/
SVHN.py
File metadata and controls
80 lines (68 loc) · 2.5 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms
from torchvision.datasets import SVHN
class DeepModel(nn.Module):
def __init__(self, in_size=2700, out_size=10):
super().__init__()
# 6 hidden layers
self.linear1 = nn.Linear(in_size, 1024)
self.linear2 = nn.Linear(1024, 512)
self.linear3 = nn.Linear(512, 256)
self.linear4 = nn.Linear(256, 128)
self.linear5 = nn.Linear(128, 64)
self.linear6 = nn.Linear(64, 32)
# output layer
self.linear7 = nn.Linear(32, out_size)
def forward(self, xb):
# Flatten the image tensors
xb = xb.view(xb.size(0), -1)
# Get intermediate outputs using hidden layer
out = self.linear1(xb)
out = F.relu(out)
out = self.linear2(out)
out = F.relu(out)
out = self.linear3(out)
out = F.relu(out)
out = self.linear4(out)
out = F.relu(out)
out = self.linear5(out)
out = F.relu(out)
out = self.linear6(out)
out = F.relu(out)
# Get predictions using output layer
out = self.linear7(out)
return out
class ConvNet(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 6, 5, padding=1)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5, padding=1)
self.fc = nn.Linear(576, 64)
self.cls = nn.Linear(64, 10)
def forward(self, x, y=None):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = torch.flatten(x, start_dim=1)
x = F.relu(self.fc(x))
x = self.cls(x)
return x
def get_exec_device():
return torch.device('cpu')
def load_datasets(data_dir):
transform = transforms.Compose([
transforms.CenterCrop([30, 30]),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
extra_dataset = SVHN(root=data_dir, split='extra', download=True, transform=transform)
train_dataset = SVHN(root=data_dir, split='train', download=True, transform=transform)
dataset = torch.utils.data.ConcatDataset([train_dataset, extra_dataset])
val_dataset = SVHN(root=data_dir, split='test', download=True, transform=transform)
val_ds = val_dataset
return dataset, val_ds
def accuracy(outputs, labels):
_, preds = torch.max(outputs, dim=1)
return torch.tensor(torch.sum(preds == labels).item() / len(preds))