Skip to content

PyCNN v2.2

Choose a tag to compare

@77axel 77axel released this 08 Sep 20:39
· 58 commits to main since this release
0cffe3c

📦 Release: PyCNN v2.2 (fixed some issues)

Added HuggingFace CNN datasets support, further performance improvements using Numba JIT and Cython and extra modifications to syntax usage.


🚀 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
  • 🚀 ** More CPU based performance optimizations** for faster computation and memory efficiency
  • 🔄 Automatic backend conversion when loading models trained on a different backend
  • 🛢️ Hugging Face CNN datasets support

🖥️ PyCNN Usage

Training model

from pycnn.pycnn import PyCNN

pycnn= CNN()
pycnn.cuda(True)  # Enable CUDA
pycnn.init(
    image_size=64, # If unspecified the default is 64
    batch_size=32, # If unspecified the default is 32
    layers=[256, 128, 64, 32, 16, 8, 4], # Allows you to define any type of dense layer,  If unspecified the default is [128, 64]
    learning_rate=0.0001, # If unspecified the default is 0.0001
    epochs=1000, # If unspecified the default is 50
    filters = [
        [# Custom filter 1],
        [# Custom filter 2],
        [# Custom filter 3],
        [# Custom filter ...],
    ] # If unspecified, the framework will use the default filters.
)

pycnn.adam() # If specified, the framework will use the adam optimizer.

pycnn.dataset.local(
    path_to_you_dataset_folder, 
    max_image=1000 # If unspecified, the framework will use all images from each class.
) # Use this method if you want to load your local dataset folder.

pycnn.dataset.hf(
    huggingface_dataset_name, 
    max_image=1000, # If unspecified, the framework will use all images from each class.
    cached=True # Using the cached database helps you bypass downloading the dataset each time it is loaded (the default behavior when cached=True).
    split="train" # Specify which split of the dataset to use for training the model (the default is the train split).

) # Use this method if you want to load a HuggingFace dataset folder.

pycnn.train_model(
    visualize=True, # 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=2 # 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

pycnn.save_model(path=your_save_path) # if your your_save_path is unspecified the framework will save it in "./model.bin" bu default
pycnn.load_model(path=your_model_path)

Prediction

result = pycnn.predict(your_image_path) # Returns a tuple of (class name, confidense value)
print(result) 

The framework 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.pycnn import PyCNN
from os import listdir

pycnn = PyCNN()
pycnn.cuda(True)

pycnn.init(
    layers=[512, 256],
    epochs=500,
)

pycnn.dataset.hf("cifar10", max_image=500, cached=True)
pycnn.adam()
pycnn.train_model(early_stop=15)
pycnn.save_model("pycnn_cifar10.bin")

testdir = "cifar10_test"
_max = 1000
for classname in listdir(testdir):
  x = 0
  correct = 0
  for filename in listdir(f"{testdir}/{classname}"):
    if x == _max:
      break
    if pycnn.predict(f"{testdir}/{classname}/{filename}")[0] == classname:
      correct += 1
    x += 1
  print(classname, correct)

Output:


🧾 Changelog

v2.2

  • New HuggingFace CNN datasets support
  • New More CPU based performance optimizations using Cython
  • Adam optimizer support for improved training performance
  • Dynamic user-defined layers for fully customizable architectures
  • 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

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.2

Optional: Install CuPy for CUDA support:

pip install cupy-cuda118  # Match your CUDA version

💬 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