facebookresearch / facebookresearch/segment-anything

Size mismatch errors in running automatic_mask_generation_example.ipynb

Open
#68 9 comments 1 reaction 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
54.9k
Forks
6.4k
PR merge metrics
No merged PRs in 30d

Description

When I ran blocks in the automatic_mask_generation_example.ipynb, I got the size mismatch for image_encoder.blocks errors.
Here is the code:
```
import sys
sys.path.append("..")
from segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor
sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
sam.to(device=device)
mask_generator = SamAutomaticMaskGenerator(sam)
```
Here is the errors:

```
RuntimeError Traceback (most recent call last)
Cell In[24], line 5
2 sys.path.append("..")
3 from segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor
----> 5 sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
6 sam.to(device=device)
8 mask_generator = SamAutomaticMaskGenerator(sam)

File ~/segment-anything/notebooks/../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 ~/segment-anything/notebooks/../segment_anything/build_sam.py:106, in _build_sam(encoder_embed_dim, encoder_depth, encoder_num_heads, encoder_global_attn_indexes, checkpoint)
104 with open(checkpoint, "rb") as f:
105 state_dict = torch.load(f)
--> 106 sam.load_state_dict(state_dict)
107 return sam
...
size mismatch for image_encoder.blocks.23.mlp.lin1.weight: copying a param with shape torch.Size([4096, 1024]) from checkpoint, the shape in current model is torch.Size([5120, 1280]).
size mismatch for image_encoder.blocks.23.mlp.lin1.bias: copying a param with shape torch.Size([4096]) from checkpoint, the shape in current model is torch.Size([5120]).
size mismatch for image_encoder.blocks.23.mlp.lin2.weight: copying a param with shape torch.Size([1024, 4096]) from checkpoint, the shape in current model is torch.Size([1280, 5120]).
size mismatch for image_encoder.blocks.23.mlp.lin2.bias: copying a param with shape torch.Size([1024]) from checkpoint, the shape in current model is torch.Size([1280]).
size mismatch for image_encoder.neck.0.weight: copying a param with shape torch.Size([256, 1024, 1, 1]) from checkpoint, the shape in current model is torch.Size([256, 1280, 1, 1]).
```
```
My environment:
OS: Ubuntu 20.04.6 LTS x86_64
Kernel: 5.15.0-58-generic
CPU: 12th Gen Intel i5-12400
GPU: NVIDIA 3070 laptop
Driver Version: 515.86.01
CUDA Version: 11.7
Conda env:
Python Version: 3.8.16
pytorch Version: 1.8.0
torchaudio Version: 0.8.0
cudatoolkit Version: 10.2
```
How to fix these errors?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.