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
+29 -24
@@ -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,16 +10,17 @@ 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):
@@ -61,7 +59,9 @@ 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-------------------------------")
@@ -70,15 +70,15 @@ class FFNet(nn.Module):
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)
@@ -98,25 +98,27 @@ class LeNet(FFNet):
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()
@@ -126,25 +128,28 @@ if __name__ == "__main__":
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}"')