Spaces:
Sleeping
Sleeping
rzimmerdev
commited on
Commit
•
49b098d
1
Parent(s):
b358118
Main functionality module added: Dataset loading, caching and PyTorch Lightning calls
Browse files- src/main.py +36 -0
src/main.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from torch import nn, optim
|
2 |
+
from torch.utils.data import random_split
|
3 |
+
import pytorch_lightning as pl
|
4 |
+
|
5 |
+
|
6 |
+
from trainer import LitTrainer
|
7 |
+
from models import CNN
|
8 |
+
|
9 |
+
|
10 |
+
def main():
|
11 |
+
from torch.utils.data import DataLoader
|
12 |
+
from src.dataset import DatasetMNIST, load_mnist
|
13 |
+
|
14 |
+
mnist = load_mnist("../downloads/mnist/")
|
15 |
+
dataset, test_data = DatasetMNIST(*mnist["train"]), DatasetMNIST(*mnist["test"])
|
16 |
+
|
17 |
+
train_size = round(len(dataset) * 0.8)
|
18 |
+
validate_size = len(dataset) - train_size
|
19 |
+
train_data, validate_data = random_split(dataset, [train_size, validate_size])
|
20 |
+
|
21 |
+
train_dataloader = DataLoader(train_data, num_workers=6) # My CPU has 8 cores
|
22 |
+
validate_dataloader = DataLoader(validate_data, num_workers=2)
|
23 |
+
test_dataloader = DataLoader(test_data, num_workers=8) # My CPU has 8 cores
|
24 |
+
|
25 |
+
# grayscale channels = 1, mnist num_labels = 10
|
26 |
+
net = CNN(input_channels=1, num_classes=10)
|
27 |
+
|
28 |
+
pl_net = LitTrainer(net, nn.CrossEntropyLoss(), optim.Adam(net.parameters()))
|
29 |
+
trainer = pl.Trainer(limit_train_batches=100, max_epochs=1, default_root_dir="../checkpoints")
|
30 |
+
|
31 |
+
trainer.fit(model=pl_net, train_dataloaders=train_dataloader, val_dataloaders=validate_dataloader)
|
32 |
+
trainer.test(model=pl_net, dataloaders=test_dataloader)
|
33 |
+
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
main()
|