Skip to content

About

hybrid resnet152 and qnn ml model paper reproduction using transferred learning.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Hybrid CNN–Quantum Neural Network for Breast Cancer Classification: an honest reproduction + profiling

This repository reproduces the method of the AIIoT 2025 paper "A Novel Hybrid CNN-Quantum Neural Network Framework with Quantum Acceleration and Error Correction for High-Precision Breast Cancer Classification on AI Edge Devices" on a public dataset, compares it with a classical baseline and a size-matched classical control, and profiles where training time and memory actually go — as groundwork for FPGA acceleration of hybrid CNN–QNN models.

TL;DR

  • On CBIS-DDSM (mass ROI crops) the hybrid model reaches 70.8 ± 1.5 % test accuracy vs 72.7 ± 0.8 % for the classical ResNet152 (3 seeds, 378 held-out test images). Far from the ~97 % reported in the original paper.
  • Replacing the 8-qubit circuit by a classical Linear(8,8)+tanh layer of the same size gives 70.0 ± 0.6 %. Hybrid vs. control: McNemar p = 0.74 / 0.22 / 0.59 over the three seeds. The circuit adds nothing measurable; the small drop vs. the classical model comes from the 2048 → 8 bottleneck.
  • In a hybrid training step the frozen ResNet152 forward takes ~94 % of the time; the simulated 8-qubit layer (forward + backward) takes 6 % with our GPU simulator (12 % with PennyLane on CPU; NVIDIA's cuQuantum-based lightning.gpu is 90× slower than our simulator at this size).
  • At 8 qubits the simulation is overhead-bound (~17 µs per gate on GPU, ~50 kFLOP of real work per sample); simulator memory grows as gates × 2ⁿ × batch and runs out on a 6 GB GPU at 18 qubits.

Dataset

CBIS-DDSM from Kaggle, Mass subset, cropped ROIs only. Put jpeg/ and csv/ into ./archive/.

split images patients malignant benign
official train (→ 80/20 train/val, grouped by patient) 1318 691 637 681
official test (used once, at the end) 378 201 147 231

Labels: 0 = MALIGNANT, 1 = BENIGN (incl. BENIGN_WITHOUT_CALLBACK). No patient appears in both official splits.

Benign (class 1) Malignant (class 0)
Benign Malignant

Models

  • Classical: ImageNet ResNet152, layer3, layer4 and head fine-tuned (AdamW, lr 1e-5 backbone / 1e-3 head), dropout 0.5 + Linear(2048, 2).
  • Hybrid ("dressed" quantum head, quantum transfer learning): the classical model's fine-tuned backbone is frozen; head = Linear(2048,8) → tanh·π/2 → 8-qubit circuit → Linear(8,2). Circuit: H on all qubits, RY angle encoding, 6 × [CNOT ladder (even then odd pairs) + trainable RY], ⟨Z⟩ on every qubit. 16,458 trainable parameters.
  • Control: identical to the hybrid but the circuit is replaced by Linear(8,8) + tanh (bounded like ⟨Z⟩). 16,482 trainable parameters. This isolates what the circuit itself contributes.

Shared: same augmentation as the original scripts, class-balanced sampling, 60 epochs, ReduceLROnPlateau, best checkpoint = lowest val loss, evaluated once on the official test set. Seeds 42/43/44; the hybrid and control of seed s use the backbone of classical seed s.

Quantum simulator backends (quantum.py)

--middle what device
torch (default) our batched state-vector simulator in plain PyTorch, real float32 amplitudes (H, RY, CNOT are real), autograd GPU or CPU
pennylane the original PennyLane default.qubit TorchLayer, pinned to CPU CPU
pennylane-gpu default.qubit on CUDA, made to work by starting from a `StatePrep( 0…0⟩)` that lives on the GPU
lightning PennyLane lightning.qubit (adjoint gradients) CPU
lightning-gpu PennyLane lightning.gpu / NVIDIA cuStateVec (adjoint; pip install pennylane-lightning-gpu) GPU
classical the ablation control above —

python quantum.py checks that the torch simulator matches PennyLane (forward and gradients agree to ~1e-7). Checkpoints are interchangeable between quantum backends.

Note: with PennyLane 0.45, a default.qubit TorchLayer moved to CUDA crashes (its initial state is created on the CPU), so the original qnn.py cannot run its quantum layer on the GPU as written.

Results (3 seeds, mean ± std, official test set)

Model Test acc % Balanced acc % AUC (malignant) Recall malignant % Recall benign % Trainable params
Classical ResNet152 72.7 ± 0.8 71.8 ± 1.1 0.780 ± 0.012 68.0 ± 2.7 75.6 ± 0.9 55.6 M
Hybrid (8-qubit QNN head) 70.8 ± 1.5 70.2 ± 1.5 0.768 ± 0.005 67.3 ± 3.0 73.0 ± 2.5 16,458
Control (Linear(8,8)+tanh head) 70.0 ± 0.6 70.4 ± 1.0 0.780 ± 0.014 72.1 ± 4.5 68.7 ± 2.9 16,482

Paired tests on the 378 test images (same seed): see results/compare/stats.txt. None of the 9 comparisons survives a Bonferroni correction (p < 0.0056).

Training curves Confusion matrices

What this does not support (corrections to the earlier version of this README):

  • "Quantum sensitivity to malignant cases." The earlier single run showed 73 % vs 67 % malignant recall. Over 3 seeds the hybrid's malignant recall (67.3 %) is no higher than the classical model's (68.0 %), and the classical control is highest (72.1 %): this is an operating-point effect of the small head, not a quantum one.
  • "Accelerated convergence." The hybrid head peaks at epoch 2–12, but so does the classical control (epoch 2–8): any small head on an already fine-tuned backbone converges fast.

Data leakage in the original split

The original scripts split train/val by row. With the same seed, 181 of 264 val images (69 %) had their patient in the training set, and 160 lesions had their other mammographic view (CC/MLO) in training — hence val accuracy (76.5 %) overstated test accuracy (71.7 %). The new split is grouped by patient_id.

Profiling (profile_hybrid.py, RTX 3050 6 GB, batch 32, random inputs)

Breakdown Qubit scaling

  • Hybrid step: frozen ResNet152 forward 108 ms (bf16) vs quantum head fwd+bwd 6.7 ms (our GPU sim), 5.8 ms (our sim, CPU), 14.1 ms (PennyLane CPU), 19.8 ms (PennyLane GPU). Accelerating only the quantum layer can speed up a training step by at most ~6 % (Amdahl).
  • Classical fine-tuning step: 124 ms fwd + 129 ms bwd, 2.3 GB peak with bf16 autocast; 237 + 258 ms, 4.4 GB in fp32.
  • Scaling: at ≤ 8 qubits time grows with the number of gates, not with 2ⁿ (kernel-launch/framework overhead); the GPU overtakes the CPU from ~10 qubits (16 qubits: 115 ms vs 505 ms forward). Backprop simulation stores a state per gate: 937 MB at 16 qubits, OOM at 18 on 6 GB.
  • PennyLane Lightning (adjoint gradients): lightning.qubit (CPU) 58.7 ms and lightning.gpu (NVIDIA cuStateVec) 600 ms per batch-32 fwd+bwd at 8 qubits, vs 6.7 ms for our GPU simulator. Lightning devices do not broadcast over the batch, so each of the 32 samples is a separate circuit execution (~19 ms fixed cost each on lightning.gpu); they target single large circuits, not small batched ones. lightning.gpu is still 22× slower at 14 qubits (2.0 s vs 91 ms).
quantum head, 8 qubits, batch 32 fwd + bwd (ms)
our simulator, CPU 5.8
our simulator, GPU 6.7
PennyLane default.qubit, CPU 14.1
PennyLane default.qubit, GPU 19.9
PennyLane lightning.qubit, CPU 58.7
PennyLane lightning.gpu (cuQuantum) 600

Raw numbers: results/profile_3050/*.csv and results/profile_3050_lightning/*.csv (lightning only: python profile_hybrid.py --backends lightning-cpu,lightning-gpu --out results/profile_3050_lightning), environment in env.txt.

How to run

source ~/qnn-env/bin/activate          # Python 3.12; torch, torchvision, pennylane, pandas, scikit-learn, matplotlib, scipy
python build_index.py                  # archive/ -> data/{train,test}_index.csv
python quantum.py                      # simulator correctness check vs PennyLane
nohup ./run_all.sh > runs/run_all.log 2>&1 &   # 3 seeds x {classical, hybrid, control}, ~55 min on an RTX 3050
python compare.py                      # -> results/compare/
python profile_hybrid.py --out results/profile_3050   # run with the GPU otherwise idle

Single runs:

python train.py --model classical --out runs/classical_s42
python train.py --model hybrid --backbone runs/classical_s42/best.pth --out runs/hybrid_s42
python train.py --model hybrid --middle classical --backbone runs/classical_s42/best.pth --out runs/hctrl_s42

Useful flags: --batch 16 (if CUDA OOM), --epochs, --patience, --seed, --no-amp, --limit N (debug).

Repository layout

file purpose
build_index.py CBIS-DDSM CSVs → image index with patient ids
common.py dataset, patient-grouped split, ClassicalResNet, DressedHead, HybridModel
quantum.py GPU state-vector simulator + PennyLane backends
train.py training + one-shot test evaluation for all models
run_all.sh, post_train.sh full experiment; comparison + profiling after it
compare.py tables, McNemar / bootstrap statistics, figures
profile_hybrid.py forward/backward time + memory profiling
results/ comparison and profiling outputs
runs/ per-run logs, result.json, test predictions (checkpoints are gitignored)
legacy/ the original scripts, logs and figures (row-split validation)
poster/ LaTeX source of the poster

Caveats

  • One dataset (CBIS-DDSM masses), one backbone, three seeds; differences of 1–2 points are ~4–8 test images.
  • The quantum layer is simulated (noise-free state vector); no quantum hardware is used. The original paper's "quantum acceleration" and "error correction" are not reproduced.
  • The hybrid and control heads sit on a backbone already fine-tuned by the classical model, so they compare heads on identical features.

About

hybrid resnet152 and qnn ml model paper reproduction using transferred learning.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages