LithoBench-PDE / LithoBench_PDE.py
LoveHotpot113's picture
Duplicate from AISDL-SNU/LithoBench-PDE
18f93b2
Raw History Blame Contribute Delete
2.17 kB
"""PyTorch dataset for LithoBench-PDE, from a local folder or the Hugging Face Hub.
from LithoBench_PDE import LithoBenchPDE
ds = LithoBenchPDE('LithoBench_PDE', categories=['Contact_I']) # local
ds = LithoBenchPDE.from_hub('AISDL-SNU/LithoBench-PDE', categories=['Contact_I'], task='maxwell') # downloads to data/
sample = ds[0]
"""
import glob
import os
import torch
from torch.utils.data import Dataset
REPO = 'AISDL-SNU/LithoBench-PDE'
CATEGORIES = ['Metal_I', 'Metal_T', 'Metal_P', 'Standard', 'Contact_I', 'Contact_T']
# Metal_L (large-area tiles of 512 to 1024 px) is left out of the default list; request it explicitly.
TASKS = {'maxwell': ('M', 'E'), 'reaction_diffusion': ('h', 'm'), 'eikonal': ('R', 'T')}
class LithoBenchPDE(Dataset):
"""Samples `root/<category>/*.pt` = {M, E, h, m, R, T}; `task` selects one (input, target) pair."""
def __init__(self, root, categories=None, task=None):
self.task = task
self.files = []
for category in categories or CATEGORIES:
for path in sorted(glob.glob(os.path.join(root, category, '*.pt'))):
self.files.append((category, path))
if not self.files:
raise FileNotFoundError(f'no .pt files under {root}')
@classmethod
def from_hub(cls, repo_id=REPO, categories=None, task=None, local_dir='data'):
from huggingface_hub import snapshot_download
patterns = [f'LithoBench_PDE/{c}/*.pt' for c in categories or CATEGORIES]
snapshot_download(repo_id, repo_type='dataset', allow_patterns=patterns, local_dir=local_dir)
return cls(os.path.join(local_dir, 'LithoBench_PDE'), categories, task)
def __len__(self):
return len(self.files)
def __getitem__(self, i):
category, path = self.files[i]
sample = torch.load(path, map_location='cpu', weights_only=True)
sample_id = os.path.basename(path)[:-3]
if self.task is None:
return dict(sample, sample_id=sample_id, category=category)
x, y = TASKS[self.task]
return {'input': sample[x], 'target': sample[y], 'sample_id': sample_id, 'category': category}