# MIT磁感应断层成像 **Repository Path**: qifei-yu/MIT ## Basic Information - **Project Name**: MIT磁感应断层成像 - **Description**: CCNU物理科学与技术学院24级大创项目 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-18 - **Last Updated**: 2026-06-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # MIT Forward Solver and Data Generation This repository contains two related parts: - `MIT_Forward`: a forward magnetic induction tomography (MIT) eddy-current solver. It takes a conductivity distribution, a prescribed initial magnetic vector potential, and an excitation frequency, then trains a scalar potential `psi` and evaluates current density `J` and secondary magnetic field `B_s`. - `gendata`: a dataset-generation layer built on top of `MIT_Forward`. It saves detailed forward-calculation results, then converts them into clean inverse-process training tensors. All physical quantities use SI units: length in m, conductivity in S/m, magnetic flux density in T, current density in A/m^2, and frequency in Hz. ## Project Layout ```text . |-- MIT_Forward/ | |-- solver.py # Main forward API: EddyCurrentSolver | |-- net_psi.py # PINN scalar-potential model and training | |-- net_J.py # Current density from A0, psi, and sigma | |-- secondary_field.py # Biot-Savart volume integration for B_s | |-- distribution.py # Grid storage and geometry primitives | |-- sigma_distribution.py # Conductivity distribution | |-- A_distribution.py # Prescribed vector-potential distribution | |-- B_distribution.py # Magnetic flux density distribution | |-- current_distribution.py # Current-density distribution | |-- verify.py # Verification helpers | |-- visualization.py # PNG/HTML visualization helpers | `-- examples/run_forward_uniform_A.py |-- gendata/ | |-- generator.py # Detailed forward-calculation data API | |-- convert_forward_to_training.py # Raw-to-training conversion CLI/API | |-- example.py # Uniform-conductivity cuboid example | |-- generate_single_gaussian_loc.py # Random Gaussian-location dataset script | `-- Data Specification Document.md |-- MIT_Inverse/ | |-- FCNN.py # FCNN inverse-model architecture | |-- ResNet.py # Residual inverse-model architecture | |-- training.py # Reusable inverse-model training utilities | `-- train.py # Generic inverse-model training CLI `-- README.md ``` ## Environment The current workspace has been checked with: ```text Python 3.13.9 PyTorch 2.11.0+cu130 ``` Treat these as the versions used for the latest local check; your active `python --version` and `torch.__version__` are the source of truth for a new environment. Main dependencies: ```powershell pip install torch numpy matplotlib plotly ``` `plotly` is only needed for HTML visualization. It is not required if you only write PNG figures or generate `.pt` datasets. ## Forward Solver The main forward entry point is `MIT_Forward/solver.py`, class `EddyCurrentSolver`. The computational flow is: ```text prescribed A0(r) -> train psi(r) -> compute J(r) -> integrate B_s(sensor) ``` Important conventions: - `A_distribution` defines the prescribed vector potential `A0(r)` in T*m. - `frequency` is the excitation frequency in Hz. - `SigmaDistribution` stores conductivity on a regular cell-centered 3D grid. - Every `grid_size` dimension must be a positive integer. - The default current-density convention is the real-amplitude field ```text J_amp(r) = -sigma(r) * [omega * A0(r) + grad(psi)(r)] ``` - The optional psi gauge loss uses `(mean(psi))^2` to fix the arbitrary additive constant; it is not an L2 penalty `mean(psi^2)`. - Secondary magnetic field `B_s` is computed by Biot-Savart integration over the voxelized conductor mask. Run the built-in forward example: ```powershell python MIT_Forward/examples/run_forward_uniform_A.py --device cpu --epochs 500 ``` Fast smoke run: ```powershell python MIT_Forward/examples/run_forward_uniform_A.py --device cpu --epochs 1 --no-html --quad-order 1 --radial-samples 5 --angle-samples 4 --viz-grid 5 --secondary-viz-grid 3 ``` Default forward-example outputs are written under: ```text MIT_Forward/outputs/run_forward_uniform_A/ ``` ## Generate Forward Data And Training Data `gendata/generator.py` provides the reusable forward-data API: - `SensorArray`: fixed sensor positions and directions. Directions are normalized automatically. - `ConductivityCase`: one sample's conductivity distribution. - `SolveConfig`: arguments forwarded to `EddyCurrentSolver.solve(...)`. - `SecondaryFieldConfig`: secondary-field integration parameters. - `DatasetConfig`: measurement mode and optional raw-output fields. - `MITForwardDataGenerator`: runs all forward solves and writes one detailed raw `.pt` file. Forward data and training data are intentionally separate: - Raw forward data keeps metadata, full conductivity grids, masks, solver settings, sample metadata, and any requested secondary-field/loss details. - Pure training data contains only `sensor_values` and `conductivity`. It is produced by `gendata/convert_forward_to_training.py`. Run the uniform-conductivity cuboid example: ```powershell python gendata/example.py --device cpu ``` Fast smoke run: ```powershell python gendata/example.py --device cpu --epochs 1 --conductivities 1000 --grid-size 4 4 4 --hidden-width 8 --hidden-depth 1 --batch-inside 8 --batch-boundary 8 --quad-order 1 --quiet ``` Default example outputs: ```text gendata/outputs/example_uniform_cuboids_raw.pt gendata/outputs/example_uniform_cuboids_training.pt ``` Example with common options: ```powershell python gendata/example.py ` --device cpu ` --epochs 100 ` --conductivities 500 1000 2000 ` --measurement-mode real_component ` --save-loss-history ``` Example with a fixed cuboid training target region instead of the full grid: ```powershell python gendata/example.py ` --device cpu ` --target-mode box_region ` --target-box-bounds -0.01 0.01 -0.01 0.01 -0.01 0.01 ` --training-grid-size 12 12 12 ``` Convert an existing raw or legacy dataset without recomputing the forward solve: ```powershell python gendata/convert_forward_to_training.py ` --input gendata/outputs/single_gaussian_loc_raw.pt ` --output gendata/outputs/single_gaussian_loc_training.pt ` --target-bounds -0.010 0.010 -0.008 0.008 -0.011 0.011 ` --grid-size 16 12 18 ``` `gendata/generate_single_gaussian_loc.py` now writes: ```text gendata/outputs/single_gaussian_loc_raw.pt gendata/outputs/single_gaussian_loc_training.pt ``` For the current checked workspace, `single_gaussian_loc_training.pt` was converted directly from the existing `single_gaussian_loc_raw.pt`; no expensive forward regeneration was needed. Train the default ResNet inverse model on the converted pure training data: ```powershell python MIT_Inverse/train.py --device cpu --data-path gendata/outputs/example_uniform_cuboids_training.pt ``` Use a smaller ResNet for a quick inverse-training smoke run: ```powershell python MIT_Inverse/train.py --device cpu --data-path gendata/outputs/example_uniform_cuboids_training.pt --epochs 1 --batch-size 2 --print-every 1 --model-kwargs '{"stem_layers":[8],"residual_blocks":1,"head_layers":[8]}' --max-params none ``` Run the FCNN baseline for architecture comparison: ```powershell python MIT_Inverse/train.py --device cpu --data-path gendata/outputs/example_uniform_cuboids_training.pt --model-class MIT_Inverse.FCNN:FCNN --model-kwargs '{"hidden_layers":[512,512,512,512,512,512]}' --max-params none ``` ## Data Format Generated `.pt` files are Python dictionaries written by `torch.save(...)`. There are two current formats. Detailed raw forward data: - `format_name`: always `MIT_FORWARD_CALCULATION_DATA` - `format_version`: currently `1` - `global_metadata`: grid, sensor, frequency, solver, units, and raw-grid metadata - `sample_ids`: sample IDs in tensor order - `sensor_values`: fixed-sensor measurements - `conductivity`: full source conductivity grid - `conductivity_mask`: `torch.bool` conductor mask with the same shape - `sample_metadata`: per-sample metadata Pure training data: - `sensor_values`: fixed-sensor measurements - `conductivity`: conductivity labels, optionally cropped and resampled Default raw settings: - `measurement_mode="real_component"`: saves `B_secondary dot unit_direction` as a real-amplitude tensor with shape `[N, Ns]`. Accepted measurement modes are `real`/`real_component`, `complex`/`complex_component`, and `component_plus_vector`/`real_component_plus_vector`. `real_component_plus_vector` keeps the same scalar `sensor_values` shape as `real_component` and additionally saves `secondary_B_vectors_T`. - `grid_size=[nx, ny, nz]` must contain positive integers. - Loss history, full secondary-field vectors, and auxiliary fields are not saved unless explicitly requested. The converter can select a physical rectangular target region and resample the conductivity distribution to a new grid using trilinear interpolation. The complete unambiguous file-format specification is: ```text gendata/Data Specification Document.md ``` Minimal loading example: ```python import torch dataset = torch.load("gendata/outputs/example_uniform_cuboids_training.pt", map_location="cpu", weights_only=False) X = dataset["sensor_values"] y = dataset["conductivity"] print(X.shape, y.shape) ``` ## Notes - `MIT_Forward` can be imported as a local Python package from the repository root. Directly executed example scripts add only the repository root to `sys.path`, then use package imports. - This project loads `.pt` dictionaries with `torch.load(..., weights_only=False)` for compatibility with the saved payload format. Only load `.pt` files from trusted sources. - Use `--device cuda` or `--device cuda:N` only when CUDA is available. Otherwise use `--device cpu`. - Runtime is mainly controlled by PINN training epochs, conductivity-grid resolution, secondary-field quadrature order, and sensor count. - `__pycache__`, `outputs`, `.pt`, PNG, and HTML files are generated artifacts, not core source files.