Skip to content

Commit

Permalink
Jupyter notebook for vista 2D
Browse files Browse the repository at this point in the history
Signed-off-by: Vishwesh Nath <[email protected]>
  • Loading branch information
finalelement committed Aug 30, 2024
1 parent d01158e commit b812d59
Show file tree
Hide file tree
Showing 13 changed files with 4,649 additions and 2,200 deletions.
100 changes: 0 additions & 100 deletions vista_2d/Quickstart_guide.md

This file was deleted.

167 changes: 0 additions & 167 deletions vista_2d/README.md

This file was deleted.

80 changes: 80 additions & 0 deletions vista_2d/cell_sam_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import torch
from segment_anything.build_sam import build_sam_vit_b
from torch import nn
from torch.nn import functional as F


class CellSamWrapper(torch.nn.Module):
def __init__(
self,
auto_resize_inputs=True,
network_resize_roi=[1024, 1024],
checkpoint="sam_vit_b_01ec64.pth",
return_features=False,
*args,
**kwargs,
) -> None:
super().__init__(*args, **kwargs)

print(
f"CellSamWrapper auto_resize_inputs {auto_resize_inputs} network_resize_roi {network_resize_roi} checkpoint {checkpoint}"
)
self.network_resize_roi = network_resize_roi
self.auto_resize_inputs = auto_resize_inputs
self.return_features = return_features

model = build_sam_vit_b(checkpoint=checkpoint)

model.prompt_encoder = None
model.mask_decoder = None

model.mask_decoder = nn.Sequential(
nn.BatchNorm2d(num_features=256),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(
256,
128,
kernel_size=3,
stride=2,
padding=1,
output_padding=1,
bias=False,
),
nn.BatchNorm2d(num_features=128),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(
128, 3, kernel_size=3, stride=2, padding=1, output_padding=1, bias=True
),
)

self.model = model

def forward(self, x):
# print("CellSamWrapper x0", x.shape)
sh = x.shape[2:]

if self.auto_resize_inputs:
x = F.interpolate(x, size=self.network_resize_roi, mode="bilinear")

# print("CellSamWrapper x1", x.shape)
x = self.model.image_encoder(x) # shape: (1, 256, 64, 64)
# print("CellSamWrapper image_embeddings", x.shape)

if not self.return_features:
x = self.model.mask_decoder(x)
if self.auto_resize_inputs:
x = F.interpolate(x, size=sh, mode="bilinear")

# print("CellSamWrapper x final", x.shape)
return x
Loading

0 comments on commit b812d59

Please sign in to comment.