facebookresearch / facebookresearch/segment-anything
Problem with using model checkpoint and PytorchStreamReader failed reading zip archive: failed finding central directory
- Dominant language
- Jupyter Notebook
- Stars
- 54.9k
- Forks
- 6.4k
- PR merge metrics
- No merged PRs in 30d
Description
Hi, I encountered the following error message when running SAM. I'm not quite sure if it is because I put the model checkpoint sam_vit_h_4b8939.pth into the segment-anything folder. Hope to hear some suggestion from you. Thanks!

[/sahandlab/Software/segment-anything/segment_anything/build_sam.py:105](http://localhost:40001/sahandlab/Software/segment-anything/segment_anything/build_sam.py#line=104): FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
state_dict = torch.load(f)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[9], line 5
3 DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
4 MODEL_TYPE = "vit_h"
----> 5 sam = sam_model_registry[MODEL_TYPE](checkpoint=CHECKPOINT_PATH).to(device=DEVICE)
6 mask_generator = SamAutomaticMaskGenerator(sam)
7 predictor = SamPredictor(sam)
File [/sahandlab/Software/segment-anything/segment_anything/build_sam.py:15](http://localhost:40001/sahandlab/Software/segment-anything/segment_anything/build_sam.py#line=14), in build_sam_vit_h(checkpoint)
14 def build_sam_vit_h(checkpoint=None):
---> 15 return _build_sam(
16 encoder_embed_dim=1280,
17 encoder_depth=32,
18 encoder_num_heads=16,
19 encoder_global_attn_indexes=[7, 15, 23, 31],
20 checkpoint=checkpoint,
21 )
File [/sahandlab/Software/segment-anything/segment_anything/build_sam.py:105](http://localhost:40001/sahandlab/Software/segment-anything/segment_anything/build_sam.py#line=104), in _build_sam(encoder_embed_dim, encoder_depth, encoder_num_heads, encoder_global_attn_indexes, checkpoint)
103 if checkpoint is not None:
104 with open(checkpoint, "rb") as f:
--> 105 state_dict = torch.load(f)
106 sam.load_state_dict(state_dict)
107 return sam
File [~/.conda/envs/mothermachine/lib/python3.10/site-packages/torch/serialization.py:1072](http://localhost:40001/lab/tree/~/.conda/envs/mothermachine/lib/python3.10/site-packages/torch/serialization.py#line=1071), in load(f, map_location, pickle_module, weights_only, mmap, **pickle_load_args)
1070 orig_position = opened_file.tell()
1071 overall_storage = None
-> 1072 with _open_zipfile_reader(opened_file) as opened_zipfile:
1073 if _is_torchscript_zip(opened_zipfile):
1074 warnings.warn("'torch.load' received a zip file that looks like a TorchScript archive"
1075 " dispatching to 'torch.jit.load' (call 'torch.jit.load' directly to"
1076 " silence this warning)", UserWarning)
File [~/.conda/envs/mothermachine/lib/python3.10/site-packages/torch/serialization.py:480](http://localhost:40001/lab/tree/~/.conda/envs/mothermachine/lib/python3.10/site-packages/torch/serialization.py#line=479), in _open_zipfile_reader.__init__(self, name_or_buffer)
479 def __init__(self, name_or_buffer) -> None:
--> 480 super().__init__(torch._C.PyTorchFileReader(name_or_buffer))
RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory
Contributor guide
Research direction
Start at segment_anything/build_sam.py:105 and reproduce the checkpoint load using the reported CHECKPOINT_PATH and vit_h model setup. Check whether the referenced sam_vit_h_4b8939.pth can be read by the existing loader; done means identifying whether the failure is caused by the checkpoint or the repository code and recording the resulting resolution.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100