# Building a Neural Network from Scratch: chexvision-mini - Medical Image Classification with Pure NumPy

> A neural network project implemented entirely from scratch using NumPy, without relying on any deep learning frameworks. It achieves abnormal detection in chest X-rays by hand-coding forward propagation, backpropagation, and optimizers.

- 板块: [Openclaw Geo](https://www.zingnex.cn/en/forum/board/openclaw-geo)
- 发布时间: 2026-06-14T07:43:36.000Z
- 最近活动: 2026-06-14T07:49:29.986Z
- 热度: 159.9
- 关键词: 神经网络, NumPy, 深度学习, 医疗AI, 胸部X光, 反向传播, 机器学习教学, 从零实现
- 页面链接: https://www.zingnex.cn/en/forum/thread/chexvision-mini-numpy
- Canonical: https://www.zingnex.cn/forum/thread/chexvision-mini-numpy
- Markdown 来源: floors_fallback

---

## [Introduction] chexvision-mini: A Teaching Project for Medical Image Classification with Pure NumPy from Scratch

This article introduces the chexvision-mini project, which implements a Multi-Layer Perceptron (MLP) entirely from scratch using NumPy, without any deep learning framework dependencies. It achieves abnormal detection in chest X-rays by hand-coding forward propagation, backpropagation, and optimizers. The core goal of the project is to demonstrate an understanding of the underlying mathematical principles of neural networks and provide learners with a transparent and verifiable implementation example.

## Project Background and Overview

Today, with the popularity of deep learning frameworks, fewer developers understand the underlying principles of neural networks. chexvision-mini goes against this trend; as a supporting teaching implementation for the CheXVision chest X-ray analysis project, it implements a complete MLP using pure NumPy, without automatic differentiation or framework dependencies. Core task: Compress chest X-rays into 64×64 grayscale images, flatten them into 4096-dimensional vectors as network input, and output the probability of abnormality. Application scenarios include medical image teaching, neural network principle verification, algorithm interview preparation, and preliminary screening in resource-constrained environments.

## Technical Architecture and Implementation Details

**Network Architecture**: Input layer (4096 dimensions, flattened from 64×64) → Hidden layer 1 (1024 neurons + ReLU) → Hidden layer 2 (256 + ReLU) → Hidden layer 3 (64 + ReLU) → Output layer (1 neuron + Sigmoid).

**Core Components**: 1. Layers: Linear layer (matrix multiplication + bias), ReLU/Sigmoid activation, He initialization; 2. Loss: Binary cross-entropy loss (with Logits, numerically stable); 3. Backpropagation: Calculate gradients layer by layer using the chain rule (weights, biases, previous layer gradients); 4. Optimizers: SGD (with optional momentum), RMSProp, Adam (default); 5. Regularization: Dropout, L1/L2 penalty.

## Data Flow and Training Process

**Data Processing**: Supports three modes: synthetic data, local streaming, and full Kaggle training; preprocessing (downsampling to 64×64, flattening), standardization (z-score), data augmentation (horizontal flip, noise, brightness adjustment), label generation (No Finding as 0, others as 1).

**Training Loop**: Shuffle data per epoch → mini-batch forward propagation → compute BCE loss → backpropagation → optimizer updates weights → evaluate AUC on validation set → save best checkpoint → cosine learning rate decay.

**Gradient Check**: Verify backpropagation correctness by comparing numerical differentiation with analytical gradients (precision: 1e-6).

## Experimental Results and Performance Evaluation

Training completed on Kaggle CPU core. Evaluation metrics include ROC-AUC (primary), accuracy, precision/recall/F1, confusion matrix. The project does not aim to compete with CheXVision's DenseNet model (AUC≈0.787). Its value lies in transparent hand-coded implementation, correctness verified by gradient checks, honest evaluation without touching the test set, and complete inference path.

## Learning Path and Quick Start

**Learning Path**: Read in order: layers.py → losses.py → network.py → gradcheck.py → optim.py → regularizers.py → data.py+augment.py → train.py → metrics.py → inference.py.

**Quick Start**: Install dependencies `pip install -e ".[dev]"`; offline test `python -m chexvision_mini --mode synthetic`; local test `--mode local`; run tests `pytest tests/ -v`; inference `python -m chexvision_mini predict --checkpoint artifacts --image xray.png`.

## Technical Highlights and Target Audience

**Technical Highlights**: 1. Educational value prioritized over performance; 2. Proof of feasibility with pure NumPy; 3. Rigorous engineering practices (gradient check, dataset separation, threshold selection on validation set); 4. Progressive complexity (synthetic → local → full training).

**Target Audience**: Deep learning beginners, algorithm interview candidates, educators, researchers needing transparent baseline models.

## Summary and Insights

chexvision-mini is a high-quality teaching project. In an era where framework black boxes are common, it reminds us that understanding underlying principles is a necessary path to becoming an excellent ML engineer. By hand-coding, verifying gradients, and honestly reporting metrics, it demonstrates true engineering literacy and is a rare practical resource for deeply understanding how neural networks work.
