Download code/fix_step2_part2.py from unknownBrain/survivalanalysis_checkpointing: direct link, hf CLI and curl.
- Browser
- Download file 3.71 kB
-
https://huggingface.co/datasets/unknownBrain/survivalanalysis_checkpointing/resolve/main/code/fix_step2_part2.py
- Command line
-
hf download hf://datasets/unknownBrain/survivalanalysis_checkpointing/code/fix_step2_part2.py
-
curl -L -o fix_step2_part2.py https://huggingface.co/datasets/unknownBrain/survivalanalysis_checkpointing/resolve/main/code/fix_step2_part2.py
3.71 kB
| import re | |
| import os | |
| with open('Train_CMTLNet_Step2.py', 'r') as f: | |
| code = f.read() | |
| target = """ global_step = 0 # Initialize global step | |
| best_score = -1 # Initialize best score | |
| # Initialize DataFrames for logging | |
| log_1p19q = pd.DataFrame(columns=[ | |
| 'epoch', | |
| 'Train_loss', 'Train_Accuracy', 'Train_F1', | |
| 'Val_loss', 'Val_Accuracy', 'Val_F1' | |
| ]) | |
| log_IDH = pd.DataFrame(columns=[ | |
| 'epoch', | |
| 'Train_loss', 'Train_Accuracy', 'Train_F1', | |
| 'Val_loss', 'Val_Accuracy', 'Val_F1' | |
| ]) | |
| log_LHG = pd.DataFrame(columns=[ | |
| 'epoch', | |
| 'Train_loss', 'Train_Accuracy', 'Train_F1', | |
| 'Val_loss', 'Val_Accuracy', 'Val_F1' | |
| ]) | |
| log_mean = pd.DataFrame(columns=[ | |
| 'epoch', | |
| 'Train_loss', 'Train_Accuracy', 'Train_F1', | |
| 'Val_loss', 'Val_Accuracy', 'Val_F1' | |
| ]) | |
| model.train() # Set model to training mode | |
| # Initialize iterator indices | |
| iter_index_IDH = 0 | |
| iter_index_LHG = 0 | |
| iter_IDH = iter(train_loader_IDH) | |
| iter_LHG = iter(train_loader_LHG) | |
| for epoch in range(config.epochs_s2):""" | |
| replacement = """ global_step = 0 # Initialize global step | |
| best_score = -1 # Initialize best score | |
| start_epoch = 0 | |
| scaler = GradScaler() | |
| save_name = getattr(model, 'name', 'UFE_UCFC') | |
| save_path = os.path.join(config.data_dir, 'results', 'UFE_UCFC', f'Fold_{fold}') | |
| if not os.path.exists(save_path): | |
| os.makedirs(save_path) | |
| # Resume logic | |
| import glob | |
| checkpoints = glob.glob(os.path.join(save_path, f'checkpoint_fold{fold}_epoch*.pkl')) | |
| if checkpoints: | |
| latest_checkpoint = max(checkpoints, key=lambda x: int(x.split('epoch')[1].split('.pkl')[0])) | |
| print(f"Resuming from checkpoint: {latest_checkpoint}") | |
| checkpoint = torch.load(latest_checkpoint, weights_only=False) | |
| model.load_state_dict(checkpoint['model_state_dict']) | |
| if 'optimizer_state_dict' in checkpoint: | |
| optimizer.load_state_dict(checkpoint['optimizer_state_dict']) | |
| if 'scheduler_state_dict' in checkpoint: | |
| scheduler.load_state_dict(checkpoint['scheduler_state_dict']) | |
| if 'scaler_state_dict' in checkpoint: | |
| scaler.load_state_dict(checkpoint['scaler_state_dict']) | |
| if 'global_step' in checkpoint: | |
| global_step = checkpoint['global_step'] | |
| if 'best_f1' in checkpoint: | |
| best_score = checkpoint['best_f1'] | |
| if 'epoch' in checkpoint: | |
| start_epoch = checkpoint['epoch'] + 1 | |
| # Initialize DataFrames for logging | |
| log_1p19q = pd.DataFrame(columns=[ | |
| 'epoch', | |
| 'Train_loss', 'Train_Accuracy', 'Train_F1', | |
| 'Val_loss', 'Val_Accuracy', 'Val_F1' | |
| ]) | |
| log_IDH = pd.DataFrame(columns=[ | |
| 'epoch', | |
| 'Train_loss', 'Train_Accuracy', 'Train_F1', | |
| 'Val_loss', 'Val_Accuracy', 'Val_F1' | |
| ]) | |
| log_LHG = pd.DataFrame(columns=[ | |
| 'epoch', | |
| 'Train_loss', 'Train_Accuracy', 'Train_F1', | |
| 'Val_loss', 'Val_Accuracy', 'Val_F1' | |
| ]) | |
| log_mean = pd.DataFrame(columns=[ | |
| 'epoch', | |
| 'Train_loss', 'Train_Accuracy', 'Train_F1', | |
| 'Val_loss', 'Val_Accuracy', 'Val_F1' | |
| ]) | |
| model.train() # Set model to training mode | |
| # Initialize iterator indices | |
| iter_index_IDH = 0 | |
| iter_index_LHG = 0 | |
| iter_IDH = iter(train_loader_IDH) | |
| iter_LHG = iter(train_loader_LHG) | |
| for epoch in range(start_epoch, config.epochs_s2):""" | |
| if target in code: | |
| code = code.replace(target, replacement) | |
| else: | |
| print("Failed to replace target 1") | |
| with open('Train_CMTLNet_Step2.py', 'w') as f: | |
| f.write(code) | |
| print("Part 2 replacements done.") | |