PyCNN v2.0
📦 Release: PyCNN v2.0
This update enhances PyCNN by adding CUDA (GPU) support for accelerated training and inference when a compatible GPU and CuPy are available. It introduces Adam optimizer support, dynamic layer definitions for user-customized architectures, and various performance optimizations, while allowing users to seamlessly toggle between CPU and GPU backends.
🚀 Key Features
- ✅ Fully functional CNN implementation from scratch
- 🧠 Manual convolution, max pooling, and ReLU activations
- 🔁 Forward and backward propagation with mini-batch gradient descent
- 🏷 Multi-class classification via softmax and cross-entropy loss
- 💾 Model save/load using
pickle - 🖼 RGB image preprocessing with customizable filters
- 🔍 Predict function to classify new unseen images
- 📊 Real-time training visualization (accuracy & loss per epoch)
- ⚡ Optional CUDA acceleration for faster training and inference
- 🆕 Adam optimizer support for improved training performance
- 🛠 Dynamic user-defined layers for fully customizable architectures
- 🚀 Performance optimizations for faster computation and memory efficiency
- 🔄 Automatic backend conversion when loading models trained on a different backend
🖥️ PyCNN Usage
Training model
from pycnn.model import CNN
model = CNN()
model.cuda(True) # Enable CUDA
model.init(
image_size=64,
batch_size=32,
layers=[256, 128, 64, 32, 16, 8, 4], # Allows you to define any type of dense layer.
learning_rate=0.0001,
epochs=1000,
dataset_path="data",
max_image=1000, # If unspecified, the framework will use all images from each class.
filters = [
[# Custom filter 1],
[# Custom filter 2],
[# Custom filter 3],
[# Custom filter ...],
] # If unspecified, the framework will use the default filters
)
model.adam() # If specified, the framework will use the adam optimizer
model.load_dataset()
model.train_model(visualize=True, early_stop=2)
#visualize: Displays a real-time graph of accuracy and loss per epoch when enabled. Set to False or leave unspecified to disable this feature.
#early_stop: Stops training when overfitting begins and the number of epochs exceeds early_stop. Set to 0 or leave unspecified to disable this feature.Saving/Loading model
model.save(path=your_save_path) # if your your_save_path is unspecified the framework will save it in "./model.bin"
model.save(path=your_model_path)Prediction
result = model.predict(your_image_path)
print(result)The model will automatically convert weights, biases, and datasets to the selected backend. Models trained on GPU can still be loaded on CPU and vice versa.
Usage exemple
from pycnn.model import CNN
from os import listdir
model = CNN()
model.init(
image_size=64,
batch_size=32,
layers=[256, 128, 64, 32, 16, 8, 4],
learning_rate=0.0001,
epochs=1000,
dataset_path="data",
max_image=100,
)
model.adam()
model.load_dataset()
model.train_model(early_stop=2)
x = 0
for path in listdir("data/cat"):
if model.predict(f"data/cat/{path}") == "cat":
x += 1
if x == 10:
break
print(f"cat: {x}/10")
x = 0
for path in listdir("data/dog"):
if model.predict(f"data/dog/{path}") == "dog":
x += 1
if x == 10:
break
print(f"dog: {x}/10")🧾 Changelog
v2.0
- New Adam optimizer support for improved training performance
- New Dynamic user-defined layers for fully customizable architectures
- New Performance optimizations for faster computation and memory efficiency
- CUDA backend support via CuPy
- Automatic conversion between CPU and GPU backends
- Models can be trained on one backend and loaded on another seamlessly
- Minor improvements in training stability and performance
v0.2.0
- New: CUDA backend support via CuPy
- Automatic conversion between CPU and GPU backends
- Models can be trained on one backend and loaded on another seamlessly
- Minor improvements in training stability and performance
v0.1.1
- Real-time training visualization with Matplotlib
v0.1.0
- Initial version with full training and prediction pipeline
📌 Installation
pip install git+https://github.com/77AXEL/PyCNN.git@v2.0Optional: Install CuPy for CUDA support:
pip install cupy-cuda118 # Match your CUDA version- See the CUDA Documentation for more information on how to set it up
💬 Feedback & Contributions
We welcome issues, suggestions, and contributions!
Check the Discussions tab or see CONTRIBUTING.md
🛡 Security
Found a security issue? Please report privately to:
📧 a.x.e.l777444000@gmail.com
📜 License
Released under the MIT License
