-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
104 lines (74 loc) · 3.22 KB
/
train.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
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
### local imports
from models import LeNet5, LeNet5v2, LeNet5v2b
###
# setup train & test data (loaders)
# Define the transform for MNIST data (resize and normalize)
transform = transforms.Compose([
transforms.Resize((32, 32)), # Resize images to 32x32 (LeNet-5 requirement)
transforms.ToTensor(), # Convert images to tensors
transforms.Normalize((0.5,), (0.5,)) # Normalize the images
])
# Load the MNIST dataset
train_dataset = datasets.MNIST(root='./data', train=True, transform=transform)
test_dataset = datasets.MNIST(root='./data', train=False, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=1000, shuffle=False)
print( len(train_loader), len(test_loader))
# train - 60000 images in 938 batches (of 64)
# test 10000 images in 10 batches (of 1000)
# Create the model - pick one of LeNet5, LetNet5v2, LeNet5v2b
model = LeNet5()
# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss() # For multi-class classification
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Train the model
num_epochs = 10
eval_iter = 100
for epoch in range(num_epochs):
model.train() # Set the model to training mode
running_loss = 0.0
correct = 0
total = 0
for batch_idx, (inputs, labels) in enumerate(train_loader):
optimizer.zero_grad() # Zero the gradients
# Forward pass
outputs = model(inputs)
# Compute the loss
loss = criterion(outputs, labels)
# Backward pass and optimization
loss.backward()
optimizer.step()
running_loss += loss.item()
# Calculate accuracy
_, predicted = torch.max(outputs, dim=1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
if batch_idx % eval_iter == 0:
print( f"{epoch+1}/{num_epochs} - batch {batch_idx}/{len(train_loader)} - loss {loss.item()} - running_loss {running_loss/(batch_idx+1)}, total {total}")
# Print statistics after each epoch
train_loss = running_loss / len(train_loader)
train_accuracy = 100 * correct / total
print(f"\n==> Epoch [{epoch+1}/{num_epochs}], Loss: {train_loss:.4f}, Accuracy: {train_accuracy:.2f}%")
# Evaluate on the test set after each epoch
model.eval() # Set the model to evaluation mode
correct = 0
total = 0
with torch.no_grad(): # No need to calculate gradients during evaluation
for inputs, labels in test_loader:
outputs = model(inputs)
_, predicted = torch.max(outputs, dim=1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
test_accuracy = 100 * correct / total
print(f"Test Accuracy: {test_accuracy:.2f}%\n")
# ==> Epoch [10/10], Loss: 0.0159, Accuracy: 99.52%
# Test Accuracy: 98.59%
# Save the trained model
torch.save(model.state_dict(), './lenet5.pth')
## about 250 154 bytes - lenet5.pth
print("bye")