fixed docker Julia issues, tested reproductability across all scripts through the docker

This commit is contained in:
Pixelguy14
2026-06-03 13:38:45 -06:00
parent 3143e29843
commit 242fc6a445
13 changed files with 410 additions and 183 deletions
+34 -18
View File
@@ -28,7 +28,7 @@ set_seed(42)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
IMG_DIR = os.path.join(BASE_DIR, "..", "datos_para_ai")
MODEL_SAVE_PATH = os.path.join(BASE_DIR, "..", "models", "msi_encoder_trained.pth")
BATCH_SIZE = 32
BATCH_SIZE = 8 # Reduced from 16 to prevent OOM on low-memory GPUs
# Epochs: 50 is sufficient for convergence on normalized ion maps.
# Further training leads to minor overfitting to background noise.
EPOCHS = 50
@@ -38,6 +38,11 @@ LEARNING_RATE = 0.0003
TEMPERATURE = 0.5
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Optimization for fragmented CUDA memory
if DEVICE.type == 'cuda':
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
torch.cuda.empty_cache()
# 2. AUMENTACIONES ESPECÍFICAS DE MSI (Basado en Hu et al. 2022)
class MSIAugmentations:
def __init__(self, size=224):
@@ -125,23 +130,34 @@ if __name__ == "__main__":
start_time = time.time()
for epoch in range(EPOCHS):
epoch_start = time.time()
total_loss = 0
for (img1, img2), names in loader:
img1, img2 = img1.to(DEVICE), img2.to(DEVICE)
optimizer.zero_grad()
_, z1 = model(img1)
_, z2 = model(img2)
loss = nt_xent_loss(z1, z2, temperature=TEMPERATURE)
loss.backward()
optimizer.step()
total_loss += loss.item()
avg_loss = total_loss / len(loader)
epoch_end = time.time()
curr_mem = process.memory_info().rss / 1024 / 1024 # MB
print(f"Epoch [{epoch+1}/{EPOCHS}], Loss: {avg_loss:.4f}, Time: {epoch_end-epoch_start:.2f}s, RAM: {curr_mem:.2f} MB")
try:
for epoch in range(EPOCHS):
epoch_start = time.time()
total_loss = 0
for (img1, img2), names in loader:
img1, img2 = img1.to(DEVICE), img2.to(DEVICE)
optimizer.zero_grad()
_, z1 = model(img1)
_, z2 = model(img2)
loss = nt_xent_loss(z1, z2, temperature=TEMPERATURE)
loss.backward()
optimizer.step()
total_loss += loss.item()
avg_loss = total_loss / len(loader)
epoch_end = time.time()
curr_mem = process.memory_info().rss / 1024 / 1024 # MB
print(f"Epoch [{epoch+1}/{EPOCHS}], Loss: {avg_loss:.4f}, Time: {epoch_end-epoch_start:.2f}s, RAM: {curr_mem:.2f} MB")
except RuntimeError as e:
if "shared memory" in str(e).lower() or "shm" in str(e).lower() or "no space left on device" in str(e).lower():
print("\n" + "!"*60)
print("[ERROR] Shared memory (shm) exhaustion detected.")
print("This is a common issue when running PyTorch in Docker containers.")
print("\nFIX:")
print("Add '--shm-size=2gb' (or more) to your 'docker run' command.")
print("Example: docker run -it --rm --shm-size=2gb --gpus all ...")
print("!"*60 + "\n")
raise e
total_time = time.time() - start_time
peak_mem = process.memory_info().rss / 1024 / 1024 # MB