Brain Tumor MRI Classification: Training and Production Deployment for an Auditable Recognition Service

    This article outlines an end-to-end engineering practice for four-class brain tumor MRI classification. It covers patient-level data profiling and splitting, transfer-learning training controls, export consistency, out-of-domain rejection, explainability artifacts, and the engineering boundaries of a recognition service. The emphasis is on trustworthy data, reproducible training, uncertainty-aware prediction, and auditability rather than a single accuracy number.

    Moving a medical image classification task from an experiment script to an accessible recognition service is not mainly about a one-time accuracy score. The harder problems are whether the data is trustworthy, whether training is reproducible, whether predictions know when they are uncertain, and whether results can be audited. A common brain tumor MRI task classifies glioma, meningioma, no tumor, and pituitary tumor into four classes. With about 6,000 images after public de-duplication, the classes are imbalanced, and imaging protocols, slice thickness, and window level are mixed. The following sections describe practical methods in four areas: data, model, rejection and interpretation, and service deployment. They do not rely on any vendor-specific proprietary API.

    1. Profile and Fix the Data First

    The data split should be fixed before any augmentation. Split training, validation, and test sets by patient or acquisition batch so that adjacent slices from the same examination do not appear in both training and test sets. Otherwise, validation metrics will be inflated by spatial leakage. A starting ratio of about 7:1.5:1.5 is reasonable, but the patient must be the atomic unit, not a random file.

    Data profiling should record at least class sample counts, image size distribution, grayscale mean and variance, missing or corrupted files, duplicate hashes, and mislabels found by manual spot checks. Mislabel scanning does not need to cover every sample at once. Prioritize samples where the model is highly confident but wrong on the validation set, boundary samples that are visually similar across classes, and records whose filenames disagree with their labels. Each review should leave the original label, revised label, reviewer, and timestamp. Freeze this list before training so later comparison experiments share a common baseline.

    Augmentation should run only on the training set and preserve anatomical plausibility. Small rotation, translation, brightness and contrast perturbations, and horizontal flipping can reduce pose differences. Do not use strong geometric transformations that change the semantic location of the tumor. Estimate normalization statistics only from the training set, then apply them to validation and test sets. Replicating grayscale images into three channels is only for adapting to an image-pretrained backbone; it should not be treated as adding new medical information.

    2. Transfer Learning and Training Controls

    For four classes and limited samples, transfer from a deep residual convolutional network pretrained on large-scale natural images is usually more stable than training from scratch. In practice, freeze the early convolutional layers and train only the later residual blocks and the classification head. After validation loss stabilizes, fine-tune the whole network with a small learning rate. This preserves edge and texture priors while letting high-level layers adapt to MRI contrast.

    Do not set the learning rate once by intuition. Before formal long training, run a short sweep: choose several learning rates on logarithmic spacing, run only a few iterations or one epoch for each, and observe whether training loss decreases or oscillates and diverges. The purpose is to bound the order of magnitude, not to select an “optimal hyperparameter.” Then a small comparison within that interval on the validation set is enough.

    For long training, enable three controls. First, early stopping: monitor validation macro-average F1 or balanced accuracy, stop after several consecutive epochs without improvement, and roll back to the best checkpoint to avoid overfitting to mislabels late in training. Second, mixed precision: use half-precision compute and full-precision accumulation on supported hardware to shorten epoch time and reduce GPU memory, while retaining loss scaling to prevent underflow. Third, per-epoch persistence: write learning rate, training and validation loss, accuracy, macro precision, macro recall, and macro F1 into a results table. After a training interruption, curves can be recovered from records rather than from logs.

    An adaptive optimizer with momentum can be used. Weight decay should mainly apply to convolutional and fully connected weights; biases and normalization layer parameters usually should not be strongly decayed. When classes are imbalanced, the loss can use inverse-frequency weighting based on training-set frequency, or moderate oversampling of minority classes. Do not combine both to a degree that is hard to explain. Fix the random seed and record the data version and split list for each experiment; otherwise later heatmaps and thresholds cannot be reproduced.

    3. Export, Out-of-Domain Rejection, and Explainability Artifacts

    After training, save both native weights and an intermediate representation that is easy to load across runtimes. Native weights are for continued fine-tuning and experiment reproduction. The intermediate representation is for the inference service, avoiding binding the training framework into the online process. After export, compare class probabilities from both formats on the same validation images. Floating-point error is allowed, but large class-ranking flips are not.

    A closed-set four-class model cannot express “this image is not a brain MRI at all.” Before launch, calibrate out-of-domain rejection. An energy score aggregates logits across classes into an uncertainty score; it is simple and adds almost no storage. A nearest-neighbor score measures the distance between a test sample and training prototypes in feature space and is more sensitive to imaging protocol drift. Scan thresholds for both on in-distribution validation samples and deliberately constructed out-of-domain samples. Choose an operating point that blocks clearly abnormal input without over-rejecting true four-class cases. Write the thresholds, feature extraction layer, and distance metric into configuration rather than hard-coding them in page logic. Online requests below the threshold should return “unable to determine” and record the reason instead of forcing a class.

    Generate evaluation artifacts as a complete set. A confusion matrix shows easily confused classes such as meningioma and pituitary tumor. A calibration curve shows whether probabilities are trustworthy; if the model is overconfident, apply temperature scaling, with the scaling coefficient fitted only on the validation set. Class activation heatmaps should spot-check high-confidence correct samples, high-confidence incorrect samples, and rejected samples, confirming that the model focuses on the intracranial lesion region rather than image borders, marker text, or black borders. Heatmaps are auxiliary quality control, not diagnostic evidence; the page should avoid presenting them as localization conclusions.

    Open the test set only once after thresholds and calibration coefficients are frozen. Report per-class recall, precision, macro-average metrics, and rejection rate separately. Do not repeatedly change thresholds to refresh test-set numbers.

    4. Engineering Boundaries for the Recognition Service

    The backend can use a common web framework to provide account login and inference APIs. A relational database stores users, recognition tasks, and results, while object storage holds original images and key frames. The business keeps three entry points: single-image recognition, video frame-extraction recognition, and real-time camera recognition. All three should share the same preprocessing, model version, and rejection threshold, avoiding separate inference code for each page.

    Image tasks return synchronously with class, probability, rejection flag, and model version. Video tasks are first persisted as queued status; the backend extracts frames at fixed intervals, skips blurry frames, votes over valid frames, and retains representative frames. The real-time entry must be rate-limited: limit resolution, frame rate, and per-user concurrency. On inference timeout, return the last stable result or an explicit busy state; it must not block the web process.

    Recognition history should support paginated queries, details, and soft delete. Soft delete only marks a record as invisible, which helps with auditing and recovery from accidental deletion; physical cleanup follows a separate retention period. The details page shows the input thumbnail, class probabilities, rejection score, model version, and task duration. The dashboard should only aggregate authorized visible data: total recognitions, class distribution, rejection share, and failure reasons. It should not expose raw images on the dashboard. On the account side, distinguish at least normal users and administrators. APIs should validate login state and data ownership. Uploads should restrict format and size and perform content validation to prevent executable content from being written to the upload directory.

    At deployment, tag the model file, threshold configuration, and code version together. Replacing weights requires rerunning the export consistency check and a small golden-sample regression. Golden samples include four typical class images, images after known mislabel corrections, and non-target images that should be rejected. Logs should record request ID, model version, and error code without recording unnecessary identity credentials. Monitoring should cover latency, error rate, sudden increases in rejection rate, and queue backlog. A sudden rise in rejection rate often indicates that the imaging source has changed, not that fewer users are active.

    5. Core Workflow Outline

    The following framework-neutral pseudocode describes the control points for training and inference, omitting specific framework calls.

    Training side: load the patient-level split; compute normalization with training-set statistics; run a short learning-rate sweep and write the interval; during formal training, compute validation macro-F1 each epoch, apply early stopping, and save the best weights; export native weights and the intermediate representation; fit the calibration coefficient on the validation set and scan energy and nearest-neighbor thresholds; freeze them before evaluating the test set.

    Inference side: validate the user and file; preprocess in alignment with the training distribution; run forward inference to obtain probabilities and features; compare the rejection score first, and if it fails, mark the result as unable to determine; if it passes, apply temperature scaling and take the maximum class; write the history record and model version. Video and real-time entries only add frame extraction and rate limiting before this step.

    6. Pre-Launch Checklist

    The checklist can be short, but it must be executed: the patient-level split is intact; the mislabel list is versioned; the results table can plot complete curves; the two export formats produce consistent probabilities; rejection thresholds come from validation, not test; heatmap spot checks show no systematic focus on borders; history is paginated and supports soft delete; the three entry points share the same model version; dashboard statistics reconcile with detail records. With these in place, the classification experiment has an engineering form for continuous iteration, rather than being a one-off offline script that only demonstrates accuracy.