Metadata-Version: 2.2
Name: newton-vision
Version: 1.0.1
Summary: High-Performance Computer Vision & Deep Learning Framework in C++ & CUDA with PyTorch Parity
Keywords: deep-learning,computer-vision,cuda,autograd,cnn,resnet,mobilenet,cpp
Author-Email: newton-vision Core Team <info@newton-vision.org>
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Image Processing
Project-URL: Homepage, https://github.com/onur/newton-vision
Project-URL: Documentation, https://github.com/onur/newton-vision/tree/master/docs
Project-URL: Repository, https://github.com/onur/newton-vision.git
Requires-Python: >=3.9
Requires-Dist: numpy>=1.20.0
Description-Content-Type: text/markdown

# 🌌 newton-vision

[![PyPI Version](https://img.shields.io/pypi/v/newton-vision.svg)](https://pypi.org/project/newton-vision/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/)
[![C++17 OpenMP](https://img.shields.io/badge/C++17-OpenMP-red.svg)](https://gcc.gnu.org/)
[![CUDA Native](https://img.shields.io/badge/CUDA-Native_Kernels-green.svg)](https://developer.nvidia.com/cuda-toolkit)

**High-Performance Computer Vision & Deep Learning Framework in C++17 & CUDA with PyTorch Parity.**

`newton-vision` is a lightweight, zero-dependency deep learning framework built from scratch with custom native **C++17 OpenMP multi-threading**, **CUDA GPU global kernels**, reverse-mode **Autograd DAG Engine**, **Vision Model Zoo (ResNet-18, MobileNetV2)**, and seamless **PyTorch `state_dict` weight loading**.

---

## 🚀 Quick Installation

Install the official binary wheel directly from PyPI:

```bash
pip install newton-vision
```

Or build directly from source:

```bash
git clone https://github.com/onur/newton-vision.git
cd newton-vision
pip install .
```

---

## ⚡ Quick Start: 60-Second Training Example

```python
import numpy as np
import newton_vision as nv

# 1. Device Control & Tensor Allocation
device = "cuda" if nv.principia.is_cuda_available() else "cpu"

# 2. Build a CNN Model Architecture
class ConvNet:
    def __init__(self):
        self.conv = nv.Conv2d(in_channels=1, out_channels=8, kernel_size=3, padding=1)
        self.relu = nv.ReLU()
        self.pool = nv.MaxPool2d(kernel_size=2)
        self.fc = nv.Linear(in_features=8 * 14 * 14, out_features=10)

    def forward(self, x):
        h = self.pool(self.relu(self.conv(x)))
        flat = h.data.reshape(h.data.shape[0], -1)
        return self.fc(flat)

model = ConvNet()
criterion = nv.gravity.CrossEntropyLoss()
optimizer = nv.gravity.Adam(model.conv.parameters() + model.fc.parameters(), lr=0.01)

# 3. Dummy Synthetic Digit Training Iteration
x_data = nv.Tensor(np.random.randn(4, 1, 28, 28).astype(np.float32), requires_grad=True)
y_target = np.array([0, 3, 7, 9], dtype=np.int64)

optimizer.zero_grad()
logits = model.forward(x_data)
loss = criterion(logits, y_target)

# 4. Reverse-Topological Autograd Graph Execution
loss.backward()
optimizer.step()

print(f"Loss: {loss.data.item():.4f} | Conv Weight Grad Max: {np.max(np.abs(model.conv.w.grad)):.4f}")
```

---

## 🏛️ Architecture & Module Organization (Newton System)

The framework is organized into modular Newton-themed components:

| Module | Description | Key Components |
| :--- | :--- | :--- |
| **`nv.optics`** | Vision Layers API | `Conv2d`, `Linear`, `BatchNorm2d`, `MaxPool2d`, `AvgPool2d`, `ReLU`, `LeakyReLU`, `Sigmoid`, `Tanh` |
| **`nv.fluxion`** | Autograd Graph Engine | `Tensor`, `backward()`, `.cuda()`, `.cpu()`, `.to(device)` |
| **`nv.gravity`** | Loss & Optimizers | `MSELoss`, `CrossEntropyLoss`, `SGD`, `Adam` |
| **`nv.principia`** | System & Device Management | `is_cuda_available()`, `get_device()`, `set_device()` |
| **`nv.models`** | Vision Model Zoo | `ResNet18`, `MobileNetV2`, `resnet18()`, `mobilenet_v2()` |
| **`nv.weights`** | Weight Importer | `load_state_dict(model, state_dict)` (PyTorch `.pth` Parity) |
| **`nv.jit`** | Real-Time Server & Exporter | `InferenceEngine`, `save_model`, `load_model`, `export_onnx` |

---

## 🦁 Vision Model Zoo & PyTorch Weight Importer

### 1. Load Pretrained PyTorch Weights into `newton-vision`

```python
import numpy as np
import newton_vision as nv

# Instantiate newton-vision ResNet-18 model
model = nv.models.resnet18(num_classes=1000)

# Load state_dict weights exported from PyTorch
pytorch_state_dict = {
    "conv1.weight": np.random.randn(64, 3, 7, 7).astype(np.float32),
    "fc.weight": np.random.randn(1000, 512).astype(np.float32)
}
nv.load_state_dict(model, pytorch_state_dict)

# Run Inference
dummy_image = np.random.randn(1, 3, 224, 224).astype(np.float32)
logits = model(dummy_image)
print(f"ResNet-18 Logits Output Shape: {logits.shape}")  # Output: (1, 1000)
```

### 2. MobileNetV2 Depthwise Separable Convolutions

```python
import newton_vision as nv
mobilenet = nv.models.mobilenet_v2(num_classes=1000)
output = mobilenet(np.random.randn(1, 3, 224, 224).astype(np.float32))
print(f"MobileNetV2 Output: {output.shape}")
```

---

## ⏱️ Real-Time Inference Server & ONNX Exporter

```python
import newton_vision as nv
import numpy as np

model = nv.models.resnet18(num_classes=10)
engine = nv.InferenceEngine(model)

# Run high-speed frame-by-frame prediction
frame = np.random.randn(1, 3, 224, 224).astype(np.float32)
output, latency_ms, fps = engine.predict(frame)
print(f"Latency: {latency_ms:.2f} ms | Throughput: {fps:.1f} FPS")

# Save model checkpoint in compressed .nv binary format
nv.save_model(model, "resnet_checkpoint.nv")

# Export computational graph to standard ONNX format
nv.export_onnx(model, frame, "resnet_model.onnx")
```

---

## 📊 Benchmark: `newton-vision` vs PyTorch 2.6.0 CPU

*Execution time for single Conv2D layer forward pass (kernel 3x3, padding 1):*

| Test Configuration | `newton-vision` C++ OpenMP | PyTorch 2.6 CPU | Throughput (`newton-vision`) |
| :--- | :--- | :--- | :--- |
| **Batch=1 (128x128, 3➔16 ch)** | **1.55 ms** | **0.16 ms** | **644.9 FPS** |
| **Batch=8 (64x64, 16➔32 ch)** | **11.27 ms** | **1.61 ms** | **710.0 FPS** |
| **Batch=16 (32x32, 32➔64 ch)** | **19.59 ms** | **2.79 ms** | **816.8 FPS** |

---

## 📄 License

`newton-vision` is released under the open-source **[MIT License](LICENSE)**.
