christofy 0.0.1__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.
- christofy-0.0.1/LICENSE +21 -0
- christofy-0.0.1/MANIFEST.in +2 -0
- christofy-0.0.1/PKG-INFO +52 -0
- christofy-0.0.1/README.md +15 -0
- christofy-0.0.1/pyproject.toml +23 -0
- christofy-0.0.1/setup.cfg +4 -0
- christofy-0.0.1/src/christofy/__init__.py +2 -0
- christofy-0.0.1/src/christofy/model_trainer.py +122 -0
- christofy-0.0.1/src/christofy/predictor.py +42 -0
- christofy-0.0.1/src/christofy.egg-info/PKG-INFO +52 -0
- christofy-0.0.1/src/christofy.egg-info/SOURCES.txt +11 -0
- christofy-0.0.1/src/christofy.egg-info/dependency_links.txt +1 -0
- christofy-0.0.1/src/christofy.egg-info/top_level.txt +1 -0
christofy-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Aswin Christo
|
|
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.
|
christofy-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: christofy
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A lightweight and modular PyTorch-based image classification package
|
|
5
|
+
Author-email: Aswin Christo <aswinchristo17@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 Aswin Christo
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/aswinchristo/christofy
|
|
29
|
+
Project-URL: Bug Tracker, https://github.com/aswinchristo/christofy/issues
|
|
30
|
+
Classifier: Programming Language :: Python :: 3
|
|
31
|
+
Classifier: Operating System :: OS Independent
|
|
32
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
33
|
+
Requires-Python: >=3.9
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
License-File: LICENSE
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# Christofy 🧠📦
|
|
39
|
+
|
|
40
|
+
**Christofy** is a lightweight and modular PyTorch-based image classification package designed for both training and prediction. Built on top of ResNet18, it supports both binary and multi-class classification with clean abstractions and an easy-to-use API.
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## 🚀 Features
|
|
45
|
+
|
|
46
|
+
- ✅ Train image classifiers using CNN (ResNet18)
|
|
47
|
+
- ✅ Binary and multi-class classification support
|
|
48
|
+
- ✅ Predict the class of a single image using saved model
|
|
49
|
+
- ✅ Modular structure: clean separation between training and prediction
|
|
50
|
+
- ✅ Ready to be installed as a Python package
|
|
51
|
+
|
|
52
|
+
---
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Christofy 🧠📦
|
|
2
|
+
|
|
3
|
+
**Christofy** is a lightweight and modular PyTorch-based image classification package designed for both training and prediction. Built on top of ResNet18, it supports both binary and multi-class classification with clean abstractions and an easy-to-use API.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 🚀 Features
|
|
8
|
+
|
|
9
|
+
- ✅ Train image classifiers using CNN (ResNet18)
|
|
10
|
+
- ✅ Binary and multi-class classification support
|
|
11
|
+
- ✅ Predict the class of a single image using saved model
|
|
12
|
+
- ✅ Modular structure: clean separation between training and prediction
|
|
13
|
+
- ✅ Ready to be installed as a Python package
|
|
14
|
+
|
|
15
|
+
---
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "christofy"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="Aswin Christo", email="aswinchristo17@gmail.com" }
|
|
10
|
+
]
|
|
11
|
+
description = "A lightweight and modular PyTorch-based image classification package"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
license = { file="LICENSE" }
|
|
14
|
+
requires-python = ">=3.9"
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"License :: OSI Approved :: MIT License"
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
"Homepage" = "https://github.com/aswinchristo/christofy"
|
|
23
|
+
"Bug Tracker" = "https://github.com/aswinchristo/christofy/issues"
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import torch
|
|
3
|
+
import torch.nn as nn
|
|
4
|
+
import torch.optim as optim
|
|
5
|
+
from torch.utils.data import DataLoader, random_split
|
|
6
|
+
from torchvision import datasets, transforms, models
|
|
7
|
+
import matplotlib.pyplot as plt
|
|
8
|
+
import seaborn as sns
|
|
9
|
+
from sklearn.metrics import confusion_matrix, classification_report
|
|
10
|
+
from PIL import Image
|
|
11
|
+
import numpy as np
|
|
12
|
+
import json
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def train_cnn_classifier(data_dir, learning_rate=0.001, epochs=10, batch_size=32, output_dir="output"):
|
|
16
|
+
os.makedirs(output_dir, exist_ok=True)
|
|
17
|
+
|
|
18
|
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
19
|
+
|
|
20
|
+
# Detect classes
|
|
21
|
+
classes = sorted(os.listdir(data_dir))
|
|
22
|
+
num_classes = len(classes)
|
|
23
|
+
print(f"Detected {num_classes} classes: {classes}")
|
|
24
|
+
|
|
25
|
+
# Transforms
|
|
26
|
+
transform = transforms.Compose([
|
|
27
|
+
transforms.Resize((224, 224)),
|
|
28
|
+
transforms.RandomHorizontalFlip(),
|
|
29
|
+
transforms.ToTensor(),
|
|
30
|
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
|
31
|
+
std=[0.229, 0.224, 0.225])
|
|
32
|
+
])
|
|
33
|
+
|
|
34
|
+
dataset = datasets.ImageFolder(root=data_dir, transform=transform)
|
|
35
|
+
class_to_idx = dataset.class_to_idx
|
|
36
|
+
with open(os.path.join(output_dir, 'class_map.json'), 'w') as f:
|
|
37
|
+
json.dump(class_to_idx, f)
|
|
38
|
+
|
|
39
|
+
train_size = int(0.8 * len(dataset))
|
|
40
|
+
val_size = len(dataset) - train_size
|
|
41
|
+
train_dataset, val_dataset = random_split(dataset, [train_size, val_size])
|
|
42
|
+
|
|
43
|
+
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
|
|
44
|
+
val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False)
|
|
45
|
+
|
|
46
|
+
# Model
|
|
47
|
+
model = models.resnet18(pretrained=False)
|
|
48
|
+
model.fc = nn.Linear(model.fc.in_features, num_classes)
|
|
49
|
+
model = model.to(device)
|
|
50
|
+
|
|
51
|
+
# Loss and optimizer
|
|
52
|
+
criterion = nn.CrossEntropyLoss()
|
|
53
|
+
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
|
|
54
|
+
|
|
55
|
+
train_losses, val_losses = [], []
|
|
56
|
+
all_preds, all_labels = [], []
|
|
57
|
+
|
|
58
|
+
for epoch in range(epochs):
|
|
59
|
+
model.train()
|
|
60
|
+
total_loss = 0
|
|
61
|
+
for images, labels in train_loader:
|
|
62
|
+
images, labels = images.to(device), labels.to(device)
|
|
63
|
+
|
|
64
|
+
outputs = model(images)
|
|
65
|
+
loss = criterion(outputs, labels)
|
|
66
|
+
|
|
67
|
+
optimizer.zero_grad()
|
|
68
|
+
loss.backward()
|
|
69
|
+
optimizer.step()
|
|
70
|
+
|
|
71
|
+
total_loss += loss.item()
|
|
72
|
+
|
|
73
|
+
train_losses.append(total_loss / len(train_loader))
|
|
74
|
+
|
|
75
|
+
model.eval()
|
|
76
|
+
val_loss = 0
|
|
77
|
+
correct, total = 0, 0
|
|
78
|
+
with torch.no_grad():
|
|
79
|
+
for images, labels in val_loader:
|
|
80
|
+
images, labels = images.to(device), labels.to(device)
|
|
81
|
+
outputs = model(images)
|
|
82
|
+
loss = criterion(outputs, labels)
|
|
83
|
+
val_loss += loss.item()
|
|
84
|
+
|
|
85
|
+
_, predicted = torch.max(outputs, 1)
|
|
86
|
+
correct += (predicted == labels).sum().item()
|
|
87
|
+
total += labels.size(0)
|
|
88
|
+
|
|
89
|
+
all_preds.extend(predicted.cpu().numpy())
|
|
90
|
+
all_labels.extend(labels.cpu().numpy())
|
|
91
|
+
|
|
92
|
+
val_losses.append(val_loss / len(val_loader))
|
|
93
|
+
acc = correct / total
|
|
94
|
+
print(f"Epoch [{epoch+1}/{epochs}], Loss: {train_losses[-1]:.4f}, Val Loss: {val_losses[-1]:.4f}, Acc: {acc:.4f}")
|
|
95
|
+
|
|
96
|
+
# Save model
|
|
97
|
+
torch.save(model.state_dict(), os.path.join(output_dir, "model.pth"))
|
|
98
|
+
|
|
99
|
+
# Plot losses
|
|
100
|
+
plt.figure()
|
|
101
|
+
plt.plot(train_losses, label="Train Loss")
|
|
102
|
+
plt.plot(val_losses, label="Val Loss")
|
|
103
|
+
plt.legend()
|
|
104
|
+
plt.title("Training and Validation Loss")
|
|
105
|
+
plt.savefig(os.path.join(output_dir, "loss_plot.png"))
|
|
106
|
+
|
|
107
|
+
# Confusion matrix
|
|
108
|
+
cm = confusion_matrix(all_labels, all_preds)
|
|
109
|
+
plt.figure(figsize=(6, 6))
|
|
110
|
+
sns.heatmap(cm, annot=True, fmt="d", xticklabels=classes, yticklabels=classes, cmap="Blues")
|
|
111
|
+
plt.xlabel("Predicted")
|
|
112
|
+
plt.ylabel("Actual")
|
|
113
|
+
plt.title("Confusion Matrix")
|
|
114
|
+
plt.savefig(os.path.join(output_dir, "confusion_matrix.png"))
|
|
115
|
+
|
|
116
|
+
# Classification report
|
|
117
|
+
report = classification_report(all_labels, all_preds, target_names=classes)
|
|
118
|
+
with open(os.path.join(output_dir, "classification_report.txt"), 'w') as f:
|
|
119
|
+
f.write(report)
|
|
120
|
+
|
|
121
|
+
print("Training complete. Outputs saved to:", output_dir)
|
|
122
|
+
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import torch
|
|
3
|
+
import torch.nn as nn
|
|
4
|
+
import torch.optim as optim
|
|
5
|
+
from torch.utils.data import DataLoader, random_split
|
|
6
|
+
from torchvision import datasets, transforms, models
|
|
7
|
+
import matplotlib.pyplot as plt
|
|
8
|
+
import seaborn as sns
|
|
9
|
+
from sklearn.metrics import confusion_matrix, classification_report
|
|
10
|
+
from PIL import Image
|
|
11
|
+
import numpy as np
|
|
12
|
+
import json
|
|
13
|
+
|
|
14
|
+
def predict_class(model_path, image_path):
|
|
15
|
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
16
|
+
|
|
17
|
+
with open(os.path.join(os.path.dirname(model_path), 'class_map.json'), 'r') as f:
|
|
18
|
+
class_to_idx = json.load(f)
|
|
19
|
+
idx_to_class = {v: k for k, v in class_to_idx.items()}
|
|
20
|
+
|
|
21
|
+
transform = transforms.Compose([
|
|
22
|
+
transforms.Resize((224, 224)),
|
|
23
|
+
transforms.ToTensor(),
|
|
24
|
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
|
25
|
+
std=[0.229, 0.224, 0.225])
|
|
26
|
+
])
|
|
27
|
+
|
|
28
|
+
image = Image.open(image_path).convert('RGB')
|
|
29
|
+
image = transform(image).unsqueeze(0).to(device)
|
|
30
|
+
|
|
31
|
+
model = models.resnet18(pretrained=False)
|
|
32
|
+
model.fc = nn.Linear(model.fc.in_features, len(idx_to_class))
|
|
33
|
+
model.load_state_dict(torch.load(model_path, map_location=device))
|
|
34
|
+
model = model.to(device)
|
|
35
|
+
model.eval()
|
|
36
|
+
|
|
37
|
+
with torch.no_grad():
|
|
38
|
+
outputs = model(image)
|
|
39
|
+
_, predicted = torch.max(outputs, 1)
|
|
40
|
+
|
|
41
|
+
predicted_class = idx_to_class[predicted.item()]
|
|
42
|
+
return predicted_class
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: christofy
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A lightweight and modular PyTorch-based image classification package
|
|
5
|
+
Author-email: Aswin Christo <aswinchristo17@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 Aswin Christo
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/aswinchristo/christofy
|
|
29
|
+
Project-URL: Bug Tracker, https://github.com/aswinchristo/christofy/issues
|
|
30
|
+
Classifier: Programming Language :: Python :: 3
|
|
31
|
+
Classifier: Operating System :: OS Independent
|
|
32
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
33
|
+
Requires-Python: >=3.9
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
License-File: LICENSE
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# Christofy 🧠📦
|
|
39
|
+
|
|
40
|
+
**Christofy** is a lightweight and modular PyTorch-based image classification package designed for both training and prediction. Built on top of ResNet18, it supports both binary and multi-class classification with clean abstractions and an easy-to-use API.
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## 🚀 Features
|
|
45
|
+
|
|
46
|
+
- ✅ Train image classifiers using CNN (ResNet18)
|
|
47
|
+
- ✅ Binary and multi-class classification support
|
|
48
|
+
- ✅ Predict the class of a single image using saved model
|
|
49
|
+
- ✅ Modular structure: clean separation between training and prediction
|
|
50
|
+
- ✅ Ready to be installed as a Python package
|
|
51
|
+
|
|
52
|
+
---
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
src/christofy/__init__.py
|
|
6
|
+
src/christofy/model_trainer.py
|
|
7
|
+
src/christofy/predictor.py
|
|
8
|
+
src/christofy.egg-info/PKG-INFO
|
|
9
|
+
src/christofy.egg-info/SOURCES.txt
|
|
10
|
+
src/christofy.egg-info/dependency_links.txt
|
|
11
|
+
src/christofy.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
christofy
|