something changes

This commit is contained in:
xixu-me committed 2024-12-28 21:13:45 +08:00
1 parent 2c43a000ea
commit 98393b19b3
1 file changed
+39 -34
@@ -1,10 +1,7 @@
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
@@ -13,21 +10,22 @@ class FFNet(nn.Module):
"""
Feedforward neural network, virtual base class
"""
def __init__(self):
super(FFNet, self).__init__()
self.dnn_model = self.build_model()
def build_model(self):
""" build specific model """
"""build specific model"""
raise NotImplementedError
def forward(self, x):
""" feed data forward and return result """
"""feed data forward and return result"""
raise NotImplementedError
def train_model(self, trainloader, testloader, loss_fn, optimizer, num_epochs):
"""Train a model."""
def train_epoch(model, dataloader, loss_fn, optimizer):
"""Train a single epoch"""
num_data = len(dataloader.dataset)
@@ -61,30 +59,32 @@ class FFNet(nn.Module):
correct += (pred.argmax(1) == y).type(torch.float).sum().item()
test_loss /= num_batches
correct /= num_data
print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
print(
f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n"
)
for epoch in range(num_epochs):
print(f"Epoch {epoch+1}\n-------------------------------")
train_epoch(self, trainloader, loss_fn, optimizer)
test_epoch(self, testloader, loss_fn)
print("Done!")
def make_predict(self, images):
""" predict labels for images """
"""predict labels for images"""
self.eval()
with torch.no_grad():
outputs = self(images)
_, predicted = torch.max(outputs.data, 1) # (max, max_indices)
_, predicted = torch.max(outputs.data, 1) # (max, max_indices)
return predicted
def evaluate_model(self, testloader, n):
""" evaluation: return accuracy """
"""evaluation: return accuracy"""
correct = 0
for inputs, labels in testloader:
pred = self.make_predict(inputs)
correct += (pred == labels).sum()
return 100 * correct / n
def predict_one(self, x):
self.eval()
with torch.no_grad():
@@ -96,55 +96,60 @@ class FFNet(nn.Module):
class LeNet(FFNet):
def __init__(self):
super(LeNet, self).__init__()
def build_model(self):
""" build specific model """
"""build specific model"""
raise NotImplementedError
def forward(self, x):
""" feed data forward and return result """
"""feed data forward and return result"""
return self.dnn_model(x)
if __name__ == "__main__":
# load train and test set
trainset = torchvision.datasets.MNIST(
'../data', train=True, download=True, transform=transforms.ToTensor())
"../data", train=True, download=True, transform=transforms.ToTensor()
)
testset = torchvision.datasets.MNIST(
'../data', train=False, download=True, transform=transforms.ToTensor())
print(f'Train #: {len(trainset)}; Test #: {len(testset)}')
"../data", train=False, download=True, transform=transforms.ToTensor()
)
print(f"Train #: {len(trainset)}; Test #: {len(testset)}")
# data iterator
dataiter = iter(torch.utils.data.DataLoader(trainset, batch_size=8, shuffle=False))
images, labels = next(dataiter)
print(f'Labels: {labels}; Batch shape: {images.size()}')
print(f"Labels: {labels}; Batch shape: {images.size()}")
# construct model
model = LeNet()
print(model)
# data loader: easier iteration
BATCH_SIZE = 256
NUM_WORKERS = 4
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=NUM_WORKERS)
trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=NUM_WORKERS
)
testloader = torch.utils.data.DataLoader(
testset, batch_size=BATCH_SIZE, shuffle=False, num_workers=NUM_WORKERS)
testset, batch_size=BATCH_SIZE, shuffle=False, num_workers=NUM_WORKERS
)
# train the model
LR = 0.1
loss_fn = nn.CrossEntropyLoss() # cross entropy
optimizer = torch.optim.SGD(model.parameters(), lr=LR) # SGD
loss_fn = nn.CrossEntropyLoss() # cross entropy
optimizer = torch.optim.SGD(model.parameters(), lr=LR) # SGD
NUM_EPOCH = 5
model.train_model(trainloader, testloader, loss_fn, optimizer, NUM_EPOCH)
# check prediction accuracy
print(f'Labels : {labels}')
print(f'Prediction: {model.make_predict(images)}')
print(f'Accuracy: {model.evaluate_model(testloader, len(testset)):.2f}')
print(f"Labels : {labels}")
print(f"Prediction: {model.make_predict(images)}")
print(f"Accuracy: {model.evaluate_model(testloader, len(testset)):.2f}")
# application: predict hand-written digit
from PIL import Image
image = Image.open('number6c.png')
image = Image.open("number6c.png")
image = transforms.ToTensor()(image).unsqueeze(0)
print(f'loaded image shape: {image.size()}')
print(f"loaded image shape: {image.size()}")
print(f'Predicted: "{model.predict_one(image)}", Actual: "{6}"')