-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign_langugae_with_convolution.py
128 lines (101 loc) · 3.34 KB
/
sign_langugae_with_convolution.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import torch.nn as nn
import pandas as pd
import torch
from torch.optim import Adam
from torch.utils.data import Dataset, DataLoader
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.cuda.is_available()
train_df = pd.read_csv("data/asl_data/sign_mnist_train.csv")
valid_df = pd.read_csv("data/asl_data/sign_mnist_valid.csv")
sample_df = train_df.head().copy() # Grab the top 5 rows
sample_df.pop('label')
sample_x = sample_df.values
IMG_HEIGHT = 28
IMG_WIDTH = 28
IMG_CHS = 1
sample_x = sample_x.reshape(-1, IMG_CHS, IMG_HEIGHT, IMG_WIDTH)
sample_x.shape
class MyDataset(Dataset):
def __init__(self, base_df):
x_df = base_df.copy() # Some operations below are in-place
y_df = x_df.pop('label')
x_df = x_df.values / 255 # Normalize values from 0 to 1
x_df = x_df.reshape(-1, IMG_CHS, IMG_WIDTH, IMG_HEIGHT)
self.xs = torch.tensor(x_df).float().to(device)
self.ys = torch.tensor(y_df).to(device)
def __getitem__(self, idx):
x = self.xs[idx]
y = self.ys[idx]
return x, y
def __len__(self):
return len(self.xs)
BATCH_SIZE = 32
train_data = MyDataset(train_df)
train_loader = DataLoader(train_data, batch_size=BATCH_SIZE, shuffle=True)
train_N = len(train_loader.dataset)
valid_data = MyDataset(valid_df)
valid_loader = DataLoader(valid_data, batch_size=BATCH_SIZE)
valid_N = len(valid_loader.dataset)
batch = next(iter(train_loader))
n_classes = 24
kernel_size = 3
flattened_img_size = 75 * 3 * 3
model = nn.Sequential(
# First convolution
nn.Conv2d(IMG_CHS, 25, kernel_size, stride=1, padding=1), # 25 x 28 x 28
nn.BatchNorm2d(25),
nn.ReLU(),
nn.MaxPool2d(2, stride=2), # 25 x 14 x 14
# Second convolution
nn.Conv2d(25, 50, kernel_size, stride=1, padding=1), # 50 x 14 x 14
nn.BatchNorm2d(50),
nn.ReLU(),
nn.Dropout(.2),
nn.MaxPool2d(2, stride=2), # 50 x 7 x 7
# Third convolution
nn.Conv2d(50, 75, kernel_size, stride=1, padding=1), # 75 x 7 x 7
nn.BatchNorm2d(75),
nn.ReLU(),
nn.MaxPool2d(2, stride=2), # 75 x 3 x 3
# Flatten to Dense
nn.Flatten(),
nn.Linear(flattened_img_size, 512),
nn.Dropout(.3),
nn.ReLU(),
nn.Linear(512, n_classes)
)
model = torch.compile(model.to(device))
loss_function = nn.CrossEntropyLoss()
optimizer = Adam(model.parameters())
def get_batch_accuracy(output, y, N):
pred = output.argmax(dim=1, keepdim=True)
correct = pred.eq(y.view_as(pred)).sum().item()
return correct / N
def validate():
loss = 0
accuracy = 0
model.eval()
with torch.no_grad():
for x, y in valid_loader:
output = model(x)
loss += loss_function(output, y).item()
accuracy += get_batch_accuracy(output, y, valid_N)
print('Valid - Loss: {:.4f} Accuracy: {:.4f}'.format(loss, accuracy))
def train():
loss = 0
accuracy = 0
model.train()
for x, y in train_loader:
output = model(x)
optimizer.zero_grad()
batch_loss = loss_function(output, y)
batch_loss.backward()
optimizer.step()
loss += batch_loss.item()
accuracy += get_batch_accuracy(output, y, train_N)
print('Train - Loss: {:.4f} Accuracy: {:.4f}'.format(loss, accuracy))
epochs = 20
for epoch in range(epochs):
print('Epoch: {}'.format(epoch))
train()
validate()