cifar10-tools 0.1.0__py3-none-any.whl
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.
- cifar10_tools/__init__.py +0 -0
- cifar10_tools/pytorch/__init__.py +0 -0
- cifar10_tools/pytorch/data.py +27 -0
- cifar10_tools/pytorch/evaluation.py +38 -0
- cifar10_tools/pytorch/training.py +93 -0
- cifar10_tools/tensorflow/__init__.py +0 -0
- cifar10_tools-0.1.0.dist-info/METADATA +35 -0
- cifar10_tools-0.1.0.dist-info/RECORD +10 -0
- cifar10_tools-0.1.0.dist-info/WHEEL +4 -0
- cifar10_tools-0.1.0.dist-info/licenses/LICENSE +21 -0
|
File without changes
|
|
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
|
|
File without changes
|
|
@@ -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,10 @@
|
|
|
1
|
+
cifar10_tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
cifar10_tools/pytorch/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
cifar10_tools/pytorch/data.py,sha256=zEDdRbcCHehDg5mdOGDopKT-uCRTjF27Q_UYTAPVEhQ,626
|
|
4
|
+
cifar10_tools/pytorch/evaluation.py,sha256=i4tRYOqWATVqQVkWT_fATWRbzo9ziX2DDkXKPaiQlFE,923
|
|
5
|
+
cifar10_tools/pytorch/training.py,sha256=Sg6NlBT_DTyLzf-Ls3bYI8-8AwGFJblRj0MDnUmGP3Q,2642
|
|
6
|
+
cifar10_tools/tensorflow/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
cifar10_tools-0.1.0.dist-info/METADATA,sha256=3wdozzaT9e9M6Tf5c7EbiJ8XVXewrJiXTHxGQxhMJ0Q,1580
|
|
8
|
+
cifar10_tools-0.1.0.dist-info/WHEEL,sha256=kJCRJT_g0adfAJzTx2GUMmS80rTJIVHRCfG0DQgLq3o,88
|
|
9
|
+
cifar10_tools-0.1.0.dist-info/licenses/LICENSE,sha256=wtHfRwmCF5-_XUmYwrBKwJkGipvHVmh7GXJOKKeOe2U,1073
|
|
10
|
+
cifar10_tools-0.1.0.dist-info/RECORD,,
|
|
@@ -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.
|