facebookresearch / facebookresearch/segment-anything
what is image size to get embedding from encoder
- Dominant language
- Jupyter Notebook
- Stars
- 54.9k
- Forks
- 6.4k
- PR merge metrics
- No merged PRs in 30d
Description
Hi, I try to use encoder part for my backbone for another downstream task. I used below network architecture.
```
class Net(nn.Module):
def __init__(self,model_type,sam_checkpoint):
super(Net, self).__init__()
self.model = sam_model_registry[model_type](checkpoint=sam_checkpoint)
for param in self.model.image_encoder.parameters():
param.requires_grad = False
self.model = self.model.image_encoder
self.avgpool = nn.Sequential(nn.Flatten(),nn.Linear(1024, 350),nn.ReLU(inplace=True),nn.Dropout(p=0.1))
self.fc = nn.Linear(350, 1)
def forward(self, image):
x = self.model(image)
x = self.avgpool(x)
x = self.fc(x)
return x
```
However, I got this error :
```
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
/tmp/ipykernel_295/2872027763.py in
8 print('------------------------------- Epoch: '+str(epoch))
9
---> 10 train_fn(train_loader, model, opt, loss_fn)
11 new_loss = check_acc(val_loader, model)
12 if new_loss < loss:
/tmp/ipykernel_295/825476113.py in train_fn(loader, model, opt, loss_fn)
4 y = y.to(torch.float).unsqueeze(1).to('cuda')
5
----> 6 preds = model(x).to(torch.float)
7
8 loss = loss_fn(preds, y)
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1188 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1189 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1190 return forward_call(*input, **kwargs)
1191 # Do not call functions when jit is used
1192 full_backward_hooks, non_full_backward_hooks = [], []
/tmp/ipykernel_295/2068463466.py in forward(self, image)
11
12 def forward(self, image):
---> 13 x = self.model(image)
14 x = self.avgpool(x)
15 x = self.fc(x)
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1188 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1189 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1190 return forward_call(*input, **kwargs)
1191 # Do not call functions when jit is used
1192 full_backward_hooks, non_full_backward_hooks = [], []
/opt/conda/lib/python3.7/site-packages/segment_anything/modeling/image_encoder.py in forward(self, x)
107 x = self.patch_embed(x)
108 if self.pos_embed is not None:
--> 109 x = x + self.pos_embed
110
111 for blk in self.blocks:
RuntimeError: The size of tensor a (32) must match the size of tensor b (64) at non-singleton dimension 2
```
I tried different size of image like 3,512,512 but the error are same. any idea?
Thanks
Contributor guide
Research direction
Start in segment_anything/modeling/image_encoder.py, especially forward(), patch_embed, and pos_embed, then inspect the tensor shapes produced by the notebook model call. Compare the encoder output with the downstream avgpool input; done means the encoder can process the intended image tensor and the downstream layers receive compatible dimensions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- jupyter-notebook, python
- Domain
- computer-vision, machine-learning
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100