File size: 3,310 Bytes
60c4331
 
 
 
23739c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13eb21e
 
23739c9
8b10b57
23739c9
 
60c4331
 
 
 
 
 
 
8b10b57
 
60c4331
 
 
 
 
 
8b10b57
60c4331
 
 
 
 
 
23739c9
60c4331
 
8b10b57
60c4331
 
 
8739fa2
 
60c4331
a74a30b
 
8b10b57
a74a30b
 
 
8b10b57
 
 
 
60c4331
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os, subprocess, zipfile
from pathlib import Path
from huggingface_hub import hf_hub_download, snapshot_download

def writable_data_root():
    preferred = Path(os.environ.get('DATA_ROOT', '/data/amodal'))
    for candidate in (preferred, Path('/tmp/amodal')):
        try:
            candidate.mkdir(parents=True, exist_ok=True)
            test = candidate / '.write_test'
            test.write_text('ok')
            test.unlink(missing_ok=True)
            return candidate
        except Exception:
            continue
    raise RuntimeError('No writable data directory found; tried /data/amodal and /tmp/amodal')

data_root = writable_data_root()
cache_root = data_root.parent/'hf_cache'
os.environ.setdefault('HF_HOME', str(cache_root))
os.environ.setdefault('HUGGINGFACE_HUB_CACHE', str(cache_root/'hub'))
os.environ.setdefault('TRANSFORMERS_CACHE', str(cache_root/'transformers'))
os.environ.setdefault('HOME', str(data_root.parent))
os.environ.setdefault('XDG_CACHE_HOME', str(data_root.parent/'.cache'))
ckpt_root = data_root/'checkpoints'
ckpt_root.mkdir(parents=True, exist_ok=True)
cache_root.mkdir(parents=True, exist_ok=True)
(data_root/'LISAoutput').mkdir(parents=True, exist_ok=True)

def run(cmd, cwd=None, check=True):
    print('+', ' '.join(cmd))
    return subprocess.run(cmd, cwd=cwd, check=check)

# Public direct downloads
files = [
    ('https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth', ckpt_root/'sam_vit_h_4b8939.pth'),
    ('https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth', ckpt_root/'groundingdino_swint_ogc.pth'),
]
for url, dst in files:
    if not dst.exists():
        run(['wget', '-O', str(dst), url])

# HF-hosted RAM++ checkpoint
ram_dst = ckpt_root/'ram_plus_swin_large_14m.pth'
if not ram_dst.exists():
    src = hf_hub_download('xinyu1205/recognize-anything-plus-model', 'ram_plus_swin_large_14m.pth')
    os.symlink(src, ram_dst)

# LISA model: use snapshot cache; app.py can load repo_id directly, but snapshot warms cache.
# Requires HF_TOKEN if the repo/license requires it.
snapshot_download('xinlai/LISA-13B-llama2-v1', local_dir=str(cache_root/'LISA-13B-llama2-v1'), local_dir_use_symlinks=False)

# InstaOrder checkpoint zip is on Google Drive and can be throttled. Try gdown.
insta_ckpt = ckpt_root/'InstaOrder_InstaOrderNet_od.pth.tar'
if not insta_ckpt.exists():
    zip_path = Path('/tmp/InstaOrder_ckpt.zip')
    if not zip_path.exists():
        # Use the raw file id; older gdown versions do not support --fuzzy.
        run(['gdown', '1_GEmCmofLSkJZnidfp4vsQb2Nqq5aqBU', '-O', str(zip_path)], check=False)
    if zip_path.exists():
        try:
            with zipfile.ZipFile(zip_path) as z:
                z.extractall(ckpt_root)
        except zipfile.BadZipFile:
            print('WARNING: downloaded InstaOrder checkpoint is not a valid zip; removing it so a later run can retry.')
            zip_path.unlink(missing_ok=True)
    if not insta_ckpt.exists():
        nested = ckpt_root/'InstaOrder_ckpt'/'InstaOrder_InstaOrderNet_od.pth.tar'
        if nested.exists():
            nested.replace(insta_ckpt)
    if not insta_ckpt.exists():
        print('WARNING: InstaOrder checkpoint missing. Upload InstaOrder_ckpt.zip manually or mirror it to HF.')