126 lines
3.4 KiB
Python
126 lines
3.4 KiB
Python
from os.path import isfile
|
|
|
|
import torch
|
|
from torch import nn
|
|
from torch.utils.data import DataLoader
|
|
from torchvision import datasets
|
|
from torchvision.transforms import ToTensor
|
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
print(f"Using {device} device")
|
|
|
|
|
|
def get_data(batch_size: int = 64):
|
|
# Download training data from open datasets.
|
|
training_data = datasets.CIFAR10(
|
|
root="/home/flifloo/IA/data",
|
|
train=True,
|
|
download=True,
|
|
transform=ToTensor(),
|
|
)
|
|
|
|
# Download test data from open datasets.
|
|
testing_data = datasets.CIFAR10(
|
|
root="/home/flifloo/IA/data",
|
|
train=False,
|
|
download=True,
|
|
transform=ToTensor(),
|
|
)
|
|
|
|
# Create data loaders.
|
|
train_dataloader = DataLoader(training_data, batch_size=batch_size, shuffle=True)
|
|
test_dataloader = DataLoader(testing_data, batch_size=batch_size, shuffle=True)
|
|
|
|
return train_dataloader, test_dataloader
|
|
|
|
|
|
# Define model
|
|
class NeuralNetwork(nn.Module):
|
|
def __init__(self):
|
|
super(NeuralNetwork, self).__init__()
|
|
self.conv_relu_stack = nn.Sequential(
|
|
nn.Conv2d(3, 6, (5, 5)),
|
|
nn.MaxPool2d(2, 2),
|
|
nn.ReLU(),
|
|
nn.Conv2d(6, 16, (5, 5)),
|
|
nn.MaxPool2d(2, 2),
|
|
nn.ReLU(),
|
|
)
|
|
self.linear_relu_stack = nn.Sequential(
|
|
nn.Linear(16*(5**2), 120),
|
|
nn.ReLU(),
|
|
nn.Linear(120, 84),
|
|
nn.ReLU(),
|
|
nn.Linear(84, 10),
|
|
nn.ReLU(),
|
|
)
|
|
|
|
def forward(self, x):
|
|
x = self.conv_relu_stack(x)
|
|
x = x.view(-1, 16 * 5 * 5)
|
|
return self.linear_relu_stack(x)
|
|
|
|
|
|
def train(dataloader, model, loss_fn, optimizer):
|
|
size = len(dataloader.dataset)
|
|
for batch, (X, y) in enumerate(dataloader):
|
|
X, y = X.to(device), y.to(device)
|
|
|
|
# Compute prediction error
|
|
pred = model(X)
|
|
loss = loss_fn(pred, y)
|
|
|
|
# Backpropagation
|
|
optimizer.zero_grad()
|
|
loss.backward()
|
|
optimizer.step()
|
|
|
|
if batch % 100 == 0:
|
|
loss, current = loss.item(), batch * len(X)
|
|
print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
|
|
|
|
|
|
def test(dataloader, model, loss_fn):
|
|
size = len(dataloader.dataset)
|
|
model.eval()
|
|
test_loss, correct = 0, 0
|
|
with torch.no_grad():
|
|
for X, y in dataloader:
|
|
X, y = X.to(device), y.to(device)
|
|
pred = model(X)
|
|
test_loss += loss_fn(pred, y).item()
|
|
correct += (pred.argmax(1) == y).type(torch.float).sum().item()
|
|
test_loss /= size
|
|
correct /= size
|
|
print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
|
|
return correct
|
|
|
|
|
|
def training():
|
|
train_data, test_data = get_data()
|
|
|
|
model = NeuralNetwork().to(device)
|
|
if isfile("model.pth"):
|
|
print("Loading model from save")
|
|
model.load_state_dict(torch.load("model.pth"))
|
|
|
|
print(model)
|
|
|
|
loss_fn = nn.CrossEntropyLoss()
|
|
# lr = sur/sous appretisage
|
|
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3, momentum=.9)
|
|
|
|
e = 0
|
|
c = 0
|
|
while c < 0.90:
|
|
print(f"Epoch {e+1}\n-------------------------------")
|
|
train(train_data, model, loss_fn, optimizer)
|
|
c = test(test_data, model, loss_fn)
|
|
torch.save(model.state_dict(), "model.pth")
|
|
e += 1
|
|
print("Done!")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
training()
|