RefSeg-CA / source /code /prepare_rela.py
nmsofficial's picture
Add verified public availability and human-evaluation guide
9126e0d verified
Raw
History Blame Contribute Delete
3.64 kB
"""Download official ReLA weights and isolated compatibility dependencies."""
from pathlib import Path
import json, subprocess, urllib.request, zipfile, io, hashlib, os, sys
ROOT=Path(__file__).resolve().parents[1]
def main():
vendor=ROOT/'vendor';vendor.mkdir(exist_ok=True)
sys.path.insert(0,str(vendor))
lock=json.loads((ROOT/'protocol/rela_reconstruction_lock.json').read_text())
(ROOT/'models').mkdir(exist_ok=True);(ROOT/'third_party').mkdir(exist_ok=True)
repo=ROOT/'third_party'/('ReLA-'+lock['rela_commit'])
if not repo.exists():
with urllib.request.urlopen('https://codeload.github.com/'+lock['rela_repository']+'/zip/'+lock['rela_commit'],timeout=120) as r:blob=r.read()
zipfile.ZipFile(io.BytesIO(blob)).extractall(ROOT/'third_party')
import importlib.util
for package,version in [('pip','25.2'),('setuptools','75.8.0'),('wheel','0.45.1')]:
if importlib.util.find_spec(package) is None:
with urllib.request.urlopen(f'https://pypi.org/pypi/{package}/{version}/json',timeout=60) as r:meta=json.load(r)
url=next(x['url'] for x in meta['urls'] if x['filename'].endswith('py3-none-any.whl'))
with urllib.request.urlopen(url,timeout=120) as r:blob=r.read()
zipfile.ZipFile(io.BytesIO(blob)).extractall(vendor)
packages=['gdown==5.2.0','timm==0.6.13','fvcore==0.1.5.post20221221','yacs==0.1.8','iopath==0.1.9','omegaconf==2.3.0','antlr4-python3-runtime==4.9.3','hydra-core==1.3.2','portalocker==2.10.1','termcolor==2.4.0','cloudpickle==3.0.0','tabulate==0.9.0','opencv-python-headless==4.10.0.84','shapely==2.0.6','h5py==3.11.0']
subprocess.run(['python3','-m','pip','install','--target',str(vendor),'--no-deps',*packages],check=True)
import gdown
target=ROOT/'models/gres_swin_tiny.pth'
if not target.exists() or target.stat().st_size!=650774639:
gdown.download(id='1VwX3BQy6gaF38P7YL6YeF2m0prgpO-IG',output=str(target),quiet=False)
assert target.stat().st_size==650774639
assert hashlib.sha256(target.read_bytes()).hexdigest()==lock['checkpoint_sha256'],'ReLA checkpoint mismatch'
bert=ROOT/'models/bert-base-uncased';bert.mkdir(exist_ok=True)
for f in ['config.json','vocab.txt','tokenizer_config.json','tokenizer.json','model.safetensors']:
p=bert/f
if not p.exists():urllib.request.urlretrieve('https://huggingface.co/google-bert/bert-base-uncased/resolve/main/'+f,str(p))
assert hashlib.sha256(p.read_bytes()).hexdigest()==lock['bert_files_sha256'][f],f+' differs from executed BERT asset; obtain the recorded version'
url='https://codeload.github.com/facebookresearch/detectron2/zip/refs/tags/v0.6'
d2=ROOT/'third_party/detectron2-0.6'
if not d2.exists():
with urllib.request.urlopen(url,timeout=120) as r:blob=r.read()
zipfile.ZipFile(io.BytesIO(blob)).extractall(ROOT/'third_party')
# CPU extension suffices for this architecture; MSDeformAttn uses the
# official repository's PyTorch reference implementation on the GPU.
if os.getenv('R2_BUILD_D2')=='1':
subprocess.run(['python3','-m','pip','install','--target',str(vendor),'--no-deps','--no-build-isolation',str(d2)],check=True)
meta=dict(checkpoint_file=target.name,checkpoint_sha256=hashlib.sha256(target.read_bytes()).hexdigest(),bytes=target.stat().st_size,official_drive_file_id='1VwX3BQy6gaF38P7YL6YeF2m0prgpO-IG',packages=packages,detectron2='v0.6',slurm_job_id=os.getenv('SLURM_JOB_ID'))
(ROOT/'protocol/rela_assets.json').write_text(json.dumps(meta,indent=2));print(json.dumps(meta,indent=2),flush=True)
if __name__=='__main__':main()