temp / CT /lung2 /src /utils.py
Cccccz's picture
Add files using upload-large-folder tool
352807f verified
Raw
History Blame Contribute Delete
784 Bytes
from __future__ import annotations
import random
from pathlib import Path
from typing import Any
import numpy as np
import torch
import yaml
def load_config(path: str | Path) -> dict[str, Any]:
with open(path, encoding="utf-8") as f:
return yaml.safe_load(f)
def ensure_dir(path: str | Path) -> Path:
p = Path(path)
p.mkdir(parents=True, exist_ok=True)
return p
def seed_everything(seed: int) -> None:
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
def select_device(name: str | None = None) -> torch.device:
if name and name != "auto":
return torch.device(name)
return torch.device("cuda" if torch.cuda.is_available() else "cpu")