facebookresearch / segment-anything

The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.
Apache License 2.0
46.76k stars 5.54k forks source link

Problem with using model checkpoint and PytorchStreamReader failed reading zip archive: failed finding central directory #776

Open lindawang0122ds opened 3 hours ago

lindawang0122ds commented 3 hours ago

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!

SAM

/sahandlab/Software/segment-anything/segment_anything/build_sam.py:105: 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_registryMODEL_TYPE.to(device=DEVICE) 6 mask_generator = SamAutomaticMaskGenerator(sam) 7 predictor = SamPredictor(sam)

File /sahandlab/Software/segment-anything/segment_anything/build_sam.py:15, 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, 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, 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, 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

lindawang0122ds commented 3 hours ago

another follow-up question is should I install segment-anything whole folder and the model checkpoint into my conda vertical environment? Or is it better to put them into the same folder as the python code that need to import the sam model?