altered CNN, encoder and loss function to achieve better stability, and fixed morphing of the images lower than the desired resolution

This commit is contained in:
Pixelguy14
2026-06-04 11:58:02 -06:00
parent 242fc6a445
commit d98cfccb5f
48 changed files with 1339 additions and 1366 deletions
+31 -7
View File
@@ -15,7 +15,7 @@ using Statistics, Images, Interpolations, BenchmarkTools
data_path = "/app/data/Leaf.imzML"
mask_path = "/app/data/region_of_interest.png"
output_folder = "/app/datos_para_ai"
img_size = (224, 224)
img_size = (256, 256)
tolerance = 0.05
mkpath(output_folder)
@@ -25,6 +25,16 @@ function run_production_pipeline()
# 1. LOAD DATA
println("Step 1: Ingesting Data...")
data = OpenMSIData(data_path)
orig_height = data.image_dims[2]
orig_width = data.image_dims[1]
if orig_height < 256 || orig_width < 256
final_img_size = (orig_height, orig_width)
println(" [Notice] Original image is smaller than 256x256. Keeping original size: ", final_img_size)
else
final_img_size = (256, 256)
println(" [Notice] Resizing down to target size: ", final_img_size)
end
# 2. RUN FRAMEWORK ANALYTICS
println("Step 2: Checking Dataset Precomputed Boundaries...")
@@ -68,23 +78,37 @@ function run_production_pipeline()
slice_dict = MSI_src.get_multiple_mz_slices(data, target_masses, tolerance, mask_path=mask_path)
# 6. THREADED PROCESSING & EXPORT
println("Step 5: Threaded TrIQ Normalization & Resizing (Using $(Threads.nthreads()) threads)...")
println("Step 5: Threaded TrIQ Normalization & Safe 256x256 Zero-Padding...")
mask_matrix = MSI_src.load_and_prepare_mask(mask_path, (data.image_dims[1], data.image_dims[2]))
exported_count = Threads.Atomic{Int}(0)
# Fixed target dimensions for the CNN
target_size = (256, 256)
Threads.@threads for mz_val in target_masses
slice = get(slice_dict, mz_val, nothing)
if slice === nothing || all(iszero, slice) continue end
# Use Framework-Native TrIQ (Standard: 256 bins, 0.98 probability cutoff)
# 1. Framework-Native TrIQ (0.0 to 255.0 Scaling)
slice_quant, _ = MSI_src.TrIQ(slice, 256, 0.98, mask_matrix=mask_matrix)
# Resize cleanly to standard CNN dimensions
img_resized = imresize(slice_quant, img_size)
# 2. Allocate an empty 256x256 background matrix
padded_matrix = zeros(Float32, target_size)
# Normalize 0-255 scaling back to 0.0-1.0 float mapping for Gray storage conversion
# 3. Compute structural centering offsets based on true resolution (161x106)
h_orig, w_orig = size(slice_quant, 1), size(slice_quant, 2)
offset_y = div(target_size[1] - h_orig, 2) + 1
offset_x = div(target_size[2] - w_orig, 2) + 1
# 4. Map the pristine, raw pixels directly into the center
padded_matrix[offset_y:offset_y+h_orig-1, offset_x:offset_x+w_orig-1] .= slice_quant
# 5. Convert safely to 0.0 - 1.0 float mapping for Gray image export
normalized_gray = padded_matrix ./ 255.0
# 6. Save the unblurred, structurally perfect map
filename = "ion_$(round(mz_val, digits=2)).png"
save(joinpath(output_folder, filename), Gray.(img_resized ./ 255.0))
save(joinpath(output_folder, filename), Gray.(normalized_gray))
Threads.atomic_add!(exported_count, 1)
end