DepthAnything / Depth-Anything-V2

Depth Anything V2. A More Capable Foundation Model for Monocular Depth Estimation
https://depth-anything-v2.github.io
Apache License 2.0
3.32k stars 266 forks source link

Fail to load model - "Failed to load the file: PytorchStreamReader failed reading zip archive: failed finding central directory" #61

Open nirbarazida opened 2 months ago

nirbarazida commented 2 months ago

Hi,

I'm trying to use the code snippet provided in the readme for infrance, but it fails to load all the model's weights linked in the repo.

Directories Model Path: /content/Depth-Anything-V2/checkpoints/depth_anything_v2_vitl.pth CWD: /content/Depth-Anything-V2

Code

import cv2
import torch

from depth_anything_v2.dpt import DepthAnythingV2

DEVICE = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'

model_configs = {
    'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]},
    'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]},
    'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
    'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
}

encoder = 'vitl' # or 'vits', 'vitb', 'vitg'

model = DepthAnythingV2(**model_configs[encoder])
model.load_state_dict(torch.load(f'checkpoints/depth_anything_v2_{encoder}.pth', map_location='cpu'))
model = model.to(DEVICE).eval()

Error

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
[<ipython-input-14-e96e9994ae85>](https://localhost:8080/#) in <cell line: 18>()
     16 
     17 model = DepthAnythingV2(**model_configs[encoder])
---> 18 model.load_state_dict(torch.load(f'checkpoints/depth_anything_v2_{encoder}.pth', map_location='cpu'))
     19 model = model.to(DEVICE).eval()
     20 

1 frames
[/usr/local/lib/python3.10/dist-packages/torch/serialization.py](https://localhost:8080/#) in load(f, map_location, pickle_module, weights_only, mmap, **pickle_load_args)
   1002             orig_position = opened_file.tell()
   1003             overall_storage = None
-> 1004             with _open_zipfile_reader(opened_file) as opened_zipfile:
   1005                 if _is_torchscript_zip(opened_zipfile):
   1006                     warnings.warn("'torch.load' received a zip file that looks like a TorchScript archive"

[/usr/local/lib/python3.10/dist-packages/torch/serialization.py](https://localhost:8080/#) in __init__(self, name_or_buffer)
    454 class _open_zipfile_reader(_opener):
    455     def __init__(self, name_or_buffer) -> None:
--> 456         super().__init__(torch._C.PyTorchFileReader(name_or_buffer))
    457 
    458 

RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory
LiheYoung commented 2 months ago

Hi, could you please re-download the checkpoint file? It seems the ckp is not downloaded completely.