cifar10-tools 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 George Perdrizet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,35 @@
1
+ Metadata-Version: 2.4
2
+ Name: cifar10_tools
3
+ Version: 0.1.0
4
+ Summary: Tools for training neural networks on the CIFAR-10 task with PyTorch and TensorFlow
5
+ License: GPLv3
6
+ License-File: LICENSE
7
+ Keywords: Python,Machine learning,Deep learning,CNNs,Computer vision,Image classification,CIFAR-10
8
+ Author: gperdrizet
9
+ Author-email: george@perdrizet.org
10
+ Requires-Python: >=3.10,<3.13
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Intended Audience :: Education
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
16
+ Classifier: License :: Other/Proprietary License
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
23
+ Classifier: Topic :: Scientific/Engineering :: Image Recognition
24
+ Provides-Extra: tensorflow
25
+ Requires-Dist: numpy (>=1.24)
26
+ Requires-Dist: torch (>=2.0)
27
+ Requires-Dist: torchvision (>=0.15)
28
+ Project-URL: Documentation, https://gperdrizet.github.io/CIFAR10/README.md
29
+ Project-URL: Homepage, https://github.com/gperdrizet/CIFAR10
30
+ Project-URL: Issues, https://github.com/gperdrizet/CIFAR10/issues
31
+ Project-URL: PyPI, https://pypi.org/project/cifar10_tools
32
+ Project-URL: Repository, https://github.com/gperdrizet/CIFAR10
33
+ Description-Content-Type: text/markdown
34
+
35
+ # PyTorch: CIFAR10 demonstration
@@ -0,0 +1 @@
1
+ # PyTorch: CIFAR10 demonstration
@@ -0,0 +1,44 @@
1
+ [build-system]
2
+ requires = ["poetry-core>=1.9.0"]
3
+ build-backend = "poetry.core.masonry.api"
4
+
5
+ [tool.poetry]
6
+ name = "cifar10_tools"
7
+ version = "0.1.0"
8
+ description = "Tools for training neural networks on the CIFAR-10 task with PyTorch and TensorFlow"
9
+ authors = ["gperdrizet <george@perdrizet.org>"]
10
+ readme = "README.md"
11
+ license = "GPLv3"
12
+ packages = [{include = "cifar10_tools", from = "src"}]
13
+ exclude = ["notebooks", "models", "logs"]
14
+ classifiers = [
15
+ "Development Status :: 3 - Alpha",
16
+ "Intended Audience :: Developers",
17
+ "Intended Audience :: Education",
18
+ "Intended Audience :: Science/Research",
19
+ "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
20
+ "Operating System :: OS Independent",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.10",
23
+ "Programming Language :: Python :: 3.11",
24
+ "Programming Language :: Python :: 3.12",
25
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
26
+ "Topic :: Scientific/Engineering :: Image Recognition",
27
+ ]
28
+ keywords = ["Python", "Machine learning", "Deep learning", "CNNs", "Computer vision", "Image classification", "CIFAR-10"]
29
+
30
+ [tool.poetry.dependencies]
31
+ python = ">=3.10,<3.13"
32
+ torch = ">=2.0"
33
+ torchvision = ">=0.15"
34
+ numpy = ">=1.24"
35
+
36
+ [tool.poetry.extras]
37
+ tensorflow = ["tensorflow"]
38
+
39
+ [tool.poetry.urls]
40
+ Homepage = "https://github.com/gperdrizet/CIFAR10"
41
+ Documentation = "https://gperdrizet.github.io/CIFAR10/README.md"
42
+ Repository = "https://github.com/gperdrizet/CIFAR10"
43
+ Issues = "https://github.com/gperdrizet/CIFAR10/issues"
44
+ PyPI = "https://pypi.org/project/cifar10_tools"
File without changes
@@ -0,0 +1,27 @@
1
+ '''Data download function for CIFAR-10 dataset. Use to pre-download data
2
+ during devcontainer creation'''
3
+
4
+ from pathlib import Path
5
+ from torchvision import datasets
6
+
7
+ def download_cifar10_data(data_dir: str='data/pytorch/CIFAR10'):
8
+ '''Download CIFAR-10 dataset using torchvision.datasets.'''
9
+
10
+ data_dir = Path(data_dir)
11
+ data_dir.mkdir(parents=True, exist_ok=True)
12
+
13
+ _ = datasets.CIFAR10(
14
+ root=data_dir,
15
+ train=True,
16
+ download=True
17
+ )
18
+
19
+ _ = datasets.CIFAR10(
20
+ root=data_dir,
21
+ train=False,
22
+ download=True
23
+ )
24
+
25
+ if __name__ == '__main__':
26
+
27
+ download_cifar10_data()
@@ -0,0 +1,38 @@
1
+ '''Evaluation functions for models.'''
2
+
3
+ import numpy as np
4
+ import torch
5
+ import torch.nn as nn
6
+ from torch.utils.data import DataLoader
7
+
8
+
9
+ def evaluate_model(
10
+ model: nn.Module,
11
+ test_loader: DataLoader
12
+ ) -> tuple[float, np.ndarray, np.ndarray]:
13
+ '''Evaluate model on test set.
14
+
15
+ Note: Assumes data is already on the correct device.
16
+ '''
17
+
18
+ model.eval()
19
+ correct = 0
20
+ total = 0
21
+ all_predictions = []
22
+ all_labels = []
23
+
24
+ with torch.no_grad():
25
+
26
+ for images, labels in test_loader:
27
+
28
+ outputs = model(images)
29
+ _, predicted = torch.max(outputs.data, 1)
30
+
31
+ total += labels.size(0)
32
+ correct += (predicted == labels).sum().item()
33
+
34
+ all_predictions.extend(predicted.cpu().numpy())
35
+ all_labels.extend(labels.cpu().numpy())
36
+
37
+ accuracy = 100 * correct / total
38
+ return accuracy, np.array(all_predictions), np.array(all_labels)
@@ -0,0 +1,93 @@
1
+ '''Training functions for models.'''
2
+
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.optim as optim
6
+ from torch.utils.data import DataLoader
7
+
8
+ def train_model(
9
+ model: nn.Module,
10
+ train_loader: DataLoader,
11
+ val_loader: DataLoader,
12
+ criterion: nn.Module,
13
+ optimizer: optim.Optimizer,
14
+ epochs: int = 10,
15
+ print_every: int = 1
16
+ ) -> dict[str, list[float]]:
17
+ '''Training loop for PyTorch classification model.
18
+
19
+ Note: Assumes data is already on the correct device.
20
+ '''
21
+
22
+ history = {'train_loss': [], 'val_loss': [], 'train_accuracy': [], 'val_accuracy': []}
23
+
24
+ for epoch in range(epochs):
25
+
26
+ # Training phase
27
+ model.train()
28
+ running_loss = 0.0
29
+ correct = 0
30
+ total = 0
31
+
32
+ for images, labels in train_loader:
33
+
34
+ # Forward pass
35
+ optimizer.zero_grad()
36
+ outputs = model(images)
37
+ loss = criterion(outputs, labels)
38
+
39
+ # Backward pass
40
+ loss.backward()
41
+ optimizer.step()
42
+
43
+ # Track metrics
44
+ running_loss += loss.item()
45
+ _, predicted = torch.max(outputs.data, 1)
46
+ total += labels.size(0)
47
+ correct += (predicted == labels).sum().item()
48
+
49
+ # Calculate training metrics
50
+ train_loss = running_loss / len(train_loader)
51
+ train_accuracy = 100 * correct / total
52
+
53
+ # Validation phase
54
+ model.eval()
55
+ val_running_loss = 0.0
56
+ val_correct = 0
57
+ val_total = 0
58
+
59
+ with torch.no_grad():
60
+
61
+ for images, labels in val_loader:
62
+
63
+ outputs = model(images)
64
+ loss = criterion(outputs, labels)
65
+
66
+ val_running_loss += loss.item()
67
+ _, predicted = torch.max(outputs.data, 1)
68
+ val_total += labels.size(0)
69
+ val_correct += (predicted == labels).sum().item()
70
+
71
+ val_loss = val_running_loss / len(val_loader)
72
+ val_accuracy = 100 * val_correct / val_total
73
+
74
+ # Record metrics
75
+ history['train_loss'].append(train_loss)
76
+ history['val_loss'].append(val_loss)
77
+ history['train_accuracy'].append(train_accuracy)
78
+ history['val_accuracy'].append(val_accuracy)
79
+
80
+ # Print progress
81
+ if (epoch + 1) % print_every == 0 or epoch == 0:
82
+
83
+ print(
84
+ f'Epoch {epoch+1}/{epochs} - ' +
85
+ f'loss: {train_loss:.4f} - ' +
86
+ f'accuracy: {train_accuracy:.2f}% - ' +
87
+ f'val_loss: {val_loss:.4f} - ' +
88
+ f'val_accuracy: {val_accuracy:.2f}%'
89
+ )
90
+
91
+ print('\nTraining complete.')
92
+
93
+ return history